Переглянути джерело

Automatic tabu length, refactored menu

Jan Potocki 5 роки тому
джерело
коміт
ec78c19192
1 змінених файлів з 131 додано та 78 видалено
  1. 131
    78
      pea2plus.cpp

+ 131
- 78
pea2plus.cpp Переглянути файл

@@ -19,31 +19,29 @@ const int measureTabuStop[4] = {1, 5, 10, 15};
19 19
 // Maksymalna odleglosc miast przy automatycznym generowaniu
20 20
 const int measureSalesmanDistance = 400;
21 21
 
22
-// Kadencja tabu search
23
-const int tabuLength = 30;
24
-// Kryterium dywersyfikacji, liczba iteracji bez poprawy
25
-const int tabuIterationsToRestart = 10000;
26
-
27 22
 // Wykorzystanie reprezentacji grafu w postaci list sasiedztwa...
28 23
 // ...zamiast (domyslnie) macierzy sasiedztwa
29 24
 // (wolniejsze obliczenia, mniejsze uzycie pamieci)
30 25
 const bool useListGraph = false;
31 26
 
32
-// Liczba watkow tabu search
33
-const unsigned tabuThreadsNumber = 2;
34
-
35
-// Domyslny czas zatrzymania algorytmu tabu search [s]
36
-int tabuStopTime = 60;
37
-
27
+// Domyslna kadencja tabu search - wybor automatyczny
28
+unsigned tabuLength = 0;
38 29
 // Domyslny stan dywersyfikacji
39 30
 bool tabuDiversification = true;
31
+// Domyslne kryterium dywersyfikacji, liczba iteracji bez poprawy
32
+int tabuIterationsToRestart = 10000;
33
+// Domyslny czas zatrzymania algorytmu tabu search [s]
34
+unsigned tabuStopTime = 60;
35
+
36
+// Domyslna liczba watkow tabu search
37
+unsigned tabuThreadsNumber = 2;
40 38
 
41 39
 int main()
42 40
 {
43 41
     Stopwatch clock;                // czasomierz
44 42
     Graph *graph = NULL;            // <- tu bedziemy zapisywac adresy przez caly program
45 43
 
46
-    cout << "PEA Projekt 2 v2.0ALPHA Plus" << endl;
44
+    cout << "PEA Projekt 2 Plus v2.0ALPHA" << endl;
47 45
     cout << "Jan Potocki 2017-2019" << endl;
48 46
     cout << "(beerware)" << endl;
49 47
     if(useListGraph)
@@ -55,17 +53,15 @@ int main()
55 53
     int salesmanSelection;
56 54
     do
57 55
     {
58
-        cout << "1 - wygeneruj losowe dane" << endl;
59
-        cout << "2 - wyswietl dane" << endl;
60
-        cout << "3 - dywersyfikacja TS" << endl;
61
-        cout << "4 - czas zatrzymania TS" << endl;
62
-        cout << "5 - tabu search" << endl;
63
-        cout << "6 - algorytm zachlanny" << endl;
64
-        cout << "7 - podzial i ograniczenia" << endl;
65
-        cout << "8 - przeglad zupelny" << endl;
66
-        cout << "9 - automatyczne pomiary (tabu search)" << endl;
67
-        cout << "10 - wczytaj dane z pliku ATSP" << endl;
68
-        cout << "11 - wczytaj dane z pliku TSP" << endl;
56
+        cout << "1 - Wygeneruj losowe dane" << endl;
57
+        cout << "2 - Wyswietl dane" << endl;
58
+        cout << "3 - Ustawienia TS" << endl;
59
+        cout << "4 - Tabu search" << endl;
60
+        cout << "5 - Podzial i ograniczenia" << endl;
61
+        cout << "6 - Przeglad zupelny" << endl;
62
+        cout << "7 - Automatyczne pomiary (tabu search)" << endl;
63
+        cout << "8 - Wczytaj dane z pliku TSPLIB FULL_MATRIX" << endl;
64
+        cout << "9 - Wczytaj dane z pliku TSPLIB EUC_2D" << endl;
69 65
         cout << "Aby zakonczyc - 0" << endl;
70 66
         cout << "Wybierz: ";
71 67
         cin >> salesmanSelection;
@@ -102,41 +98,109 @@ int main()
102 98
             break;
103 99
             case 3:
104 100
             {
105
-                tabuDiversification = !tabuDiversification;
101
+                int settingsSelection;
102
+                do
103
+                {
104
+                    if(tabuDiversification == true)
105
+                        cout << "1 - Przelacz dywersyfikacje" << "\t" << "(wlaczona)" << endl;
106
+                    else
107
+                        cout << "1 - Przelacz dywersyfikacje" << "\t" << "(wylaczona)" << endl;
106 108
 
107
-                if(tabuDiversification == true)
108
-                    cout << "Dywersyfikacja TS zostala wlaczona" << endl;
109
-                else
110
-                    cout << "Dywersyfikacja TS zostala wylaczona" << endl;
111
-                cout << endl;
109
+                    cout << "2 - Kryterium dywersyfikacji" << "\t" << "(" << tabuIterationsToRestart << " iteracji)" << endl;
110
+
111
+                    if(tabuLength == 0)
112
+                        cout << "3 - Kadencja na liscie tabu" << "\t" << "(auto)" << endl;
113
+                    else
114
+                        cout << "3 - Kadencja na liscie tabu" << "\t" << "(" << tabuLength << ")" << endl;
115
+
116
+                    cout << "4 - Czas zatrzymania" << "\t\t" << "(" << tabuStopTime << " s)" << endl;
117
+                    cout << "Powrot - 0" << endl;
118
+                    cout << "Wybierz: ";
119
+                    cin >> settingsSelection;
120
+                    cout << endl;
121
+
122
+                    switch(settingsSelection)
123
+                    {
124
+                        case 1:
125
+                        {
126
+                            tabuDiversification = !tabuDiversification;
127
+
128
+                            if(tabuDiversification == true)
129
+                                cout << "Dywersyfikacja zostala wlaczona" << endl;
130
+                            else
131
+                                cout << "Dywersyfikacja zostala wylaczona" << endl;
132
+                            cout << endl;
133
+                        }
134
+                        break;
135
+                        case 2:
136
+                        {
137
+                            cout << "Podaj nowa liczbe iteracji bez poprawy: ";
138
+                            cin >> tabuIterationsToRestart;
139
+                            cout << endl;
140
+                        }
141
+                        break;
142
+                        case 3:
143
+                        {
144
+                            cout << "Podaj nowa kadencje (0 -> auto): ";
145
+                            cin >> tabuLength;
146
+                            cout << endl;
147
+                        }
148
+                        break;
149
+                        case 4:
150
+                        {
151
+                            cout << "Podaj nowy czas pracy [s]: ";
152
+                            cin >> tabuStopTime;
153
+                            cout << endl;
154
+                        }
155
+                        break;
156
+                        case 0:
157
+                        {
158
+                        }
159
+                        break;
160
+                        default:
161
+                        {
162
+                            cout << "Nieprawidlowy wybor" << endl;
163
+                            cout << endl;
164
+                        }
165
+                    }
166
+                } while(settingsSelection != 0);
112 167
             }
113 168
             break;
114 169
             case 4:
115
-            {
116
-                cout << "Poprzedni czas pracy TS: " << tabuStopTime << endl;
117
-                cout << "Podaj nowy czas: ";
118
-                cin >> tabuStopTime;
119
-                cout << endl;
120
-            }
121
-            break;
122
-            case 5:
123 170
             {
124 171
                 if(graph != NULL)
125 172
                 {
126 173
                     if(tabuStopTime != 0)
127 174
                     {
128
-                        cout << "Kadencja: " << tabuLength << endl;
175
+                        unsigned effectiveTabuLength;
176
+                        if(tabuLength == 0)
177
+                        {
178
+                            effectiveTabuLength = (graph->getVertexNumber() / 10) * 10;
179
+                            if(effectiveTabuLength == 0)
180
+                              effectiveTabuLength = 10;
181
+                        }
182
+                        else
183
+                        {
184
+                            effectiveTabuLength = tabuLength;
185
+                        }
186
+
187
+                        if(tabuLength == 0)
188
+                            cout << "Kadencja: " << effectiveTabuLength << " (auto)" << endl;
189
+                        else
190
+                            cout << "Kadencja: " << effectiveTabuLength << endl;
191
+
129 192
                         cout << "Czas zatrzymania algorytmu [s]: " << tabuStopTime << endl;
130 193
 
131 194
                         if(tabuDiversification == true)
132
-                            cout << "Dywersyfikacja wlaczona, kryterium: " << tabuIterationsToRestart << " iteracji" << endl;
195
+                            cout << "Dywersyfikacja wlaczona, kryterium: " << tabuIterationsToRestart << " iteracji bez poprawy" << endl;
133 196
                         else
134 197
                             cout << "Dywersyfikacja wylaczona" << endl;
135 198
 
199
+                        cout << "Liczba watkow: " << tabuThreadsNumber << endl;
136 200
                         cout << endl;
137 201
 
138 202
                         clock.start();
139
-                        vector<unsigned> route = Graph::travellingSalesmanTabuSearch(*graph, tabuLength, tabuDiversification, tabuIterationsToRestart, tabuStopTime, tabuThreadsNumber);
203
+                        vector<unsigned> route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, tabuDiversification, tabuIterationsToRestart, tabuStopTime, tabuThreadsNumber);
140 204
                         clock.stop();
141 205
 
142 206
                         // Wyswietlenie trasy
@@ -166,36 +230,7 @@ int main()
166 230
                 cout << endl;
167 231
             }
168 232
             break;
169
-            case 6:
170
-            {
171
-                if(graph != NULL)
172
-                {
173
-                    clock.start();
174
-                    vector<unsigned> route = Graph::travellingSalesmanGreedy(*graph, 0);
175
-                    clock.stop();
176
-
177
-                    // Wyswietlenie trasy
178
-                    unsigned distFromStart = 0;
179
-                    unsigned length = 0;
180
-                    cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
181
-                    for(int i = 1; i < route.size(); i++)
182
-                    {
183
-                        length = graph->getWeight(route.at(i - 1), route.at(i));
184
-                        distFromStart += length;
185
-
186
-                        cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
187
-                    }
188
-
189
-                    cout << "Dlugosc trasy: " << distFromStart << endl;
190
-                    cout << endl;
191
-                    cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;
192
-                }
193
-                else
194
-                    cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
195
-                cout << endl;
196
-            }
197
-            break;
198
-            case 7:
233
+            case 5:
199 234
             {
200 235
                 if(graph != NULL)
201 236
                 {
@@ -224,7 +259,7 @@ int main()
224 259
                 cout << endl;
225 260
             }
226 261
             break;
227
-            case 8:
262
+            case 6:
228 263
             {
229 264
                 if(graph != NULL)
230 265
                 {
@@ -253,12 +288,24 @@ int main()
253 288
                 cout << endl;
254 289
             }
255 290
             break;
256
-            case 9:
291
+            case 7:
257 292
             {
258 293
                 // PEA 2
259 294
                 // Jan Potocki 2017
260 295
                 if(graph != NULL)
261 296
                 {
297
+                    unsigned effectiveTabuLength;
298
+                    if(tabuLength == 0)
299
+                    {
300
+                        effectiveTabuLength = (graph->getVertexNumber() / 10) * 10;
301
+                        if(effectiveTabuLength == 0)
302
+                          effectiveTabuLength = 10;
303
+                    }
304
+                    else
305
+                    {
306
+                        effectiveTabuLength = tabuLength;
307
+                    }
308
+
262 309
                     double measureResults[measureNumber], measureResultsDiv[measureNumber];
263 310
                     for(int i = 0; i < measureNumber; i++)
264 311
                     {
@@ -267,8 +314,14 @@ int main()
267 314
                     }
268 315
 
269 316
                     cout << "Pomiary dla problemu komiwojazera, tabu search" << tabuLength << endl;
270
-                    cout << "Kadencja: " << tabuLength << endl;
271
-                    cout << "Kryterium dywersyfikacji: " << tabuIterationsToRestart << " iteracji" << endl;
317
+
318
+                    if(tabuLength == 0)
319
+                        cout << "Kadencja: " << effectiveTabuLength << " (auto)" << endl;
320
+                    else
321
+                        cout << "Kadencja: " << effectiveTabuLength << endl;
322
+
323
+                    cout << "Kryterium dywersyfikacji: " << tabuIterationsToRestart << " iteracji bez poprawy" << endl;
324
+                    cout << "Liczba watkow: " << tabuThreadsNumber << endl;
272 325
 
273 326
                     // Petla pomiarowa
274 327
                     for(int krok = 0; krok < measureIterations; krok++)
@@ -281,7 +334,7 @@ int main()
281 334
                             // Bez dywersyfikacji
282 335
                             cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " bez dywersyfikacji)..." << endl;
283 336
 
284
-                            route = Graph::travellingSalesmanTabuSearch(*graph, tabuLength, false, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
337
+                            route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, false, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
285 338
 
286 339
                             routeLength = 0;
287 340
                             for(int j = 1; j < route.size(); j++)
@@ -291,7 +344,7 @@ int main()
291 344
                             // Z dywersyfikacja
292 345
                             cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " z dywersyfikacja)..." << endl;
293 346
 
294
-                            route = Graph::travellingSalesmanTabuSearch(*graph, tabuLength, true, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
347
+                            route = Graph::travellingSalesmanTabuSearch(*graph, effectiveTabuLength, true, tabuIterationsToRestart, measureTabuStop[i], tabuThreadsNumber);
295 348
 
296 349
                             routeLength = 0;
297 350
                             for(int j = 1; j < route.size(); j++)
@@ -329,7 +382,7 @@ int main()
329 382
                 }
330 383
             }
331 384
             break;
332
-            case 10:
385
+            case 8:
333 386
             {
334 387
                 // Jan Potocki 2017
335 388
                 string filename, fileInput;
@@ -397,7 +450,7 @@ int main()
397 450
                 }
398 451
             }
399 452
             break;
400
-            case 11:
453
+            case 9:
401 454
             {
402 455
                 // Jan Potocki 2017
403 456
                 string filename, fileInput;

Завантаження…
Відмінити
Зберегти