6 Commits

Author SHA1 Message Date
Jan Potocki 3691314819 Fixed newline (part II) 2020-01-20 20:14:34 +01:00
Jan Potocki 2d45eaf383 Removed commented endl 2020-01-20 19:22:37 +01:00
Jan Potocki 0b6e9418f9 Fixed newline in output (TS) 2020-01-20 19:22:07 +01:00
Jan Potocki 01f2e91aac Minor if-else simplification 2020-01-20 19:19:01 +01:00
Jan Potocki 4292c63a15 Fixed crash when entire neighborhood is on tabu list 2020-01-20 19:18:13 +01:00
Jan Potocki babb720caa Semi global shared optimum (without thread sync) 2020-01-20 18:18:02 +01:00
2 changed files with 7 additions and 27 deletions
+6 -26
View File
@@ -5,6 +5,7 @@
#include <queue>
#include <random>
#include <thread>
#include <iostream>
Graph::Graph()
@@ -425,7 +426,7 @@ std::vector<unsigned> Graph::travellingSalesmanTabuSearch(Graph &graph, unsigned
{
// ALGORYTM wielawotkowy oparty na metaheurystyce tabu search
// Pomocniczy kod uruchamiajacy watki wlasciwego algorytmu w najbardziej optymalny sposob
// Implementacja: Jan Potocki 2019
// Implementacja: Jan Potocki 2019-2020
std::vector<unsigned> startVertexVector;
std::vector<std::thread> threadsVector;
@@ -491,15 +492,13 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
// ALGORYTM oparty na metaheurystyce tabu search z dywersyfikacja i sasiedztwem typu swap
// Rdzen przeznaczony do uruchamiania jako jeden watek
// Projekt i implementacja: Jan Potocki 2017
// (refactoring 2019)
// (refactoring 2019-2020)
Stopwatch onboardClock;
std::vector<unsigned> optimalRoute; // Tu bedziemy zapisywac optymalne (w danej chwili) rozwiazanie
int optimalRouteLength = -1; // -1 - bedziemy odtad uznawac, ze to jest nieskonczonosc ;-)
std::vector<unsigned> currentRoute; // Rozpatrywane rozwiazanie
// Wyznaczenie poczatkowego rozwiazania algorytmem zachlannym
//currentRoute = Graph::travellingSalesmanGreedy(graph);
currentRoute = startRoute;
// Inicjalizacja glownej petli...
@@ -566,7 +565,6 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
// ...jezeli niespelnione - pomijamy ruch
continue;
if(nextRouteLength == -1 || nextRouteLength > neighbourRouteLength)
{
nextRouteLength = neighbourRouteLength;
@@ -601,7 +599,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
stopCounter = 0;
}
// Synchronizacja globalnie najlepszej trasy (1)
// Synchronizacja globalnie najlepszej trasy
globalOptimumMutex.lock();
if(globalOptimumLength == -1 || globalOptimumLength > nextRouteLength)
{
@@ -662,34 +660,16 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
// Intensyfikacja przeszukiwania przez skrócenie kadencji
// (jezeli w ostatnim przebiegu znaleziono nowe minimum)
currentRoute = optimalRoute;
currentTabuSteps = tabuSteps / 4;
currentTabuSteps = tabuSteps; /// 4;
intensification = false;
// PEA 2 Plus
// Jan Potocki 2019
}
else
{
// Synchronizacja globalnie najlepszej trasy (2)
globalOptimumMutex.lock();
optimalRouteLength = globalOptimumLength;
optimalRoute = globalOptimum;
globalOptimumMutex.unlock();
// W innym przypadku wlasciwa dywersyfikacja przez wygenerowanie nowego
// rozwiazania startowego algorytmem hybrydowym losowo-zachlannym
// (ale nie znacznie gorszego niz juz znalezione)
int criticalLength = (15 * optimalRouteLength) / 10;
int currentRouteLength;
do
{
currentRoute = Graph::travellingSalesmanHybrid(graph);
currentRouteLength = 0;
for(int i = 1; i < currentRoute.size(); i++)
currentRouteLength += graph.getWeight(currentRoute.at(i - 1), currentRoute.at(i));
} while(currentRouteLength > criticalLength);
currentRoute = Graph::travellingSalesmanHybrid(graph);
currentTabuSteps = tabuSteps;
intensification = false;
}
+1 -1
View File
@@ -439,6 +439,7 @@ int main(int argc, char *argv[])
// Wyswietlenie trasy
unsigned distFromStart = 0;
unsigned length = 0;
cout << endl;
cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
for(int i = 1; i < route.size(); i++)
{
@@ -448,7 +449,6 @@ int main(int argc, char *argv[])
cout << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
}
cout << endl;
cout << "Dlugosc trasy: " << distFromStart << endl;
cout << endl;
cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;