Example #1
0
 public function step(PathNode $previousNode, $exploredPaths = array())
 {
     // TODO: If previousNode is null, try to find the next node using the graph
     // TODO: This should be fetched from the graph eventually
     //$unexploredNeighbors = $previousNode->computeNeighbors();
     // Make a random choice
     /** @var PathNode $randomChoice */
     $randomChoice = $previousNode->getRandomNextNode($previousNode->getUsedNodes());
     // Check if we have reached a dead-end
     if (is_null($randomChoice)) {
         $this->nodeGraph->addPathLengthStatistic($previousNode);
         return;
     }
     // Otherwise, prepare for the next step
     $randomChoiceNode = new PathNode($this->dimensions, $randomChoice);
     $randomChoiceNode->setParentNode($previousNode);
     $this->step($randomChoiceNode);
 }
Example #2
0
 /**
  * Attempt to update the best path
  *
  * @param PathNode $node
  */
 protected function updateBestPath(PathNode $node)
 {
     $pathLength = $node->getLength();
     if ($this->bestPathLength < $pathLength) {
         $this->bestPathLength = $pathLength;
         $this->bestPath = $node;
     }
 }
Example #3
0
 /**
  * Get the best path node or just null
  *
  * @throws Exception
  * @return null|PathNode
  */
 public function getBestPathTail()
 {
     // If we don't have any data, just return any
     if (empty($this->bestPathLengths)) {
         if ($this->initial) {
             $newNode = new PathNode($this->dimensions, $this->getStringIdentifier());
             $randomNode = new PathNode($this->dimensions, $newNode->getRandomNextNode());
             $randomNode->setParentNode($newNode);
             return $randomNode;
         } else {
             throw new Exception('This should not happen');
         }
     }
     $bestSoFar = 0;
     $bestPathIndex = null;
     foreach ($this->bestPathLengths as $stringIdentifier => $pathLength) {
         if ($pathLength > $bestSoFar) {
             $bestSoFar = $pathLength;
             $bestPathIndex = $stringIdentifier;
         }
     }
     return $this->bestPaths[$bestPathIndex];
 }