<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Andrew Jackson</title>
	<atom:link href="http://www.a-jackson.co.uk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.a-jackson.co.uk</link>
	<description>.Net Development</description>
	<lastBuildDate>Thu, 10 May 2012 20:34:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>A* Pathfinding</title>
		<link>http://www.a-jackson.co.uk/2012/05/astar_pathfinding/</link>
		<comments>http://www.a-jackson.co.uk/2012/05/astar_pathfinding/#comments</comments>
		<pubDate>Thu, 10 May 2012 20:30:41 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[AI]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.a-jackson.co.uk/?p=683</guid>
		<description><![CDATA[Pathfinding algorithms search for the route between to points. This could be in game&#8217;s development for the AI agents, robotics or even the SatNav in your car. While there are many different pathfinding algorithms, I&#8217;m going to focus on A*. A* (pronounced A-star) requires that the area be known with the start and end point [...]]]></description>
			<content:encoded><![CDATA[<p>Pathfinding algorithms search for the route between to points. This could be in game&#8217;s development for the AI agents, robotics or even the SatNav in your car. While there are many different pathfinding algorithms, I&#8217;m going to focus on A*.</p>
<p>A* (pronounced A-star) requires that the area be known with the start and end point defined. Essentially, it then searches for the lowest cost route from A to B. The reason I say lowest cost and not shortest is that it is possible to define an areas or routes that are more difficult to cross and thus make them more costly routes, even though they may be shorter.</p>
<p>Firstly, you must split the environment into nodes. These can be any size or shape. For this example I&#8217;m using, I&#8217;m splitting the environment into squares 10&#215;10. These nodes can be passable or blocked. Travelling from one node to another has a cost associated with it. Travelling horizontally or vertically could have a weight of 1.0 while diagonal moves are 1.4. These weights can be used to approximate environmental hazards or difficulties to deter the algorithm from selecting it as a path.</p>
<p>This screenshot shows the start conditions of the environment. The black nodes are blocked and the path will search from green to red.</p>
<p style="text-align: center;">
<p style="text-align: left;"><img class="aligncenter size-medium wp-image-686" title="Pathfinding1" src="http://www.a-jackson.co.uk/wp-content/uploads/2012/05/Pathfinding1-260x300.png" alt="" width="260" height="300" /></p>
<p style="text-align: left;">As you can see there are several different routes available. This algorithm does not allow diagonal moves and all moves have a cost of 1. Turning also has an additional cost of 1.</p>
<p style="text-align: left;">Each node requires 3 values, G, H and F.</p>
<ol>
<li>H is a heuristic estimate to the goal. It is the estimated cost to reach the target node, it does not take into account the individual costs of the nodes in-between or the presence of any blocked nodes. This can be precomputed for every node or calculated when needed.</li>
<li>G is the cost to reach the current node from the start location. This is calculated as the algorithm runs.</li>
<li>F is the sum of H and G</li>
</ol>
<div>Additionally, each node requires a reference to a parent node. That is, the node that precedes the current node in the optimal path.</div>
<p style="text-align: left;">The algorithm maintains 2 lists of nodes while it runs, an open list and a closed list. Both are initially empty.</p>
<p style="text-align: left;">The open list is nodes that are available to be moved to, the closed list are nodes that have already been checked.</p>
<p style="text-align: left;">Once it starts, the current node is initialised to the start node, in this case [0,0]. All passable, adjacent nodes, that are not on the closed list, are then checked, in this case [1,0].</p>
<p style="text-align: left;">If the node is already on the open list, then its G value is calculated assuming the current node as the parent. This is done because the route to that node may be cheaper going through the current node than through its previous parent. If this is the case, the G value is updated and the current node is set as the parent.</p>
<p style="text-align: left;">If the node is not already on the open list, then the current node is set as its parent and the G value calculated.</p>
<p style="text-align: left;">If at this point, the open list is empty, then there is no possible route as there are no new nodes to move to. Otherwise, the node on the open list with the lowest F value is set as the current node and the loop starts over, the current node is removed from the open list and added to the closed list. This continues until the current node is the end node.</p>
<p style="text-align: left;">Once that has happened. The path can be found by tracking backwards through the nodes&#8217; parents. The nodes that are passed through represent the lowest cost route between the start and end nodes.</p>
<p style="text-align: left;"><a href="http://www.a-jackson.co.uk/wp-content/uploads/2012/05/Pathfinding2.png"><img class="aligncenter size-medium wp-image-687" title="Pathfinding2" src="http://www.a-jackson.co.uk/wp-content/uploads/2012/05/Pathfinding2-260x300.png" alt="" width="260" height="300" /></a></p>
<p style="text-align: left;">The following links are downloads to the code and executable of the sample in the screenshot above. It is C# for Visual Studio 2008/2010.</p>
<p style="text-align: left;">Note: There is a file embedded within this post, please visit this post to download the file.</p>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.a-jackson.co.uk/2012/05/astar_pathfinding/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A Generic Asynchronous Method</title>
		<link>http://www.a-jackson.co.uk/2012/05/a-generic-asynchronous-method/</link>
		<comments>http://www.a-jackson.co.uk/2012/05/a-generic-asynchronous-method/#comments</comments>
		<pubDate>Wed, 09 May 2012 21:17:28 +0000</pubDate>
		<dc:creator>Andrew</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Async]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Generics]]></category>

		<guid isPermaLink="false">http://www.a-jackson.co.uk/?p=607</guid>
		<description><![CDATA[I was recently writing a library that made requests to a web service, I wanted the library to provide both asynchronous and synchronous methods for each request. I did not want to write: public Result Request() { } public void RequestAsync(object userState) { Task.Factory.StartNew(() =&#62; { try { Result result = Request(); RequestCompleted(this, new RequestEventArgs(result, [...]]]></description>
			<content:encoded><![CDATA[<p>I was recently writing a library that made requests to a web service, I wanted the library to provide both asynchronous and synchronous methods for each request. I did not want to write:</p>
<pre class="brush: c#; ">

public Result Request()
{
}

public void RequestAsync(object userState)
{
	Task.Factory.StartNew(() =&gt;
	{
		try
		{
			Result result = Request();
			RequestCompleted(this, new RequestEventArgs(result, userState));
		}
		catch (Exception e)
		{
			RequestCompleted(this, new RequestEventArgs(e, userState));
		}
	});
}
</pre>
<p>for every single request. So I made a generic solution. My first problem was that every request returns a different data type, depending on what the request was.</p>
<p>I had to make my event handler delegate and event args class both use generics.</p>
<pre class="brush: c#; ">

public delegate void ResponseEventHandler&lt;TResponse&gt;(
	object sender,
	ResponseEventArgs&lt;TResponse&gt; e);

public class ResponseEventArgs&lt;TResponse&gt;: EventArgs
{
	public ResponseEventArgs(TResponse response, object userState)
	{
		this.Response = response;
		this.UserState = userState;
	}

	public ResponseEventArgs(Exception exception, object userState)
	{
		this.Exception = exception;
		this.UserState = userState;
	}

	public TResponse Response { get; private set; }
	public Exception Exception {get; private set; }
	public object UserState { get; private set; }
}
</pre>
<p>Finally, I wanted this easy to use. There are hundreds of different request methods in my library and I don&#8217;t want to have to write loads of code for them all.</p>
<p>I ended up making use of delegates to wrap the entire process up in a method that I can call from my Async methods.</p>
<p>Looking at the RequestAsync method above, all I have is the call to the synchronous method, then raising the event. There is a different event for each request. Both of these can be specified as delegate parameters.</p>
<pre class="brush: c#; ">

protected void ExecuteAsync&lt;TResponse&gt;(
	Func&lt;TResponse&gt; executeRequest,
	ResponseEventHandle&lt;TResponse&gt; eventHandler,
	object userState)
{
	Task.Factory.StartNew(() =&gt;
	{
		try
		{
			TResponse result = executeRequest();
			this.RaiseEvent(
				eventHandler,
				new ResponseEventArgs&lt;TResponse&gt;(result, userState));
		}
		catch (Exception e)
		{
			this.RaiseEvent(
				eventHandler,
				new ResponseEventArgs&lt;TResponse&gt;(e, userState));
		}
	});
}

private void RaiseEvent&lt;TResponse&gt;(
	ResponseEventHandler&lt;TResponse&gt; eventHandler,
	ResponseEventArgs&lt;TResponse&gt; e)
{
	if (eventHandler != null)
	{
		eventHandler(this, e);
	}
}
</pre>
<p>My RequestAsync method then becomes</p>
<pre class="brush: c#; ">

public void RequestAsync(object userState)
{
	this.ExecuteAsync(this.Request, this.RequestCompleted, userState);
}
</pre>
<p>By writing a method to handle the actual web request and processing the response (de-serialising JSON in this case) in a similar manner, I was able to make the synchronous methods a single line of code as well. This saved me a lot time writing dozens of very similar functions. For a long time, I never used generics outside of data structures like List&lt;&gt; but using generics like this can save a lot of time writing repetitive and tedious code. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.a-jackson.co.uk/2012/05/a-generic-asynchronous-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

