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
|
// 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;
|
||||||
std::vector<std::vector<unsigned>> resultsVector(threadsNumber);
|
|
||||||
|
|
||||||
std::vector<int> resultsLength(threadsNumber);
|
std::mutex globalOptimumMutex;
|
||||||
std::vector<unsigned> optimalResult;
|
std::vector<unsigned> globalOptimum;
|
||||||
int optimalResultIndex;
|
unsigned globalOptimumLength = -1;
|
||||||
int optimalResultLength;
|
|
||||||
|
|
||||||
std::random_device randomSrc;
|
std::random_device randomSrc;
|
||||||
std::default_random_engine randomGen(randomSrc());
|
std::default_random_engine randomGen(randomSrc());
|
||||||
@@ -479,45 +477,28 @@ std::vector<unsigned> Graph::travellingSalesmanTabuSearch(Graph &graph, unsigned
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Uruchomienie watku
|
// 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
|
// Petla potwierdzajaca zakonczenie watkow
|
||||||
for(int i = 0; i < threadsNumber; i++)
|
for(int i = 0; i < threadsNumber; i++)
|
||||||
threadsVector.at(i).join();
|
threadsVector.at(i).join();
|
||||||
|
|
||||||
// Przegladanie wszystkich rozwiazan i wybor optymalnego
|
return globalOptimum;
|
||||||
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);
|
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)
|
||||||
|
|
||||||
return optimalResult;
|
|
||||||
}
|
|
||||||
|
|
||||||
void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, std::vector<unsigned> startRoute, std::vector<unsigned> &result, int &resultLength)
|
|
||||||
{
|
{
|
||||||
// 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...
|
||||||
@@ -623,6 +604,19 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|||||||
stopCounter = 0;
|
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...
|
// Weryfikacja listy tabu...
|
||||||
int tabuPos = 0;
|
int tabuPos = 0;
|
||||||
while(tabuPos < tabuArray.size())
|
while(tabuPos < tabuArray.size())
|
||||||
@@ -690,6 +684,5 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|||||||
stopCounter = 0;
|
stopCounter = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = optimalRoute;
|
std::cout << std::endl;
|
||||||
resultLength = optimalRouteLength;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#ifndef GRAPH_H
|
#ifndef GRAPH_H
|
||||||
#define GRAPH_H
|
#define GRAPH_H
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <mutex>
|
||||||
|
|
||||||
|
|
||||||
class Graph
|
class Graph
|
||||||
@@ -25,7 +26,7 @@ class Graph
|
|||||||
unsigned vertexNumber;
|
unsigned vertexNumber;
|
||||||
|
|
||||||
private:
|
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
|
class RouteComparison
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user