-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPathFinder.cpp
More file actions
137 lines (115 loc) · 4.33 KB
/
Copy pathPathFinder.cpp
File metadata and controls
137 lines (115 loc) · 4.33 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
#include "PathFinder.h"
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <limits>
#include <cmath>
PathFinder::PathFinder(std::shared_ptr<RailNetwork> net) : network(net) {}
double PathFinder::calculateHeuristic(int station1_id, int station2_id) const {
auto s1 = network->getStation(station1_id);
auto s2 = network->getStation(station2_id);
if (!s1 || !s2) return 0.0;
return s1->location.distanceTo(s2->location);
}
std::vector<int> PathFinder::reconstructPath(const std::map<int, int>& came_from, int start, int goal) const {
std::vector<int> path;
int current = goal;
while (current != start) {
path.push_back(current);
auto it = came_from.find(current);
if (it == came_from.end()) {
return {};
}
current = it->second;
}
path.push_back(start);
std::reverse(path.begin(), path.end());
return path;
}
std::vector<int> PathFinder::findOptimalPath(int start_station_id, int goal_station_id) {
if (start_station_id == goal_station_id) {
return {start_station_id};
}
auto comp = [](const PathNode& a, const PathNode& b) {
return a.f_cost() > b.f_cost();
};
std::priority_queue<PathNode, std::vector<PathNode>, decltype(comp)> open_set(comp);
std::map<int, double> g_scores;
std::map<int, int> came_from;
std::set<int> closed_set;
double h_start = calculateHeuristic(start_station_id, goal_station_id);
open_set.push(PathNode(start_station_id, 0.0, h_start));
g_scores[start_station_id] = 0.0;
while (!open_set.empty()) {
PathNode current = open_set.top();
open_set.pop();
if (current.station_id == goal_station_id) {
return reconstructPath(came_from, start_station_id, goal_station_id);
}
if (closed_set.find(current.station_id) != closed_set.end()) {
continue;
}
closed_set.insert(current.station_id);
const auto& neighbors = network->getAdjacentStations(current.station_id);
for (int neighbor_id : neighbors) {
if (closed_set.find(neighbor_id) != closed_set.end()) {
continue;
}
auto track = network->getTrackBetween(current.station_id, neighbor_id);
if (!track || !track->is_active) {
continue;
}
double tentative_g = current.g_cost + track->distance;
if (g_scores.find(neighbor_id) == g_scores.end() || tentative_g < g_scores[neighbor_id]) {
g_scores[neighbor_id] = tentative_g;
came_from[neighbor_id] = current.station_id;
double h = calculateHeuristic(neighbor_id, goal_station_id);
open_set.push(PathNode(neighbor_id, tentative_g, h));
}
}
}
return {};
}
double PathFinder::calculatePathDistance(const std::vector<int>& path) const {
if (path.size() < 2) return 0.0;
double total_distance = 0.0;
for (size_t i = 0; i < path.size() - 1; ++i) {
auto track = network->getTrackBetween(path[i], path[i + 1]);
if (track) {
total_distance += track->distance;
}
}
return total_distance;
}
double PathFinder::estimateTravelTime(const std::vector<int>& path) const {
if (path.size() < 2) return 0.0;
double total_time = 0.0;
for (size_t i = 0; i < path.size() - 1; ++i) {
auto track = network->getTrackBetween(path[i], path[i + 1]);
if (track) {
total_time += (track->distance / track->speed_limit);
}
}
return total_time;
}
void PathFinder::printPath(const std::vector<int>& path) const {
if (path.empty()) {
std::cout << "No path found" << std::endl;
return;
}
std::cout << "Path: ";
for (size_t i = 0; i < path.size(); ++i) {
auto station = network->getStation(path[i]);
if (station) {
std::cout << station->name;
if (i < path.size() - 1) {
std::cout << " -> ";
}
}
}
std::cout << std::endl;
std::cout << "Distance: " << calculatePathDistance(path) << " km" << std::endl;
std::cout << "Est. Time: " << estimateTravelTime(path) << " hours" << std::endl;
}