-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestFirstSearch.h
61 lines (57 loc) · 1.86 KB
/
BestFirstSearch.h
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
//
// Created by shachar Meir on 04/02/2020.
//
#ifndef TEST__BESTFIRSTSEARCH_H_
#define TEST__BESTFIRSTSEARCH_H_
#include <queue>
#include <set>
#include "Searcher.h"
#include "Heap.h"
#include "StateComparator.h"
#include "StatePointerComparator.h"
template <typename T>
class BestFirstSearch: public Searcher<T> {
public:
std::list<State<T> *> search(Searchable<T> *searchable) override {
searchable->reset();
std::list<State<T>*> open_list;
std::set<State<T>*> closed_list;
State<T>* start = searchable->getInitialState();
open_list.push_back(start);
while (!open_list.empty()){
open_list.sort(StatePointerComparator<T>());
State<T>* current = open_list.back();
open_list.pop_back();
if (searchable->isGoal(current)){
auto path = this->backTracePath(current);
searchable->reset();
return path;
}
std::list<State<T>*> neighbors = searchable->getNeighborStates(current);
for (State<T>* neighbor: neighbors){
if (closed_list.find(neighbor) == closed_list.end()) {
if (std::find(open_list.begin(), open_list.end(), neighbor) == open_list.end()) {
neighbor->SetCameFrom(current);
open_list.push_back(neighbor);
}
double n_cost = neighbor->GetShortestPathCost();
double c_cost = current->GetShortestPathCost();
if (n_cost > c_cost + neighbor->GetCost() || n_cost == -1) {
neighbor->SetShortestPathCost(c_cost + neighbor->GetCost());
}
if (searchable->isGoal(neighbor)) {
auto path = this->backTracePath(neighbor);
searchable->reset();
return path;
}
}
}
closed_list.insert(current);
}
return std::list<State<T> *>();
}
int getNumberOfNodesEvaluated() override {
return 0;
}
};
#endif //TEST__BESTFIRSTSEARCH_H_