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 22 deletions
+6 -21
View File
@@ -5,6 +5,7 @@
#include <queue> #include <queue>
#include <random> #include <random>
#include <thread> #include <thread>
#include <iostream> #include <iostream>
Graph::Graph() Graph::Graph()
@@ -425,7 +426,7 @@ std::vector<unsigned> Graph::travellingSalesmanTabuSearch(Graph &graph, unsigned
{ {
// ALGORYTM wielawotkowy oparty na metaheurystyce tabu search // ALGORYTM wielawotkowy oparty na metaheurystyce tabu search
// Pomocniczy kod uruchamiajacy watki wlasciwego algorytmu w najbardziej optymalny sposob // 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<unsigned> startVertexVector;
std::vector<std::thread> threadsVector; 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 // ALGORYTM oparty na metaheurystyce tabu search z dywersyfikacja i sasiedztwem typu swap
// Rdzen przeznaczony do uruchamiania jako jeden watek // Rdzen przeznaczony do uruchamiania jako jeden watek
// Projekt i implementacja: Jan Potocki 2017 // Projekt i implementacja: Jan Potocki 2017
// (refactoring 2019) // (refactoring 2019-2020)
Stopwatch onboardClock; Stopwatch onboardClock;
std::vector<unsigned> optimalRoute; // Tu bedziemy zapisywac optymalne (w danej chwili) rozwiazanie std::vector<unsigned> optimalRoute; // Tu bedziemy zapisywac optymalne (w danej chwili) rozwiazanie
int optimalRouteLength = -1; // -1 - bedziemy odtad uznawac, ze to jest nieskonczonosc ;-) int optimalRouteLength = -1; // -1 - bedziemy odtad uznawac, ze to jest nieskonczonosc ;-)
std::vector<unsigned> currentRoute; // Rozpatrywane rozwiazanie std::vector<unsigned> currentRoute; // Rozpatrywane rozwiazanie
// Wyznaczenie poczatkowego rozwiazania algorytmem zachlannym
//currentRoute = Graph::travellingSalesmanGreedy(graph);
currentRoute = startRoute; currentRoute = startRoute;
// Inicjalizacja glownej petli... // Inicjalizacja glownej petli...
@@ -566,15 +565,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
// ...jezeli niespelnione - pomijamy ruch // ...jezeli niespelnione - pomijamy ruch
continue; continue;
if(nextRouteLength == -1 || nextRouteLength > neighbourRouteLength)
if(nextRouteLength == -1)
{
nextRouteLength = neighbourRouteLength;
nextRoute = neighbourRoute;
nextTabu.at(1) = i;
nextTabu.at(2) = j;
}
else if(nextRouteLength > neighbourRouteLength)
{ {
nextRouteLength = neighbourRouteLength; nextRouteLength = neighbourRouteLength;
nextRoute = neighbourRoute; nextRoute = neighbourRoute;
@@ -608,7 +599,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
stopCounter = 0; stopCounter = 0;
} }
// Synchronizacja globalnie najlepszej trasy (1) // Synchronizacja globalnie najlepszej trasy
globalOptimumMutex.lock(); globalOptimumMutex.lock();
if(globalOptimumLength == -1 || globalOptimumLength > nextRouteLength) if(globalOptimumLength == -1 || globalOptimumLength > nextRouteLength)
{ {
@@ -669,7 +660,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
// Intensyfikacja przeszukiwania przez skrócenie kadencji // Intensyfikacja przeszukiwania przez skrócenie kadencji
// (jezeli w ostatnim przebiegu znaleziono nowe minimum) // (jezeli w ostatnim przebiegu znaleziono nowe minimum)
currentRoute = optimalRoute; currentRoute = optimalRoute;
currentTabuSteps = tabuSteps / 4; currentTabuSteps = tabuSteps; /// 4;
intensification = false; intensification = false;
// PEA 2 Plus // PEA 2 Plus
// Jan Potocki 2019 // Jan Potocki 2019
@@ -681,12 +672,6 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
currentRoute = Graph::travellingSalesmanHybrid(graph); currentRoute = Graph::travellingSalesmanHybrid(graph);
currentTabuSteps = tabuSteps; currentTabuSteps = tabuSteps;
intensification = false; intensification = false;
// Synchronizacja globalnie najlepszej trasy (2)
globalOptimumMutex.lock();
optimalRouteLength = globalOptimumLength;
optimalRoute = globalOptimum;
globalOptimumMutex.unlock();
} }
} }
+1 -1
View File
@@ -439,6 +439,7 @@ int main(int argc, char *argv[])
// Wyswietlenie trasy // Wyswietlenie trasy
unsigned distFromStart = 0; unsigned distFromStart = 0;
unsigned length = 0; unsigned length = 0;
cout << endl;
cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl; cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
for(int i = 1; i < route.size(); i++) 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 << route.at(i) << '\t' << length << '\t' << distFromStart << endl;
} }
cout << endl;
cout << "Dlugosc trasy: " << distFromStart << endl; cout << "Dlugosc trasy: " << distFromStart << endl;
cout << endl; cout << endl;
cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl; cout << "Czas wykonania algorytmu [s]: " << clock.read() << endl;