|
|
|
@@ -426,15 +426,13 @@ 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;
|
|
|
|
|
std::vector<std::vector<unsigned>> resultsVector(threadsNumber);
|
|
|
|
|
|
|
|
|
|
std::vector<int> resultsLength(threadsNumber);
|
|
|
|
|
std::vector<unsigned> optimalResult;
|
|
|
|
|
int optimalResultIndex;
|
|
|
|
|
int optimalResultLength;
|
|
|
|
|
std::mutex globalOptimumMutex;
|
|
|
|
|
std::vector<unsigned> globalOptimum;
|
|
|
|
|
unsigned globalOptimumLength = -1;
|
|
|
|
|
|
|
|
|
|
std::random_device randomSrc;
|
|
|
|
|
std::default_random_engine randomGen(randomSrc());
|
|
|
|
@@ -479,45 +477,28 @@ std::vector<unsigned> Graph::travellingSalesmanTabuSearch(Graph &graph, unsigned
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Uruchomienie watku
|
|
|
|
|
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))));
|
|
|
|
|
threadsVector.push_back(std::thread(Graph::travellingSalesmanTabuSearchEngine, std::ref(graph), tabuSteps, diversification, iterationsToRestart, minStopTime, startRoute, std::ref(globalOptimum), std::ref(globalOptimumLength), std::ref(globalOptimumMutex)));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Petla potwierdzajaca zakonczenie watkow
|
|
|
|
|
for(int i = 0; i < threadsNumber; i++)
|
|
|
|
|
threadsVector.at(i).join();
|
|
|
|
|
|
|
|
|
|
// Przegladanie wszystkich rozwiazan i wybor optymalnego
|
|
|
|
|
optimalResultIndex = 0;
|
|
|
|
|
optimalResultLength = resultsLength.at(0);
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < threadsNumber; i++)
|
|
|
|
|
{
|
|
|
|
|
if(resultsLength.at(i) < optimalResultLength)
|
|
|
|
|
{
|
|
|
|
|
optimalResultIndex = i;
|
|
|
|
|
optimalResultLength = resultsLength.at(i);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
optimalResult = resultsVector.at(optimalResultIndex);
|
|
|
|
|
|
|
|
|
|
return optimalResult;
|
|
|
|
|
return globalOptimum;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, std::vector<unsigned> startRoute, std::vector<unsigned> &result, int &resultLength)
|
|
|
|
|
void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, std::vector<unsigned> startRoute, std::vector<unsigned> &globalOptimum, unsigned &globalOptimumLength, std::mutex &globalOptimumMutex)
|
|
|
|
|
{
|
|
|
|
|
// 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...
|
|
|
|
@@ -535,7 +516,9 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|
|
|
|
|
|
|
|
|
while(cheeseSupplied == true)
|
|
|
|
|
{
|
|
|
|
|
std::vector<unsigned> nextRoute;
|
|
|
|
|
std::vector<unsigned> nextRoute = currentRoute;
|
|
|
|
|
// ...na wszelki wypadek, gdyby cale sasiedztwo bylo na liscie tabu
|
|
|
|
|
// (zeby algorytm sie nie wywalil)
|
|
|
|
|
int nextRouteLength = -1;
|
|
|
|
|
|
|
|
|
|
std::vector<unsigned> nextTabu(3, 0);
|
|
|
|
@@ -582,14 +565,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|
|
|
|
// ...jezeli niespelnione - pomijamy ruch
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if(nextRouteLength == -1)
|
|
|
|
|
{
|
|
|
|
|
nextRouteLength = neighbourRouteLength;
|
|
|
|
|
nextRoute = neighbourRoute;
|
|
|
|
|
nextTabu.at(1) = i;
|
|
|
|
|
nextTabu.at(2) = j;
|
|
|
|
|
}
|
|
|
|
|
else if(nextRouteLength > neighbourRouteLength)
|
|
|
|
|
if(nextRouteLength == -1 || nextRouteLength > neighbourRouteLength)
|
|
|
|
|
{
|
|
|
|
|
nextRouteLength = neighbourRouteLength;
|
|
|
|
|
nextRoute = neighbourRoute;
|
|
|
|
@@ -623,6 +599,19 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|
|
|
|
stopCounter = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Synchronizacja globalnie najlepszej trasy
|
|
|
|
|
globalOptimumMutex.lock();
|
|
|
|
|
if(globalOptimumLength == -1 || globalOptimumLength > nextRouteLength)
|
|
|
|
|
{
|
|
|
|
|
globalOptimumLength = nextRouteLength;
|
|
|
|
|
globalOptimum = nextRoute;
|
|
|
|
|
|
|
|
|
|
onboardClock.stop();
|
|
|
|
|
std::cout << "Nowa najlepsza trasa: " << globalOptimumLength;
|
|
|
|
|
std::cout << " (w czasie " << onboardClock.read() << " s)" << std::endl;
|
|
|
|
|
}
|
|
|
|
|
globalOptimumMutex.unlock();
|
|
|
|
|
|
|
|
|
|
// Weryfikacja listy tabu...
|
|
|
|
|
int tabuPos = 0;
|
|
|
|
|
while(tabuPos < tabuArray.size())
|
|
|
|
@@ -671,7 +660,7 @@ 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
|
|
|
|
@@ -689,7 +678,4 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|
|
|
|
// Reset licznika iteracji przed restartem
|
|
|
|
|
stopCounter = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
result = optimalRoute;
|
|
|
|
|
resultLength = optimalRouteLength;
|
|
|
|
|
}
|
|
|
|
|