Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. #include "annealing.h"
  2.  
  3.  
  4.  
  5. double cooling_temperature=0.9999;
  6. double initial_temperature=1000.0;
  7. double minimal_temperature=0.0001;
  8. unsigned int iterations=100;
  9. double current_temperature;
  10.  
  11.  
  12. double current_length;
  13. double best_length;
  14. int* current_path;
  15. int* best_path;
  16.  
  17. int get_iterations() { return iterations; }
  18. double get_minimal_temperature() { return minimal_temperature; }
  19. double get_initial_temperature() { return initial_temperature; }
  20. double get_cooling_temperature() { return cooling_temperature; }
  21.  
  22. void random_swap(int numberOfCities)
  23. {
  24. int a = std::rand() % numberOfCities;
  25. int b = std::rand() % numberOfCities;
  26. while (a == b) b = std::rand() % numberOfCities;
  27.  
  28. current_path = best_path;
  29. int temporary = current_path[a];
  30. current_path[a] = current_path[b];
  31. current_path[b] = temporary;
  32. }
  33.  
  34. double path_length(int** matrix,int* path, int numberOfCities)
  35. {
  36. int len=0;
  37. int vertex1, vertex2;
  38. for(int i = 0; i<numberOfCities-1;i++)
  39. {
  40. vertex1 = path[i];
  41. vertex2 = path[i+1];
  42. len+=matrix[vertex1][vertex2];
  43. }
  44. len+=matrix[vertex2][path[0]];
  45.  
  46. return len;
  47. }
  48.  
  49. double probability()
  50. {
  51. double power = -((current_length - best_length) / current_temperature);
  52. return pow(M_E, power);
  53. }
  54.  
  55. void annealing(int** matrix,int numberOfCities)
  56. {
  57. current_temperature = initial_temperature;
  58. current_path = new int[numberOfCities];
  59. best_path = new int[numberOfCities];
  60.  
  61.  
  62. for (unsigned int i = 0; i <numberOfCities; i++)
  63. current_path[i] = i;
  64. current_length = path_length(matrix,current_path, numberOfCities);
  65.  
  66. for (size_t i = 0; i < numberOfCities; i++)
  67. best_path[i] = current_path[i];
  68. best_length = current_length;
  69.  
  70. srand((unsigned int)time(NULL));
  71.  
  72.  
  73.  
  74. while (current_temperature > minimal_temperature)
  75. {
  76. for (int j = 0; j < iterations; j++)
  77. {
  78. random_swap(numberOfCities);
  79. current_length = path_length(matrix,current_path,numberOfCities);
  80.  
  81. if (current_length < best_length || ((double)rand() / (double)RAND_MAX) < probability())
  82. {
  83. best_path = current_path;
  84. best_length = current_length;
  85.  
  86. // for (unsigned int i = 0; i < numberOfCities; i++)
  87. // cout << " " << best_path[i];
  88. // cout << " " << best_path[0] << " | " << best_length << endl;
  89.  
  90. }
  91. }
  92. current_temperature *= cooling_temperature;
  93. }
  94.  
  95. for (unsigned int i = 0; i < numberOfCities; i++)
  96. cout << " " << best_path[i];
  97. cout << " " << best_path[0] << " | " << best_length << endl;
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement