-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
374 lines (290 loc) · 11 KB
/
main.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
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
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <limits>
#include <cmath>
#include <vector>
#include <GLFW/glfw3.h>
using namespace std;
const int COORD_LIMIT = 100;
struct DropZone {
double x;
double y;
bool visited;
double distance;
size_t centroid;
};
double dist(DropZone& p1, DropZone& p2) {
return sqrt(((p2.x - p1.x)*(p2.x - p1.x)) + ((p2.y - p1.y)*(p2.y - p1.y)));
}
double distNoSqrt(DropZone& p1, DropZone& p2) {
return ((p2.x - p1.x)*(p2.x - p1.x)) + ((p2.y - p1.y)*(p2.y - p1.y));
}
class DroneFlight {
public:
vector<DropZone> drops;
size_t dropsCount = 0;
vector<size_t> bestPath;
double bestPathCost = 0;
double findMST(size_t permLength) {
for (size_t i = permLength; i < dropsCount; i++) {
drops[currentPath[i]].visited = false;
drops[currentPath[i]].distance = numeric_limits<double>::infinity();
}
drops[currentPath[permLength]].distance = 0;
size_t currentNode = 0;
for (size_t loop = permLength; loop < dropsCount; loop++) {
double minDistance = numeric_limits<double>::infinity();
for (size_t i = permLength; i < dropsCount; i++) {
if (!drops[currentPath[i]].visited && drops[currentPath[i]].distance < minDistance) {
minDistance = drops[currentPath[i]].distance;
currentNode = currentPath[i];
}
}
drops[currentNode].visited = true;
for (size_t i = permLength; i < dropsCount; i++) {
if (drops[currentPath[i]].visited) continue;
double distanceFromCurrentToOtherConnected = distNoSqrt(drops[currentNode], drops[currentPath[i]]);
if (drops[currentPath[i]].distance > distanceFromCurrentToOtherConnected)
drops[currentPath[i]].distance = distanceFromCurrentToOtherConnected;
}
}
double cost = 0;
for (size_t i = permLength; i < dropsCount; i++) {
cost += sqrt(drops[currentPath[i]].distance);
}
return cost;
}
void arbitraryTSP() {
bestPath.push_back(0);
bestPath.push_back(0);
double totalCost = 0;
for (size_t i = 1; i < drops.size(); i++) {
size_t minInsertionIndex = 0;
double minCost = numeric_limits<double>::infinity();
for (size_t j = 0; j < bestPath.size() - 1; j++) {
double newCost = dist(drops[bestPath[j]], drops[i])
+ dist(drops[i], drops[bestPath[j+1]])
- dist(drops[bestPath[j]], drops[bestPath[j+1]]);
if (newCost < minCost) {
minCost = newCost;
minInsertionIndex = j;
}
}
totalCost += minCost;
bestPath.insert(bestPath.begin() + ((int)minInsertionIndex + 1), i);
}
bestPathCost = totalCost;
bestPath.pop_back();
reverse(bestPath.begin() + 1, bestPath.end());
currentPath = bestPath;
}
void genPerms(size_t permLength) {
if (permLength == dropsCount) {
double d = dist(drops[currentPath[0]], drops[currentPath[dropsCount - 1]]);
currentPathCost += d;
if (bestPathCost >= currentPathCost) {
bestPathCost = currentPathCost;
bestPath = currentPath;
}
currentPathCost -= d;
return;
} // if ..complete path
if (!promising(permLength)) {
return;
} // if ..not promising
for (size_t i = permLength; i < dropsCount; i++) {
swap(currentPath[permLength], currentPath[i]);
currentPathCost += dist(drops[currentPath[permLength]], drops[currentPath[permLength - 1]]);;
genPerms(permLength + 1);
currentPathCost -= dist(drops[currentPath[permLength]], drops[currentPath[permLength - 1]]);;
swap(currentPath[permLength], currentPath[i]);
} // for ..unpermuted elementsa
} // genPerms()
private:
vector<size_t> currentPath;
double currentPathCost = 0;
bool promising(size_t permLength) {
if (dropsCount - permLength <= 4) return true;
double costOfMST = findMST(permLength);
double minCostFirst = numeric_limits<double>::infinity();
double minCostLast = numeric_limits<double>::infinity();
for (size_t i = permLength; i < dropsCount; i++) {
double costFirst = distNoSqrt(drops[currentPath[i]], drops[currentPath[0]]);
if (costFirst < minCostFirst) {
minCostFirst = costFirst;
}
double costLast = distNoSqrt(drops[currentPath[i]], drops[currentPath[permLength - 1]]);
if (costLast < minCostLast) {
minCostLast = costLast;
}
}
if (currentPathCost + costOfMST + sqrt(minCostFirst) + sqrt(minCostLast) < bestPathCost) {
return true;
}
return false;
}
};
void initCentroids(vector<DropZone>& locations, vector<DropZone>& centroids) {
srand(2);
for (auto& centroid : centroids) {
size_t randomIndex = rand() % locations.size();
centroid.x = locations[randomIndex].x;
centroid.y = locations[randomIndex].y;
}
}
void setClosestCentroids(vector<DropZone>& locations, vector<DropZone>& centroids) {
for (auto& location : locations) {
double minDistance = numeric_limits<double>::infinity();
size_t newCentroid = 0;
for (auto& centroid : centroids) {
double newDist = dist(location, centroid);
if (minDistance > newDist) {
minDistance = newDist;
newCentroid = centroid.centroid;
}
}
location.centroid = newCentroid;
}
}
bool recalibrateCentroids(vector<DropZone>& locations, vector<DropZone>& centroids, size_t droneCount) {
vector<double> sumX(droneCount, 0);
vector<double> sumY(droneCount, 0);
vector<double> clusterCounts(droneCount, 0);
vector<DropZone> oldCentroids = centroids;
for (auto& location : locations) {
sumX[location.centroid] += location.x;
sumY[location.centroid] += location.y;
clusterCounts[location.centroid]++;
}
double centroidVariation = 0;
for (size_t cluster = 0; cluster < droneCount; cluster++) {
centroids[cluster].x = sumX[cluster] / clusterCounts[cluster];
centroids[cluster].y = sumY[cluster] / clusterCounts[cluster];
centroidVariation += dist(oldCentroids[cluster], centroids[cluster]);
}
if (centroidVariation < 1e-5) return false;
else return true;
}
int main(int argc, char* argv[]) {
ios_base::sync_with_stdio(false);
cout << setprecision(2);
cout << fixed;
size_t droneCount;
cin >> droneCount;
vector<DroneFlight> drones(droneCount, DroneFlight());
size_t num;
cin >> num;
if (num / droneCount > 25) {
cerr << "# of locations exceeds fleet capacity (>25 locations per drone). Exiting...\n";
exit(1);
}
bool printCentroids;
cin >> printCentroids;
vector<DropZone> locations(num);
for (size_t i = 0; i < num; i++) {
cin >> locations[i].x;
if (locations[i].x < -COORD_LIMIT || locations[i].x > COORD_LIMIT) {
cerr << "Coordinates must be between -" << COORD_LIMIT << " and " << COORD_LIMIT << ". Exiting...\n";
exit(1);
}
cin >> locations[i].y;
if (locations[i].y < -COORD_LIMIT || locations[i].y > COORD_LIMIT) {
cerr << "Coordinates must be between -" << COORD_LIMIT << " and " << COORD_LIMIT << ". Exiting...\n";
exit(1);
}
locations[i].distance = numeric_limits<double>::infinity();
locations[i].visited = false;
}
vector<DropZone> centroids(droneCount, DropZone());
for (size_t centroid = 0; centroid < droneCount; centroid++) {
centroids[centroid].centroid = centroid;
}
bool epsilon = true;
initCentroids(locations, centroids);
// int curr = 1;
while (epsilon) {
setClosestCentroids(locations, centroids);
epsilon = recalibrateCentroids(locations, centroids, droneCount);
// cout << "Iteration: " << curr++ << "\n";
}
for (auto& location : locations) {
drones[location.centroid].dropsCount++;
drones[location.centroid].drops.push_back(location);
}
size_t count = 1;
for (auto& drone : drones) {
drone.arbitraryTSP();
drone.genPerms(1);
cout << "Drone " << count++ << " cost: " << drone.bestPathCost << "\n";
}
// --------------------GFLW BEGIN-----------------------
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(1000, 1000, "Drone Routes", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
const vector<vector<double>> colors = {
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
{1, 1, 0},
{0, 1, 1},
{1, 0, 1},
};
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
if (printCentroids) {
glPointSize(12);
for (auto& centroid : centroids) {
glBegin(GL_POINTS);
glColor3f(1, 1, 1);
count = (count + 1) % colors.size();
glVertex2f(centroid.x/COORD_LIMIT, centroid.y/COORD_LIMIT);
glEnd();
}
}
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_POINT_SMOOTH);
glPointSize(8);
glLineWidth(3);
size_t count = 0;
for (auto& drone : drones) {
glBegin(GL_POINTS);
glColor3f(colors[count][0], colors[count][1], colors[count][2]);
count = (count + 1) % colors.size();
for (size_t i = 0; i < drone.dropsCount; i++) {
glVertex2f(drone.drops[drone.bestPath[i]].x/COORD_LIMIT, drone.drops[drone.bestPath[i]].y/COORD_LIMIT);
}
glEnd();
}
count = 0;
for (auto& drone : drones) {
glBegin(GL_LINE_LOOP);
glColor3f(colors[count][0], colors[count][1], colors[count][2]);
count = (count + 1) % colors.size();
for (size_t i = 0; i < drone.dropsCount; i++) {
glVertex2f(drone.drops[drone.bestPath[i]].x/COORD_LIMIT, drone.drops[drone.bestPath[i]].y/COORD_LIMIT);
}
glEnd();
}
/* Swap front and back buffers */
glfwSwapBuffers(window);
/* Poll for and process events */
glfwPollEvents();
}
glfwTerminate();
// --------------------GFLW END-----------------------
return 0;
}