myLoveOnlyForYou

Untitled

Apr 1st, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.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];
  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 + 1 << " - > " << j + 1 << " : " << 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. }
  71.  
  72. // считаем длину каждой особи
  73. double best_individual = 1000000000, worst_individual = -1;
  74. int number_best_individual, number_worst_individual;
  75. for (int i = 0; i < amount; i++) {
  76. double SUM = 0;
  77. cout << path[i][0] << " ";
  78. for (int j = 1; j < n; j++) {
  79. cout << path[i][j] << " ";
  80. SUM += w[path[i][j - 1]][path[i][j]];
  81. }
  82. cout << " : " << SUM << endl;
  83. if (SUM < best_individual) {
  84. best_individual = SUM;
  85. number_best_individual = i;
  86. }
  87. if (SUM > worst_individual) {
  88. worst_individual = SUM;
  89. number_worst_individual = i;
  90. }
  91. }
  92. cout << best_individual << " " << number_best_individual << endl << worst_individual << " " << number_worst_individual;
  93.  
  94.  
  95. for (int h = 0; h < 3; h++) {
  96.  
  97. }
  98.  
  99. return 0;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment