Advertisement
Guest User

Untitled

a guest
Dec 15th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <iomanip>
  5. #include <ctime>
  6. #include <algorithm>
  7.  
  8. double function(double x, double y)
  9. {
  10.  
  11. double f;
  12. f = exp(-pow(x,2) - pow(y,2));
  13. return f;
  14. }
  15.  
  16.  
  17. int main()
  18. {
  19. setlocale(0, "rus");
  20. std::vector <double> firstchromosome;
  21. std::vector <double> secondchromosome;
  22. std::vector <double> FIT;
  23.  
  24. srand(time(0));
  25.  
  26. for (size_t i = 0; i < 4; ++i)
  27. {
  28. firstchromosome.push_back(((double)rand() / RAND_MAX)*4-2);
  29.  
  30. secondchromosome.push_back (((double)rand() / RAND_MAX)*4-2);
  31.  
  32. FIT.push_back(function(firstchromosome[i], secondchromosome[i]));
  33. }
  34.  
  35. std::cout << "| N | x | y | FIT |Максимальный результат|Средний результат|" << std::endl;
  36. std::cout << "|" << std::setw(3) << std::setprecision(3) << 0;
  37. for (size_t i = 0; i < 4; i++) {
  38. std::cout << "| " << std::fixed << std::setprecision(3) << std::setw(10) << firstchromosome[i] << "| ";
  39. std::cout << std::setw(10) << secondchromosome[i] << "|";
  40. std::cout << std::setw(10) << FIT[i] << "| ";
  41.  
  42. if (i == 0)
  43. {
  44. std::cout << std::setw(21) << *std::max_element(FIT.begin(), FIT.end()) << "|";
  45. std::cout << std::setw(17) << (FIT[0] + FIT[1] + FIT[2] + FIT[3]) / 4 << "|";
  46. }
  47.  
  48. else
  49. {
  50. std::cout << std::setw(22) << "|";
  51. std::cout << std::setw(18) << "|";
  52. }
  53.  
  54. std::cout<< std::endl << "|" << std::setw(5) << std::fixed << std::setprecision(3);
  55. }
  56.  
  57.  
  58. std::vector <double> firstchromosome2;
  59. std::vector <double> secondchromosome2;
  60. std::vector <double> FIT2;
  61.  
  62. for (size_t k = 1; k < 100; ++k) {
  63.  
  64. double sum = FIT[0] + FIT[1] + FIT[2] + FIT[3];
  65.  
  66. double persent1 = FIT[0]*100 / sum; //процентное соотношение каждой точки
  67. double persent2 = FIT[1]*100 / sum;
  68. double persent3 = FIT[2]*100 / sum;
  69. double persent4 = FIT[3]*100 / sum;
  70.  
  71.  
  72.  
  73. for (size_t i = 0; i < 2; ++i)
  74. {
  75. size_t j = rand() % 100 + 1;
  76. if (j <= persent1)
  77. {
  78. firstchromosome2.push_back(firstchromosome[0]);
  79. secondchromosome2.push_back(secondchromosome[0]);
  80. }
  81.  
  82. if (j > persent1 && j <= persent2+persent1)
  83. {
  84. firstchromosome2.push_back(firstchromosome[1]);
  85. secondchromosome2.push_back(secondchromosome[1]);
  86. }
  87.  
  88. if (j > persent1 && j > persent2+persent1 && j <= persent3+persent1+persent2)
  89. {
  90. firstchromosome2.push_back(firstchromosome[2]);
  91. secondchromosome2.push_back(secondchromosome[2]);
  92. }
  93.  
  94. if (j > persent1 && j > persent2 + persent1 && j > persent3 + persent1 + persent2)
  95. {
  96. firstchromosome2.push_back(firstchromosome[3]);
  97. secondchromosome2.push_back(secondchromosome[3]);
  98. }
  99. }
  100.  
  101.  
  102. firstchromosome2.push_back(firstchromosome2[0]);
  103. secondchromosome2.push_back(secondchromosome2[1]);
  104.  
  105. firstchromosome2.push_back(firstchromosome2[1]);
  106. secondchromosome2.push_back(secondchromosome2[0]);
  107.  
  108.  
  109. for (size_t i = 0; i < 4; ++i)
  110. {
  111. double mutation = rand() % 100 + 1;
  112. if (mutation <= 25)
  113. {
  114. mutation = rand()%2;
  115.  
  116. if (mutation == 0)
  117. {
  118. mutation = (((double)rand() / RAND_MAX)/10 - 0.1);
  119. firstchromosome2[i] = firstchromosome2[i] + mutation;
  120. }
  121.  
  122. else {
  123. mutation = (((double)rand() / RAND_MAX) / 10 - 0.1);
  124.  
  125. secondchromosome2[i] = secondchromosome2[i] + mutation;
  126. }
  127. }
  128.  
  129. }
  130.  
  131. for (size_t i = 0; i < 4; i++)
  132. {
  133. FIT2.push_back(function(firstchromosome2[i], secondchromosome2[i]));
  134. }
  135.  
  136.  
  137. std::cout << std::setw(3) << std::fixed << std::setprecision(3) << k;
  138.  
  139. for (size_t i = 0; i < 4; ++i)
  140. {
  141.  
  142. std::cout << "| " << std::fixed << std::setprecision(3) << std::setw(10) << firstchromosome[i] << "| ";
  143. std::cout << std::setw(10) << secondchromosome[i] << "|";
  144. std::cout << std::setw(10) << FIT[i] << "| ";
  145.  
  146. if (i == 0)
  147. {
  148. std::cout << std::setw(21) << *std::max_element(FIT.begin(), FIT.end()) << "|";
  149. std::cout << std::setw(17) << (FIT[0] + FIT[1] + FIT[2] + FIT[3]) / 4 << "|";
  150. }
  151.  
  152. else
  153. {
  154. std::cout << std::setw(22) << "|";
  155. std::cout << std::setw(18) << "|";
  156. }
  157.  
  158. std::cout << std::endl << "|" << std::setw(5) << std::fixed << std::setprecision(3);
  159. }
  160.  
  161.  
  162. firstchromosome = firstchromosome2;
  163. secondchromosome = secondchromosome2;
  164. FIT = FIT2;
  165.  
  166. firstchromosome2.clear();
  167. secondchromosome2.clear();
  168. FIT2.clear();
  169. }
  170.  
  171. std::vector<double>::iterator it = std::max_element(FIT.begin(), FIT.end());
  172.  
  173. size_t index = std::distance(FIT.begin(), it);
  174.  
  175. std::cout<< " Лучшее значение FIT функции 100 популяции " << *it << " при х = " << firstchromosome[index] << " и у = " << secondchromosome[index] << "|";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement