Advertisement
Tetriss

Terekhov RK#3

Dec 22nd, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <iomanip>
  5.  
  6. using namespace std;
  7.  
  8. struct GenAl
  9.  
  10. {
  11.     double x;
  12.     double y;
  13.     double f;
  14. };
  15.  
  16. double function(const double& x, const double& y)
  17. {
  18.     return sin(x)/(1 + x*x + y*y);
  19. }
  20.  
  21. double probability(const double& f1, const double& f2, const double& f3, const double& f4)
  22. {
  23.     return 1 - f1 / (f1 + f2 + f3 + f4);
  24. }
  25.  
  26. int crossover(const double& p1, const double& p2, const double& p3, const double& p4)
  27. {
  28.     double value = rand()/(RAND_MAX/(p1 + p2 + p3 + p4));
  29.     if (value < p1) return 0;
  30.     if (value > p1 && value < (p1 + p2)) return 1;
  31.     if (value > (p1 + p2) && value < (p1 + p2 + p3)) return 2;
  32.     if (value > (p1 + p2 + p3) && value < (p1 + p2 + p3 + p4)) return 3;
  33. }
  34.  
  35.  
  36. double mutation(const double& gen)
  37. {
  38.     if (gen >= 0) return gen - 0.05;
  39.     else return gen + 0.05;
  40. }
  41.  
  42. int main()
  43. {
  44.     double ax = -2, bx = 2, ay = -2, by = 2;
  45.     int n = 1;
  46.     vector <GenAl> g(4);
  47.     for (auto i = 0; i < 4; i++)
  48.     {
  49.         g[i].x = rand()/(RAND_MAX/2.0) - 2;
  50.         g[i].y = rand() / (RAND_MAX / 2.0) - 2;
  51.         g[i].f = function(g[i].x, g[i].y);
  52.     }
  53.     double avg = 0;
  54.     cout << "0" << endl;
  55.     cout << " x y f(x,y)" << endl;
  56.     sort(g.begin(), g.end(), [](auto a, auto b) { return a.f < b.f; });
  57.     for (auto i : g)
  58.  
  59.     {
  60.         cout << fixed << setprecision(4) << setw(7) << i.x << " " << setw(7) << i.y << " " << setw(7) << i.f << " " << endl;
  61.         avg += i.f;
  62.     }
  63.     avg /= 4;
  64.     cout << endl << "max: " << g[3].f << " average: " << avg << endl << endl;
  65.     while (n <= 50)
  66.     {
  67.         vector <double> prob(4);
  68.         prob[0] = probability(g[0].f, g[1].f, g[2].f, g[3].f);
  69.         prob[1] = probability(g[1].f, g[0].f, g[2].f, g[3].f);
  70.         prob[2] = probability(g[2].f, g[1].f, g[0].f, g[3].f);
  71.         prob[3] = probability(g[3].f, g[1].f, g[2].f, g[0].f);
  72.         int i = crossover(prob[0], prob[1], prob[2], prob[3]);
  73.         g.erase(g.begin() + i);
  74.         g.push_back({});
  75.         vector <GenAl> g_copy = g;
  76.         g[0].x = g_copy[2].x;
  77.         g[1].x = g_copy[2].x;
  78.         g[2].x = g_copy[0].x;
  79.         g[3].x = g_copy[1].x;
  80.         g[0].y = g_copy[1].y;
  81.         g[1].y = g_copy[0].y;
  82.         g[2].y = g_copy[2].y;
  83.         g[3].y = g_copy[2].y;
  84.         for (auto i = 0; i < 4; i++)
  85.         {
  86.             if (rand() % 2 == 0) g[i].x = g[i].x - 0.05;
  87.             else g[i].y = g[i].y - 0.05;
  88.         }
  89.         for (auto i = 0; i < 4; i++) g[i].f = function(g[i].x, g[i].y);
  90.         double avg = 0;
  91.         cout << n++ << " " << endl;
  92.         cout << " x y f(x,y)" << endl;
  93.         sort(g.begin(), g.end(), [](auto a, auto b) { return a.f < b.f; });
  94.         for (auto i : g)
  95.         {
  96.             cout << fixed << setprecision(4) << setw(7) << i.x << " " << setw(7) << i.y << " " << setw(7) << i.f << " " << endl;
  97.             avg += i.f;
  98.         }
  99.         avg /= 4;
  100.         cout << endl << "max: " << g[3].f << " average: " << avg << endl << endl;
  101.     }
  102.     return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement