Advertisement
frentzy

hillclimbing restart aleator

May 10th, 2018
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.58 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. #include<time.h>
  5. #define N 20
  6.  
  7. using namespace std;
  8. int *config;//configuratie = vector ce reprezinta o permutare adica o aranindexare a damelor
  9.  
  10.  
  11. void init() {
  12.     int M[N];
  13.     int lungime_M = N;//lungime_M a vectorului M
  14.     int i;//indicele de configuratie
  15.     for (int i = 0; i < N; i++) {
  16.         M[i] = i + 1;
  17.     }
  18.     i = 0;
  19.     while (lungime_M > 0) {
  20.         int g = rand() % lungime_M;
  21.         config[i++] = M[g];
  22.         for (int index = g; index < lungime_M - 1; index++) {
  23.             M[index] = M[index + 1];//scoate elementul M[g] din lista
  24.         }
  25.         lungime_M--;
  26.     }
  27.  
  28. }
  29. int Evaluare(int *c) {
  30.     int erori = 0; //nr de atacuri pe diagonala
  31.     for (int i = 0;i < N - 1;i++) {
  32.         for (int j = i + 1;j < N;j++) {
  33.             if (abs(config[i]) - config[j] == abs(i - j)) {
  34.                 erori++;
  35.             }
  36.         }
  37.     }
  38.     return erori;
  39. }
  40.  
  41. int *Perturbare(int *c) {
  42.     int x, y;
  43.     x = rand() % N;
  44.     y = rand() % N;
  45.     while (x == y) {
  46.         y = rand() % N;
  47.     }
  48.     int temp = c[x];
  49.     c[x] = c[y];
  50.     c[y] = temp;
  51.  
  52.     return c;
  53. }
  54.  
  55. void Afisare(int *c) {
  56.     cout << "\nAfisare:\n";
  57.     for (int i = 0;i < N;i++) {
  58.         cout << c[i] << " ";
  59.     }
  60. }
  61.  
  62. void HillClimbing() {
  63.     init();
  64.     bool verifica = true;
  65.     cout << "init ";
  66.  
  67.     Afisare(config);
  68. start:
  69.     for (int i = 0;i <100000;i++) {
  70.         int *config1 = Perturbare(config);
  71.         if (Evaluare(config1)<Evaluare(config)) {
  72.             config = config1;
  73.         }
  74.         if (Evaluare(config) == 0) {
  75.             verifica = false;
  76.             break;
  77.         }
  78.     }
  79.     if (verifica) {
  80.         init();
  81.         goto start;
  82.     }
  83. }
  84.  
  85. void HillClimbingRestartAleator() {
  86.     init();
  87.     int nr_pasi = 0;//umarul pasilor care nu duc la o solutie mai buna
  88.     cout << "init ";
  89.     Afisare(config);
  90.     cout << "\nevaluarea configuratiei este " << Evaluare(config);
  91.     for (int i = 0; i <10000; i++) {
  92.         if (nr_pasi < 10) {
  93.  
  94.  
  95.             int *config1 = Perturbare(config);
  96.             if (Evaluare(config1) < Evaluare(config)) {
  97.                 config = config1;
  98.  
  99.             }
  100.             else {
  101.                 nr_pasi++;
  102.  
  103.             }
  104.         }
  105.         else {
  106.             init();
  107.             nr_pasi = 0;
  108.         }
  109.         if (Evaluare(config) == 0) {
  110.             break;
  111.         }
  112.     }
  113. }
  114.  
  115.  
  116.  
  117. void main() {
  118.     int *c;
  119.     c = new int[N];
  120.     config = new int[N];
  121.     time_t t;
  122.     srand((unsigned)(time(&t)));
  123.     /*init();
  124.  
  125.     for (int i = 0; i < N; i++) {
  126.     cout << config[i] << " ";
  127.     }
  128.     cout << "\nevaluarea configuratiei este " << Evaluare(config);
  129.     //cout << "\npertubarea configuratiei este " << *Perturbare(config);
  130.     c = Perturbare(config);
  131.     cout <<"\nPasul schimbat "<< endl
  132.  
  133.     for (int i = 0;i < N;i++) {
  134.     cout << c[i]<<" ";
  135.     }*/
  136.  
  137.     HillClimbingRestartAleator();
  138.     Afisare(config);
  139.     cout << "\nevaluarea configuratiei este " << Evaluare(config);
  140.     _getch();
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement