-
Notifications
You must be signed in to change notification settings - Fork 0
/
Project4.cpp
286 lines (253 loc) · 8.4 KB
/
Project4.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
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
// Project4.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "Node.h"
#include "Edges.h"
#include <sstream>
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <stack>
#include <queue>
#include <functional>
#include <cstdio>
#include <ctime>
using namespace std;
using std::getline;
/*
struct node_comparison
{
bool operator()(const Node& a, const Node& b) const
{
return a.totalWeight > b.totalWeight;
}
};*/
template <class T, class F>
class reservable_priority_queue: public std::priority_queue<T, F>
{
public:
typedef typename std::priority_queue<T, F>::size_type size_type;
reservable_priority_queue(size_type capacity = 0) { reserve(capacity); };
void reserve(size_type capacity) { this->c.reserve(capacity); }
size_type capacity() const { return this->c.capacity(); }
};
int main(int argc, char* argv[])
{
string start, end, criteria;
string startCity = "";
string endCity = "";
string outputFile, citiesFile, routesFile;
if (argc > 1) {
start = argv[4];
end = argv[5];
criteria = argv[6];
if (criteria.compare("cheapest") == 0) {
criteria = "2";
}else{
criteria = "1";
}
outputFile = argv[3];
routesFile = argv[2];
citiesFile = argv[1];
}else{
cout << "~ Capital to Captial. All in one travel calculator 1.0 ~" << endl << endl;
cout << "Starting Country: ";
getline(cin,start);
cout << "Destination Country: ";
getline(cin,end);
cout << "Choose a criteria: \n1. Fastest Route\n2. Cheapest Route" << endl;
cin >> criteria;
cout << "Output file name (end with .html)" << endl;
cin >> outputFile;
cout << endl;
citiesFile = "cities.csv";
routesFile = "routes.csv";
}
stack<Node> path;
stack<Node> unresolvedNeighbors;
map<string, Node> nodeMap;
map<string, Node*> previous;
map<string, double> dist;
//Country Name, City Name, Latitude, Longitude
ifstream nodes;
nodes.open(citiesFile.c_str());
string country, city, latitude, longitude;
bool newCountry = true;
while(nodes.good()) {
getline(nodes,country,',');
getline(nodes,city,',');
getline(nodes,latitude,',');
getline(nodes,longitude);
Node node(country, city, latitude, longitude);
nodeMap[city] = node;
dist[city] = 999999;
previous[city] = NULL;
if (country == start) {
startCity = city;
}
if (country == end) {
endCity = city;
}
}
//Origin City, Destination City, Type of Transport, Average time required, Average cost USD, "Notes (these may include the CSV delimiter)"
ifstream edges;
edges.open(routesFile.c_str());
string origin, destination, transport, time, cost, notes;
int numEdges = 0;
while(edges.good()) {
numEdges++;
getline(edges, origin, ',');
getline(edges, destination, ',');
getline(edges, transport, ',');
getline(edges, time, ',');
getline(edges, cost, ',');
getline(edges, notes);
//if country node exists
if (nodeMap.find(origin) != nodeMap.end() && nodeMap.find(destination) != nodeMap.end()) {
//for each city, add where it connects to
nodeMap[origin].setConnection(Edges(origin,destination,transport,time,cost,notes), nodeMap[destination], atoi(criteria.c_str()));
}
}
reservable_priority_queue<Node, vector<Node> > minHeap;
minHeap.reserve(numEdges);
//add neighbors of start node, assuming not visited of course
int intCriteria = atoi(criteria.c_str());
Node& startNode = nodeMap[startCity];
previous[startCity] = NULL;
startNode.totalWeight = 0;
minHeap.push(startNode);
dist[startCity] = 0;
Node* currentAdjNode;
string adjCity;
double edgeWeight;
double sourceWeight;
double weight;
clock_t startTime = clock();
while(!minHeap.empty()) {
//dist[city] holds shortest path for that city
//if current.totalWeight does not match, then it's not the newest one. skip.
while (minHeap.top().totalWeight != dist[minHeap.top().city]) {
minHeap.pop();
}
Node nt = minHeap.top();
double currentCityDis = dist[minHeap.top().city];
nt.visited = true;
if ((currentCityDis == 999999 && currentCityDis != 0) || nt.city == endCity) {
break;
}
//vector<Node*> &adjNodes = nt.connections;
//calculate total weight from source to node
for (unsigned int i = 0; i < nt.connections.size(); i++) {
currentAdjNode = nt.connections.at(i);
adjCity = currentAdjNode->city;
if (!currentAdjNode->visited && nt.city != adjCity) {
edgeWeight = nt.edgeWeight(adjCity, intCriteria);
sourceWeight = dist[nt.city];
weight = sourceWeight + edgeWeight;
//if the total weight < the total weight set at that country OR there is weight set
if (weight < dist[adjCity]) {
//set dist of node from start to weight
dist[adjCity] = weight;
//set previous node of current city
previous[adjCity] = &nodeMap[nt.city];
//create copy and push into heap
currentAdjNode->totalWeight = weight;
minHeap.push(*currentAdjNode);
}
}
}
minHeap.pop();
}
clock_t endTime = clock();
clock_t clockTicksTaken = endTime - startTime;
double timeInSeconds = clockTicksTaken / (double) CLOCKS_PER_SEC;
Node* current = &nodeMap[endCity];
double weightCost = dist[current->city];
while(previous[current->city] != NULL && current->city != startCity) {
path.push(*current);
current = previous[current->city];
}
path.push(*current);
//ouputHTML o(path, atoi(criteria.c_str()), start, end, outputFile);
/*
int i = 0;
while(!path.empty()) {
for (int j = 0; j <= i; j++) {
cout << ">>";
}
cout << " " << path.top().country << "(" << path.top().city << ")" << endl;
path.pop();
i++;
}
if (criteria == "1") {
cout << "Time Taken: " << weightCost << " hours" << endl;
}else{
cout << "Total Cost: " << weightCost << " USD" << endl;
}
string test;
cin >> test;
*/
int numMarkerPairs = path.size() - 1;
string criteriaString = "Fastest";
double totalTime = 0;
double totalCost = 0;
switch (atoi(criteria.c_str())) {
case 1:
criteriaString = "Fastest";
break;
case 2:
criteriaString = "Cheapest";
break;
}
ofstream write;
write.open(outputFile.c_str());
stringstream header;
header << "<HTML><HEAD><TITLE>" << criteriaString << " path from "<< start <<" to "<< end << "</TITLE></HEAD>"
"<script type=\"text/javascript\" src=\"http://maps.google.com/maps/api/js?sensor=false\"></script>"
"<script>function initialize() { "
"var myOptions = { zoom: 3, center: new google.maps.LatLng(0, 0), mapTypeId: google.maps.MapTypeId.ROADMAP};"
"var map=new google.maps.Map(document.getElementById(\"map\"), myOptions);";
write << header.str();
Node p = path.top();
path.pop();
for(int i = 0; i < numMarkerPairs; i++) {
string city1 = path.top().city;
string country1 = p.country;
double latitude1 = p.latitude;
double longitude1 = p.longitude;
// ==================================
Node node = path.top(); path.pop();
string city2 = node.city;
string country2 = node.country;
double latitude2 = node.latitude;
double longitude2 = node.longitude;
double cost = p.edgeWeight(city2,2);
double time = p.edgeWeight(city2,1);
string transport = p.getTransportType(city2);
totalTime += time;
totalCost += cost;
if (transport == "plane") {
cost /= 500;
time /= 500;
}
stringstream markers;
markers <<
"var marker" << i << " = new google.maps.Marker({ position: new google.maps.LatLng(" << latitude1 << ", " << longitude1 << "), map: map, title:\""<< city1 <<", "<< country1 <<"\"});"
"var marker" << (i+1) << " = new google.maps.Marker({ position: new google.maps.LatLng(" << latitude2 << ", " << longitude2 << "), map: map, title:\""<< city2 <<", "<< country2 <<"\"});";
write << markers.str();
stringstream set;
set << "var contentString"<< i <<" = \""<< city1 <<", "<< country1 <<" --> "<< city2 <<", "<< country2 <<" ("<< transport <<" - "<< time <<" hours - $"<< cost <<")\";"
"var path"<< i <<" = new google.maps.Polyline({ path: [new google.maps.LatLng(" << latitude1 << ", " << longitude1 << "), new google.maps.LatLng(" << latitude2 << ", " << longitude2 << ")], strokeColor: \"#0000FF\", strokeOpacity: 1.0, strokeWeight: 2}); "
"path"<< i <<".setMap(map); "
"google.maps.event.addListener(path"<< i <<", \"click\", function(event) { alert(contentString"<< i <<"); });";
write << set.str();
p = node;
}
string footer =
"} google.maps.event.addDomListener(window, \"load\", initialize);"
"</script></HEAD><BODY><div id=\"map\" style=\"width:100%;height:90%;\"></div></BODY></HTML>";
write << footer;
write.close();
return 0;
}