-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
536 lines (451 loc) · 19.4 KB
/
main.cpp
File metadata and controls
536 lines (451 loc) · 19.4 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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
* COP3530 Fall 2021
* Project 3
*
* Date: 12/09/2021
* Group member: Zhen Wu, Nandini Tripathi, and Elizabeth Thorner
*
* Delivery simulation using the greedy approach by Dijkstra and Bellman-Ford algorithm.
* The program calculates a local optimal path the driver can take to finish his/her delivery trip.
* Estimation of how many gallons of gas will be used, CO2 emission, and estimated time will be also calculated.
*/
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <utility>
#include <chrono>
#include <cstdlib>
#include <random>
#include <string>
#include <vector>
#include <set>
#include <map>
using namespace std::chrono;
using namespace std;
typedef high_resolution_clock Clock;
vector<int> Dijkstra(map<int, set<pair<int, int>>>& _graph, int _numNodes, int _source, vector<int>& _prevNode);
vector<int> BellmanFord(map<int, set<pair<int, int>>>& _graph, int _numNodes, int _source, vector<int>& _prevNode);
int main()
{
//graph variables
int numberNode = -1;
int maxEdgePerNode = -1; //minimum of 2 edges (2 edges will just be a straight path)
int numDeliveryAddress = -1;
int density = 1;
int distanceApart = 1;
int aveSpeed = 1;
cout << "This program will simulate paths that a delivery driver could take to deliver all of his/her packages " << endl;
cout << "using the greedy approach. The user can choose between Dijkstra's algorithm or the Bellman-Ford algorithm or both. " << endl;
cout << "The program will output the exact path the driver must follow to finish the trip. Estimations of " << endl;
cout << "gallons of gas used, CO2 emissions emitted, estimated time, and the total cost will be also calculated." << endl << endl;
cout << "Choose delivery location density:" << endl;
cout << "1 = Countryside (addresses are no more than 1100 meters apart)" << endl;
cout << "2 = Town (addresses are no more than 500 meters apart)" << endl;
cout << "3 = City (addresses are no more than 100 meters apart)" << endl;
cout << "4 = Metropolis (addresses are no more than 50 meters apart)" << endl;
cin >> density;
cout << endl;
if (density == 1)
{
distanceApart = 1100;
aveSpeed = 75;
}
else if (density == 2)
{
distanceApart = 500;
aveSpeed = 45;
}
else if (density == 3)
{
distanceApart = 100;
aveSpeed = 35;
}
else if (density == 4)
{
distanceApart = 50;
aveSpeed = 30;
}
while (numberNode < 2)
{
cout << "Enter the number of vertices (minimum of 2): " << endl;
cin >> numberNode;
}
while (maxEdgePerNode < 2)
{
cout << "Enter the max number of edges per node (minimum of 2, but no more than 11): " << endl;
cin >> maxEdgePerNode;
}
while (numDeliveryAddress < 1)
{
cout << "Enter the number of delivery addresses you want in your list (cant be >= the number of vertices): " << endl;
cin >> numDeliveryAddress;
}
cout << endl;
//create random
static random_device rd;
static mt19937 rng(rd());
static uniform_int_distribution<int> distanceRange(10, distanceApart); //meter
static uniform_int_distribution<int> neighborRange(2, 12); // 10 possible nearby neighbors
//adjacency list of the entire city's address and roads
// <from, pair<to, weight>>
map<int, set<pair<int, int>>> graph;
vector<int> edgeCount(numberNode + 2, 0); //number of edges for each address tracker
int random = 0;
//add all the address and edges to the graph
for (int i = 1; i <= numberNode; i++)
{
//from itself to 10 other possble neighbor within (itself + 2) to (itself + 12) (ex: address 3 can have numEdgePerNode edges to address 5 - 15)
set<int> nearbyNeighbor;
while ((nearbyNeighbor.size() != maxEdgePerNode - 2) && nearbyNeighbor.size() != numberNode - i - 1 && i != numberNode) //2 edges is reserved for straight path
{
random = i + neighborRange(rd);
if (random <= numberNode) // neighbor edge cant be larger then number of nodes that exist
{
nearbyNeighbor.emplace(random);
}
}
//add edges from current address to nearby neighbor
for (auto j = nearbyNeighbor.begin(); j != nearbyNeighbor.end(); j++)
{
if (edgeCount[i] < maxEdgePerNode - 1 && edgeCount[*j] < maxEdgePerNode - 2) //save 1 edge for self and 2 edge for nearby Neighbor
{
random = distanceRange(rd);
graph[i].emplace(make_pair(*j, random));
graph[*j].emplace(make_pair(i, random));
edgeCount[i] += 1;
edgeCount[*j] += 1;
}
}
random = distanceRange(rd);
graph[i].emplace(make_pair(i + 1, random)); //edge for the next neighbor (from, to)
graph[i + 1].emplace(make_pair(i, random)); // (to, from)
edgeCount[i] += 1; //increment edge tracker
edgeCount[i + 1] += 1;
}
graph.erase(numberNode + 1); //delete the one excess node from the graph
graph[numberNode].erase(make_pair(numberNode + 1, random)); //delete the unreachable node from the last address
//print the entire map
int userInput = 2;
cout << "Do you want to print the entire graph? " << endl;
cout << "1 = Yes " << endl;
cout << "2 = No " << endl;
cin >> userInput;
if (userInput == 1)
{
for (auto i = graph.begin(); i != graph.end(); i++)
{
cout << "Vertex: " << i->first << endl;
cout << "Edges: ";
for (auto j = i->second.begin(); j != i->second.end(); j++)
{
cout << "(" << j->first << ", " << j->second << "); ";
}
cout << endl << endl;
}
}
cout << endl;
//generate a certain amount of random delivery address
static uniform_int_distribution<int> deliveryRange(2, numberNode); // random
set<int> deliveryList;
while (deliveryList.size() != numDeliveryAddress)
{
deliveryList.emplace(deliveryRange(rd));
}
cout << "Delivery addresses list: ";
for (auto i = deliveryList.begin(); i != deliveryList.end(); i++)
{
cout << *i << ", ";
}
cout << endl << endl;
cout << "Enter the algorithm you would like to use" << endl;
cout << "1 = Dijkstra's" << endl;
cout << "2 = Bellman-Ford" << endl;
cout << "3 = Both" << endl;
cin >> userInput;
cout << endl;
double miles = 0;
vector<int> path; //path taken tracker
if (userInput == 1) // Dijkstra
{
auto t1 = Clock::now();
int totalDistance = 0; //keep track of total distance travelled
int sourceNode = 1; //go to the next closest node
int preSourceNode = 1;
while (!deliveryList.empty())
{
int min = 300000000; //closest node distance
vector<int> prevNode(numberNode + 1, -2);
vector<int> closestDistance = Dijkstra(graph, numberNode, sourceNode, prevNode);
if (sourceNode != 1)
{
deliveryList.erase(sourceNode); //deleted the node that just delivered to
}
for (auto i = deliveryList.begin(); i != deliveryList.end(); i++)
{
if (closestDistance[*i] < min)
{
min = closestDistance[*i]; //find the closest node
sourceNode = *i;
}
}
while (prevNode[preSourceNode] != -1)
{
path.push_back(preSourceNode);
preSourceNode = prevNode[preSourceNode];
}
if (deliveryList.size() != 0)
{
totalDistance += min;
}
else //push the last desitaion node into path
{
path.push_back(sourceNode);
}
}
miles = (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100;
auto t2 = Clock::now();
cout << "It took " << duration_cast<seconds>(t2 - t1).count() << " seconds to use Dijkstra's algorithm" << endl;
cout << "Total distance traveled: " << (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100 << " miles " << "(" << totalDistance << " meters)." << endl;
cout << "Estimated time to complete trip (with an average speed of " << aveSpeed << " miles per hour): " << (double)((int)((miles / aveSpeed) * 100)) / 100 << " hours." << endl;
cout << endl;
}
else if (userInput == 2) //Bellman-Ford
{
auto t1 = Clock::now();
int totalDistance = 0; //keep track of total distance travelled
int sourceNode = 1; //go to the next closest node
int preSourceNode = 1;
while (!deliveryList.empty())
{
int min = 300000000; //closest node distance
vector<int> prevNode(numberNode + 1, -2);
vector<int> closestDistance = BellmanFord(graph, numberNode, sourceNode, prevNode);
if (sourceNode != 1)
{
deliveryList.erase(sourceNode); //deleted the node that just delivered to
}
for (auto i = deliveryList.begin(); i != deliveryList.end(); i++)
{
if (closestDistance[*i] < min)
{
min = closestDistance[*i]; //find the closest node
sourceNode = *i;
}
}
while (prevNode[preSourceNode] != -1)
{
path.push_back(preSourceNode);
preSourceNode = prevNode[preSourceNode];
}
if (deliveryList.size() != 0)
{
totalDistance += min;
}
else //push the last desitaion node into path
{
path.push_back(sourceNode);
}
}
miles = (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100;
auto t2 = Clock::now();
cout << "It took " << duration_cast<seconds>(t2 - t1).count() << " seconds to use the Bellman-Ford algorithm" << endl;
cout << "Total distance traveled: " << (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100 << " miles " << "(" << totalDistance << " meters)." << endl;
cout << "Estimated time to complete trip (with an average speed of " << aveSpeed << " miles per hour): " << (double)((int)((miles / aveSpeed) * 100)) / 100 << " hours." << endl;
cout << endl;
}
else if (userInput == 3)
{
set<int>tempList = deliveryList;
auto t1 = Clock::now();
int totalDistance = 0; //keep track of total distance travelled
int sourceNode = 1; //go to the next closest node
int preSourceNode = 1;
while (!tempList.empty())
{
int min = 300000000; //closest node distance
vector<int> prevNode(numberNode + 1, -2);
vector<int> closestDistance = Dijkstra(graph, numberNode, sourceNode, prevNode);
if (sourceNode != 1)
{
tempList.erase(sourceNode); //deleted the node that just delivered to
}
for (auto i = tempList.begin(); i != tempList.end(); i++)
{
if (closestDistance[*i] < min)
{
min = closestDistance[*i]; //find the closest node
sourceNode = *i;
}
}
while (prevNode[preSourceNode] != -1)
{
path.push_back(preSourceNode);
preSourceNode = prevNode[preSourceNode];
}
if (tempList.size() != 0)
{
totalDistance += min;
}
else //push the last desitaion node into path
{
path.push_back(sourceNode);
}
}
miles = (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100;
auto t2 = Clock::now();
cout << "It took " << duration_cast<seconds>(t2 - t1).count() << " seconds to use Dijkstra's algorithm" << endl;
cout << "Total distance traveled: " << (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100 << " miles " << "(" << totalDistance << " meters)." << endl;
cout << "Estimated time to complete trip (with an average speed of " << aveSpeed << " miles per hour): " << (double)((int)((miles / aveSpeed) * 100)) / 100 << " hours." << endl;
cout << endl;
t1 = Clock::now(); //Bellman-Ford
path.clear(); //clear for Bellman-Ford to put the path
totalDistance = 0; //keep track of total distance travelled
sourceNode = 1; //go to the next closest node
preSourceNode = 1;
while (!deliveryList.empty())
{
int min = 300000000; //closest node distance
vector<int> prevNode(numberNode + 1, -2);
vector<int> closestDistance = BellmanFord(graph, numberNode, sourceNode, prevNode);
if (sourceNode != 1)
{
deliveryList.erase(sourceNode); //deleted the node that just delivered to
}
for (auto i = deliveryList.begin(); i != deliveryList.end(); i++)
{
if (closestDistance[*i] < min)
{
min = closestDistance[*i]; //find the closest node
sourceNode = *i;
}
}
while (prevNode[preSourceNode] != -1)
{
path.push_back(preSourceNode);
preSourceNode = prevNode[preSourceNode];
}
if (deliveryList.size() != 0)
{
totalDistance += min;
}
else //push the last desitaion node into path
{
path.push_back(sourceNode);
}
}
miles = (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100;
t2 = Clock::now();
cout << "It took " << duration_cast<seconds>(t2 - t1).count() << " seconds to use the Bellman-Ford algorithm" << endl;
cout << "Total distance traveled: " << (double)((int)(((double)totalDistance / 1609.34) * 100)) / 100 << " miles " << "(" << totalDistance << " meters)." << endl;
cout << "Estimated time to complete trip (with an average speed of " << aveSpeed << " miles per hour): " << (double)((int)((miles / aveSpeed) * 100)) / 100 << " hours." << endl;
cout << endl;
}
//print the path
cout << "Do you want to print the path the driver took?" << endl;
cout << "1 = yes" << endl;
cout << "2 = no" << endl;
cin >> userInput;
cout << endl;
if (userInput == 1)
{
cout << "Note: #1 is the warehouse" << endl;
for (int i = 0; i < path.size(); i++)
{
cout << path[i] << "->";
}
cout << "end" << endl << endl;
}
int numOfDrivers = 1;
double pay = 0;
double mpg = 0;
double costPerGallon = 0;
cout << "Trip cost calculation" << endl;
cout << "---------------------" << endl;
cout << "Enter the number of workers: ";
cin >> numOfDrivers;
cout << endl;
cout << "Enter the hourly wage: ";
cin >> pay;
cout << endl;
cout << "Enter the vehicle mpg: ";
cin >> mpg;
cout << endl;
cout << "Enter the price per gallon of gas: ";
cin >> costPerGallon;
cout << endl;
cout << "Result:" << endl;
cout << "Wage: $" << (numOfDrivers * pay) * (double)((int)((miles / aveSpeed) * 100)) / 100 << endl;
cout << "Gas: $" << (double)((int)(((miles / mpg) * costPerGallon) * 100)) / 100 << endl;
cout << "Trip total cost: $" << (double)((int)(((miles / mpg) * costPerGallon) * 100)) / 100 + (numOfDrivers * pay) * (double)((int)((miles / aveSpeed) * 100)) / 100 << endl;
//source: https://www.epa.gov/energy/greenhouse-gases-equivalencies-calculator-calculations-and-references
cout << "The CO2 emission for this trip is: " << 8.887 * (miles / mpg) << " x10^-3 metric tons." << endl;
return 0;
}
//return the distance from source to each node
vector<int> Dijkstra(map<int, set<pair<int, int>>>& _graph, int _numNodes, int _source, vector<int>& _prevNode)
{
vector<int> visited(_numNodes + 1, 0); // keep track which nodes is visited
vector<int> distance(_numNodes + 1, 300000000); // track the distance from the source node
_prevNode[_source] = -1;
int minIndex = _source;
distance[_source] = 0;
for (int i = 0; i < _numNodes; i++)
{
int min = 300000000;
//find the smallest distance thats not been to
for (int k = 0; k < distance.size(); k++)
{
if (distance[k] < min && visited[k] == 0)
{
min = distance[k];
minIndex = k;
}
}
//do calculate to its childrens
for (auto j = _graph[minIndex].begin(); j != _graph[minIndex].end(); j++)
{
//if the new distance is less then update
if (distance[minIndex] + j->second < distance[j->first])
{
distance[j->first] = distance[minIndex] + j->second;
_prevNode[j->first] = minIndex;
}
}
//finished visiting this node
visited[minIndex] = 1; //set to true
}
return distance;
}
//return the distance from source to each node
vector<int> BellmanFord(map<int, set<pair<int, int>>>& _graph, int _numNodes, int _source, vector<int>& _prevNode)
{
vector<int> visited(_numNodes + 1, 0); // keep track which nodes is visited
vector<int> distance(_numNodes + 1, 300000000); // track the distance from the source node
_prevNode[_source] = -1;
distance[_source] = 0;
while (true) //BellmanFord runs at most v-1 times or when after an iteration that cant improve distance (break)
{
bool hasChanged = false;
for (auto j = _graph.begin(); j != _graph.end(); j++) //visit each node every iteration
{
if (distance[j->first] != 300000000) //if the node havent been to then we skip
{
for (auto k = j->second.begin(); k != j->second.end(); k++)
{
if (distance[j->first] + k->second < distance[k->first]) //if distance is less update
{
distance[k->first] = distance[j->first] + k->second;
_prevNode[k->first] = j->first;
hasChanged = true;
}
}
}
}
if (!hasChanged)
{
break;
}
}
return distance;
}