Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. #include <vector>
  5. #include <random>
  6. #include <algorithm>
  7.  
  8. // Область допустимых значений
  9. std::pair Dx = {0, 2}, Dy = {-2, 2};
  10.  
  11. // Данная функция
  12. double Function(std::pair<double, double> X_Y) {
  13.     return sin(X_Y.first) * sin(X_Y.second) / (1 + X_Y.first * X_Y.first + X_Y.second * X_Y.second);
  14. }
  15.  
  16. std::vector<size_t> Roulette(const std::vector<std::pair<double, double>> &Population) {
  17.     double Sum = 0;
  18.     std::vector<size_t> Return;
  19.     Return.reserve(Population.size());
  20.  
  21.     for (const auto &i : Population)
  22.         Sum += Function(i);
  23.  
  24.     for (const auto &i : Population)
  25.         Return.emplace_back(Function(i) / Sum * 100);
  26.  
  27.     return Return;
  28. }
  29.  
  30. // Сортировка по убыванию
  31. struct SortStruct {
  32.     bool operator() (std::pair<double, double> i, std::pair<double, double> j) {
  33.         return (Function(i) > Function(j));
  34.     }
  35. } MySortMethod;
  36.  
  37.  
  38. void CrossOver(std::vector<size_t> Vec, std::vector<std::pair<double,double>> &Population) {
  39.     std::vector<std::pair<double, double>> Return;
  40.     for(const auto &i: Population) {
  41.         std::random_device Rand;
  42.         std::mt19937 Mt(Rand());
  43.         std::uniform_real_distribution<double> Dist (0, 100);
  44.         double Probability = Dist(Mt);
  45.  
  46.         size_t  Sum = 0, Index = 0;
  47.         for (size_t j = 0; j < Vec.size(); j++) {
  48.             Sum += Vec[j];
  49.  
  50.             if (Sum > Probability) {
  51.                 Index = j;
  52.                 break;
  53.             }
  54.  
  55.             // Если на последней итерации Sum < Probability
  56.             if (j == Vec.size() - 1)
  57.                 Index = -1;
  58.         }
  59.  
  60.         if (Index != -1) {
  61.             Return.emplace_back(std::make_pair(Population[Index].first, i.second));
  62.             Return.emplace_back(std::make_pair(i.first, Population[Index].second));
  63.         }
  64.     }
  65.  
  66.     // Сортируем по убыванию
  67.     // дольше, но легче чем 4 раза искать наибольший элемент, исключая результаты предыдущих итераций
  68.     std::sort(Return.begin(), Return.end(), MySortMethod);
  69.  
  70.     Return.resize(4);
  71.     Population.resize(4);
  72.     Population = Return;
  73. }
  74.  
  75.  
  76. int main() {
  77.     std::random_device Device;
  78.     std::mt19937 Mt(Device());
  79.     std::vector<std::pair<double, double>> Population;
  80.     Population.reserve(4);
  81.  
  82.     for (int i = 0; i < 4; i++) {
  83.         std::pair<double, double> Point;
  84.  
  85.         std::uniform_real_distribution<double> DistX(Dx.first, Dx.second);
  86.         Point.first = DistX(Mt);
  87.         std::uniform_real_distribution<double> DistY(Dy.first, Dy.second);
  88.         Point.second = DistY(Mt);
  89.  
  90.         Population.emplace_back(Point);
  91.     }
  92.  
  93.     std::cout << "| № |    X    |    Y    |   Fit   |    Max    |  Average  |\n";
  94.     for (size_t i = 0; i < 10; ++i) {
  95.         CrossOver(Roulette(Population), Population);
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement