예제 #1
0
 /**
  * Run the pathfinder algorithm
  *
  * @return \SplStack
  */
 public function run()
 {
     $start = $this->start;
     $goal = $this->goal;
     $open = new Set([$start], function (NodeInterface $node) use($goal) {
         return $this->getHeuristic($goal, $node);
     });
     $closed = new Set();
     $open->add($start);
     $path = new \SplStack();
     $step = null;
     while (!$open->isEmpty()) {
         $current = $open->current();
         $path->push($current->getNode());
         $step = new Step($current->getNode(), $step);
         if ($current->getNode() == $goal) {
             break;
         }
         $closed->add($current->getNode());
         $neighbours = $current->getNode()->getNeighbours();
         foreach ($neighbours as $neighbour) {
             if ($closed->has($neighbour)) {
                 continue;
             }
             if (!$open->has($neighbour)) {
                 $open->add($neighbour, $this->getHeuristic($current->getNode(), $neighbour) + $neighbour->getCost());
             }
         }
     }
     return $path;
 }
예제 #2
0
 /**
  * Test constructor adds nodes and the heuristic callback
  */
 public function testConstruct()
 {
     $nodes = [new Node(1, 1)];
     $heuristic = function (NodeInterface $node) {
         return rand(1, 10);
     };
     $set = new Set($nodes, $heuristic);
     $this->assertEquals($heuristic, $set->getHeuristic());
     $this->assertEquals(current($nodes), current($set->getItems())->getNode());
 }