Browse Source

Automatic tabu length, refactored menu

Jan Potocki 5 years ago
parent
commit
ec78c19192
1 changed files with 131 additions and 78 deletions
  1. 131
    78
      pea2plus.cpp

+ 131
- 78
pea2plus.cpp View File

19
 // Maksymalna odleglosc miast przy automatycznym generowaniu
19
 // Maksymalna odleglosc miast przy automatycznym generowaniu
20
 const int measureSalesmanDistance = 400;
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
 // Wykorzystanie reprezentacji grafu w postaci list sasiedztwa...
22
 // Wykorzystanie reprezentacji grafu w postaci list sasiedztwa...
28
 // ...zamiast (domyslnie) macierzy sasiedztwa
23
 // ...zamiast (domyslnie) macierzy sasiedztwa
29
 // (wolniejsze obliczenia, mniejsze uzycie pamieci)
24
 // (wolniejsze obliczenia, mniejsze uzycie pamieci)
30
 const bool useListGraph = false;
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
 // Domyslny stan dywersyfikacji
29
 // Domyslny stan dywersyfikacji
39
 bool tabuDiversification = true;
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
 int main()
39
 int main()
42
 {
40
 {
43
     Stopwatch clock;                // czasomierz
41
     Stopwatch clock;                // czasomierz
44
     Graph *graph = NULL;            // <- tu bedziemy zapisywac adresy przez caly program
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
     cout << "Jan Potocki 2017-2019" << endl;
45
     cout << "Jan Potocki 2017-2019" << endl;
48
     cout << "(beerware)" << endl;
46
     cout << "(beerware)" << endl;
49
     if(useListGraph)
47
     if(useListGraph)
55
     int salesmanSelection;
53
     int salesmanSelection;
56
     do
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
         cout << "Aby zakonczyc - 0" << endl;
65
         cout << "Aby zakonczyc - 0" << endl;
70
         cout << "Wybierz: ";
66
         cout << "Wybierz: ";
71
         cin >> salesmanSelection;
67
         cin >> salesmanSelection;
102
             break;
98
             break;
103
             case 3:
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
             break;
168
             break;
114
             case 4:
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
                 if(graph != NULL)
171
                 if(graph != NULL)
125
                 {
172
                 {
126
                     if(tabuStopTime != 0)
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
                         cout << "Czas zatrzymania algorytmu [s]: " << tabuStopTime << endl;
192
                         cout << "Czas zatrzymania algorytmu [s]: " << tabuStopTime << endl;
130
 
193
 
131
                         if(tabuDiversification == true)
194
                         if(tabuDiversification == true)
132
-                            cout << "Dywersyfikacja wlaczona, kryterium: " << tabuIterationsToRestart << " iteracji" << endl;
195
+                            cout << "Dywersyfikacja wlaczona, kryterium: " << tabuIterationsToRestart << " iteracji bez poprawy" << endl;
133
                         else
196
                         else
134
                             cout << "Dywersyfikacja wylaczona" << endl;
197
                             cout << "Dywersyfikacja wylaczona" << endl;
135
 
198
 
199
+                        cout << "Liczba watkow: " << tabuThreadsNumber << endl;
136
                         cout << endl;
200
                         cout << endl;
137
 
201
 
138
                         clock.start();
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
                         clock.stop();
204
                         clock.stop();
141
 
205
 
142
                         // Wyswietlenie trasy
206
                         // Wyswietlenie trasy
166
                 cout << endl;
230
                 cout << endl;
167
             }
231
             }
168
             break;
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
                 if(graph != NULL)
235
                 if(graph != NULL)
201
                 {
236
                 {
224
                 cout << endl;
259
                 cout << endl;
225
             }
260
             }
226
             break;
261
             break;
227
-            case 8:
262
+            case 6:
228
             {
263
             {
229
                 if(graph != NULL)
264
                 if(graph != NULL)
230
                 {
265
                 {
253
                 cout << endl;
288
                 cout << endl;
254
             }
289
             }
255
             break;
290
             break;
256
-            case 9:
291
+            case 7:
257
             {
292
             {
258
                 // PEA 2
293
                 // PEA 2
259
                 // Jan Potocki 2017
294
                 // Jan Potocki 2017
260
                 if(graph != NULL)
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
                     double measureResults[measureNumber], measureResultsDiv[measureNumber];
309
                     double measureResults[measureNumber], measureResultsDiv[measureNumber];
263
                     for(int i = 0; i < measureNumber; i++)
310
                     for(int i = 0; i < measureNumber; i++)
264
                     {
311
                     {
267
                     }
314
                     }
268
 
315
 
269
                     cout << "Pomiary dla problemu komiwojazera, tabu search" << tabuLength << endl;
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
                     // Petla pomiarowa
326
                     // Petla pomiarowa
274
                     for(int krok = 0; krok < measureIterations; krok++)
327
                     for(int krok = 0; krok < measureIterations; krok++)
281
                             // Bez dywersyfikacji
334
                             // Bez dywersyfikacji
282
                             cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " bez dywersyfikacji)..." << endl;
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
                             routeLength = 0;
339
                             routeLength = 0;
287
                             for(int j = 1; j < route.size(); j++)
340
                             for(int j = 1; j < route.size(); j++)
291
                             // Z dywersyfikacja
344
                             // Z dywersyfikacja
292
                             cout << "Pomiar " << measureTabuStop[i] << " [s] (" << krok + 1 << " z " << measureIterations << " z dywersyfikacja)..." << endl;
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
                             routeLength = 0;
349
                             routeLength = 0;
297
                             for(int j = 1; j < route.size(); j++)
350
                             for(int j = 1; j < route.size(); j++)
329
                 }
382
                 }
330
             }
383
             }
331
             break;
384
             break;
332
-            case 10:
385
+            case 8:
333
             {
386
             {
334
                 // Jan Potocki 2017
387
                 // Jan Potocki 2017
335
                 string filename, fileInput;
388
                 string filename, fileInput;
397
                 }
450
                 }
398
             }
451
             }
399
             break;
452
             break;
400
-            case 11:
453
+            case 9:
401
             {
454
             {
402
                 // Jan Potocki 2017
455
                 // Jan Potocki 2017
403
                 string filename, fileInput;
456
                 string filename, fileInput;

Loading…
Cancel
Save