Repository navigation
Expand file tree
/
Copy pathFinal.cpp
More file actions
350 lines (293 loc) · 12.3 KB
/
Copy pathFinal.cpp
File metadata and controls
350 lines (293 loc) · 12.3 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <string>
#include <sstream>
#include <utility>
#include <queue>
#include <limits>
#include <algorithm>
#include <cmath>
#include <iomanip>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
using namespace std;
class Node {
public:
string name;
vector<pair<Node*, double>> neighbors;
double lat, lon;
Node(const string& name, double lat = 0, double lon = 0) : name(name), lat(lat), lon(lon) {}
};
class Graph {
public:
unordered_map<string, Node*> nodes;
struct RideMetrics {
double distance;
double time;
double price;
};
// Calculate metrics for a specific path segment
RideMetrics calculateSegmentMetrics(const vector<string>& path) const {
RideMetrics metrics;
// If there's only one entry in the path, set distance to 0
if (path.size() <= 1) {
metrics.distance = 0.0;
} else {
metrics.distance = calculatePathDistance(path);
}
metrics.time = metrics.distance / 50.0;
metrics.price = metrics.distance * 10.0;
return metrics;
}
Graph() {}
~Graph() {
for (auto& entry : nodes) {
delete entry.second;
}
}
void addEdge(const string& node1, const string& node2, double distance) {
if (nodes.find(node1) == nodes.end()) {
nodes[node1] = new Node(node1);
}
if (nodes.find(node2) == nodes.end()) {
nodes[node2] = new Node(node2);
}
nodes[node1]->neighbors.emplace_back(nodes[node2], distance);
nodes[node2]->neighbors.emplace_back(nodes[node1], distance);
}
void loadGraphFromFile(const string& filePath) {
for (auto& entry : nodes) {
delete entry.second;
}
nodes.clear();
ifstream file(filePath);
if (!file) {
cerr << "Error: Could not open the file." << endl;
return;
}
string line;
while (getline(file, line)) {
if (line.find_first_not_of(" \t\n\v\f\r") == string::npos) {
continue;
}
stringstream ss(line);
string node1, node2;
double distance;
if (!(ss >> node1 >> node2 >> distance)) {
continue;
}
addEdge(node1, node2, distance);
}
file.close();
cout << "Graph updated from file." << endl;
}
void displayGraph() const {
for (const auto& entry : nodes) {
const string& city = entry.first;
const auto& neighbors = entry.second->neighbors;
cout << city << " -> ";
for (const auto& neighborEntry : neighbors) {
cout << "(" << neighborEntry.first->name << ", " << neighborEntry.second << ") ";
}
cout << endl;
}
}
double heuristic(const Node* start, const Node* goal) const {
double lat1 = start->lat * M_PI / 180.0;
double lon1 = start->lon * M_PI / 180.0;
double lat2 = goal->lat * M_PI / 180.0;
double lon2 = goal->lon * M_PI / 180.0;
double dlat = lat2 - lat1;
double dlon = lon2 - lon1;
double a = sin(dlat / 2) * sin(dlat / 2) +
cos(lat1) * cos(lat2) * sin(dlon / 2) * sin(dlon / 2);
double c = 2 * atan2(sqrt(a), sqrt(1 - a));
return 6371.0 * c;
}
vector<string> aStarShortestPath(const string& startNode, const string& endNode) const {
if (nodes.find(startNode) == nodes.end() || nodes.find(endNode) == nodes.end()) {
return {};
}
unordered_map<string, double> dist;
unordered_map<string, string> cameFrom;
priority_queue<pair<double, string>, vector<pair<double, string>>, greater<>> openSet;
for (const auto& entry : nodes) {
dist[entry.first] = numeric_limits<double>::infinity();
}
dist[startNode] = 0.0;
openSet.push({0.0, startNode});
while (!openSet.empty()) {
string current = openSet.top().second;
openSet.pop();
if (current == endNode) {
return reconstructPath(cameFrom, startNode, endNode);
}
for (const auto& neighbor : nodes.at(current)->neighbors) {
const string& neighborNode = neighbor.first->name;
double weight = neighbor.second;
double tentativeDist = dist[current] + weight + heuristic(nodes.at(current), neighbor.first);
if (tentativeDist < dist[neighborNode]) {
dist[neighborNode] = tentativeDist;
cameFrom[neighborNode] = current;
openSet.push({tentativeDist, neighborNode});
}
}
}
return {};
}
vector<string> reconstructPath(const unordered_map<string, string>& cameFrom,
const string& start, const string& end) const {
vector<string> path;
string current = end;
while (current != start) {
path.push_back(current);
current = cameFrom.at(current);
}
path.push_back(start);
reverse(path.begin(), path.end());
return path;
}
double calculatePathDistance(const vector<string>& path) const {
double totalDistance = 0.0;
for (size_t i = 0; i < path.size() - 1; ++i) {
for (const auto& neighbor : nodes.at(path[i])->neighbors) {
if (neighbor.first->name == path[i + 1]) {
totalDistance += neighbor.second;
break;
}
}
}
return totalDistance;
}
// Calculate metrics for individual rides
RideMetrics calculateIndividualRideMetrics(const string& start, const string& end) {
vector<string> path = aStarShortestPath(start, end);
return calculateSegmentMetrics(path);
}
// Calculate shared ride metrics for both users
pair<RideMetrics, RideMetrics> calculateSharedRideMetrics(
const string& user1Start, const string& user1End,
const string& user2Start, const string& user2End) {
RideMetrics user1Metrics = {0.0, 0.0, 0.0};
RideMetrics user2Metrics = {0.0, 0.0, 0.0};
vector<string> user1InitialPath;
vector<string> sharedPath;
vector<string> user2FinalPath;
// Calculate path segments
user1InitialPath = aStarShortestPath(user1Start, user2Start);
sharedPath = aStarShortestPath(user2Start, user1End);
user2FinalPath = aStarShortestPath(user1End, user2End);
// Calculate metrics for each segment
RideMetrics initialSegment = calculateSegmentMetrics(user1InitialPath);
RideMetrics sharedSegment = calculateSegmentMetrics(sharedPath);
RideMetrics finalSegment = calculateSegmentMetrics(user2FinalPath);
// Calculate User 1's metrics (initial solo + shared segment)
user1Metrics.distance = initialSegment.distance + sharedSegment.distance;
user1Metrics.time = initialSegment.time + sharedSegment.time;
user1Metrics.price = initialSegment.price + (sharedSegment.price / 2.0);
// Calculate User 2's metrics (shared + final solo segment)
user2Metrics.distance = sharedSegment.distance + finalSegment.distance;
user2Metrics.time = sharedSegment.time + finalSegment.time;
user2Metrics.price = (sharedSegment.price / 2.0) + finalSegment.price;
return {user1Metrics, user2Metrics};
}
};
int main() {
string filePath = "cities.txt";
Graph graph;
graph.loadGraphFromFile(filePath);
string user1Origin, user1Dest, user2Origin, user2Dest;
cout << "Enter User 1 Origin: ";
cin >> user1Origin;
cout << "Enter User 1 Destination: ";
cin >> user1Dest;
cout << "Enter User 2 Origin: ";
cin >> user2Origin;
cout << "Enter User 2 Destination: ";
cin >> user2Dest;
// Calculate individual ride metrics
auto user1Individual = graph.calculateIndividualRideMetrics(user1Origin, user1Dest);
auto user2Individual = graph.calculateIndividualRideMetrics(user2Origin, user2Dest);
// Calculate shared ride metrics
pair<Graph::RideMetrics, Graph::RideMetrics> sharedMetrics = graph.calculateSharedRideMetrics(
user1Origin, user1Dest, user2Origin, user2Dest);
// Manually unpack the pair into individual variables
Graph::RideMetrics user1Shared = sharedMetrics.first;
Graph::RideMetrics user2Shared = sharedMetrics.second;
// Display paths before decision
cout << "\nPaths before decision to share:" << endl;
// Individual paths
cout << "\nUser 1 Solo Path: ";
vector<string> user1Path = graph.aStarShortestPath(user1Origin, user1Dest);
for (const auto& city : user1Path) {
cout << city << " -> ";
}
cout << "END" << endl;
cout << "User 2 Solo Path: ";
vector<string> user2Path = graph.aStarShortestPath(user2Origin, user2Dest);
for (const auto& city : user2Path) {
cout << city << " -> ";
}
cout << "END" << endl;
// Shared paths
vector<string> initialPath = graph.aStarShortestPath(user1Origin, user2Origin);
vector<string> sharedPath = graph.aStarShortestPath(user2Origin, user1Dest);
vector<string> finalPath = graph.aStarShortestPath(user1Dest, user2Dest);
cout << "\nUser 1 solo: ";
for (const auto& city : initialPath) {
cout << city << " -> ";
}
cout << "\nShared segment: ";
for (const auto& city : sharedPath) {
cout << city << " -> ";
}
cout << "\nUser 2 solo: ";
for (const auto& city : finalPath) {
cout << city << " -> ";
}
cout << "END" << endl;
// Display ride comparison before decision
cout << "\nRide Stats Comparison:" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "| Ride Type | Distance (km) | Time (hours) | Price (units) |" << endl;
cout << "------------------------------------------------------------------" << endl;
cout << "| User 1 Solo | " << fixed << setprecision(2)
<< user1Individual.distance << " | "
<< user1Individual.time << " | "
<< user1Individual.price << " |" << endl;
cout << "| User 2 Solo | " << user2Individual.distance << " | "
<< user2Individual.time << " | "
<< user2Individual.price << " |" << endl;
cout << "| User 1 Shared | " << user1Shared.distance << " | "
<< user1Shared.time << " | "
<< user1Shared.price << " |" << endl;
cout << "| User 2 Shared | " << user2Shared.distance << " | "
<< user2Shared.time << " | "
<< user2Shared.price << " |" << endl;
cout << "------------------------------------------------------------------" << endl;
// Prompt user for decision
cout << "The final price for User 1: " << user1Shared.price - user1Shared.price * 0.3 << " units" << endl;
cout << "The final price for User 2: " << user2Shared.price - user2Shared.price * 0.3 << " units" << endl;
cout << "\nDo you want to share a ride? (yes/no): "<<endl;
string decision;
cin >> decision;
if (decision == "yes") {
cout << "\nCongratulations! You've decided to share the ride!" << endl;
// Final prices
cout << "\nFinal prices:" << endl;
cout <<endl;
cout <<"If you decide to share a ride" << endl;
cout << "User 1 Discounted Price: " << user1Shared.price - user1Shared.price * 0.3 << " units" << endl;
cout << "User 2 Discounted Price " << user2Shared.price - user2Shared.price * 0.3 << " units" << endl;
} else {
cout << "\nIndividual ride prices will be:" << endl;
cout << "User 1: " << user1Individual.price << " units" << endl;
cout << "User 2: " << user2Individual.price << " units" << endl;
}
return 0;
}