1 Commits

Author SHA1 Message Date
Jan Potocki babb720caa Semi global shared optimum (without thread sync) 2020-01-20 18:18:02 +01:00
2 changed files with 18 additions and 32 deletions
+15 -28
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...
@@ -517,9 +516,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
while(cheeseSupplied == true) while(cheeseSupplied == true)
{ {
std::vector<unsigned> nextRoute = currentRoute; std::vector<unsigned> nextRoute;
// ...na wszelki wypadek, gdyby cale sasiedztwo bylo na liscie tabu
// (zeby algorytm sie nie wywalil)
int nextRouteLength = -1; int nextRouteLength = -1;
std::vector<unsigned> nextTabu(3, 0); std::vector<unsigned> nextTabu(3, 0);
@@ -566,8 +563,14 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
// ...jezeli niespelnione - pomijamy ruch // ...jezeli niespelnione - pomijamy ruch
continue; continue;
if(nextRouteLength == -1)
if(nextRouteLength == -1 || nextRouteLength > neighbourRouteLength) {
nextRouteLength = neighbourRouteLength;
nextRoute = neighbourRoute;
nextTabu.at(1) = i;
nextTabu.at(2) = j;
}
else if(nextRouteLength > neighbourRouteLength)
{ {
nextRouteLength = neighbourRouteLength; nextRouteLength = neighbourRouteLength;
nextRoute = neighbourRoute; nextRoute = neighbourRoute;
@@ -601,7 +604,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,27 +672,9 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
} }
else else
{ {
// Synchronizacja globalnie najlepszej trasy (2)
globalOptimumMutex.lock();
optimalRouteLength = globalOptimumLength;
optimalRoute = globalOptimum;
globalOptimumMutex.unlock();
// W innym przypadku wlasciwa dywersyfikacja przez wygenerowanie nowego // W innym przypadku wlasciwa dywersyfikacja przez wygenerowanie nowego
// rozwiazania startowego algorytmem hybrydowym losowo-zachlannym // 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); 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);
currentTabuSteps = tabuSteps; currentTabuSteps = tabuSteps;
intensification = false; intensification = false;
} }
@@ -698,4 +683,6 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
// Reset licznika iteracji przed restartem // Reset licznika iteracji przed restartem
stopCounter = 0; stopCounter = 0;
} }
std::cout << std::endl;
} }
+2 -3
View File
@@ -31,7 +31,7 @@ unsigned tabuLength = 0;
// Domyslny stan dywersyfikacji // Domyslny stan dywersyfikacji
bool tabuDiversification = true; bool tabuDiversification = true;
// Domyslne kryterium dywersyfikacji, liczba iteracji bez poprawy // Domyslne kryterium dywersyfikacji, liczba iteracji bez poprawy
int tabuIterationsToRestart = 5000; int tabuIterationsToRestart = 10000;
// Domyslny czas zatrzymania algorytmu tabu search [s] // Domyslny czas zatrzymania algorytmu tabu search [s]
unsigned tabuStopTime = 60; unsigned tabuStopTime = 60;
@@ -206,7 +206,7 @@ unsigned autoTabuLength(Graph &graph)
{ {
unsigned computedTabuLength; unsigned computedTabuLength;
computedTabuLength = 10 + (graph.getVertexNumber() / 10) * 10; computedTabuLength = (2 * graph.getVertexNumber() / 10) * 10;
if(computedTabuLength == 0) if(computedTabuLength == 0)
computedTabuLength = 10; computedTabuLength = 10;
@@ -439,7 +439,6 @@ 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++)
{ {