-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrojanmap.h
273 lines (215 loc) · 9.97 KB
/
trojanmap.h
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
#ifndef TROJAN_MAP_H
#define TROJAN_MAP_H
#define DOT_SIZE 5
#define LINE_WIDTH 3
using namespace std;
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#define INT_MAX 1e18
// A Node is the location of one point in the map.
class Node {
public:
Node(){};
Node(const Node &n){id = n.id; lat = n.lat; lon = n.lon; name = n.name; neighbors = n.neighbors;};
std::string id; // A unique id assign to each point
double lat; // Latitude
double lon; // Longitude
std::string name; // Name of the location. E.g. "Bank of America".
std::vector<std::string> neighbors; // List of the ids of all neighbor points.
};
struct hashfun{
size_t operator() (const Node& rhs) const{
return hash<string>()(rhs.id);
}
};
struct equalfun{
bool operator() (const Node& a, const Node& b) const{
return a.id == b.id;
}
};
struct individual{
std::string gnome;
double fitness;
bool operator< (const individual& t) const{
return fitness < t.fitness;
}
};
class TrojanMap {
public:
// A map of ids to Nodes.
std::unordered_map<std::string, Node> data;
//-----------------------------------------------------
// TODO: You do not and should not change the following functions:
// Create the menu.
void PrintMenu();
// Create the Dynamic menu.
// void DynamicPrintMenu();
// Read in the data
void CreateGraphFromCSVFile();
// Visualization
// Given a location id, plot the point on the map.
void PlotPoint(std::string id);
// Given a lat and lon, plot the point on the map.
void PlotPoint(double lat, double lon);
// Given a vector of location ids draws the path (connects the points)
void PlotPath(std::vector<std::string> &location_ids);
// Given a vector of location ids draws the points on the map (no path).
void PlotPoints(std::vector<std::string> &location_ids);
// Given a vector of location ids draws the points on the map with path.
void PlotPointsandEdges(std::vector<std::string> &location_ids, std::vector<double> &square);
// Given a vector of location ids draws the points with their order on the map (no path).
void PlotPointsOrder(std::vector<std::string> &location_ids);
// Given a vector of location ids and origin, draws the points with their label.
void PlotPointsLabel(std::vector<std::string> &location_ids, std::string origin);
// Create the videos of the progress to get the path
void CreateAnimation(std::vector<std::vector<std::string>>);
// Transform the location to the position on the map
std::pair<double, double> GetPlotLocation(double lat, double lon);
//-----------------------------------------------------
// TODO: Implement these functions and create unit tests for them:
// Get the Latitude of a Node given its id.
double GetLat(std::string id);
// Get the Longitude of a Node given its id.
double GetLon(std::string id);
// Get the name of a Node given its id.
std::string GetName(std::string id);
// Get the id given its name.
std::string GetID(std::string name);
// Get the neighbor ids of a Node.
std::vector<std::string> GetNeighborIDs(std::string id);
// Get the distance between 2 nodes.
double CalculateDistance(const std::string &a, const std::string &b);
// Calculates the total path length for the locations inside the vector.
double CalculatePathLength(const std::vector<std::string> &path);
// Returns a vector of names given a partial name.
std::vector<std::string> Autocomplete(std::string name);
// Returns lat and long of the given the name.
std::pair<double, double> GetPosition(std::string name);
// Given the name of two locations, it should return the **ids** of the nodes
// on the shortest path.
std::vector<std::string> CalculateShortestPath_Dijkstra(std::string location1_name,
std::string location2_name);
std::vector<std::string> CalculateShortestPath_Bellman_Ford(std::string location1_name,
std::string location2_name);
// Given CSV filename, it read and parse locations data from CSV file,
// and return locations vector for topological sort problem.
std::vector<std::string> ReadLocationsFromCSVFile(std::string locations_filename);
// Given CSV filenames, it read and parse dependencise data from CSV file,
// and return dependencies vector for topological sort problem.
std::vector<std::vector<std::string>> ReadDependenciesFromCSVFile(std::string dependencies_filename);
// Given a vector of location names, it should return a sorting of nodes
// that satisfies the given dependencies.
std::vector<std::string> DeliveringTrojan(std::vector<std::string> &location_names,
std::vector<std::vector<std::string>> &dependencies);
void backtrack(int u, vector<std::string>& ids, double& curp, double& maxp, std::vector<std::string>& path, std::vector<std::vector<std::string>>& res);
void opt2_solver(std::vector<std::string>& ids, std::map<std::vector<std::string>, double>& mp, double& bestp, std::vector<std::vector<std::string>>& res);
void opt2SA_solver(double& t, std::vector<std::string>& ids, std::map<std::vector<std::string>, double>& mp, double& bestp, std::vector<std::vector<std::string>>& res);
void opt3_solver(std::vector<std::string>& ids, std::map<std::vector<std::string>, double>& mp, double& bestp, std::vector<std::vector<std::string>>& res);
// Given a vector of location ids, it should reorder them such that the path
// that covers all these points has the minimum length.
// The return value is a pair where the first member is the total_path,
// and the second member is the reordered vector of points.
// (Notice that we don't find the optimal answer. You can return an estimated
// path.)
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan(
std::vector<std::string> &location_ids);
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan_2opt(
std::vector<std::string> &location_ids);
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan_3opt(
std::vector<std::string> &location_ids);
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan_2optSA(
std::vector<std::string> &location_ids);
void simulate_anneal(std::vector<std::string>& ids, double& bestp);
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan_SA(
std::vector<std::string> &location_ids);
std::pair<double, std::vector<std::vector<std::string>>> TravellingTrojan_GA(
std::vector<std::string> &location_ids);
void TSPUtil(std::vector<std::vector<double>>& map, std::pair<double, std::vector<std::vector<std::string>>>& prog, double& rs);
// Given a subgraph specified by a square-shape area, determine whether there is a
// cycle or not in this subgraph.
// vector square has 4 elements: left/right/top/bottom bound in order.
bool dfs_cycle(Node& p, std::unordered_set<Node, hashfun, equalfun> & st, std::vector<double> &square, std::string fa);
bool CycleDetection(std::vector<double> &square);
// Given a location id and k, find the k closest points on the map
void kclose_solver(std::string name, std::string origin, std::set<std::pair<double, std::string>>& distance, int k, std::unordered_map<std::string, double>& hash, double dist);
// void kclose_solver(std::string& name, std::map<double, std::string>& mp, int k, std::unordered_map<std::string, double>& hash, double dist);
void kclose_solver(std::string& name, std::string& origin, std::unordered_set<std::string>& st, std::priority_queue<std::pair<double, std::string>>& pq, int k);
void kclose_solver(std::string& origin, std::priority_queue<std::pair<double, std::string>>& pq, std::unordered_set<std::string>& st, int k);
std::vector<std::string> FindKClosestPoints(std::string name, int k);
private:
// Function to return a random number
// from start and end
int rand_num(int start, int end){
int r = end - start;
int rnum = start + rand() % r;
return rnum;
}
// Function to check if the character
// has already occurred in the string
bool repeat(std::string s, char ch){
for (int i = 0; i < s.size(); i++) {
if (s[i] == ch)
return true;
}
return false;
}
// Function to return a mutated GNOME
// Mutated GNOME is a string
// with a random interchange
// of two genes to create variation in species
std::string mutatedGene(std::string gnome, int V)
{
while (true) {
int r = rand_num(1, V);
int r1 = rand_num(1, V);
if (r1 != r) {
swap(gnome[r], gnome[r1]);
break;
}
}
return gnome;
}
// Function to return a valid GNOME string
// required to create the population
std::string create_gnome(int V){
std::string gnome = "0";
while (true) {
if (gnome.size() == V) {
gnome += gnome[0];
break;
}
int temp = rand_num(1, V);
if (!repeat(gnome, (char)(temp + 48)))
gnome += (char)(temp + 48);
}
return gnome;
}
// Function to return the fitness value of a gnome.
// The fitness value is the path length
// of the path represented by the GNOME.
double cal_fitness(std::string gnome, std::vector<std::vector<double>>& map){
double f = 0;
for (int i = 0; i < gnome.size() - 1; i++) {
if (map[gnome[i] - 48][gnome[i + 1] - 48] == INT_MAX)
return INT_MAX;
f += map[gnome[i] - 48][gnome[i + 1] - 48];
}
return f;
}
// Function to return the updated value
// of the cooling element.
int cooldown(int temp){
return (90 * temp) / 100;
}
// Comparator for GNOME struct.
bool lessthan(struct individual t1, struct individual t2){
return t1.fitness < t2.fitness;
}
//----------------------------------------------------- User-defined functions
};
#endif