forked from RafaelPortacio/notwaze
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.cpp
109 lines (91 loc) · 3.61 KB
/
cli.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
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
#include <iostream>
#include <functional>
#include <math.h>
#include <time.h>
#include <chrono>
#include "graph.hpp"
#include "io.hpp"
#include "shortest-path.hpp"
int main() {
// Initialization variables
int algoritm = -1;
int shortest_or_fastest = -1;
std::string starting_point_str_x;
std::string starting_point_str_y;
std::string ending_point_str_x;
std::string ending_point_str_y;
std::cout << "... Loading index" << std::flush;
// Load graph
const Graph graph = load_graph_from_json_file("rj_graph_database.json");
// Algoritm
std::cout << ", pronto!" << std::endl;
std::cout << "Which algoritm to use?" << std::endl;
while (algoritm != 0 && algoritm != 1 && algoritm != 2) {
std::cout << "[0] Dijkstra" << std::endl
<< "[1] A* with euclidean heuristic" << std::endl
<< "[2] A* with manhattan heuristic" << std::endl;
std::cin >> algoritm;
}
while (shortest_or_fastest != 0 && shortest_or_fastest != 1) {
std::cout << "[0] Shortest path" << std::endl
<< "[1] Fastest path" << std::endl;
std::cin >> shortest_or_fastest;
}
// Starting point and ending point
std::cout << "Enter the origin: ";
std::cin >> starting_point_str_x;
std::cin >> starting_point_str_y;
std::cout << std::endl;
std::cout << "Enter the destination: ";
std::cin >> ending_point_str_x;
std::cin >> ending_point_str_y;
std::cout << std::endl;
starting_point_str_x = starting_point_str_x.substr(0, starting_point_str_x.size()-1);
ending_point_str_x = ending_point_str_x.substr(0, ending_point_str_x.size()-1);
double starting_point_x = std::stod(starting_point_str_x);
double starting_point_y = std::stod(starting_point_str_y);
double ending_point_x = std::stod(ending_point_str_x);
double ending_point_y = std::stod(ending_point_str_y);
// Get the desired method
ShortestPathMethod method;
if (algoritm == 0)
method = ShortestPathMethod::Dijkstra;
else if (algoritm == 1)
method = ShortestPathMethod::AStarEuclidean;
else if (algoritm == 2)
method = ShortestPathMethod::AStarManhattan;
else
throw std::runtime_error("bad shortest path method");
ShortestOrFastest short_or_fast;
if (shortest_or_fastest == 0)
short_or_fast = ShortestOrFastest::get_weight_length;
else
short_or_fast = ShortestOrFastest::get_weight_eta;
// Get shortest path
auto [compute_time, maybe_data] = get_path_data(graph,
{starting_point_x, starting_point_y},
{ending_point_x, ending_point_y},
method,
short_or_fast);
// Time and run code
if (!maybe_data)
return 1;
auto [eta, length, path] = *maybe_data;
// Print shortest path
if (algoritm == 0) {
std::cout << "The shortest path using Dijkstra is ";
} else if (algoritm == 1) {
std::cout << "The shortest path using A star with euclidean heuristic is ";
} else {
std::cout << "The shortest path using A star with manhattan heuristic is ";
};
std::cout << length;
std::cout << " meters (";
std::cout << (compute_time / 1000.0);
std::cout << " seconds)" << std::endl;
// Print path
std::cout << "Coordinates of the shortest path:" << std::endl;
for (const auto& pt : path)
std::cout << "[" << pt.first << ", " << pt.second << "]" << std::endl;
return 0;
}