-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathMain.cpp
More file actions
287 lines (243 loc) · 9.74 KB
/
Main.cpp
File metadata and controls
287 lines (243 loc) · 9.74 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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#include "crow.h"
#include "Graph.h"
#include "Algo.h"
#include "kdtree.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <unordered_map>
#include <limits>
#include <cmath>
#include <vector>
#include <set>
struct CORS
{
struct context {};
void before_handle(crow::request &req, crow::response &res, context &ctx)
{
res.add_header("Access-Control-Allow-Origin", "*");
res.add_header("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
res.add_header("Access-Control-Allow-Headers", "Content-Type, Authorization");
if (req.method == "OPTIONS"_method)
{
res.code = 200;
res.end();
}
}
void after_handle(crow::request &req, crow::response &res, context &ctx)
{
if (!res.headers.count("Access-Control-Allow-Origin"))
{
res.add_header("Access-Control-Allow-Origin", "*");
res.add_header("Access-Control-Allow-Methods", "GET, POST, PUT, OPTIONS");
res.add_header("Access-Control-Allow-Headers", "Content-Type, Authorization");
}
}
};
// Load node coordinates
void loadNodeCoordinates(Graph &g, const std::string &filename)
{
std::ifstream in(filename);
if (!in.is_open())
{
std::cerr << " Failed to open " << filename << std::endl;
return;
}
std::string line;
std::getline(in, line); // skip header
while (std::getline(in, line))
{
std::stringstream ss(line);
std::string idStr, latStr, lonStr;
if (!std::getline(ss, idStr, ',')) continue;
if (!std::getline(ss, latStr, ',')) continue;
if (!std::getline(ss, lonStr, ',')) continue;
try
{
long long id = std::stoll(idStr);
double lat = std::stod(latStr);
double lon = std::stod(lonStr);
g.addNode(id, lat, lon);
}
catch (...) { continue; }
}
std::cout << " Node coordinates loaded from " << filename << std::endl;
}
// Load graph edges
Graph loadGraph(const std::string& filename, Graph& g) {
std::ifstream in(filename);
if (!in.is_open()) {
std::cerr << " Failed to open " << filename << std::endl;
return g;
}
std::string line;
long long currentNode = -1;
while (std::getline(in, line)) {
if (line.empty()) continue;
std::stringstream ss(line);
std::string token;
ss >> token;
if (token == "Node:") {
ss >> currentNode;
if (!g.get_nodes().count(currentNode))
g.addNode(currentNode, 0, 0); // placeholder
} else {
long long neighbor = std::stoll(token);
double weight; ss >> weight;
if (!g.get_nodes().count(neighbor))
g.addNode(neighbor, 0, 0); // placeholder
g.adEdge(currentNode, neighbor, weight);
}
}
std::cout << " Graph edges loaded from " << filename << std::endl;
return g;
}
int main()
{
crow::App<CORS> app;
Graph g;
loadNodeCoordinates(g, "nodes.csv");
loadGraph("nodes.txt", g);
g.buildNodeIndexMapping();
std::cout << " Node index mapping built. Total indexed nodes: " << g.indexToId.size() << std::endl;
Algorithms algo;
// Build KD-tree from graph nodes
std::vector<KDPoint> kdpoints;
kdpoints.reserve(g.get_nodes().size());
for (const auto &p : g.get_nodes()) {
long long id = p.first;
const Node &node = p.second;
KDPoint kp;
kp.id = id;
kp.lat = node.get_latitude();
kp.lon = node.get_longitude();
kdpoints.push_back(kp);
}
KDTree kdt;
kdt.build(kdpoints);
// Health check
CROW_ROUTE(app, "/")([]() { return " Server is running!"; });
// Shortest path route
CROW_ROUTE(app, "/shortest-path").methods("POST"_method)([&](const crow::request &req)
{
try {
auto body = crow::json::load(req.body);
if (!body || !body.has("start") || !body.has("end") ||
!body["start"].has("lat") || !body["start"].has("lng") ||
!body["end"].has("lat") || !body["end"].has("lng"))
{
return crow::response(400, "Invalid JSON or missing start/end lat/lng");
}
double startLat = body["start"]["lat"].d();
double startLng = body["start"]["lng"].d();
double endLat = body["end"]["lat"].d();
double endLng = body["end"]["lng"].d();
// Tune K as needed (8..32)
const int K = 8;
// Valid predicate for KD-tree: exclude nodes with no neighbors
auto validPredicate = [&](long long id) -> bool {
const auto &neighbors = g.getNeighbors(id);
return !neighbors.empty();
};
// Get K nearest candidates for start and end
auto startCandidates = kdt.kNearest(startLat, startLng, K, [&](long long id){
// also require node exists and has neighbors
if (!g.hasNode(id)) return false;
return validPredicate(id);
});
auto endCandidates = kdt.kNearest(endLat, endLng, K, [&](long long id){
if (!g.hasNode(id)) return false;
return validPredicate(id);
});
if (startCandidates.empty() || endCandidates.empty()) {
return crow::response(500, "Failed to find nearest connected nodes");
}
double totalDistance = std::numeric_limits<double>::infinity();
long long chosenStart = -1, chosenEnd = -1;
// Try A* on pairs of candidates until a path is found.
// We keep the behavior "try multiple nearest" but avoid scanning entire graph.
bool found = false;
for (long long sId : startCandidates) {
for (long long eId : endCandidates) {
// optional short-circuit: same node
if (sId == eId) {
// small sanity check: still run A* to ensure it's valid
}
// call A*
double dist = algo.Astar(g, sId, eId);
if (dist != std::numeric_limits<double>::infinity()) {
totalDistance = dist;
chosenStart = sId;
chosenEnd = eId;
found = true;
break;
}
// else try next end candidate
}
if (found) break;
}
if (!found) {
// If no path found among top-K candidates, you can choose to:
// - increase K and retry
// - or fallback to original scan behaviour (try nearest one-by-one).
// For now, we will expand K progressively once (doubling) up to a reasonable cap.
int newK = std::min((int)g.get_nodes().size(), K * 4);
if (newK > K) {
auto startCandidates2 = kdt.kNearest(startLat, startLng, newK, [&](long long id){
if (!g.hasNode(id)) return false;
return validPredicate(id);
});
auto endCandidates2 = kdt.kNearest(endLat, endLng, newK, [&](long long id){
if (!g.hasNode(id)) return false;
return validPredicate(id);
});
for (long long sId : startCandidates2) {
for (long long eId : endCandidates2) {
double dist = algo.Astar(g, sId, eId);
if (dist != std::numeric_limits<double>::infinity()) {
totalDistance = dist;
chosenStart = sId;
chosenEnd = eId;
found = true;
break;
}
}
if (found) break;
}
}
}
if (!found)
return crow::response(500, "No path found between nearest candidates");
// Read path coordinates from file (preserving your existing Algo/printPath CSV behavior)
std::ifstream pathFile("path_cordinates.csv");
if (!pathFile.is_open()) return crow::response(500, "Failed to read path coordinates");
std::string line;
std::getline(pathFile, line); // skip header if present
crow::json::wvalue result;
result["path"] = crow::json::wvalue::list();
int idx = 0;
while (std::getline(pathFile, line)) {
std::stringstream ss(line);
std::string latStr, lonStr;
if (!std::getline(ss, latStr, ',')) break;
if (!std::getline(ss, lonStr, ',')) break;
result["path"][idx]["lat"] = std::stod(latStr);
result["path"][idx]["lng"] = std::stod(lonStr);
idx++;
}
pathFile.close();
result["distance_meters"] = totalDistance;
result["start_node"] = chosenStart;
result["end_node"] = chosenEnd;
crow::response res(result);
res.add_header("Content-Type", "application/json");
return res;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return crow::response(500, "Internal server error");
}
});
std::cout << "Crow server started on port 5000\n";
int port = std::stoi(std::getenv("PORT") ? std::getenv("PORT") : "5000");
app.port(port).multithreaded().run();
}