-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAstar.h
More file actions
67 lines (63 loc) · 2.65 KB
/
Astar.h
File metadata and controls
67 lines (63 loc) · 2.65 KB
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
//
// Created by chen96 on 23/01/2020.
//
#ifndef PROBLEMSOLVER__ASTAR_H_
#define PROBLEMSOLVER__ASTAR_H_
#include <vector>
#include "MutualSearches.h"
using namespace std;
template<class Problem>
class Astar : public MutualSearches<Problem> {
private:
int numberOfNodesEvaluated = 0;
public:
// The function returns the back trace of the cheapest path from start state to end state
vector<State<Problem> *> search(Searchable<Problem> *searchable) {
// Gets the initial state
State<Problem> *currState = searchable->getInitialState();
// Sets the heuristic cost of the state
currState->setHeuristicCost(searchable->getDistance(currState, searchable->getGoalState()));
// Adds the state to the open list
this->addToOpenList(currState);
while (this->openListSize() > 0) {
// Pop the open list
currState = this->popOpenListAstar();
// Checks if the current state is the goal state
if (currState->Equals(searchable->getGoalState())) {
// Returns the back trace
return this->backTrace(searchable->getInitialState(), searchable->getGoalState());
}
numberOfNodesEvaluated++;
// Adds the current state to the colsed list
this->addToClosedList(currState);
// Gets the neighbors of the current state
list<State<Problem> *> *neighbors = searchable->getAllPossibleStates(currState);
// Goes over the neighbors
for (typename list<State<Problem> *>::iterator it = neighbors->begin(); it != neighbors->end(); ++it) {
// g = The known distance between the current state and its neighbor
double g = currState->getCumulativeCost() + (*it)->getCost();
// h = Estimate the distance between the current state and the destination state
double h = searchable->getDistance((*it), searchable->getGoalState());
double f = g + h;
// Checks if the neighbor is not in the open list and not in the closed list
if (!this->closedContains(*it) && !this->openContains(*it)) {
// Sets the neighbor fields
(*it)->setCameFrom(currState);
(*it)->setCumulativeCost(g);
(*it)->setHeuristicCost(h);
this->addToOpenList(*it);
} else if (!this->closedContains(*it) && this->openContains(*it)) {
// Checks if f of the currState is smaller than the f of the neighbor
if (f < (*it)->getCumulativeCost() + (*it)->getHeuristicCost()) {
// Sets the neighbor fields
(*it)->setCameFrom(currState);
(*it)->setCumulativeCost(g);
(*it)->setHeuristicCost(h);
}
}
}
}
throw "no Path";
}
};
#endif //PROBLEMSOLVER__ASTAR_H_