Note that there are some explanatory texts on larger screens.

plurals
  1. POA* implementation in PHP validation
    text
    copied!<p>This is a code that I got from the site <a href="http://codezilla.com/projects/a-star/" rel="nofollow">here</a> and I'd like to know whether this implementation of A* is correct. I have looked at it and compare it with the wikipedia page and it seems valid.. The reason why I ask is because in the site it says there is still a bug in this code, I tried finding it but can't find any. I want to change it though so that it takes an origin and destination as input parameter</p> <pre><code>&lt;?php class AStarSolver { function solve(&amp;$s) { include_once('PQueue.class.php'); $o = new PQueue(); $l = array(); $c = array(); $p = array(); $a = $s-&gt;getStartIndex(); $z = $s-&gt;getGoalIndex(); $d = $s-&gt;goalDistance($a); $n0 = array('g'=&gt;0, 'h'=&gt;$d, 'i'=&gt;$a, 'p'=&gt;NULL, 'f'=&gt;$d); $o-&gt;push($n0, -$d); $l[$a] = TRUE; while (! $o-&gt;isEmpty()) { $n = $o-&gt;pop(); if ($n['i'] == $z) { while ($n) { $p[] = $n['i']; $n = $n['p']; } break; } foreach ($s-&gt;getNeighbors($n['i']) as $j =&gt; $w) { if ((isset($l[$j]) || isset($c[$j])) &amp;&amp; isset($m) &amp;&amp; $m['g'] &lt;= $n['g']+$w) continue; $d = $s-&gt;goalDistance($j); $m = array('g'=&gt;$n['g']+$w, 'h'=&gt;$d, 'i'=&gt;$j, 'p'=&gt;$n, 'f'=&gt;$n['g']+$w+$d); if (isset($c[$j])) unset($c[$j]); if (! isset($l[$j])) { $o-&gt;push($m, -$m['f']); $l[$j] = TRUE; } } $c[$n['i']] = $n; } return $p; } } ?&gt; </code></pre> <p>The code to the Pqueue can be found <a href="http://codetidy.com/443" rel="nofollow">here</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload