-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAStar.php
204 lines (165 loc) · 5.43 KB
/
AStar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* @template TState
*/
class AStar
{
/** @var DomainLogicInterface<TState> */
private DomainLogicInterface $domainLogic;
/** @var NodeCollectionInterface<TState> | NodeHashTable<TState> */
private NodeCollectionInterface $openList;
/** @var NodeCollectionInterface<TState> | NodeHashTable<TState> */
private NodeCollectionInterface $closedList;
/**
* @param DomainLogicInterface<TState> $domainLogic
*/
public function __construct(DomainLogicInterface $domainLogic)
{
$this->domainLogic = $domainLogic;
$this->openList = new NodeHashTable();
$this->closedList = new NodeHashTable();
}
/**
* @param TState $start
* @param TState $goal
* @return iterable<TState>
*/
public function run(mixed $start, mixed $goal): iterable
{
$startNode = new Node($start);
$goalNode = new Node($goal);
return $this->executeAlgorithm($startNode, $goalNode);
}
/**
* @param Node<TState> $start
* @param Node<TState> $goal
* @return iterable<TState>
*/
private function executeAlgorithm(Node $start, Node $goal): iterable
{
$path = [];
$this->clear();
$start->setG(0);
$start->setH($this->calculateEstimatedCost($start, $goal));
$this->openList->add($start);
while (!$this->openList->isEmpty()) {
/** @var Node<TState> $currentNode Cannot be null because the open list is not empty */
$currentNode = $this->openList->extractBest();
$this->closedList->add($currentNode);
if ($currentNode->getId() === $goal->getId()) {
$path = $this->generatePathFromStartNodeTo($currentNode);
break;
}
$successors = $this->getAdjacentNodesWithTentativeScore($currentNode, $goal);
$this->evaluateSuccessors($successors, $currentNode);
}
return $path;
}
/**
* Sets the algorithm to its initial state
*/
private function clear(): void
{
$this->openList->clear();
$this->closedList->clear();
}
/**
* @param Node<TState> $node
* @return iterable<Node<TState>>
*/
private function generateAdjacentNodes(Node $node): iterable
{
$adjacentNodes = [];
$adjacentStates = $this->domainLogic->getAdjacentNodes($node->getState());
foreach ($adjacentStates as $state) {
$adjacentNodes[] = new Node($state);
}
return $adjacentNodes;
}
/**
* @param Node<TState> $node
* @param Node<TState> $adjacent
* @return float | int
*/
private function calculateRealCost(Node $node, Node $adjacent): float | int
{
$state = $node->getState();
$adjacentState = $adjacent->getState();
return $this->domainLogic->calculateRealCost($state, $adjacentState);
}
/**
* @param Node<TState> $start
* @param Node<TState> $end
* @return float | int
*/
private function calculateEstimatedCost(Node $start, Node $end): float | int
{
$startState = $start->getState();
$endState = $end->getState();
return $this->domainLogic->calculateEstimatedCost($startState, $endState);
}
/**
* @param Node<TState> $node
* @return iterable<TState>
*/
private function generatePathFromStartNodeTo(Node $node): iterable
{
$path = [];
$currentNode = $node;
while ($currentNode !== null) {
array_unshift($path, $currentNode->getState());
$currentNode = $currentNode->getParent();
}
return $path;
}
/**
* @param Node<TState> $node
* @param Node<TState> $goal
* @return iterable<Node<TState>>
*/
private function getAdjacentNodesWithTentativeScore(Node $node, Node $goal): iterable
{
$nodes = $this->generateAdjacentNodes($node);
foreach ($nodes as $adjacentNode) {
$adjacentNode->setG($node->getG() + $this->calculateRealCost($node, $adjacentNode));
$adjacentNode->setH($this->calculateEstimatedCost($adjacentNode, $goal));
}
return $nodes;
}
/**
* @param iterable<Node<TState>> $successors
* @param Node<TState> $parent
*/
private function evaluateSuccessors(iterable $successors, Node $parent): void
{
foreach ($successors as $successor) {
if ($this->nodeAlreadyPresentInListWithBetterOrSameRealCost($successor, $this->openList)) {
continue;
}
if ($this->nodeAlreadyPresentInListWithBetterOrSameRealCost($successor, $this->closedList)) {
continue;
}
$successor->setParent($parent);
$this->closedList->remove($successor);
$this->openList->add($successor);
}
}
/**
* @param Node<TState> $node
* @param NodeCollectionInterface<TState> $nodeList
* @return bool
*/
private function nodeAlreadyPresentInListWithBetterOrSameRealCost(
Node $node,
NodeCollectionInterface $nodeList
): bool {
if ($nodeList->contains($node)) {
/** @var Node<TState> $nodeInList Cannot be null because the list contains it */
$nodeInList = $nodeList->get($node->getId());
if ($node->getG() >= $nodeInList->getG()) {
return true;
}
}
return false;
}
}