|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Letournel\PathFinder\Algorithms\ShortestPath; |
|
4
|
|
|
|
|
5
|
|
|
use Letournel\PathFinder\AlgorithmShortestPath; |
|
6
|
|
|
use Letournel\PathFinder\Core\Heuristic; |
|
7
|
|
|
use Letournel\PathFinder\Core\Node; |
|
8
|
|
|
use Letournel\PathFinder\Core\NodeGrid; |
|
9
|
|
|
use Letournel\PathFinder\Core\NodeMap; |
|
10
|
|
|
use Letournel\PathFinder\Core\NodePath; |
|
11
|
|
|
use Letournel\PathFinder\Core\NodePriorityQueueMin; |
|
12
|
|
|
use Letournel\PathFinder\Distance; |
|
13
|
|
|
|
|
14
|
|
|
class AStar implements AlgorithmShortestPath |
|
15
|
|
|
{ |
|
16
|
|
|
/* |
|
17
|
|
|
* For more info see |
|
18
|
|
|
* http://en.wikipedia.org/wiki/A*_search_algorithm |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
private |
|
22
|
|
|
$distance, |
|
|
|
|
|
|
23
|
|
|
$grid, |
|
24
|
|
|
$heuristic; |
|
25
|
|
|
|
|
26
|
|
|
public function __construct(Distance $distance, Heuristic $heuristic) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->distance = $distance; |
|
29
|
|
|
$this->heuristic = $heuristic; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
public function setGrid(NodeGrid $grid) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->grid = $grid; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function computeLength(Node $source, Node $target) |
|
38
|
|
|
{ |
|
39
|
|
|
$shortestPath = $this->computePath($source, $target); |
|
40
|
|
|
|
|
41
|
|
|
return $shortestPath->computeLength($this->distance); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function computePath(Node $source, Node $target) |
|
45
|
|
|
{ |
|
46
|
|
|
if(! $this->grid instanceof NodeGrid) |
|
47
|
|
|
{ |
|
48
|
|
|
throw new \RuntimeException('Invalid Grid'); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$fScorePriorityQueue = new NodePriorityQueueMin(); |
|
52
|
|
|
$gScoreMap = new NodeMap(); |
|
53
|
|
|
$openedMap = new NodeMap(); |
|
54
|
|
|
$closedMap = new NodeMap(); |
|
55
|
|
|
$previousMap = new NodeMap(); |
|
56
|
|
|
|
|
57
|
|
|
$fScorePriorityQueue->insert($source, 0); |
|
58
|
|
|
$gScoreMap->insert($source, 0); |
|
|
|
|
|
|
59
|
|
|
$openedMap->insert($source); |
|
60
|
|
|
|
|
61
|
|
|
while(! $fScorePriorityQueue->isEmpty()) |
|
62
|
|
|
{ |
|
63
|
|
|
$node = $fScorePriorityQueue->extract(); |
|
64
|
|
|
$closedMap->insert($node); |
|
65
|
|
|
|
|
66
|
|
|
if($node->getId() === $target->getId()) |
|
67
|
|
|
{ |
|
68
|
|
|
return new NodePath($previousMap->lookupFrom($node)); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
$neighbors = $this->grid->getWalkableNeighbors($node); |
|
72
|
|
|
foreach($neighbors as $neighbor) |
|
73
|
|
|
{ |
|
74
|
|
|
if($closedMap->exists($neighbor)) |
|
75
|
|
|
{ |
|
76
|
|
|
continue; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$gScore = $gScoreMap->lookup($node) + $this->distance->compute($node, $neighbor); |
|
80
|
|
|
if(! $openedMap->exists($neighbor) || $gScore < $gScoreMap->lookup($neighbor)) |
|
81
|
|
|
{ |
|
82
|
|
|
$fScore = $gScore + $this->heuristic->compute($node, $target); |
|
83
|
|
|
$fScorePriorityQueue->insert($neighbor, $fScore); |
|
84
|
|
|
$gScoreMap->insert($neighbor, $gScore); |
|
|
|
|
|
|
85
|
|
|
$openedMap->insert($neighbor); |
|
86
|
|
|
$previousMap->insert($neighbor, $node); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
// no path found |
|
92
|
|
|
return array(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
Only declaring a single property per statement allows you to later on add doc comments more easily.
It is also recommended by PSR2, so it is a common style that many people expect.