Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <string>
  5. #include <random>
  6. #include <chrono>
  7. using namespace std;
  8.  
  9. random_device device;
  10. mt19937 generator(device());
  11. int **distances_matrix;
  12. int **flows_matrix;
  13.  
  14.  
  15. int foo(vector<int> ans) {
  16. int global_summ = 0;
  17. for (int i = 0; i < ans.size(); i++) {
  18. int plant_1 = ans[i];
  19. for (int j = i + 1; j < ans.size(); j++) {
  20. int plant_2 = ans[j];
  21. global_summ += distances_matrix[i][j] * flows_matrix[plant_1][plant_2];
  22. }
  23. }
  24. return global_summ;
  25. }
  26.  
  27. vector<int> start_solution(int count_) {
  28. vector<int> ans;
  29. for (int i = 0; i < count_; i++) {
  30. ans.push_back(i);
  31. }
  32. uniform_int_distribution<int> distribution(0, ans.size() - 1);
  33. for (int i = 0; i < (count_ * 100); i++)
  34. swap(ans[distribution(generator)], ans[ans.size() - distribution(generator) - 1]);
  35. return ans;
  36. }
  37.  
  38. vector<int> solution_swap(vector<int> ans) {
  39. uniform_int_distribution<int> distribution(0, ans.size() - 1);
  40. int position = distribution(generator);
  41. int direction = distribution(generator) % 2;
  42. if (direction) {
  43. swap(ans[position], ans[(position + 1) % (ans.size() - 1)]);
  44. }
  45. else if (position == 0) {
  46. swap(ans[position], ans[ans.size() - 1]);
  47. }
  48. else {
  49. swap(ans[position], ans[(position - 1)]);
  50. }
  51. return ans;
  52. }
  53.  
  54. vector<int> compare(vector<int> ans_) {
  55. vector<int> best = ans_;
  56. int iter = 1000000;
  57. while (iter) {
  58. ans_ = solution_swap(ans_);
  59. if (foo(best) > foo(ans_))
  60. best = ans_;
  61. iter--;
  62. }
  63. return best;
  64. }
  65.  
  66. //сам поиск(функцию, которая будет брать решение, делать из него соседа и сравнивать их)
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74. int main() {
  75. string file_name = "C:/Users/Mike/Documents/Visual Studio 2017/projects/tai20a";
  76. ifstream file(file_name);
  77. int count;
  78. file >> count;
  79. distances_matrix = new int*[count];
  80. flows_matrix = new int*[count];
  81. for (int i = 0; i < count; i++) {
  82. distances_matrix[i] = new int[count];
  83. for (int j = 0; j < count; j++) {
  84. file >> distances_matrix[i][j];
  85. }
  86. }
  87. for (int i = 0; i < count; i++) {
  88. flows_matrix[i] = new int[count];
  89. for (int j = 0; j < count; j++) {
  90. file >> flows_matrix[i][j];
  91. }
  92. }
  93. vector<int> start = start_solution(count);
  94. vector<int> ans = compare(start);
  95. cout << foo(ans)<<" "<<foo(start);
  96. system("pause");
  97. return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement