|
@@ -0,0 +1,681 @@
|
|
1
|
+#include "Graph.h"
|
|
2
|
+#include "Stopwatch.h"
|
|
3
|
+#include <algorithm>
|
|
4
|
+#include <chrono>
|
|
5
|
+#include <queue>
|
|
6
|
+#include <random>
|
|
7
|
+#include <thread>
|
|
8
|
+
|
|
9
|
+#include <iostream>
|
|
10
|
+
|
|
11
|
+Graph::Graph()
|
|
12
|
+{
|
|
13
|
+ //ctor
|
|
14
|
+}
|
|
15
|
+
|
|
16
|
+Graph::~Graph()
|
|
17
|
+{
|
|
18
|
+ //dtor
|
|
19
|
+}
|
|
20
|
+
|
|
21
|
+unsigned Graph::getVertexNumber()
|
|
22
|
+{
|
|
23
|
+ return vertexNumber;
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+void Graph::randomGenerateFullGraph(Graph &graph, unsigned maxWeight)
|
|
27
|
+{
|
|
28
|
+ std::random_device randomSrc;
|
|
29
|
+ std::default_random_engine randomGen(randomSrc());
|
|
30
|
+ std::uniform_int_distribution<> weightDist(1, maxWeight);
|
|
31
|
+
|
|
32
|
+ for(int i = 0; i < graph.vertexNumber; i++)
|
|
33
|
+ {
|
|
34
|
+ for(int j = 0; j < graph.vertexNumber; j++)
|
|
35
|
+ {
|
|
36
|
+ if(i != j)
|
|
37
|
+ {
|
|
38
|
+ // Bez warunku na krawedzie juz wygenerowane...
|
|
39
|
+ // ...z tym radzi sobie juz metoda addEdge
|
|
40
|
+ int randomWeight = weightDist(randomGen);
|
|
41
|
+ graph.addEdge(i, j, randomWeight);
|
|
42
|
+ }
|
|
43
|
+ }
|
|
44
|
+ }
|
|
45
|
+}
|
|
46
|
+
|
|
47
|
+std::vector<unsigned> Graph::travellingSalesmanBruteForce(Graph &graph)
|
|
48
|
+{
|
|
49
|
+ // ALGORYTM przegladu zupelnego
|
|
50
|
+ // Implementacja: Jan Potocki 2017
|
|
51
|
+ // (refactoring 2019)
|
|
52
|
+ std::vector<unsigned> vertexArray;
|
|
53
|
+
|
|
54
|
+ // Generowanie "spisu" wierzcholkow
|
|
55
|
+ // (od razu w odpowiedniej kolejnosci dla next_permutation)
|
|
56
|
+ for(int i = 1; i < graph.vertexNumber; i++)
|
|
57
|
+ vertexArray.push_back(i);
|
|
58
|
+
|
|
59
|
+ std::vector<unsigned> minCombination;
|
|
60
|
+ int minRoute = -1;
|
|
61
|
+
|
|
62
|
+ // Petla przegladajaca kolejne permutacje
|
|
63
|
+ do
|
|
64
|
+ {
|
|
65
|
+ std::vector<unsigned> combination;
|
|
66
|
+
|
|
67
|
+ // Dodanie wierzcholka startowego i pierwszego na trasie
|
|
68
|
+ combination.push_back(0);
|
|
69
|
+ combination.push_back(vertexArray.front());
|
|
70
|
+
|
|
71
|
+ // W petli reszta wiercholkow
|
|
72
|
+ for(int i = 1; i < vertexArray.size(); i++)
|
|
73
|
+ combination.push_back(vertexArray.at(i));
|
|
74
|
+
|
|
75
|
+ // Powrot do wierzcholka startowego
|
|
76
|
+ combination.push_back(0);
|
|
77
|
+ // PEA 2
|
|
78
|
+ // Jan Potocki 2017
|
|
79
|
+
|
|
80
|
+ int route = 0;
|
|
81
|
+ for(int i = 1; i < combination.size(); i++)
|
|
82
|
+ route += graph.getWeight(combination.at(i - 1), combination.at(i));
|
|
83
|
+
|
|
84
|
+ if(minRoute == -1 || route < minRoute)
|
|
85
|
+ {
|
|
86
|
+ minRoute = route;
|
|
87
|
+ minCombination = combination;
|
|
88
|
+ }
|
|
89
|
+ }
|
|
90
|
+ while(next_permutation(vertexArray.begin(), vertexArray.end()));
|
|
91
|
+
|
|
92
|
+ return minCombination;
|
|
93
|
+}
|
|
94
|
+
|
|
95
|
+std::vector<unsigned> Graph::travellingSalesmanBranchAndBound(Graph &graph)
|
|
96
|
+{
|
|
97
|
+ // ALGORYTM pracujacy w oparciu o kolejke priorytetowa i niejawnie utworzone drzewo
|
|
98
|
+ // Zrodlo: www.ii.uni.wroc.pl/~prz/2011lato/ah/opracowania/met_podz_ogr.opr.pdf
|
|
99
|
+ // Autor: Mateusz Lyczek 2011
|
|
100
|
+ // Implementacja: Jan Potocki 2017
|
|
101
|
+ std::priority_queue<std::vector<unsigned>, std::vector< std::vector<unsigned> >, RouteComparison> routeQueue;
|
|
102
|
+ std::vector<unsigned> optimalRoute; // Tu bedziemy zapisywac optymalne (w danej chwili) rozwiazanie
|
|
103
|
+ int optimalRouteLength = -1; // -1 - bedziemy odtad uznawac, ze to jest nieskonczonosc ;-)
|
|
104
|
+
|
|
105
|
+ // UMOWA
|
|
106
|
+ // Pierwszy element wektora to dlugosc trasy (trzeba ustawic "z palca"!)
|
|
107
|
+ // Kolejne to wierzcholki na trasie
|
|
108
|
+ std::vector<unsigned> currentRoute; // Niejawne tworzenie drzewa, tu bedzie korzen
|
|
109
|
+ currentRoute.push_back(0); // Poczatkowe oszacowanie nie ma znaczenia
|
|
110
|
+ currentRoute.push_back(0); // Wierzcholek startowy (korzen drzewa rozwiazan)
|
|
111
|
+ routeQueue.push(currentRoute); // Dodanie do kolejki korzenia
|
|
112
|
+
|
|
113
|
+ while(!routeQueue.empty())
|
|
114
|
+ {
|
|
115
|
+ // Przypisanie korzenia do dalszej roboty
|
|
116
|
+ currentRoute = routeQueue.top();
|
|
117
|
+ routeQueue.pop();
|
|
118
|
+
|
|
119
|
+ // Sprawdzenie, czy rozwiazanie jest warte rozwijania, czy odrzucic
|
|
120
|
+ if(optimalRouteLength == -1 || currentRoute.at(0) < optimalRouteLength)
|
|
121
|
+ {
|
|
122
|
+ for(int i = 0; i < graph.vertexNumber; i++)
|
|
123
|
+ {
|
|
124
|
+ // Petla wykonywana dla kazdego potomka rozpatrywanego wlasnie rozwiazania w drzewie
|
|
125
|
+ // Ustalenie, czy dany wierzcholek mozna jeszcze wykorzystac, czy juz zostal uzyty
|
|
126
|
+ bool vertexUsed = false;
|
|
127
|
+ for(int j = 1; j < currentRoute.size(); j++)
|
|
128
|
+ {
|
|
129
|
+ if(currentRoute.at(j) == i)
|
|
130
|
+ {
|
|
131
|
+ vertexUsed = true;
|
|
132
|
+ break;
|
|
133
|
+ }
|
|
134
|
+ }
|
|
135
|
+ if(vertexUsed)
|
|
136
|
+ continue;
|
|
137
|
+
|
|
138
|
+ // Niejawne utworzenie nowego wezla reprezuntujacego rozpatrywane rozwiazanie...
|
|
139
|
+ std::vector<unsigned> nextRoute = currentRoute;
|
|
140
|
+ //unsigned nextLength = graph.getWeight(nextRoute.back(), i);
|
|
141
|
+ nextRoute.push_back(i);
|
|
142
|
+
|
|
143
|
+ // Dalej bedziemy postepowac roznie...
|
|
144
|
+ if(nextRoute.size() > graph.vertexNumber)
|
|
145
|
+ {
|
|
146
|
+ // Doszlismy wlasnie do liscia
|
|
147
|
+ // Dodajemy droge powrotna, nie musimy nic szacowac
|
|
148
|
+ // (wszystko juz wiemy)
|
|
149
|
+ nextRoute.push_back(0);
|
|
150
|
+
|
|
151
|
+ nextRoute.at(0) = 0;
|
|
152
|
+
|
|
153
|
+ for(int j = 1; j < nextRoute.size() - 1; j++)
|
|
154
|
+ {
|
|
155
|
+ // Liczymy dystans od poczatku do konca
|
|
156
|
+ nextRoute.at(0) += graph.getWeight(nextRoute.at(j), nextRoute.at(j+ 1));
|
|
157
|
+ }
|
|
158
|
+ if(optimalRouteLength == -1 || nextRoute.at(0) < optimalRouteLength)
|
|
159
|
+ {
|
|
160
|
+ optimalRouteLength = nextRoute.at(0);
|
|
161
|
+ nextRoute.erase(nextRoute.begin());
|
|
162
|
+ optimalRoute = nextRoute;
|
|
163
|
+ }
|
|
164
|
+ }
|
|
165
|
+ else
|
|
166
|
+ {
|
|
167
|
+ // Liczenie tego, co juz wiemy, od nowa...
|
|
168
|
+ // (dystans od poczatku)
|
|
169
|
+ nextRoute.at(0) = 0;
|
|
170
|
+ for(int j = 1; j < nextRoute.size() - 1; j++)
|
|
171
|
+ {
|
|
172
|
+ nextRoute.at(0) += graph.getWeight(nextRoute.at(j), nextRoute.at(j + 1));
|
|
173
|
+ }
|
|
174
|
+
|
|
175
|
+ // Reszte szacujemy...
|
|
176
|
+ // Pomijamy od razu wierzcholek startowy
|
|
177
|
+ for(int j = 1; j < graph.vertexNumber; j++)
|
|
178
|
+ {
|
|
179
|
+ // Odrzucenie wierzcholkow juz umieszczonych na trasie
|
|
180
|
+ bool vertexUsed = false;
|
|
181
|
+ for(int k = 1; k < currentRoute.size(); k++)
|
|
182
|
+ {
|
|
183
|
+ if(j == currentRoute.at(k))
|
|
184
|
+ {
|
|
185
|
+ vertexUsed = true;
|
|
186
|
+ break;
|
|
187
|
+ }
|
|
188
|
+ }
|
|
189
|
+ if(vertexUsed)
|
|
190
|
+ continue;
|
|
191
|
+
|
|
192
|
+ int minEdge = -1;
|
|
193
|
+ for(int k = 0; k < graph.vertexNumber; k++)
|
|
194
|
+ {
|
|
195
|
+ // Odrzucenie krawedzi do wierzcholka 0 przy ostatnim wierzcholku w czesciowym rozwiazaniu
|
|
196
|
+ // Wyjatkiem jest ostatnia mozliwa krawedz
|
|
197
|
+ if(j == i && k == 0)
|
|
198
|
+ continue;
|
|
199
|
+
|
|
200
|
+ // Odrzucenie krawedzi do wierzcholka umieszczonego juz na rozwazanej trasie
|
|
201
|
+ bool vertexUsed = false;
|
|
202
|
+ for(int l = 2; l < nextRoute.size(); l++)
|
|
203
|
+ {
|
|
204
|
+ if(k == nextRoute.at(l))
|
|
205
|
+ {
|
|
206
|
+ vertexUsed = true;
|
|
207
|
+ break;
|
|
208
|
+ }
|
|
209
|
+ }
|
|
210
|
+ if(vertexUsed)
|
|
211
|
+ continue;
|
|
212
|
+
|
|
213
|
+ // Odrzucenie samego siebie
|
|
214
|
+ if(k == j)
|
|
215
|
+ continue;
|
|
216
|
+
|
|
217
|
+ // Znalezienie najkrotszej mozliwej jeszcze do uzycia krawedzi
|
|
218
|
+ unsigned consideredLength = graph.getWeight(j, k);
|
|
219
|
+
|
|
220
|
+ if(minEdge == -1)
|
|
221
|
+ minEdge = consideredLength;
|
|
222
|
+ else if(minEdge > consideredLength)
|
|
223
|
+ minEdge = consideredLength;
|
|
224
|
+ }
|
|
225
|
+ nextRoute.at(0) += minEdge;
|
|
226
|
+ }
|
|
227
|
+
|
|
228
|
+ // ...i teraz zastanawiamy sie co dalej
|
|
229
|
+ if(optimalRouteLength == -1 || nextRoute.at(0) < optimalRouteLength)
|
|
230
|
+ {
|
|
231
|
+ routeQueue.push(nextRoute);
|
|
232
|
+ }
|
|
233
|
+ }
|
|
234
|
+ }
|
|
235
|
+ }
|
|
236
|
+ else
|
|
237
|
+ {
|
|
238
|
+ // Jezeli jedno rozwiazanie odrzucilismy, to wszystkie inne tez mozemy
|
|
239
|
+ // (kolejka priorytetowa, inne nie moga byc lepsze)
|
|
240
|
+ break;
|
|
241
|
+ }
|
|
242
|
+ }
|
|
243
|
+
|
|
244
|
+ return optimalRoute;
|
|
245
|
+}
|
|
246
|
+
|
|
247
|
+std::vector<unsigned> Graph::travellingSalesmanGreedy(Graph &graph, unsigned startVertex)
|
|
248
|
+{
|
|
249
|
+ // ALGORYTM zachlanny z wierzcholkiem startowym przekazanym w parametrze
|
|
250
|
+ // Implementacja: Jan Potocki 2017
|
|
251
|
+ std::vector<unsigned> route;
|
|
252
|
+
|
|
253
|
+ // std::random_device randomSrc;
|
|
254
|
+ // std::default_random_engine randomGen(randomSrc());
|
|
255
|
+ // std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
|
|
256
|
+
|
|
257
|
+ // Losowanie wierzcholka startowego
|
|
258
|
+ //route.push_back(vertexDist(randomGen));
|
|
259
|
+ route.push_back(startVertex);
|
|
260
|
+
|
|
261
|
+ for(int i = 0; i < graph.vertexNumber - 1; i++)
|
|
262
|
+ {
|
|
263
|
+ int minEdge = -1;
|
|
264
|
+ unsigned nextVertex;
|
|
265
|
+ for(int j = 0; j < graph.vertexNumber; j++)
|
|
266
|
+ {
|
|
267
|
+ // Odrzucenie samego siebie lub wierzcholka startowego
|
|
268
|
+ // (zeby bylo szybciej)
|
|
269
|
+ if(route.back() == j || route.front() == j)
|
|
270
|
+ continue;
|
|
271
|
+
|
|
272
|
+ // Odrzucenie krawedzi do wierzcholka umieszczonego juz na trasie
|
|
273
|
+ bool vertexUsed = false;
|
|
274
|
+ for(int k = 0; k < route.size(); k++)
|
|
275
|
+ {
|
|
276
|
+ if(j == route.at(k))
|
|
277
|
+ {
|
|
278
|
+ vertexUsed = true;
|
|
279
|
+ break;
|
|
280
|
+ }
|
|
281
|
+ }
|
|
282
|
+ if(vertexUsed)
|
|
283
|
+ continue;
|
|
284
|
+
|
|
285
|
+ // Znalezienie najkrotszej mozliwej jeszcze do uzycia krawedzi
|
|
286
|
+ unsigned consideredLength = graph.getWeight(route.back(), j);
|
|
287
|
+
|
|
288
|
+ if(minEdge == -1)
|
|
289
|
+ {
|
|
290
|
+ minEdge = consideredLength;
|
|
291
|
+ nextVertex = j;
|
|
292
|
+ }
|
|
293
|
+ else if(minEdge > consideredLength)
|
|
294
|
+ {
|
|
295
|
+ minEdge = consideredLength;
|
|
296
|
+ nextVertex = j;
|
|
297
|
+ }
|
|
298
|
+ }
|
|
299
|
+ route.push_back(nextVertex);
|
|
300
|
+ }
|
|
301
|
+
|
|
302
|
+ route.push_back(startVertex);
|
|
303
|
+ return route;
|
|
304
|
+}
|
|
305
|
+
|
|
306
|
+std::vector<unsigned> Graph::travellingSalesmanHybrid(Graph &graph)
|
|
307
|
+{
|
|
308
|
+ // ALGORYTM hybrydowy losowo-zachlanny
|
|
309
|
+ // Losowa czesc wierzcholkow jest losowana, reszta zachlannie
|
|
310
|
+ // Implementacja: Jan Potocki 2019
|
|
311
|
+ std::vector<unsigned> route;
|
|
312
|
+
|
|
313
|
+ std::random_device randomSrc;
|
|
314
|
+ std::default_random_engine randomGen(randomSrc());
|
|
315
|
+ std::uniform_int_distribution<> vertexNumberDist(1, graph.vertexNumber);
|
|
316
|
+ std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
|
|
317
|
+
|
|
318
|
+ // Liczba losowanych wierzcholkow
|
|
319
|
+ unsigned randomVertexNumber = vertexNumberDist(randomGen);
|
|
320
|
+
|
|
321
|
+ // Czesc losowa
|
|
322
|
+ for(int i = 0; i < randomVertexNumber; i++)
|
|
323
|
+ {
|
|
324
|
+ unsigned randomVertex;
|
|
325
|
+ bool vertexUsed;
|
|
326
|
+
|
|
327
|
+ do
|
|
328
|
+ {
|
|
329
|
+ randomVertex = vertexDist(randomGen);
|
|
330
|
+ vertexUsed = false;
|
|
331
|
+
|
|
332
|
+ for(int j = 0; j < route.size(); j++)
|
|
333
|
+ {
|
|
334
|
+ if(route.at(j) == randomVertex)
|
|
335
|
+ {
|
|
336
|
+ vertexUsed = true;
|
|
337
|
+ break;
|
|
338
|
+ }
|
|
339
|
+ }
|
|
340
|
+ } while(vertexUsed == true);
|
|
341
|
+
|
|
342
|
+ route.push_back(randomVertex);
|
|
343
|
+ }
|
|
344
|
+
|
|
345
|
+ // Czesc zachlanna
|
|
346
|
+ for(int i = 0; i < graph.vertexNumber - randomVertexNumber; i++)
|
|
347
|
+ {
|
|
348
|
+ int minEdge = -1;
|
|
349
|
+ unsigned nextVertex;
|
|
350
|
+ for(int j = 0; j < graph.vertexNumber; j++)
|
|
351
|
+ {
|
|
352
|
+ // Odrzucenie samego siebie lub wierzcholka startowego
|
|
353
|
+ // (zeby bylo szybciej)
|
|
354
|
+ if(route.back() == j || route.front() == j)
|
|
355
|
+ continue;
|
|
356
|
+
|
|
357
|
+ // Odrzucenie krawedzi do wierzcholka umieszczonego juz na trasie
|
|
358
|
+ bool vertexUsed = false;
|
|
359
|
+ for(int k = 0; k < route.size(); k++)
|
|
360
|
+ {
|
|
361
|
+ if(j == route.at(k))
|
|
362
|
+ {
|
|
363
|
+ vertexUsed = true;
|
|
364
|
+ break;
|
|
365
|
+ }
|
|
366
|
+ }
|
|
367
|
+ if(vertexUsed)
|
|
368
|
+ continue;
|
|
369
|
+
|
|
370
|
+ // Znalezienie najkrotszej mozliwej jeszcze do uzycia krawedzi
|
|
371
|
+ unsigned consideredLength = graph.getWeight(route.back(), j);
|
|
372
|
+
|
|
373
|
+ if(minEdge == -1)
|
|
374
|
+ {
|
|
375
|
+ minEdge = consideredLength;
|
|
376
|
+ nextVertex = j;
|
|
377
|
+ }
|
|
378
|
+ else if(minEdge > consideredLength)
|
|
379
|
+ {
|
|
380
|
+ minEdge = consideredLength;
|
|
381
|
+ nextVertex = j;
|
|
382
|
+ }
|
|
383
|
+ }
|
|
384
|
+ route.push_back(nextVertex);
|
|
385
|
+ }
|
|
386
|
+
|
|
387
|
+ route.push_back(route.front());
|
|
388
|
+ return route;
|
|
389
|
+}
|
|
390
|
+
|
|
391
|
+std::vector<unsigned> Graph::travellingSalesmanRandom(Graph &graph)
|
|
392
|
+{
|
|
393
|
+ // ALGORYTM losowy
|
|
394
|
+ // Implementacja: Jan Potocki 2019
|
|
395
|
+ std::vector<unsigned> route;
|
|
396
|
+
|
|
397
|
+ std::random_device randomSrc;
|
|
398
|
+ std::default_random_engine randomGen(randomSrc());
|
|
399
|
+ std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
|
|
400
|
+
|
|
401
|
+ for(int i = 0; i < graph.vertexNumber; i++)
|
|
402
|
+ {
|
|
403
|
+ unsigned randomVertex;
|
|
404
|
+ bool vertexUsed;
|
|
405
|
+
|
|
406
|
+ do
|
|
407
|
+ {
|
|
408
|
+ randomVertex = vertexDist(randomGen);
|
|
409
|
+ vertexUsed = false;
|
|
410
|
+
|
|
411
|
+ for(int j = 0; j < route.size(); j++)
|
|
412
|
+ {
|
|
413
|
+ if(route.at(j) == randomVertex)
|
|
414
|
+ {
|
|
415
|
+ vertexUsed = true;
|
|
416
|
+ break;
|
|
417
|
+ }
|
|
418
|
+ }
|
|
419
|
+ } while(vertexUsed == true);
|
|
420
|
+
|
|
421
|
+ route.push_back(randomVertex);
|
|
422
|
+ }
|
|
423
|
+
|
|
424
|
+ route.push_back(route.front());
|
|
425
|
+ return route;
|
|
426
|
+}
|
|
427
|
+
|
|
428
|
+std::vector<unsigned> Graph::travellingSalesmanTabuSearch(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, unsigned threadsNumber)
|
|
429
|
+{
|
|
430
|
+ // ALGORYTM wielawotkowy oparty na metaheurystyce tabu search
|
|
431
|
+ // Pomocniczy kod uruchamiajacy watki wlasciwego algorytmu w najbardziej optymalny sposob
|
|
432
|
+ // Implementacja: Jan Potocki 2019
|
|
433
|
+ std::vector<unsigned> startVertexVector;
|
|
434
|
+ std::vector<std::thread> threadsVector;
|
|
435
|
+ std::vector<std::vector<unsigned>> resultsVector(threadsNumber);
|
|
436
|
+ std::vector<int> resultsLength(threadsNumber);
|
|
437
|
+ std::vector<unsigned> optimalResult;
|
|
438
|
+ int optimalResultIndex;
|
|
439
|
+ int optimalResultLength;
|
|
440
|
+
|
|
441
|
+ std::random_device randomSrc;
|
|
442
|
+ std::default_random_engine randomGen(randomSrc());
|
|
443
|
+ std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
|
|
444
|
+
|
|
445
|
+ for(int i = 0; i < threadsNumber; i++)
|
|
446
|
+ {
|
|
447
|
+ std::vector<unsigned> startRoute;
|
|
448
|
+ unsigned startVertex;
|
|
449
|
+ bool startVertexUsed;
|
|
450
|
+
|
|
451
|
+ if(i < graph.vertexNumber)
|
|
452
|
+ {
|
|
453
|
+ do
|
|
454
|
+ {
|
|
455
|
+ startVertex = vertexDist(randomGen);
|
|
456
|
+ startVertexUsed = false;
|
|
457
|
+
|
|
458
|
+ for(int j = 0; j < startVertexVector.size(); j++)
|
|
459
|
+ {
|
|
460
|
+ if(startVertexVector.at(j) == startVertex)
|
|
461
|
+ {
|
|
462
|
+ startVertexUsed = true;
|
|
463
|
+ break;
|
|
464
|
+ }
|
|
465
|
+ }
|
|
466
|
+ } while(startVertexUsed == true);
|
|
467
|
+
|
|
468
|
+ startVertexVector.push_back(startVertex);
|
|
469
|
+ startRoute = Graph::travellingSalesmanGreedy(graph, startVertex);
|
|
470
|
+ }
|
|
471
|
+ else
|
|
472
|
+ {
|
|
473
|
+ startRoute = Graph::travellingSalesmanRandom(graph);
|
|
474
|
+ }
|
|
475
|
+
|
|
476
|
+ threadsVector.push_back(std::thread(Graph::travellingSalesmanTabuSearchEngine, std::ref(graph), tabuSteps, diversification, iterationsToRestart, minStopTime, startRoute, std::ref(resultsVector.at(i)), std::ref(resultsLength.at(i))));
|
|
477
|
+ }
|
|
478
|
+
|
|
479
|
+ for(int i = 0; i < threadsNumber; i++)
|
|
480
|
+ threadsVector.at(i).join();
|
|
481
|
+
|
|
482
|
+ optimalResultIndex = 0;
|
|
483
|
+ optimalResultLength = resultsLength.at(0);
|
|
484
|
+
|
|
485
|
+ for(int i = 0; i < threadsNumber; i++)
|
|
486
|
+ {
|
|
487
|
+ if(resultsLength.at(i) < optimalResultLength)
|
|
488
|
+ {
|
|
489
|
+ optimalResultIndex = i;
|
|
490
|
+ optimalResultLength = resultsLength.at(i);
|
|
491
|
+ }
|
|
492
|
+ }
|
|
493
|
+
|
|
494
|
+ optimalResult = resultsVector.at(optimalResultIndex);
|
|
495
|
+
|
|
496
|
+ return optimalResult;
|
|
497
|
+}
|
|
498
|
+
|
|
499
|
+void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, std::vector<unsigned> startRoute, std::vector<unsigned> &result, int &resultLength)
|
|
500
|
+{
|
|
501
|
+ // ALGORYTM oparty na metaheurystyce tabu search z dywersyfikacja i sasiedztwem typu swap
|
|
502
|
+ // Rdzen przeznaczony do uruchamiania jako jeden watek
|
|
503
|
+ // Projekt i implementacja: Jan Potocki 2017
|
|
504
|
+ // (refactoring 2019)
|
|
505
|
+ Stopwatch onboardClock;
|
|
506
|
+
|
|
507
|
+ std::vector<unsigned> optimalRoute; // Tu bedziemy zapisywac optymalne (w danej chwili) rozwiazanie
|
|
508
|
+ int optimalRouteLength = -1; // -1 - bedziemy odtad uznawac, ze to jest nieskonczonosc ;-)
|
|
509
|
+ std::vector<unsigned> currentRoute; // Rozpatrywane rozwiazanie
|
|
510
|
+
|
|
511
|
+ // Wyznaczenie poczatkowego rozwiazania algorytmem zachlannym
|
|
512
|
+ //currentRoute = Graph::travellingSalesmanGreedy(graph);
|
|
513
|
+ currentRoute = startRoute;
|
|
514
|
+
|
|
515
|
+ // Inicjalizacja glownej petli...
|
|
516
|
+ std::vector< std::vector<unsigned> > tabuArray;
|
|
517
|
+ unsigned currentTabuSteps = tabuSteps;
|
|
518
|
+ int stopCounter = 0;
|
|
519
|
+ bool timeNotExceeded = true;
|
|
520
|
+ onboardClock.start();
|
|
521
|
+
|
|
522
|
+ // Rdzen algorytmu
|
|
523
|
+ while(timeNotExceeded == true)
|
|
524
|
+ {
|
|
525
|
+ bool cheeseSupplied = true;
|
|
526
|
+ bool intensification = false;
|
|
527
|
+
|
|
528
|
+ while(cheeseSupplied == true)
|
|
529
|
+ {
|
|
530
|
+ std::vector<unsigned> nextRoute;
|
|
531
|
+ int nextRouteLength = -1;
|
|
532
|
+
|
|
533
|
+ std::vector<unsigned> nextTabu(3, 0);
|
|
534
|
+ nextTabu.at(0) = currentTabuSteps;
|
|
535
|
+
|
|
536
|
+ // Generowanie sasiedztwa typu swap przez zamiane wierzcholkow
|
|
537
|
+ // (wierzcholka startowego i zarazem ostatniego nie ruszamy,
|
|
538
|
+ // pomijamy tez od razu aktualny wierzcholek)
|
|
539
|
+ for(int i = 1; i < graph.vertexNumber - 1; i++)
|
|
540
|
+ {
|
|
541
|
+ for(int j = i + 1; j < graph.vertexNumber; j++)
|
|
542
|
+ {
|
|
543
|
+ std::vector<unsigned> neighbourRoute = currentRoute;
|
|
544
|
+
|
|
545
|
+ // Zamiana
|
|
546
|
+ unsigned buffer = neighbourRoute.at(j);
|
|
547
|
+ neighbourRoute.at(j) = neighbourRoute.at(i);
|
|
548
|
+ neighbourRoute.at(i) = buffer;
|
|
549
|
+
|
|
550
|
+ unsigned neighbourRouteLength = 0;
|
|
551
|
+ for(int i = 1; i < neighbourRoute.size(); i++)
|
|
552
|
+ neighbourRouteLength += graph.getWeight(neighbourRoute.at(i - 1), neighbourRoute.at(i));
|
|
553
|
+
|
|
554
|
+ // Sprawdzenie, czy dany ruch nie jest na liscie tabu
|
|
555
|
+ // (dwa wierzcholki)
|
|
556
|
+ bool tabu = false;
|
|
557
|
+ for(int k = 0; k < tabuArray.size(); k++)
|
|
558
|
+ {
|
|
559
|
+ if(tabuArray.at(k).at(1) == i && tabuArray.at(k).at(2) == j)
|
|
560
|
+ {
|
|
561
|
+ tabu = true;
|
|
562
|
+ break;
|
|
563
|
+ }
|
|
564
|
+
|
|
565
|
+ if(tabuArray.at(k).at(1) == j && tabuArray.at(k).at(2) == i)
|
|
566
|
+ {
|
|
567
|
+ tabu = true;
|
|
568
|
+ break;
|
|
569
|
+ }
|
|
570
|
+ }
|
|
571
|
+
|
|
572
|
+ // Kryterium aspiracji...
|
|
573
|
+ if(tabu == true && neighbourRouteLength >= optimalRouteLength)
|
|
574
|
+ // ...jezeli niespelnione - pomijamy ruch
|
|
575
|
+ continue;
|
|
576
|
+
|
|
577
|
+ if(nextRouteLength == -1)
|
|
578
|
+ {
|
|
579
|
+ nextRouteLength = neighbourRouteLength;
|
|
580
|
+ nextRoute = neighbourRoute;
|
|
581
|
+ nextTabu.at(1) = i;
|
|
582
|
+ nextTabu.at(2) = j;
|
|
583
|
+ }
|
|
584
|
+ else if(nextRouteLength > neighbourRouteLength)
|
|
585
|
+ {
|
|
586
|
+ nextRouteLength = neighbourRouteLength;
|
|
587
|
+ nextRoute = neighbourRoute;
|
|
588
|
+ nextTabu.at(1) = i;
|
|
589
|
+ nextTabu.at(2) = j;
|
|
590
|
+ }
|
|
591
|
+ }
|
|
592
|
+ }
|
|
593
|
+
|
|
594
|
+ currentRoute = nextRoute;
|
|
595
|
+
|
|
596
|
+ if(optimalRouteLength == -1)
|
|
597
|
+ {
|
|
598
|
+ optimalRouteLength = nextRouteLength;
|
|
599
|
+ optimalRoute = nextRoute;
|
|
600
|
+
|
|
601
|
+ // Reset licznika
|
|
602
|
+ stopCounter = 0;
|
|
603
|
+ }
|
|
604
|
+ else if(optimalRouteLength > nextRouteLength)
|
|
605
|
+ {
|
|
606
|
+ optimalRouteLength = nextRouteLength;
|
|
607
|
+ optimalRoute = nextRoute;
|
|
608
|
+
|
|
609
|
+ // Zaplanowanie intensyfikacji
|
|
610
|
+ intensification = true;
|
|
611
|
+
|
|
612
|
+ // Reset licznika
|
|
613
|
+ stopCounter = 0;
|
|
614
|
+ }
|
|
615
|
+
|
|
616
|
+ // Weryfikacja listy tabu...
|
|
617
|
+ // ...aktualizacja kadencji na liscie tabu
|
|
618
|
+ for(int i = 0; i < tabuArray.size(); i++)
|
|
619
|
+ {
|
|
620
|
+ tabuArray.at(i).at(0)--;
|
|
621
|
+ }
|
|
622
|
+
|
|
623
|
+ //...usuniecie zerowych kadencji
|
|
624
|
+ for(int i = 0; i < tabuArray.size(); i++)
|
|
625
|
+ {
|
|
626
|
+ if(tabuArray.at(i).at(0) == 0)
|
|
627
|
+ tabuArray.erase(tabuArray.begin() + i);
|
|
628
|
+ }
|
|
629
|
+
|
|
630
|
+ // ...dopisanie ostatniego ruchu do listy tabu
|
|
631
|
+ tabuArray.push_back(nextTabu);
|
|
632
|
+
|
|
633
|
+ // Zliczenie iteracji
|
|
634
|
+ stopCounter++;
|
|
635
|
+
|
|
636
|
+ // Zmierzenie czasu
|
|
637
|
+ onboardClock.stop();
|
|
638
|
+ if(onboardClock.read() > minStopTime)
|
|
639
|
+ timeNotExceeded = false;
|
|
640
|
+
|
|
641
|
+ // Sprawdzenie warunku zatrzymania
|
|
642
|
+ if(diversification == true)
|
|
643
|
+ {
|
|
644
|
+ // Przy aktywowanej dywersyfikacji - po zadanej liczbie iteracji bez poprawy
|
|
645
|
+ if(stopCounter >= iterationsToRestart || timeNotExceeded == false)
|
|
646
|
+ cheeseSupplied = false;
|
|
647
|
+ }
|
|
648
|
+ else
|
|
649
|
+ {
|
|
650
|
+ // Przy nieaktywowanej dywersyfikacji - po uplynieciu okreslonego czasu
|
|
651
|
+ if(timeNotExceeded == false)
|
|
652
|
+ cheeseSupplied = false;
|
|
653
|
+ }
|
|
654
|
+ }
|
|
655
|
+
|
|
656
|
+ // Dywersyfikacja
|
|
657
|
+ if(diversification == true)
|
|
658
|
+ {
|
|
659
|
+ if(intensification == true)
|
|
660
|
+ {
|
|
661
|
+ // Intensyfikacja przeszukiwania przy ostatnim minimum
|
|
662
|
+ currentRoute = optimalRoute;
|
|
663
|
+ currentTabuSteps = tabuSteps / 4;
|
|
664
|
+ intensification = false;
|
|
665
|
+ }
|
|
666
|
+ else
|
|
667
|
+ {
|
|
668
|
+ // Algorytm hybrydowy losowo-zachlanny
|
|
669
|
+ currentRoute = Graph::travellingSalesmanHybrid(graph);
|
|
670
|
+ currentTabuSteps = tabuSteps;
|
|
671
|
+ intensification = false;
|
|
672
|
+ }
|
|
673
|
+ }
|
|
674
|
+
|
|
675
|
+ // Reset licznika iteracji przed restartem
|
|
676
|
+ stopCounter = 0;
|
|
677
|
+ }
|
|
678
|
+
|
|
679
|
+ result = optimalRoute;
|
|
680
|
+ resultLength = optimalRouteLength;
|
|
681
|
+}
|