Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.56 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <vector>
  4. #include <ctime>
  5. #include <algorithm>
  6.  
  7. using namespace std;
  8.  
  9. int countCaptures(vector<int> queens)
  10. {
  11.     int counter = 0;
  12.     for(int i=0; i<queens.size()-1; ++i)
  13.     {
  14.         for(int j=i+1; j<queens.size(); ++j)
  15.         {
  16.             if(i-queens[i] == j-queens[j] || i+queens[i] == j+queens[j]) counter++;
  17.         }
  18.     }
  19.     return counter;
  20. }
  21.  
  22. void showVector(vector<int> queens)
  23. {
  24.     for(int i=0; i<queens.size(); i++) cout << queens[i] << "\t";
  25.     cout << endl;
  26. }
  27.  
  28. void showBoard(vector<int> queens)
  29. {
  30.     for(int i=0; i<queens.size(); i++)
  31.     {
  32.         for(int j=0; j<queens.size(); j++)
  33.         {
  34.             if(queens[i]==j) cout << "1 ";
  35.             else cout << "0 ";
  36.         }
  37.         cout << endl;
  38.     }
  39. }
  40.  
  41. void randomSwapElements(vector<int>& queens)
  42. {
  43.     int pos1 = rand()%queens.size();
  44.     int pos2 = rand()%queens.size();
  45.     int temp = queens[pos1];
  46.     queens[pos1] = queens[pos2];
  47.     queens[pos2] = temp;
  48. }
  49.  
  50.  
  51. int main()
  52. {
  53.     srand(time(nullptr));
  54.     int n, k, g;
  55.     float m;
  56.     cout << "Podaj rozmiar planszy: ";
  57.     cin >> n;
  58.     cout << "Podaj licznosc populacji: ";
  59.     cin >> k;
  60.     g = 1;
  61.     cout << "Podaj liczbe generacji: ";
  62.     cin >> g;
  63.     cout << "Podaj prawdopodobienstwo mutacji: ";
  64.     cin >> m;
  65.     vector<vector <int>> population(k, vector<int>(n));
  66.     vector<vector <int>> newPopulation(k, vector<int>(n));
  67.     vector <int> best(n);
  68.     vector <int> captures(k,0);
  69.     vector <int> ranking(k,0);
  70.     vector <float> averages(g,0);
  71.     vector <int> bests (g);
  72.     bool isFound = false;
  73.     for(int i=0; i<k; ++i)
  74.     {
  75.         for(int j=0; j<n; ++j)
  76.         {
  77.             population[i][j] = j;
  78.         }
  79.         random_shuffle(begin(population[i]), end(population[i]));
  80.     }
  81.     int capturesMax, rankSum;
  82.  
  83.  
  84.     for(int i=0; i<g; i++)
  85.     {
  86.         best = population[0];
  87.         capturesMax = 0;
  88.         rankSum = 0;
  89.         for(int j=0; j<k; j++)
  90.         {
  91.             captures[j] = countCaptures(population[j]);
  92.             averages[i] += captures[j];
  93.             if(captures[j]==0)
  94.             {
  95.                 best = population[j];
  96.                 isFound = true;
  97.                 break;
  98.             }
  99.             if(captures[j] > capturesMax) capturesMax = captures[j];
  100.             if(captures[j] < countCaptures(best)) best = population[j];
  101.         }
  102.         averages[i] /= k;
  103.         bests[i] = countCaptures(best);
  104.  
  105.         cout << "average\t" << i << ":\t" << averages[i] << endl;
  106.         cout << "best\t" << i << ":\t" << bests[i] << endl;
  107.  
  108.         if(isFound == true) break;
  109.  
  110.         for(int j=0; j<k; j++)
  111.         {
  112.             ranking[j] = capturesMax - captures[j] + 1;
  113.             rankSum += ranking[j];
  114.         }
  115.  
  116.         int roulette;
  117.         for(int j=0; j<k; j++)
  118.         {
  119.             roulette = rand()%rankSum+1;
  120.             int temp = 0;
  121.             for(int l=0; l<k; l++)
  122.             {
  123.                 temp += ranking[l];
  124.                 if(temp >= roulette)
  125.                 {
  126.                     newPopulation[j] = population[l];
  127.                     break;
  128.                 }
  129.             }
  130.         }
  131.  
  132.         for(int j=0; j<k; j++)
  133.         {
  134.             if(rand()%101 < m*100)
  135.             {
  136.                 randomSwapElements(newPopulation[j]);
  137.             }
  138.         }
  139.  
  140.         population = newPopulation;
  141.     }
  142.  
  143.  
  144.  
  145.     cout << endl << endl;
  146.     showVector(best);
  147.     showBoard(best);
  148.     cout << endl << "Liczba bic: " << countCaptures(best);
  149.  
  150.  
  151.  
  152.     return 0;
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement