Facebook
From Torrid Baboon, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 153
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <math.h>
  5. #include <fstream>
  6. #include <windows.h>
  7. #define LENGTH_GEN 8
  8.  
  9. using namespace std;
  10. struct Person
  11. {
  12.     char gen[LENGTH_GEN + 1];
  13.     unsigned int value;
  14. };
  15.  
  16. void copyGen(char gen1[], const char gen2[])
  17. {
  18.     for (int i = 0; i < LENGTH_GEN; i++)
  19.     {
  20.         if (gen2[i] == 0 || gen2[i] == '0')
  21.             gen1[i] = '0';
  22.         else if (gen2[i] == 1 || gen2[i] == '1')
  23.             gen1[i] = '1';
  24.         else
  25.             gen1[i] = -1;
  26.     }
  27. }
  28.  
  29. unsigned int binToDec(const char gen[])
  30. {
  31.     unsigned int value = 0;
  32.     char genTmp[LENGTH_GEN];
  33.     copyGen(genTmp, gen);
  34.  
  35.     for (int i = LENGTH_GEN - 1, j = 0; i >= 0; i--, j++)
  36.     {
  37.         if (genTmp[i] == '1')
  38.             value += pow(2, j);
  39.     }
  40.  
  41.     return value;
  42. }
  43.  
  44. void bindValue(Person persons[], const unsigned int cardin)
  45. {
  46.     for (int i = 0; i < cardin; i++)
  47.         persons[i].value = binToDec(persons[i].gen);
  48. }
  49.  
  50. void initPersons(Person persons[], const unsigned int cardin)
  51. {
  52.     srand(time(NULL));
  53.  
  54.     char gen[LENGTH_GEN];
  55.     for (int i = 0; i < cardin; i++)
  56.     {
  57.         for (int j = 0; j < LENGTH_GEN; j++)
  58.         {
  59.             gen[j] = rand() % 2;
  60.         }
  61.         copyGen(persons[i].gen, gen);
  62.         persons[i].value = binToDec(gen);
  63.         persons[i].gen[LENGTH_GEN] = 0;
  64.     }
  65. }
  66.  
  67. void typePersons(const Person persons[], const unsigned int cardin)
  68. {
  69.     for (int i = 0; i < cardin; i++)
  70.         cout << i << ": " << persons[i].gen << ", wartosc: " << persons[i].value << endl;
  71. }
  72.  
  73. double punktValue(const double a, const double b, const double c, const double x)
  74. {
  75.     double y = a * x * x + b * x + c;
  76.     return y;
  77. }
  78.  
  79. void clear(string nazwafileu)
  80. {
  81.     remove("file.txt");
  82. }
  83.  
  84. void mutation(char gen[], const float probMutation)
  85. {
  86.     int random;
  87.  
  88.     for (int i = 0; i < LENGTH_GEN; i++)
  89.     {
  90.         random = rand() % 100 + 1;
  91.         if ((float)random <= probMutation * 100)
  92.         {
  93.             if (gen[i] == '0')
  94.             {
  95.                 gen[i] = '1';
  96.             }
  97.             else if (gen[i] == '1')
  98.             {
  99.                 gen[i] = '0';
  100.             }
  101.         }
  102.     }
  103. }
  104.  
  105. void best(Person persons[], const unsigned int cardin, const double a, const double b, const double c)
  106. {
  107.     double value, maxvalue = punktValue(a, b, c, persons[0].value);
  108.     double xMax = persons[0].value;
  109.  
  110.     for (int i = 1; i < cardin; i++)
  111.     {
  112.         value = punktValue(a, b, c, persons[i].value);
  113.         if (value > maxvalue)
  114.         {
  115.             maxvalue = value;
  116.             xMax = persons[i].value;
  117.         }
  118.     }
  119.     cout << "Wartosc funkcji najlepiej przystosowanego Osobnika wynosi: " << maxvalue << ", dla x = " << xMax << endl;
  120.  
  121.     ofstream file;
  122.     file.open("wynik.txt", ios::app);
  123.  
  124.     if (!file)
  125.         cout << "Nie moge otworzyc fileu!n";
  126.     file << xMax << " " << maxvalue << endl;
  127.     file.close();
  128. }
  129.  
  130.  
  131.  
  132. void hybirdParents(char *child, const char parentOne[], const char parentTwo[], const int param)
  133. {
  134.     int cutPoint = rand() % (LENGTH_GEN - 1) + 1;
  135.  
  136.     if (param == 1)
  137.     {
  138.         for (int i = 0; i < cutPoint; i++)
  139.             child[i] = parentOne[i];
  140.  
  141.         for (int i = cutPoint; i < LENGTH_GEN; i++)
  142.             child[i] = parentTwo[i];
  143.     }
  144.     else
  145.     {
  146.         for (int i = 0; i < cutPoint; i++)
  147.             child[i] = parentTwo[i];
  148.  
  149.         for (int i = cutPoint; i < LENGTH_GEN; i++)
  150.             child[i] = parentOne[i];
  151.     }
  152. }
  153.  
  154. void hybrid(Person persons[], const unsigned int cardin, const float probHybird)
  155. {
  156.     bool busyArray[cardin];
  157.     for (int i = 0; i < cardin; i++)
  158.         busyArray[i] = false;
  159.  
  160.     int index, random;
  161.     char child1[LENGTH_GEN + 1];
  162.     char child2[LENGTH_GEN + 1];
  163.     child1[LENGTH_GEN] = 0;
  164.     child2[LENGTH_GEN] = 0;
  165.  
  166.     for (int i = 0; i < cardin / 2; i++)
  167.     {
  168.         index = rand() % (cardin / 2) + (cardin / 2);
  169.         if (!busyArray[index])
  170.         {
  171.             busyArray[i] = true;
  172.             busyArray[index] = true;
  173.             random = rand() % 100 + 1;
  174.             if ((float)random <= probHybird * 100)
  175.             {
  176.                 hybirdParents(child1, persons[i].gen, persons[index].gen, 1);
  177.                 hybirdParents(child2, persons[i].gen, persons[index].gen, 2);
  178.                 copyGen(persons[i].gen, child1);
  179.                 copyGen(persons[index].gen, child2);
  180.             }
  181.         }
  182.         else
  183.         {
  184.             i--;
  185.             continue;
  186.         }
  187.     }
  188. }
  189.  
  190. void selection(Person persons[], const unsigned int cardin, const double a, const double b, const double c)
  191. {
  192.     double fsum = 0;
  193.     double f[cardin];
  194.     double p[cardin];
  195.  
  196.     for (int i = 0; i < cardin; i++)
  197.     {
  198.         f[i] = punktValue(a, b, c, persons[i].value);
  199.         fsum += f[i];
  200.     }
  201.  
  202.     for (int i = 0; i < cardin; i++)
  203.     {
  204.         p[i] = f[i] / fsum;
  205.     }
  206.  
  207.     Person personsTmp[cardin];
  208.     float random, tmp;
  209.  
  210.     for (int i = 0; i < cardin; i++)
  211.     {
  212.         random = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
  213.         tmp = 0;
  214.  
  215.         for (int j = 0; j < cardin; j++)
  216.         {
  217.             tmp += p[j];
  218.             if (tmp >= random)
  219.             {
  220.                 copyGen(personsTmp[i].gen, persons[j].gen);
  221.                 personsTmp[i].value = persons[j].value;
  222.                 break;
  223.             }
  224.         }
  225.     }
  226.  
  227.     for (int i = 0; i < cardin; i++)
  228.     {
  229.         copyGen(persons[i].gen, personsTmp[i].gen);
  230.         persons[i].value = personsTmp[i].value;
  231.     }
  232. }
  233.  
  234. int main()
  235. {
  236.     //f(x)=ax^2+bx+c
  237.     const unsigned int populationNumber = 15;
  238.     const unsigned int populationSize = 10;
  239.     const float probHybird = 0.8;
  240.     const float probMutation = 0.1;
  241.     unsigned int NOS = 40;
  242.     double a = 4.0;
  243.     double b = 7.0;
  244.     double c = 2.0;
  245.  
  246.     clear("wynik.txt");
  247.     cout << "Obliczam wspolczynnik a = 4, b = 7 i c = 2 rownania f(x)=ax^2+bx+c ...." << endl;
  248.     Sleep(2000);
  249.     cout << "Zaczynam krzyzowanie 40 razy, aby znalezc najlepszego!" << endl;
  250.     Sleep(1000);
  251.  
  252.     if (populationSize % 2 != 0)
  253.     {
  254.         cout << "Liczebnosc populacji musi byc parzysta!" << endl;
  255.         return 1;
  256.     }
  257.  
  258.     Person persons[populationSize];
  259.     initPersons(persons, populationSize);
  260.  
  261.     for (int k = 0; k < NOS; k++)
  262.     {
  263.         cout << endl
  264.              << endl
  265.              << "Program nr: " << k + 1 << endl;
  266.         for (int i = 0; i < populationNumber; i++)
  267.         {
  268.             cout << endl
  269.                  << "Populacja nr: " << i + 1 << endl;
  270.             typePersons(persons, populationSize);
  271.             hybrid(persons, populationSize, probHybird);
  272.             for (int j = 0; j < populationSize; j++) {
  273.                 mutation(persons[j].gen, probMutation);
  274.             }
  275.             selection(persons, populationSize, a, b, c);
  276.             bindValue(persons, populationSize);
  277.  
  278.             if (i == populationNumber - 1) {
  279.                 best(persons, populationSize, a, b, c);
  280.             }
  281.         }
  282.     }
  283.  
  284.     return 0;
  285. }
  286.