Advertisement
Guest User

diff_algo

a guest
Jan 23rd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.46 KB | None | 0 0
  1. // diff_alg.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <time.h>
  6. #include <iomanip>
  7. #include <iostream>
  8. #include <random>
  9. using namespace std;
  10.  
  11. #define number_of_variables 1
  12. #define values 1
  13. #define amount_number 10
  14. #define a -5.12
  15. #define b 5.12
  16.  
  17. struct individual
  18. {
  19.     double x_values[number_of_variables];
  20.     double value_of_fitness_func;
  21. };
  22.  
  23. void    init_individuals(individual *indiv_array)
  24. {
  25.     mt19937 rand_for_gen((unsigned)time(NULL));
  26.     uniform_real_distribution <> dist(a, b);
  27.     int i;
  28.     int j;
  29.  
  30.     i = 0;
  31.     while (i < amount_number)
  32.     {
  33.         j = 0;
  34.         while (j < number_of_variables)
  35.         {
  36.             indiv_array[i].x_values[j] = dist(rand_for_gen);
  37.             indiv_array[i].value_of_fitness_func = 0;
  38.             j++;
  39.         }
  40.         i++;
  41.     }
  42. }
  43.  
  44. void sphere_model_func(individual *indiv_array)
  45. {
  46.     int i;
  47.     int j;
  48.  
  49.     i = 0;
  50.     while (i < amount_number)
  51.     {
  52.         indiv_array[i].value_of_fitness_func = 0;
  53.         j = 0;
  54.         while (j < number_of_variables)
  55.         {
  56.             indiv_array[i].value_of_fitness_func += indiv_array[i].x_values[j] * indiv_array[i].x_values[j];
  57.             j++;
  58.         }
  59.         i++;
  60.     }
  61. }
  62.  
  63. void  my_func(individual *indiv_array)
  64. {
  65.     int i;
  66.  
  67.     i = 0;
  68.     while (i < amount_number)
  69.     {
  70.         indiv_array[i].value_of_fitness_func = indiv_array[i].x_values[0] * indiv_array[i].x_values[1] * indiv_array[i].x_values[2];
  71.         i++;
  72.     }
  73. }
  74.  
  75. void show_me_individuals(individual *indiv_array)
  76. {
  77.     int i;
  78.     int j;
  79.  
  80.     i = 0;
  81.     while (i < amount_number)
  82.     {
  83.         j = 0;
  84.         while (j < number_of_variables)
  85.         {
  86.             cout << j << "Value: ";
  87.             cout << indiv_array[i].x_values[j] << " ";
  88.             cout << "Func value: " << indiv_array[i].value_of_fitness_func << endl;
  89.             j++;
  90.         }
  91.         i++;
  92.     }
  93. }
  94.  
  95. void sort_by_fitness_func(individual *unsorted_ar)
  96. {
  97.     int i;
  98.     int j;
  99.     individual temp;
  100.  
  101.     i = 0;
  102.     while (i < amount_number)
  103.     {
  104.         j = 0;
  105.         while (j < amount_number)
  106.         {
  107.             if (unsorted_ar[i].value_of_fitness_func > unsorted_ar[j].value_of_fitness_func)
  108.                 j++;
  109.             else
  110.             {
  111.                 temp = unsorted_ar[j];
  112.                 unsorted_ar[j] = unsorted_ar[i];
  113.                 unsorted_ar[i] = temp;
  114.                 j++;
  115.             }
  116.         }
  117.         i++;
  118.     }
  119. }
  120.  
  121. void check_childrens(individual *new_chil)
  122. {
  123.     int i;
  124.     int j;
  125.  
  126.     i = 0;
  127.     while (i < amount_number)
  128.     {
  129.         j = 0;
  130.         while (j < number_of_variables)
  131.         {
  132.             if (new_chil[i].x_values[j] < a)
  133.                 new_chil[i].x_values[j] = a;
  134.             if (new_chil[i].x_values[j] > b)
  135.                 new_chil[i].x_values[j] = b;
  136.             j++;
  137.         }
  138.         i++;
  139.     }
  140. }
  141.  
  142. void    init_mutant(individual *first, individual *mutants)
  143. {
  144.     mt19937 rand_for_gen((unsigned)time(NULL));
  145.     uniform_int_distribution <> dist1(0, amount_number - 1);
  146.     double  f;
  147.     int     i;
  148.     int     j;
  149.     int     r1;
  150.     int     r2;
  151.     int     r3;
  152.  
  153.     i = 0;
  154.     j = 0;
  155.     f = 1.25;
  156.     while (i < amount_number)
  157.     {
  158.         j = 0;
  159.         while (j < number_of_variables)
  160.         {
  161.             r1 = dist1(rand_for_gen);
  162.             while (r1 == i)
  163.                 r1 = dist1(rand_for_gen);
  164.             r2 = dist1(rand_for_gen);
  165.             while (r2 == i || r2 == r1)
  166.                 r2 = dist1(rand_for_gen);
  167.             r3 = dist1(rand_for_gen);
  168.             while (r3 == i || r3 == r1 || r3 == r2)
  169.                 r3 = dist1(rand_for_gen);
  170.             mutants[i].x_values[j] = first[r1].x_values[j] + f * (first[r2].x_values[j] - first[r3].x_values[j]);
  171.             j++;
  172.         }
  173.         i++;
  174.     }
  175.     check_childrens(mutants);
  176.     //my_func(mutants);
  177.     sphere_model_func(mutants);
  178. }
  179.  
  180. void    init_first_again(individual *first, individual *last)
  181. {
  182.     int i;
  183.     int j;
  184.  
  185.     i = 0;
  186.     while (i < amount_number)
  187.     {
  188.         j = 0;
  189.         while (j < number_of_variables)
  190.         {
  191.             first[i].x_values[j] = last[i].x_values[j];
  192.             j++;
  193.         }
  194.         first[i].value_of_fitness_func = last[i].value_of_fitness_func;
  195.         i++;
  196.     }
  197. }
  198.  
  199. void    find_best(individual *first, individual *temp_array, individual *last)
  200. {
  201.     int         i;
  202.     int         j;
  203.  
  204.     i = 0;
  205.     while (i < amount_number)
  206.     {
  207.         j = 0;
  208.         while (j < number_of_variables)
  209.         {
  210.             if (temp_array[i].value_of_fitness_func <= first[i].value_of_fitness_func)
  211.                 last[i].x_values[j] = temp_array[i].x_values[j];
  212.             else
  213.                 last[i].x_values[j] = first[i].x_values[j];
  214.             j++;
  215.         }
  216.         i++;
  217.     }
  218.     //my_func(last);
  219.     sphere_model_func(last);
  220. }
  221.  
  222. void    make_one_great_evil(individual *first, individual *mutants, individual *last)
  223. {
  224.     mt19937 rand_for_gen((unsigned)time(NULL));
  225.     uniform_int_distribution <> dist1(0, number_of_variables - 1);
  226.     uniform_real_distribution <> dist2(0, 1);
  227.     individual  t_array[amount_number];
  228.     double      cr;
  229.     double      rand_num;
  230.     int         i;
  231.     int         j;
  232.     int         any_number;
  233.  
  234.     cr = 0.2;
  235.     i = 0;
  236.     while (i < amount_number)
  237.     {
  238.         j = 0;
  239.         while (j < number_of_variables)
  240.         {
  241.             any_number = dist1(rand_for_gen);
  242.             rand_num = dist2(rand_for_gen);
  243.             if (j == any_number || cr >= rand_num)
  244.                 t_array[i].x_values[j] = mutants[i].x_values[j];
  245.             else
  246.                 t_array[i].x_values[j] = first[i].x_values[j];
  247.             j++;
  248.         }
  249.         i++;
  250.     }
  251.     sphere_model_func(t_array);
  252.     //my_func(t_array);
  253.     find_best(first,t_array, last);
  254.     init_first_again(first, last);
  255. }
  256.  
  257. int main()
  258. {
  259.     int iter;
  260.     cout << "limits [" << a << ";" << b << "]" << endl;
  261.     individual first[amount_number];
  262.     individual mutant_array[amount_number];
  263.     individual collaborative_array[amount_number];
  264.  
  265.     iter = 0;
  266.     init_individuals(first);
  267.     sphere_model_func(first);
  268.     //my_func(first);
  269.     show_me_individuals(first);
  270.     cout << endl;
  271.    
  272.  
  273.     while (iter < 300)
  274.     {
  275.         init_mutant(first, mutant_array);
  276.         make_one_great_evil(first, mutant_array, collaborative_array);
  277.         iter++;
  278.     }
  279.     cout << endl;
  280.     sort_by_fitness_func(collaborative_array);
  281.     show_me_individuals(collaborative_array);
  282.  
  283.     cout << endl;
  284.     system("pause");
  285.     return 0;
  286. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement