Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| babb720caa |
@@ -5,6 +5,7 @@
|
||||
#include <queue>
|
||||
#include <random>
|
||||
#include <thread>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
Graph::Graph()
|
||||
@@ -425,7 +426,7 @@ 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;
|
||||
|
||||
@@ -491,15 +492,13 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
||||
// 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...
|
||||
@@ -517,9 +516,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
||||
|
||||
while(cheeseSupplied == true)
|
||||
{
|
||||
std::vector<unsigned> nextRoute = currentRoute;
|
||||
// ...na wszelki wypadek, gdyby cale sasiedztwo bylo na liscie tabu
|
||||
// (zeby algorytm sie nie wywalil)
|
||||
std::vector<unsigned> nextRoute;
|
||||
int nextRouteLength = -1;
|
||||
|
||||
std::vector<unsigned> nextTabu(3, 0);
|
||||
@@ -566,8 +563,14 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
||||
// ...jezeli niespelnione - pomijamy ruch
|
||||
continue;
|
||||
|
||||
|
||||
if(nextRouteLength == -1 || nextRouteLength > neighbourRouteLength)
|
||||
if(nextRouteLength == -1)
|
||||
{
|
||||
nextRouteLength = neighbourRouteLength;
|
||||
nextRoute = neighbourRoute;
|
||||
nextTabu.at(1) = i;
|
||||
nextTabu.at(2) = j;
|
||||
}
|
||||
else if(nextRouteLength > neighbourRouteLength)
|
||||
{
|
||||
nextRouteLength = neighbourRouteLength;
|
||||
nextRoute = neighbourRoute;
|
||||
@@ -601,7 +604,7 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
||||
stopCounter = 0;
|
||||
}
|
||||
|
||||
// Synchronizacja globalnie najlepszej trasy (1)
|
||||
// Synchronizacja globalnie najlepszej trasy
|
||||
globalOptimumMutex.lock();
|
||||
if(globalOptimumLength == -1 || globalOptimumLength > nextRouteLength)
|
||||
{
|
||||
@@ -669,27 +672,9 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
||||
}
|
||||
else
|
||||
{
|
||||
// Synchronizacja globalnie najlepszej trasy (2)
|
||||
globalOptimumMutex.lock();
|
||||
optimalRouteLength = globalOptimumLength;
|
||||
optimalRoute = globalOptimum;
|
||||
globalOptimumMutex.unlock();
|
||||
|
||||
// W innym przypadku wlasciwa dywersyfikacja przez wygenerowanie nowego
|
||||
// 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);
|
||||
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;
|
||||
intensification = false;
|
||||
}
|
||||
@@ -698,4 +683,6 @@ void Graph::travellingSalesmanTabuSearchEngine(Graph &graph, unsigned tabuSteps,
|
||||
// Reset licznika iteracji przed restartem
|
||||
stopCounter = 0;
|
||||
}
|
||||
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
+2
-3
@@ -31,7 +31,7 @@ unsigned tabuLength = 0;
|
||||
// Domyslny stan dywersyfikacji
|
||||
bool tabuDiversification = true;
|
||||
// Domyslne kryterium dywersyfikacji, liczba iteracji bez poprawy
|
||||
int tabuIterationsToRestart = 5000;
|
||||
int tabuIterationsToRestart = 10000;
|
||||
// Domyslny czas zatrzymania algorytmu tabu search [s]
|
||||
unsigned tabuStopTime = 60;
|
||||
|
||||
@@ -206,7 +206,7 @@ unsigned autoTabuLength(Graph &graph)
|
||||
{
|
||||
unsigned computedTabuLength;
|
||||
|
||||
computedTabuLength = 10 + (graph.getVertexNumber() / 10) * 10;
|
||||
computedTabuLength = (2 * graph.getVertexNumber() / 10) * 10;
|
||||
if(computedTabuLength == 0)
|
||||
computedTabuLength = 10;
|
||||
|
||||
@@ -439,7 +439,6 @@ int main(int argc, char *argv[])
|
||||
// Wyswietlenie trasy
|
||||
unsigned distFromStart = 0;
|
||||
unsigned length = 0;
|
||||
cout << endl;
|
||||
cout << route.at(0) << '\t' << length << '\t' << distFromStart << endl;
|
||||
for(int i = 1; i < route.size(); i++)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user