-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestFirstSearch.cpp
29 lines (28 loc) · 1.01 KB
/
BestFirstSearch.cpp
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
#include "BestFirstSearch.h"
SearchSolution* BestFirstSearch::search(Searchable<Point>* searchable) {
_numOfNodesEvaluated = 0;
State<Point>* start = searchable->getInitialState();
State<Point>* goal = searchable->getGoalState();
_open.push(start, start->getCost());
++_numOfNodesEvaluated;
while (!_open.empty()) {
State<Point>* minimal = _open.pop();
_close.push(minimal, searchable->getPathCost(minimal));
if (minimal->equals(goal))
break;
else
for (State<Point>* neighbor : searchable->getNeighbors(minimal)) {
++_numOfNodesEvaluated;
int previousCost = searchable->getPathCost(neighbor);
int newCost = searchable->getPathCost(minimal) + neighbor->getCost();
if (!_close.exists(neighbor) && !_open.exists(neighbor)) {
neighbor->setParent(minimal);
_open.push(neighbor, searchable->getPathCost(neighbor));
} else if (newCost < previousCost) {
neighbor->setParent(minimal);
_open.push(neighbor, newCost);
}
}
}
return new SearchSolution(searchable->getPath(goal));
}