This is the code I got from the site here , and I would like to know if this implementation of A * is correct. I looked at it and compared it to the wikipedia page, and it seems valid. The reason I ask is because the site says that there is an error in this code, I tried to find it, but I can not find it. I want to change it, so that it accepts the beginning and assignment as an input parameter
<?php class AStarSolver { function solve(&$s) { include_once('PQueue.class.php'); $o = new PQueue(); $l = array(); $c = array(); $p = array(); $a = $s->getStartIndex(); $z = $s->getGoalIndex(); $d = $s->goalDistance($a); $n0 = array('g'=>0, 'h'=>$d, 'i'=>$a, 'p'=>NULL, 'f'=>$d); $o->push($n0, -$d); $l[$a] = TRUE; while (! $o->isEmpty()) { $n = $o->pop(); if ($n['i'] == $z) { while ($n) { $p[] = $n['i']; $n = $n['p']; } break; } foreach ($s->getNeighbors($n['i']) as $j => $w) { if ((isset($l[$j]) || isset($c[$j])) && isset($m) && $m['g'] <= $n['g']+$w) continue; $d = $s->goalDistance($j); $m = array('g'=>$n['g']+$w, 'h'=>$d, 'i'=>$j, 'p'=>$n, 'f'=>$n['g']+$w+$d); if (isset($c[$j])) unset($c[$j]); if (! isset($l[$j])) { $o->push($m, -$m['f']); $l[$j] = TRUE; } } $c[$n['i']] = $n; } return $p; } } ?>
The code in Pqueue can be found here.
algorithm php a-star shortest-path
aherlambang
source share