Browse Source

Code cleanup

Jan Potocki 5 years ago
parent
commit
9f02651088
2 changed files with 34 additions and 12 deletions
  1. 23
    9
      Graph.cpp
  2. 11
    3
      pea2plus.cpp

+ 23
- 9
Graph.cpp View File

250
     // Implementacja: Jan Potocki 2017
250
     // Implementacja: Jan Potocki 2017
251
     std::vector<unsigned> route;
251
     std::vector<unsigned> route;
252
 
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));
253
+    // Przypisanie wierzcholka startowego
259
     route.push_back(startVertex);
254
     route.push_back(startVertex);
260
 
255
 
261
     for(int i = 0; i < graph.vertexNumber - 1; i++)
256
     for(int i = 0; i < graph.vertexNumber - 1; i++)
370
             // Znalezienie najkrotszej mozliwej jeszcze do uzycia krawedzi
365
             // Znalezienie najkrotszej mozliwej jeszcze do uzycia krawedzi
371
             unsigned consideredLength = graph.getWeight(route.back(), j);
366
             unsigned consideredLength = graph.getWeight(route.back(), j);
372
 
367
 
368
+            // PEA 2 Plus
369
+            // Jan Potocki 2019
373
             if(minEdge == -1)
370
             if(minEdge == -1)
374
             {
371
             {
375
                 minEdge = consideredLength;
372
                 minEdge = consideredLength;
433
     std::vector<unsigned> startVertexVector;
430
     std::vector<unsigned> startVertexVector;
434
     std::vector<std::thread> threadsVector;
431
     std::vector<std::thread> threadsVector;
435
     std::vector<std::vector<unsigned>> resultsVector(threadsNumber);
432
     std::vector<std::vector<unsigned>> resultsVector(threadsNumber);
433
+
436
     std::vector<int> resultsLength(threadsNumber);
434
     std::vector<int> resultsLength(threadsNumber);
437
     std::vector<unsigned> optimalResult;
435
     std::vector<unsigned> optimalResult;
438
     int optimalResultIndex;
436
     int optimalResultIndex;
442
     std::default_random_engine randomGen(randomSrc());
440
     std::default_random_engine randomGen(randomSrc());
443
     std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
441
     std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
444
 
442
 
443
+    // Petla uruchamiajaca watki
445
     for(int i = 0; i < threadsNumber; i++)
444
     for(int i = 0; i < threadsNumber; i++)
446
     {
445
     {
446
+        // Generowanie startowego rozwiazania...
447
         std::vector<unsigned> startRoute;
447
         std::vector<unsigned> startRoute;
448
         unsigned startVertex;
448
         unsigned startVertex;
449
         bool startVertexUsed;
449
         bool startVertexUsed;
450
 
450
 
451
         if(i < graph.vertexNumber)
451
         if(i < graph.vertexNumber)
452
         {
452
         {
453
+            // ...dopoki ma to sens - algorytmem zachlannym z innym wierzcholkiem startowym
454
+            // (dla kazdego watku)
453
             do
455
             do
454
             {
456
             {
455
                 startVertex = vertexDist(randomGen);
457
                 startVertex = vertexDist(randomGen);
465
                 }
467
                 }
466
             } while(startVertexUsed == true);
468
             } while(startVertexUsed == true);
467
 
469
 
470
+            // PEA 2 Plus
471
+            // Jan Potocki 2019
468
             startVertexVector.push_back(startVertex);
472
             startVertexVector.push_back(startVertex);
469
             startRoute = Graph::travellingSalesmanGreedy(graph, startVertex);
473
             startRoute = Graph::travellingSalesmanGreedy(graph, startVertex);
470
         }
474
         }
471
         else
475
         else
472
         {
476
         {
477
+            // ...jezeli wszystkie wierzcholki sa juz wykorzystane - w pelni losowo
473
             startRoute = Graph::travellingSalesmanRandom(graph);
478
             startRoute = Graph::travellingSalesmanRandom(graph);
474
         }
479
         }
475
 
480
 
481
+        // Uruchomienie watku
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))));
482
         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
     }
483
     }
478
 
484
 
485
+    // Petla potwierdzajaca zakonczenie watkow
479
     for(int i = 0; i < threadsNumber; i++)
486
     for(int i = 0; i < threadsNumber; i++)
480
         threadsVector.at(i).join();
487
         threadsVector.at(i).join();
481
 
488
 
489
+    // Przegladanie wszystkich rozwiazan i wybor optymalnego
482
     optimalResultIndex = 0;
490
     optimalResultIndex = 0;
483
     optimalResultLength = resultsLength.at(0);
491
     optimalResultLength = resultsLength.at(0);
484
 
492
 
592
             }
600
             }
593
 
601
 
594
             currentRoute = nextRoute;
602
             currentRoute = nextRoute;
603
+            // PEA 2 Plus
604
+            // Jan Potocki 2019
595
 
605
 
596
             if(optimalRouteLength == -1)
606
             if(optimalRouteLength == -1)
597
             {
607
             {
606
                 optimalRouteLength = nextRouteLength;
616
                 optimalRouteLength = nextRouteLength;
607
                 optimalRoute = nextRoute;
617
                 optimalRoute = nextRoute;
608
 
618
 
609
-                // Zaplanowanie intensyfikacji
619
+                // Zaplanowanie intensyfikacji przy znalezieniu nowego optimum
610
                 intensification = true;
620
                 intensification = true;
611
 
621
 
612
                 // Reset licznika
622
                 // Reset licznika
658
         {
668
         {
659
             if(intensification == true)
669
             if(intensification == true)
660
             {
670
             {
661
-                // Intensyfikacja przeszukiwania przy ostatnim minimum
671
+                // Intensyfikacja przeszukiwania przez skrócenie kadencji
672
+                // (jezeli w ostatnim przebiegu znaleziono nowe minimum)
662
                 currentRoute = optimalRoute;
673
                 currentRoute = optimalRoute;
663
                 currentTabuSteps = tabuSteps / 4;
674
                 currentTabuSteps = tabuSteps / 4;
664
                 intensification = false;
675
                 intensification = false;
676
+                // PEA 2 Plus
677
+                // Jan Potocki 2019
665
             }
678
             }
666
             else
679
             else
667
             {
680
             {
668
-                // Algorytm hybrydowy losowo-zachlanny
681
+                // W innym przypadku wlasciwa dywersyfikacja przez wygenerowanie nowego
682
+                // rozwiazania startowego algorytmem hybrydowym losowo-zachlannym
669
                 currentRoute = Graph::travellingSalesmanHybrid(graph);
683
                 currentRoute = Graph::travellingSalesmanHybrid(graph);
670
                 currentTabuSteps = tabuSteps;
684
                 currentTabuSteps = tabuSteps;
671
                 intensification = false;
685
                 intensification = false;

+ 11
- 3
pea2plus.cpp View File

40
 
40
 
41
 void parseTSPLIB_FULL_MATRIX(const char *filename, Graph **graph)
41
 void parseTSPLIB_FULL_MATRIX(const char *filename, Graph **graph)
42
 {
42
 {
43
-    // Jan Potocki 2017
43
+    // Parser plikow FULL_MATRIX z TSPLIB
44
+    // Implementacja: Jan Potocki 2017
44
     string fileInput;
45
     string fileInput;
45
     ifstream salesmanDataFile;
46
     ifstream salesmanDataFile;
46
 
47
 
105
 
106
 
106
 void parseTSPLIB_EUC_2D(const char *filename, Graph **graph)
107
 void parseTSPLIB_EUC_2D(const char *filename, Graph **graph)
107
 {
108
 {
108
-    // Jan Potocki 2017
109
+  // Parser plikow EUC_2D z TSPLIB
110
+  // Implementacja: Jan Potocki 2017
109
     string fileInput;
111
     string fileInput;
110
     vector<float> xCoord, yCoord;
112
     vector<float> xCoord, yCoord;
111
     ifstream salesmanDataFile;
113
     ifstream salesmanDataFile;
241
                 cout << endl;
243
                 cout << endl;
242
                 cout << "-h" << "\t\t\t" << "wyswietlenie pomocy (opisu parametrow)" << endl;
244
                 cout << "-h" << "\t\t\t" << "wyswietlenie pomocy (opisu parametrow)" << endl;
243
                 return 0;
245
                 return 0;
246
+                // PEA 2 Plus
247
+                // Jan Potocki 2019
244
             }
248
             }
245
             else
249
             else
246
             {
250
             {
299
                 if(graph != NULL)
303
                 if(graph != NULL)
300
                     graph->displayGraph();
304
                     graph->displayGraph();
301
                 else
305
                 else
302
-                    cout << "Brak wygenerowanych danych" << endl;
306
+                    cout << "+++ MELON MELON MELON +++ Brak zaladowanych danych +++" << endl;
303
                 cout << endl;
307
                 cout << endl;
304
             }
308
             }
305
             break;
309
             break;
385
                             effectiveTabuLength = (graph->getVertexNumber() / 10) * 10;
389
                             effectiveTabuLength = (graph->getVertexNumber() / 10) * 10;
386
                             if(effectiveTabuLength == 0)
390
                             if(effectiveTabuLength == 0)
387
                               effectiveTabuLength = 10;
391
                               effectiveTabuLength = 10;
392
+                              // PEA 2 Plus
393
+                              // Jan Potocki 2019
388
                         }
394
                         }
389
                         else
395
                         else
390
                         {
396
                         {
591
             break;
597
             break;
592
             case 8:
598
             case 8:
593
             {
599
             {
600
+                // PEA 2 Plus
594
                 // Jan Potocki 2019
601
                 // Jan Potocki 2019
595
                 string filename;
602
                 string filename;
596
 
603
 
602
             break;
609
             break;
603
             case 9:
610
             case 9:
604
             {
611
             {
612
+                // PEA 2 Plus
605
                 // Jan Potocki 2019
613
                 // Jan Potocki 2019
606
                 string filename;
614
                 string filename;
607
 
615
 

Loading…
Cancel
Save