Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.52 KB | None | 0 0
  1. #include "pch.h"
  2. #include "TabuSearch.h"
  3. #include <vector>
  4. #include <iostream>
  5. #include <algorithm>
  6. #include<time.h>
  7. #include "Creator.h"
  8. using namespace std;
  9. Creator creator;
  10. TabuSearch::TabuSearch()
  11. {
  12.  
  13. }
  14. TabuSearch::~TabuSearch()
  15. {
  16. }
  17.  
  18. vector<int> TabuSearch::createInitialSolution(int size)
  19. {
  20. vector<int> initialSolution;
  21. for (int i=0 ;i <size;i++)
  22. {
  23. initialSolution.push_back(i);
  24. }
  25. srand(time(0));
  26. random_shuffle(initialSolution.begin(), initialSolution.end());
  27. /*for (int i = 0; i < size; i++)
  28. {
  29. cout << initialSolution[i] << " ";
  30. }*/
  31. return initialSolution;
  32. }
  33.  
  34. vector<vector<int>> TabuSearch::createExchangesMatrix(int size)
  35. {
  36. vector<vector<int>> exchangesMatrix;
  37. for (int i =0 ; i < size ; i++)
  38. {
  39. vector<int> newColumn;
  40. for (int j = 0; j < size; j++)
  41. {
  42. newColumn.push_back(0);
  43. }
  44. exchangesMatrix.push_back(newColumn);
  45. }
  46. return exchangesMatrix;
  47. }
  48.  
  49. int TabuSearch::find(vector <int> &vector, int number)
  50. {
  51. for (int i=0; i < vector.size(); i++)
  52. {
  53. if (vector[i] == number)
  54. {
  55. return i;
  56. }
  57. }
  58. return NULL;
  59. }
  60. void TabuSearch::decrementMatrix(vector<vector<int>> &exchangesMatrix)
  61. {
  62. for (int i = 0; i < exchangesMatrix.size(); i++)
  63. {
  64. for (int j = 0; j < exchangesMatrix.size(); j++)
  65. {
  66. if (exchangesMatrix[i][j] != 0 || exchangesMatrix[j][i] != 0)
  67. {
  68. exchangesMatrix[i][j]--;
  69. exchangesMatrix[j][i]--;
  70. }
  71. }
  72. }
  73. }
  74.  
  75. void TabuSearch::calculateRoad(int size, vector<vector<int>> &roadsMatrix)
  76. {
  77. vector<vector<int>> exchangesMatrix = createExchangesMatrix(size);
  78. vector<int> initialSolution = createInitialSolution(size);
  79. vector<int> temporalPath(size);
  80. vector<int> bestNeighbourPath(size);
  81.  
  82. int j = 0;
  83. int bestRoad=INT_MAX;
  84. int currentRoad = 0;
  85. int counter = 0;
  86. int swappedA;
  87. int swappedB;
  88. int bestNeighbourCost = INT_MAX;
  89. //cout << endl;
  90. int a, b;
  91. for (int k = 0; k < initialSolution.size(); k++)
  92. {
  93. cout << initialSolution[k] << " ";
  94. }
  95. cout << endl;
  96.  
  97. for (int k = 0; k < 1000; k++)
  98. {
  99. j = 0;
  100. decrementMatrix(exchangesMatrix);
  101. for (int i = 0; i < size; i++) // wybieranie najlepszego sasiada
  102. {
  103. for (j; j < size; j++)
  104. {
  105. temporalPath = initialSolution;
  106. swap(temporalPath[i], temporalPath[j]);
  107. /*for (int vertex : temporalPath)
  108. {
  109. cout << vertex << " ";
  110. }
  111. cout << endl;*/
  112. currentRoad = creator.calculateRoad(temporalPath, roadsMatrix);
  113. if (exchangesMatrix[temporalPath[i]][temporalPath[j]] == 0 && exchangesMatrix[temporalPath[j]][temporalPath[i]] == 0 )
  114. {
  115. if (currentRoad < bestNeighbourCost)
  116. {
  117. bestNeighbourCost = currentRoad;
  118. bestNeighbourPath = temporalPath;
  119. swappedA = temporalPath[i];
  120. swappedB = temporalPath[j];
  121. cout << "new best neighbour" << endl;
  122. }
  123. if (currentRoad < bestRoad)
  124. {
  125. bestSolution = temporalPath;
  126. bestRoad = currentRoad;
  127. }
  128. }
  129. else if (currentRoad < bestRoad)
  130. {
  131. bestSolution = temporalPath;
  132. bestRoad = currentRoad;
  133. bestNeighbourPath = temporalPath;
  134. exchangesMatrix[temporalPath[i]][temporalPath[j]] += 5;
  135. exchangesMatrix[temporalPath[j]][temporalPath[i]] += 5;
  136. }
  137. }
  138. j++;
  139. }
  140. initialSolution = bestNeighbourPath;
  141. exchangesMatrix[swappedA][swappedB] = 5;
  142. exchangesMatrix[swappedB][swappedA] = 5;
  143. }
  144. for (int vertex : bestSolution)
  145. {
  146. cout << vertex << " ";
  147. }
  148. cout << endl << "Best road found: "<< bestRoad;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement