-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutualSearches.h
More file actions
126 lines (109 loc) · 3.85 KB
/
MutualSearches.h
File metadata and controls
126 lines (109 loc) · 3.85 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
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
//
// Created by duni on 16/01/2020.
//
#ifndef PROBLEMSOLVER_MUTUALSEARCHES_H
#define PROBLEMSOLVER_MUTUALSEARCHES_H
#include "Searcher.h"
#include <list>
//#include <hash_map>
#include <unordered_set>
using namespace std;
template<class P>
class MutualSearches : public Searcher<P> {
private:
list<State<P> *> *openList;
unordered_set<State<P> *> *closed;
int evaluatedNodes;
public:
// The Constructor
MutualSearches<P>() {
this->openList = new list<State<P> *>();
this->evaluatedNodes = 0;
this->closed = new unordered_set<State<P> *>();
}
// Returns the back trace of the path from state start to state end
vector<State<P> *> backTrace(State<P> *start, State<P> *end) {
vector<State<P> *> nodeTrace;
// Push the end state
nodeTrace.emplace(nodeTrace.begin(), end);
// The current state is the "cameFrom" state of the end state
State<P> *current = end->getCameFrom();
// The loop runs until it gets to the start state
while (!(current->Equals(start))) {
// Pudh the current state to the vector
nodeTrace.emplace(nodeTrace.begin(), current);
current = current->getCameFrom();
}
// Push the start state to the vector
nodeTrace.emplace(nodeTrace.begin(), start);
return nodeTrace;
};
virtual vector<State<P> *> search(Searchable<P> *) = 0;
// Returns the size of the open list
int openListSize() {
return openList->size();
}
// Returns the number of nodes evaluated
int getNumberOfNodesEvaluated() {
return evaluatedNodes;
}
// Adds the state to the open list
void addToOpenList(State<P> *s) {
openList->emplace_back(s);
}
// Checks if the open list contains the given state
bool openContains(State<P> *s) {
for (typename list<State<P> *>::iterator iter = this->openList->begin();
iter != this->openList->end(); iter++) {
if ((*iter)->getState() == s->getState()) {
return true;
}
}
return false;
}
// Adds the given state to the closed list
void addToClosedList(State<P> *s) {
closed->insert(s);
}
// Checks if the closed list contains the given state
bool closedContains(State<P> *s) {
for (typename unordered_set<State<P> *>::iterator iter = this->closed->begin(); iter != this->closed->end();
iter++) {
if ((*iter)->getState() == s->getState()) {
return true;
}
}
return false;
}
protected:
// The function pop the open list
State<P> *popOpenList() {
// Initialize the min state
State<P> *min = *openList->begin();
for (typename list<State<P> *>::iterator it = openList->begin(); it != openList->end(); ++it) {
// Checks id the cumulative cost of the current state is smaller than the cumulative cost of min state
if ((*it)->getCumulativeCost() < min->getCumulativeCost()) {
min = *it;
}
}
openList->remove(min);
evaluatedNodes++;
return min;
}
// The function pop the open list of Astar algorithm
State<P> *popOpenListAstar() {
// Initialize the min state
State<P> *min = *openList->begin();
for (typename list<State<P> *>::iterator it = openList->begin(); it != openList->end(); ++it) {
// Checks id the f value (g+h) of the current state is smaller than the f value (g+h) of min state
if (((*it)->getCumulativeCost() + (*it)->getHeuristicCost())
< (min->getCumulativeCost() + min->getHeuristicCost())) {
min = *it;
}
}
openList->remove(min);
evaluatedNodes++;
return min;
}
};
#endif //PROBLEMSOLVER_MUTUALSEARCHES_H