<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Project Euler problem 83 &#8211; how creeps can help</title>
	<atom:link href="http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/feed/" rel="self" type="application/rss+xml" />
	<link>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/</link>
	<description>Eli Bendersky's personal website</description>
	<lastBuildDate>Wed, 28 Jul 2010 12:22:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Python Coder</title>
		<link>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/comment-page-1/#comment-181609</link>
		<dc:creator>Python Coder</dc:creator>
		<pubDate>Thu, 09 Jul 2009 20:28:53 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1743#comment-181609</guid>
		<description>thanks, after changing closedset and openset to sets it runs in a little over 2 seconds, wow!
such a difference it makes</description>
		<content:encoded><![CDATA[<p>thanks, after changing closedset and openset to sets it runs in a little over 2 seconds, wow!<br />
such a difference it makes</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eliben</title>
		<link>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/comment-page-1/#comment-181393</link>
		<dc:creator>eliben</dc:creator>
		<pubDate>Thu, 09 Jul 2009 03:46:36 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1743#comment-181393</guid>
		<description>@Python Coder,

Perhaps you could use a real &#039;set&#039; or a dict type for the open and closed sets instead of lists? Removing from a list is O(n).

You can download and compare my A* code from the Creeps game link I provided - it&#039;s quite short and simple, and well commented.</description>
		<content:encoded><![CDATA[<p>@Python Coder,</p>
<p>Perhaps you could use a real &#8217;set&#8217; or a dict type for the open and closed sets instead of lists? Removing from a list is O(n).</p>
<p>You can download and compare my A* code from the Creeps game link I provided &#8211; it&#8217;s quite short and simple, and well commented.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Python Coder</title>
		<link>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/comment-page-1/#comment-181167</link>
		<dc:creator>Python Coder</dc:creator>
		<pubDate>Wed, 08 Jul 2009 05:07:49 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1743#comment-181167</guid>
		<description>Hey i tried using A star to solve this problem and I get the correct answer but it is so AWFULLY sloww (500 secs). My program looks at all 6400 cells, what do you think I am doing wrong?
heuristic:
[code]
def h(n, goal):
....return 11 * (abs(n.col - goal.col) + abs(n.row - goal.row))
[/code]
If i change the 11 to a bigger number I get the wrong answer.
Any suggestions 
[code]
`&lt;code&gt;def astar(start, goal, matrix):
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;closedset = []
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;openset = []
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;start__abENT__#46;hscore = h(start, goal, matrix)
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;start__abENT__#46;fscore = start__abENT__#46;hscore + start__abENT__#46;gscore
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;openset__abENT__#46;append(start)

__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;while openset:
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;x = None           # find node with lowest fscore
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;for n in openset:
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;if not x or n__abENT__#46;fscore __abENT__lt; x__abENT__#46;fscore:
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;x = n

        if x__abENT__#46;row == goal__abENT__#46;row and x__abENT__#46;col == goal__abENT__#46;col:
            return x__abENT__#46;gscore

        print x__abENT__#46;row, x__abENT__#46;col, len(closedset), x__abENT__#46;fscore

        openset__abENT__#46;remove(x)
        closedset__abENT__#46;append(x)

        neighbors = []
        if x__abENT__#46;row != len(matrix)-1:
            neighbors__abENT__#46;append(matrix[x__abENT__#46;row+1][x__abENT__#46;col])

        if x__abENT__#46;row != 0:
            neighbors__abENT__#46;append(matrix[x__abENT__#46;row-1][x__abENT__#46;col])

        if x__abENT__#46;col != len(matrix[0])-1:
            neighbors__abENT__#46;append(matrix[x__abENT__#46;row][x__abENT__#46;col+1])

        if x__abENT__#46;col != 0:
            neighbors__abENT__#46;append(matrix[x__abENT__#46;row][x__abENT__#46;col-1])

__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;for y in neighbors:
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;if y in closedset:
__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;__abENT__#46;continue

            gscore = x__abENT__#46;gscore + y__abENT__#46;gscore
            better = False

            if not y in openset:
                openset__abENT__#46;append(y)
                y__abENT__#46;hscore = h(y, goal, matrix)
                better = True 

            elif gscore __abENT__lt; y__abENT__#46;gscore:
                better = True

            if better:
                y__abENT__#46;parent = x
                y__abENT__#46;gscore = gscore
                y__abENT__#46;fscore = y__abENT__#46;gscore + y__abENT__#46;hscore

    return None&lt;/code&gt;`
[/code]
Sorry for the long code =/</description>
		<content:encoded><![CDATA[<p>Hey i tried using A star to solve this problem and I get the correct answer but it is so AWFULLY sloww (500 secs). My program looks at all 6400 cells, what do you think I am doing wrong?<br />
heuristic:<br />
[code]<br />
def h(n, goal):<br />
....return 11 * (abs(n.col - goal.col) + abs(n.row - goal.row))<br />
[/code]<br />
If i change the 11 to a bigger number I get the wrong answer.<br />
Any suggestions<br />
[code]<br />
<code class="backtick">def astar(start, goal, matrix):<br />
&#46;&#46;&#46;&#46;closedset = []<br />
&#46;&#46;&#46;&#46;openset = []<br />
&#46;&#46;&#46;&#46;start&#46;hscore = h(start, goal, matrix)<br />
&#46;&#46;&#46;&#46;start&#46;fscore = start&#46;hscore + start&#46;gscore<br />
&#46;&#46;&#46;&#46;openset&#46;append(start)</p>
<p>&#46;&#46;&#46;&#46;while openset:<br />
&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;x = None           # find node with lowest fscore<br />
&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;for n in openset:<br />
&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;if not x or n&#46;fscore &lt; x&#46;fscore:<br />
&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;x = n</p>
<p>        if x&#46;row == goal&#46;row and x&#46;col == goal&#46;col:<br />
            return x&#46;gscore</p>
<p>        print x&#46;row, x&#46;col, len(closedset), x&#46;fscore</p>
<p>        openset&#46;remove(x)<br />
        closedset&#46;append(x)</p>
<p>        neighbors = []<br />
        if x&#46;row != len(matrix)-1:<br />
            neighbors&#46;append(matrix[x&#46;row+1][x&#46;col])</p>
<p>        if x&#46;row != 0:<br />
            neighbors&#46;append(matrix[x&#46;row-1][x&#46;col])</p>
<p>        if x&#46;col != len(matrix[0])-1:<br />
            neighbors&#46;append(matrix[x&#46;row][x&#46;col+1])</p>
<p>        if x&#46;col != 0:<br />
            neighbors&#46;append(matrix[x&#46;row][x&#46;col-1])</p>
<p>&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;for y in neighbors:<br />
&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;if y in closedset:<br />
&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;&#46;continue</p>
<p>            gscore = x&#46;gscore + y&#46;gscore<br />
            better = False</p>
<p>            if not y in openset:<br />
                openset&#46;append(y)<br />
                y&#46;hscore = h(y, goal, matrix)<br />
                better = True </p>
<p>            elif gscore &lt; y&#46;gscore:<br />
                better = True</p>
<p>            if better:<br />
                y&#46;parent = x<br />
                y&#46;gscore = gscore<br />
                y&#46;fscore = y&#46;gscore + y&#46;hscore</p>
<p>    return None</code><br />
[/code]<br />
Sorry for the long code =/</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack Diederich</title>
		<link>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/comment-page-1/#comment-177625</link>
		<dc:creator>Jack Diederich</dc:creator>
		<pubDate>Sun, 21 Jun 2009 02:08:34 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1743#comment-177625</guid>
		<description>Ahh, nevermind.  You&#039;re saying that A* collapses to Dijkstra.</description>
		<content:encoded><![CDATA[<p>Ahh, nevermind.  You&#8217;re saying that A* collapses to Dijkstra.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jack Diederich</title>
		<link>http://eli.thegreenplace.net/2009/06/19/project-euler-problem-83-how-creeps-can-help/comment-page-1/#comment-177622</link>
		<dc:creator>Jack Diederich</dc:creator>
		<pubDate>Sun, 21 Jun 2009 01:52:47 +0000</pubDate>
		<guid isPermaLink="false">http://eli.thegreenplace.net/?p=1743#comment-177622</guid>
		<description>Oy ye of little faith!  A simple A* Heuristic solves this problem in a fraction of a second on my 1GHz laptop.  50 lines of python including comments and vertical whiespace.

Some things to remember about A*:

- the heapq module is your friend
- If you arrive at a node with a cost higher than you&#039;ve seen at this node before you can toss out the current info, that path was a dead end.
- the Heuristic doesn&#039;t need to be complicated (that&#039;s a hint)

Here is the skeleton of my main routine

def main(score_function):
..stack = []
..def push(pos, cost):
....score = score_function(pos, cost)
....heapq.heappush(stack, (score, (pos, cost)))
..def pop():
....score, (pos, cost) = heapq.heappop(stack)
....return pos, cost

..while stack:
....cur_pos, cur_cost = pop() # lowest &#039;score&#039; entry to date
....# do stuff

The reason the score function is passed in is so you can try new ones easily.</description>
		<content:encoded><![CDATA[<p>Oy ye of little faith!  A simple A* Heuristic solves this problem in a fraction of a second on my 1GHz laptop.  50 lines of python including comments and vertical whiespace.</p>
<p>Some things to remember about A*:</p>
<p>- the heapq module is your friend<br />
- If you arrive at a node with a cost higher than you&#8217;ve seen at this node before you can toss out the current info, that path was a dead end.<br />
- the Heuristic doesn&#8217;t need to be complicated (that&#8217;s a hint)</p>
<p>Here is the skeleton of my main routine</p>
<p>def main(score_function):<br />
..stack = []<br />
..def push(pos, cost):<br />
&#8230;.score = score_function(pos, cost)<br />
&#8230;.heapq.heappush(stack, (score, (pos, cost)))<br />
..def pop():<br />
&#8230;.score, (pos, cost) = heapq.heappop(stack)<br />
&#8230;.return pos, cost</p>
<p>..while stack:<br />
&#8230;.cur_pos, cur_cost = pop() # lowest &#8217;score&#8217; entry to date<br />
&#8230;.# do stuff</p>
<p>The reason the score function is passed in is so you can try new ones easily.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
