forked from RafaelPortacio/notwaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shortest-path.hpp
256 lines (206 loc) · 9.11 KB
/
shortest-path.hpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#pragma once
#include <tuple>
#include <optional>
#include <vector>
#include <algorithm>
#include <queue>
#include <chrono>
#include "graph.hpp"
#include "priority-queue.hpp"
// Comparator
namespace {
struct Compare {
bool operator() (const std::pair<node_id, weight_t>& l, const std::pair<node_id, weight_t>& r) {
return l.second < r.second;
}
};
}
// Methods enum
enum class ShortestPathMethod {
Dijkstra,
AStarEuclidean,
AStarManhattan,
};
enum class ShortestOrFastest {
get_weight_eta,
get_weight_length,
};
// Dijkstra
template <typename GetWeight>
std::optional<std::vector<node_id>> shortest_path_dijkstra(const Graph& graph,
const node_id& start_point, const node_id& end_point,
GetWeight get_weight) {
// Base case
if (start_point == end_point)
return {{start_point}};
// Storage structures
PriorityQueue<std::pair<node_id, weight_t>, Compare> frontier;
std::unordered_map<node_id, node_id> came_from;
std::unordered_map<node_id, weight_t> cost_so_far;
// Initiazation
frontier.push({start_point, 0});
cost_so_far.insert({start_point, 0});
// Main loop
while (!frontier.empty()) {
std::pair<node_id, weight_t> current = frontier.top();
frontier.pop();
if (current.first == end_point)
break;
for (auto iter = graph.cbegin_outedges(current.first); iter != graph.cend_outedges(current.first); ++iter) {
// Fill in came_from and cost_so_far
weight_t new_cost = cost_so_far.at(current.first) + iter->second.eta;
if (!cost_so_far.count(iter->first) || new_cost < cost_so_far.at(iter->first)) {
cost_so_far[iter->first] = new_cost;
weight_t priority = new_cost; // there is no heuristic
frontier.push({iter->first, priority});
came_from[iter->first] = current.first;
}
}
}
if (came_from.find(end_point) == came_from.cend())
// If `end_point` isn't in `came_from`, then we haven't really found a path
return std::nullopt;
// Constructor path
std::vector<node_id> path {end_point};
node_id current = end_point;
while (current != start_point) {
current = came_from.at(current);
path.push_back(current);
}
return path;
}
// A*
template <typename Heuristic, typename GetWeight>
std::optional<std::vector<node_id>> shortest_path_astar(const Graph& graph,
const node_id& start_point, const node_id& end_point,
Heuristic heuristic, GetWeight get_weight) {
// Base case
if (start_point == end_point)
return {{start_point}};
// Storage structures
PriorityQueue<std::pair<node_id, weight_t>, Compare> frontier;
std::unordered_map<node_id, node_id> came_from;
std::unordered_map<node_id, weight_t> cost_so_far;
// Initiazation
frontier.push({start_point, 0});
cost_so_far.insert({start_point, 0});
// Main loop
while (!frontier.empty()) {
std::pair<node_id, weight_t> current = frontier.top();
frontier.pop();
if (current.first == end_point)
break;
for (auto iter = graph.cbegin_outedges(current.first); iter != graph.cend_outedges(current.first); ++iter) {
// Fill in came_from and cost_so_far
weight_t new_cost = cost_so_far.at(current.first) + get_weight(iter->second);
if (!cost_so_far.count(iter->first) || new_cost < cost_so_far.at(iter->first)) {
cost_so_far[iter->first] = new_cost;
weight_t priority = new_cost + heuristic(graph[iter->first], graph[end_point]);
frontier.push({iter->first, priority});
came_from[iter->first] = current.first;
}
}
}
if (came_from.find(end_point) == came_from.cend())
// If `end_point` isn't in `came_from`, then we haven't really found a path
return std::nullopt;
// Constructor path
std::vector<node_id> path {end_point};
node_id current = end_point;
while (current != start_point) {
current = came_from.at(current);
path.push_back(current);
}
return path;
}
inline weight_t euclidean_heuristic(const Node& current, const Node& destination) {
return euclidean_distance(current, destination);
}
inline weight_t manhattan_heuristic(const Node& current, const Node& destination) {
return manhattan_distance(current, destination);
}
inline weight_t get_weight_eta(const Edge& edge) {
return edge.eta;
}
inline weight_t get_weight_length(const Edge& edge) {
return edge.length;
}
std::pair<unsigned long, std::optional<std::tuple<weight_t, weight_t, std::vector<std::pair<double, double>>>>>
get_path_data(const Graph& graph,
const std::pair<double, double>& starting_point,
const std::pair<double, double>& ending_point,
ShortestPathMethod method,
ShortestOrFastest short_or_fast) {
auto [start_edge, end_edge, start_proj, end_proj,
start_proj_fraction, end_proj_fraction]
= graph.coords_to_ids(starting_point, ending_point);
node_id starting_point_id = start_edge.second;
node_id ending_point_id = end_edge.first;
if(start_proj.first == 0)
starting_point_id = start_edge.first;
if(end_proj.first == 1)
ending_point_id = end_edge.second;
std::pair<node_id,node_id> end_edge_swap = {end_edge.second, end_edge.first};
if((start_edge == end_edge && 1 - start_proj_fraction <= end_proj_fraction)||
start_edge == end_edge_swap) {
std::vector<std::pair<double,double>> coord_path {{end_proj.second.latitude, end_proj.second.longitude},
{start_proj.second.latitude, start_proj.second.longitude}};
const Edge& edge = graph.get_edge(start_edge.first, start_edge.second);
weight_t eta = edge.eta*std::abs(1 - start_proj_fraction - end_proj_fraction);
weight_t length = edge.length*std::abs(1 - start_proj_fraction - end_proj_fraction);
return {0, {{eta, length, coord_path}}};
}
std::optional<std::vector<node_id>> maybe_path;
auto start_time = std::chrono::high_resolution_clock::now();
switch (method) {
case ShortestPathMethod::Dijkstra:
if (short_or_fast == ShortestOrFastest::get_weight_eta)
maybe_path = shortest_path_dijkstra(graph, starting_point_id, ending_point_id, get_weight_eta);
else
maybe_path = shortest_path_dijkstra(graph, starting_point_id, ending_point_id, get_weight_length);
break;
case ShortestPathMethod::AStarEuclidean:
if (short_or_fast == ShortestOrFastest::get_weight_eta)
maybe_path = shortest_path_astar(graph, starting_point_id, ending_point_id, euclidean_heuristic, get_weight_eta);
else
maybe_path = shortest_path_astar(graph, starting_point_id, ending_point_id, euclidean_heuristic, get_weight_length);
break;
case ShortestPathMethod::AStarManhattan:
if (short_or_fast == ShortestOrFastest::get_weight_eta)
maybe_path = shortest_path_astar(graph, starting_point_id, ending_point_id, manhattan_heuristic, get_weight_eta);
else
maybe_path = shortest_path_astar(graph, starting_point_id, ending_point_id, manhattan_heuristic, get_weight_length);
break;
}
auto end_time = std::chrono::high_resolution_clock::now();
unsigned long int compute_time
= std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time).count();
if (!maybe_path)
return {compute_time, std::nullopt};
// return json::object({{"compute-time", compute_time}});
std::vector<node_id> path = *maybe_path;
std::vector<std::pair<double, double>> coord_path = {};
for (auto it = path.begin(); it != path.end(); it++)
coord_path.push_back({graph.get_node(*it).latitude, graph.get_node(*it).longitude});
if (end_proj.first == 2)
coord_path.insert(coord_path.begin(),
{(end_proj.second).latitude, (end_proj.second).longitude});
if (start_proj.first == 2)
coord_path.push_back({(start_proj.second).latitude, (start_proj.second).longitude});
weight_t eta = 0;
weight_t length = 0;
for (size_t i = 0; i < path.size()-1; ++i) {
const Edge& edge = graph.get_edge(path[i+1], path[i]);
eta += edge.eta;
length += edge.length;
}
{
const Edge& edge = graph.get_edge(start_edge.first, start_edge.second);
eta += edge.eta*start_proj_fraction;
length += edge.length*start_proj_fraction;
const Edge& edge2 = graph.get_edge(end_edge.first, end_edge.second);
eta += edge2.eta*end_proj_fraction;
length += edge2.length*end_proj_fraction;
}
return {compute_time, {{eta, length, coord_path}}};
}