Experimental: initial shared global optimum in TS
This commit is contained in:
@@ -5,7 +5,6 @@
|
|||||||
#include <queue>
|
#include <queue>
|
||||||
#include <random>
|
#include <random>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
Graph::Graph()
|
Graph::Graph()
|
||||||
@@ -440,6 +439,9 @@ std::vector<unsigned> Graph::travellingSalesmanTabuSearch(Graph &graph, unsigned
|
|||||||
std::default_random_engine randomGen(randomSrc());
|
std::default_random_engine randomGen(randomSrc());
|
||||||
std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
|
std::uniform_int_distribution<> vertexDist(0, graph.vertexNumber - 1);
|
||||||
|
|
||||||
|
std::mutex globalOptimumMutex;
|
||||||
|
unsigned globalOptimum = -1;
|
||||||
|
|
||||||
// Petla uruchamiajaca watki
|
// Petla uruchamiajaca watki
|
||||||
for(int i = 0; i < threadsNumber; i++)
|
for(int i = 0; i < threadsNumber; i++)
|
||||||
{
|
{
|
||||||
@@ -479,7 +481,7 @@ 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(resultsVector.at(i)), std::ref(resultsLength.at(i)), std::ref(globalOptimum), std::ref(globalOptimumMutex)));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Petla potwierdzajaca zakonczenie watkow
|
// Petla potwierdzajaca zakonczenie watkow
|
||||||
@@ -504,7 +506,7 @@ std::vector<unsigned> Graph::travellingSalesmanTabuSearch(Graph &graph, unsigned
|
|||||||
return optimalResult;
|
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)
|
void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps, bool diversification, int iterationsToRestart, unsigned minStopTime, std::vector<unsigned> startRoute, std::vector<unsigned> &result, int &resultLength, unsigned &globalOptimum, std::mutex &globalOptimumMutex)
|
||||||
{
|
{
|
||||||
// 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
|
||||||
@@ -603,17 +605,20 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|||||||
// PEA 2 Plus
|
// PEA 2 Plus
|
||||||
// Jan Potocki 2019
|
// Jan Potocki 2019
|
||||||
|
|
||||||
if(optimalRouteLength == -1)
|
globalOptimumMutex.lock();
|
||||||
|
if(globalOptimum == -1)
|
||||||
{
|
{
|
||||||
optimalRouteLength = nextRouteLength;
|
globalOptimum = nextRouteLength;
|
||||||
optimalRoute = nextRoute;
|
optimalRoute = nextRoute;
|
||||||
|
|
||||||
// Reset licznika
|
// Reset licznika
|
||||||
stopCounter = 0;
|
stopCounter = 0;
|
||||||
|
|
||||||
|
std::cout << "Nowa najlepsza trasa: " << globalOptimum << std::endl;
|
||||||
}
|
}
|
||||||
else if(optimalRouteLength > nextRouteLength)
|
else if(globalOptimum > nextRouteLength)
|
||||||
{
|
{
|
||||||
optimalRouteLength = nextRouteLength;
|
globalOptimum = nextRouteLength;
|
||||||
optimalRoute = nextRoute;
|
optimalRoute = nextRoute;
|
||||||
|
|
||||||
// Zaplanowanie intensyfikacji przy znalezieniu nowego optimum
|
// Zaplanowanie intensyfikacji przy znalezieniu nowego optimum
|
||||||
@@ -621,15 +626,18 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
|||||||
|
|
||||||
// Reset licznika
|
// Reset licznika
|
||||||
stopCounter = 0;
|
stopCounter = 0;
|
||||||
|
|
||||||
|
std::cout << "Nowa najlepsza trasa: " << globalOptimum << std::endl;
|
||||||
}
|
}
|
||||||
|
globalOptimumMutex.unlock();
|
||||||
|
|
||||||
// Weryfikacja listy tabu...
|
// Weryfikacja listy tabu...
|
||||||
int tabuPos = 0;
|
int tabuPos = 0;
|
||||||
while(tabuPos < tabuArray.size())
|
while(tabuPos < tabuArray.size())
|
||||||
{
|
{
|
||||||
// ...aktualizacja kadencji na liscie tabu
|
// ...aktualizacja kadencji na liscie tabu
|
||||||
tabuArray.at(tabuPos).at(0)--;
|
tabuArray.at(tabuPos).at(0)--;
|
||||||
|
|
||||||
//...usuniecie zerowych kadencji
|
//...usuniecie zerowych kadencji
|
||||||
if(tabuArray.at(tabuPos).at(0) == 0)
|
if(tabuArray.at(tabuPos).at(0) == 0)
|
||||||
tabuArray.erase(tabuArray.begin() + tabuPos);
|
tabuArray.erase(tabuArray.begin() + tabuPos);
|
||||||
|
|||||||
@@ -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> &result, int &resultLength, unsigned &globalOptimum, std::mutex &globalOptimumMutex);
|
||||||
|
|
||||||
class RouteComparison
|
class RouteComparison
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user