Semi global shared optimum (without thread sync)
This commit is contained in:
@@ -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...
|
||||
@@ -623,6 +604,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())
|
||||
@@ -690,6 +684,5 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
||||
stopCounter = 0;
|
||||
}
|
||||
|
||||
result = optimalRoute;
|
||||
resultLength = optimalRouteLength;
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef GRAPH_H
|
||||
#define GRAPH_H
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
|
||||
class Graph
|
||||
@@ -25,7 +26,7 @@ class Graph
|
||||
unsigned vertexNumber;
|
||||
|
||||
private:
|
||||
static void travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, std::vector<unsigned> startRoute, std::vector<unsigned> &result, int &resultLength);
|
||||
static void travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, std::vector<unsigned> startRoute, std::vector<unsigned> &globalOptimum, unsigned &globalOptimumLength, std::mutex &globalOptimumMutex);
|
||||
|
||||
class RouteComparison
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user