Advertisement
myLoveOnlyForYou

Untitled

Apr 1st, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.44 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cmath>
  4. #include <string>
  5. #include <iomanip>
  6. #include <regex>
  7. #include <cstdlib>
  8.  
  9. using namespace std;
  10.  
  11. struct City {
  12. double x, y;
  13. };
  14.  
  15. int main() {
  16. freopen("input.txt", "r", stdin);
  17. freopen("output.txt", "w", stdout);
  18. int n, vertex_1, vertex_2; cin >> n;
  19. int amount = 4; // кол-во особей первоначально
  20. double **w = new double*[n];
  21. int **path = new int*[amount];
  22. for (int i = 0; i < amount; i++) {
  23. path[i] = new int[n + 1];
  24. for (int j = 0; j < n; j++)
  25. path[i][j] = -1;
  26. }
  27. int *visited = new int[n];
  28. City *cities = new City[n];
  29.  
  30. // коорд в матрицу весов
  31. for (int i = 0; i < n; i++)
  32. cin >> cities[i].x >> cities[i].y;
  33. for (int i = 0; i < n; i++) {
  34. w[i] = new double[n];
  35. visited[i] = 0;
  36. for (int j = 0; j < n; j++) {
  37. double x1 = cities[i].x, y1 = cities[i].y;
  38. double x2 = cities[j].x, y2 = cities[j].y;
  39. w[i][j] = sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
  40. }
  41. }
  42.  
  43. for (int i = 0; i < n; i++) {
  44. for (int j = 0; j < n; j++) {
  45. cout << i << " - > " << j << " : " << w[i][j] << endl;
  46. }
  47. }
  48. // создание первоначальной популяции
  49. for (int i = 0; i < amount; i++) {
  50. int start_pos = rand() % n;
  51. path[i][0] = start_pos;
  52. for (int j = 1; j < n; j++) {
  53. int next_pos;
  54. while (true) {
  55. bool cond = true;
  56. next_pos = rand() % n;
  57. for (int k = 0; k < n; k++) {
  58. if (k != j) {
  59. if (next_pos == path[i][k] || next_pos == path[i][0]) {
  60. cond = false;
  61. break;
  62. }
  63. }
  64. }
  65. if (cond == true)
  66. break;
  67. }
  68. path[i][j] = next_pos;
  69. }
  70. path[i][n] = start_pos;
  71. }
  72.  
  73. // считаем длину каждой особи
  74. double best_individual = 1000000000, worst_individual = -1;
  75. int number_best_individual, number_worst_individual;
  76. for (int i = 0; i < amount; i++) {
  77. double SUM = 0;
  78. cout << path[i][0] << " ";
  79. for (int j = 1; j <= n; j++) {
  80. cout << path[i][j] << " ";
  81. SUM += w[path[i][j - 1]][path[i][j]];
  82. }
  83. cout << ": " << SUM << endl;
  84. if (SUM < best_individual) {
  85. best_individual = SUM;
  86. number_best_individual = i;
  87. }
  88. else if (SUM > worst_individual) {
  89. worst_individual = SUM;
  90. number_worst_individual = i;
  91. }
  92. }
  93. cout << best_individual << " " << number_best_individual << endl << worst_individual << " " << number_worst_individual << endl;
  94.  
  95.  
  96. for (int start_mut = 0; start_mut < 3; start_mut++) {
  97. // мутация
  98. int counter = n - 1;
  99. path[number_worst_individual][0] = path[number_best_individual][0];
  100. path[number_worst_individual][n] = path[number_best_individual][n];
  101. for (int h = 1; h < n; h++) {
  102. path[number_worst_individual][h] = path[number_best_individual][counter];
  103. counter--;
  104. }
  105.  
  106. best_individual = 100000000; worst_individual = -1;
  107.  
  108. for (int i = 0; i < amount; i++) {
  109.  
  110. double SUM = 0;
  111. cout << path[i][0] << " ";
  112. for (int j = 1; j <= n; j++) {
  113. cout << path[i][j] << " ";
  114. SUM += w[path[i][j - 1]][path[i][j]];
  115. }
  116. cout << ": " << SUM << endl;
  117. if (SUM < best_individual) {
  118. best_individual = SUM;
  119. number_best_individual = i;
  120. }
  121. else if (SUM > worst_individual) {
  122. worst_individual = SUM;
  123. number_worst_individual = i;
  124. }
  125. }
  126. cout << best_individual << " " << number_best_individual << endl << worst_individual << " " << number_worst_individual << endl;
  127. }
  128.  
  129. return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement