Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include "TabuSearch.h"
- #include <vector>
- #include <iostream>
- #include <algorithm>
- #include<time.h>
- #include "Creator.h"
- using namespace std;
- Creator creator;
- TabuSearch::TabuSearch()
- {
- }
- TabuSearch::~TabuSearch()
- {
- }
- vector<int> TabuSearch::createInitialSolution(int size)
- {
- vector<int> initialSolution;
- for (int i=0 ;i <size;i++)
- {
- initialSolution.push_back(i);
- }
- srand(time(0));
- random_shuffle(initialSolution.begin(), initialSolution.end());
- /*for (int i = 0; i < size; i++)
- {
- cout << initialSolution[i] << " ";
- }*/
- return initialSolution;
- }
- vector<vector<int>> TabuSearch::createExchangesMatrix(int size)
- {
- vector<vector<int>> exchangesMatrix;
- for (int i =0 ; i < size ; i++)
- {
- vector<int> newColumn;
- for (int j = 0; j < size; j++)
- {
- newColumn.push_back(0);
- }
- exchangesMatrix.push_back(newColumn);
- }
- return exchangesMatrix;
- }
- int TabuSearch::find(vector <int> &vector, int number)
- {
- for (int i=0; i < vector.size(); i++)
- {
- if (vector[i] == number)
- {
- return i;
- }
- }
- return NULL;
- }
- void TabuSearch::decrementMatrix(vector<vector<int>> &exchangesMatrix)
- {
- for (int i = 0; i < exchangesMatrix.size(); i++)
- {
- for (int j = 0; j < exchangesMatrix.size(); j++)
- {
- if (exchangesMatrix[i][j] != 0 || exchangesMatrix[j][i] != 0)
- {
- exchangesMatrix[i][j]--;
- exchangesMatrix[j][i]--;
- }
- }
- }
- }
- void TabuSearch::calculateRoad(int size, vector<vector<int>> &roadsMatrix)
- {
- vector<vector<int>> exchangesMatrix = createExchangesMatrix(size);
- vector<int> initialSolution = createInitialSolution(size);
- vector<int> temporalPath(size);
- vector<int> bestNeighbourPath(size);
- int j = 0;
- int bestRoad=INT_MAX;
- int currentRoad = 0;
- int counter = 0;
- int swappedA;
- int swappedB;
- int bestNeighbourCost = INT_MAX;
- //cout << endl;
- int a, b;
- for (int k = 0; k < initialSolution.size(); k++)
- {
- cout << initialSolution[k] << " ";
- }
- cout << endl;
- for (int k = 0; k < 1000; k++)
- {
- j = 0;
- decrementMatrix(exchangesMatrix);
- for (int i = 0; i < size; i++) // wybieranie najlepszego sasiada
- {
- for (j; j < size; j++)
- {
- temporalPath = initialSolution;
- swap(temporalPath[i], temporalPath[j]);
- /*for (int vertex : temporalPath)
- {
- cout << vertex << " ";
- }
- cout << endl;*/
- currentRoad = creator.calculateRoad(temporalPath, roadsMatrix);
- if (exchangesMatrix[temporalPath[i]][temporalPath[j]] == 0 && exchangesMatrix[temporalPath[j]][temporalPath[i]] == 0 )
- {
- if (currentRoad < bestNeighbourCost)
- {
- bestNeighbourCost = currentRoad;
- bestNeighbourPath = temporalPath;
- swappedA = temporalPath[i];
- swappedB = temporalPath[j];
- cout << "new best neighbour" << endl;
- }
- if (currentRoad < bestRoad)
- {
- bestSolution = temporalPath;
- bestRoad = currentRoad;
- }
- }
- else if (currentRoad < bestRoad)
- {
- bestSolution = temporalPath;
- bestRoad = currentRoad;
- bestNeighbourPath = temporalPath;
- exchangesMatrix[temporalPath[i]][temporalPath[j]] += 5;
- exchangesMatrix[temporalPath[j]][temporalPath[i]] += 5;
- }
- }
- j++;
- }
- initialSolution = bestNeighbourPath;
- exchangesMatrix[swappedA][swappedB] = 5;
- exchangesMatrix[swappedB][swappedA] = 5;
- }
- for (int vertex : bestSolution)
- {
- cout << vertex << " ";
- }
- cout << endl << "Best road found: "<< bestRoad;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement