-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_star_transfers.cpp
177 lines (148 loc) · 5.09 KB
/
a_star_transfers.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
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
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <unordered_map>
#include <queue>
#include <map>
#include <climits>
#include <algorithm>
#include <limits>
#include <utility>
#include <cmath>
struct Edge
{
int id;
std::string company;
std::string line;
int departureTime;
int arrivalTime;
std::string startStop;
std::string endStop;
float startStopLat;
float startStopLon;
float endStopLat;
float endStopLon;
};
class Graph
{
public:
// Manhattan distance heuristic function
double heuristic(const std::string ¤t, const std::string &goal, const Graph &graph)
{
double currentX = graph.adjList.at(current).front().startStopLon;
double currentY = graph.adjList.at(current).front().startStopLat;
double goalX = graph.adjList.at(goal).front().startStopLon;
double goalY = graph.adjList.at(goal).front().startStopLat;
return std::abs(currentX - goalX) + std::abs(currentY - goalY); // Manhattan distance
}
std::unordered_map<std::string, std::vector<Edge>> adjList;
void addEdge(const Edge &edge)
{
adjList[edge.startStop].push_back(edge);
}
std::vector<Edge> a_star(const std::string& start, const std::string& end, int startTime) {
std::unordered_map<std::string, int> transferCount;
std::unordered_map<std::string, Edge> cameFrom;
std::priority_queue<std::pair<int, std::string>, std::vector<std::pair<int, std::string>>, std::greater<>> openSet;
for (const auto& pair : this->adjList) {
transferCount[pair.first] = std::numeric_limits<int>::max();
}
transferCount[start] = 0; // No transfers to start
openSet.emplace(0, start);
while (!openSet.empty()) {
std::string current = openSet.top().second;
openSet.pop();
if (current == end) {
break;
}
// Current line taken to reach the 'current' stop
std::string currentLine = (cameFrom.count(current) > 0) ? cameFrom[current].line : "";
for (const auto& edge : this->adjList.at(current)) {
if (edge.departureTime < startTime) continue;
int newTransferCount = transferCount[current] + (edge.line != currentLine ? 1 : 0); // Increase if line changes
if (newTransferCount < transferCount[edge.endStop]) {
transferCount[edge.endStop] = newTransferCount;
cameFrom[edge.endStop] = edge;
openSet.emplace(newTransferCount, edge.endStop);
}
}
}
std::vector<Edge> path;
for (std::string at = end; at != start && cameFrom.find(at) != cameFrom.end(); at = cameFrom[at].startStop) {
path.push_back(cameFrom[at]);
}
std::reverse(path.begin(), path.end());
return path;
}
};
int convertTimeToMinutes(const std::string &time)
{
int hours, minutes, seconds;
char colon;
std::istringstream ss(time);
ss >> hours >> colon >> minutes >> colon >> seconds;
return hours * 60 + minutes;
}
std::string minutesToHHMM(int minutes)
{
int hours = minutes / 60;
int mins = minutes % 60;
std::ostringstream oss;
oss << (hours < 10 ? "0" : "") << hours << ':' << (mins < 10 ? "0" : "") << mins;
return oss.str();
}
void loadEdgesFromCSV(Graph &graph, const std::string &filename)
{
std::ifstream file(filename);
std::string line;
std::getline(file, line); // Skip header
while (std::getline(file, line))
{
std::istringstream s(line);
std::vector<std::string> fields;
std::string field;
while (getline(s, field, ','))
{
fields.push_back(field);
}
Edge edge;
edge.id = std::stoi(fields[0]);
edge.company = fields[1];
edge.line = fields[2];
edge.departureTime = convertTimeToMinutes(fields[3]);
edge.arrivalTime = convertTimeToMinutes(fields[4]);
edge.startStop = fields[5];
edge.endStop = fields[6];
edge.startStopLat = std::stof(fields[7]);
edge.startStopLon = std::stof(fields[8]);
edge.endStopLat = std::stof(fields[9]);
edge.endStopLon = std::stof(fields[10]);
graph.addEdge(edge);
}
}
int main()
{
Graph graph;
const std::string filename = "connection_graph.csv";
loadEdgesFromCSV(graph, filename);
// Example run
std::string startStop = "Borowska (Szpital)";
std::string endStop = "PL. GRUNWALDZKI";
std::string startTimeStr = "12:00";
int startTime = convertTimeToMinutes(startTimeStr);
auto shortestPath = graph.a_star(startStop, endStop, startTime);
// Output
std::cout << "Shortest path from \"" << startStop << "\" to \"" << endStop
<< "\" starting at " << startTimeStr << ":\n";
for (const auto &edge : shortestPath)
{
std::cout << "Start: " << edge.startStop
<< ", End: " << edge.endStop
<< ", Line: " << edge.line
<< ", Departure: " << minutesToHHMM(edge.departureTime)
<< ", Arrival: " << minutesToHHMM(edge.arrivalTime) << std::endl;
}
return 0;
}