-
Notifications
You must be signed in to change notification settings - Fork 0
/
BestFirstSearch.cpp
65 lines (55 loc) · 2.07 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
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
#include "BestFirstSearch.h"
#include "CityGraph.h"
#include <queue>
#include <functional>
#include <vector>
#include <iostream>
using std::vector;
using std::string;
using std::unordered_map;
using std::priority_queue;
vector<string> bestFirstSearch(const CityGraph& graph, const string& start, const string& goal, double& totalDistance) {
// Convert to HeuristicCity graph if not already
unordered_map<string, HeuristicCity> heuristicGraph;
for (const auto& pair : graph) {
const auto& city = pair.second;
heuristicGraph[pair.first] = HeuristicCity(city.name, city.latitude, city.longitude, 0.0);
std::cout << heuristicGraph[pair.first].adjacents.size() << std::endl;
}
// Assume goal city coordinates are known and update heuristics
const HeuristicCity& goalCity = heuristicGraph.at(goal);
for (auto& pair : heuristicGraph) {
pair.second.updateHeuristic(goalCity);
}
for (auto& pair : graph) {
heuristicGraph[pair.first].adjacents = pair.second.adjacents;
}
auto compare = [&](const string& a, const string& b) {
return heuristicGraph.at(a).heuristic > heuristicGraph.at(b).heuristic;
};
priority_queue<string, vector<string>, decltype(compare)> frontier(compare);
unordered_map<string, string> cameFrom;
frontier.push(start);
cameFrom[start] = "";
while (!frontier.empty()) {
string current = frontier.top();
frontier.pop();
if (current == goal) {
vector<string> path;
for (string at = goal; at != ""; at = cameFrom[at]) {
path.push_back(at);
}
reverse(path.begin(), path.end());
return path;
} else {
std::cout << heuristicGraph.at(current).adjacents.size() << std::endl;
for (const string& next : heuristicGraph.at(current).adjacents) {
if (!cameFrom.count(next)) {
frontier.push(next);
cameFrom[next] = current;
}
}
}
}
return {}; // If no path found
}