Advertisement
ExIsTeR

IALab5

May 10th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. #include<iostream>
  2. #include<stdlib.h>
  3. #include<conio.h>
  4. #include<time.h>
  5. #define N 40
  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. cout << "init ";
  65. Afisare(config);
  66. cout << "\nevaluarea configuratiei este " << Evaluare(config);
  67. for (int i = 0; i <1000; i++) {
  68. int *config1 = Perturbare(config);
  69. if (Evaluare(config1)<Evaluare(config)) {
  70. config = config1;
  71. }
  72. if (Evaluare(config) == 0) {
  73. break;
  74. }
  75. }
  76. }
  77.  
  78. void HillClimbingRestartAleator() {
  79. init();
  80. int nr_pasi=0;//umarul pasilor care nu duc la o solutie mai buna
  81. cout << "init ";
  82. Afisare(config);
  83. cout << "\nevaluarea configuratiei este " << Evaluare(config);
  84. for (int i = 0; i <10000; i++) {
  85. if (nr_pasi < 10) {
  86.  
  87.  
  88. int *config1 = Perturbare(config);
  89. if (Evaluare(config1) < Evaluare(config)) {
  90. config = config1;
  91.  
  92. }
  93. else {
  94. nr_pasi++;
  95.  
  96. }
  97. }
  98. else {
  99. init();
  100. nr_pasi = 0;
  101. }
  102. if (Evaluare(config) == 0) {
  103. break;
  104. }
  105. }
  106. }
  107.  
  108. void main() {
  109. int *c;
  110. c = new int[N];
  111. config = new int[N];
  112. time_t t;
  113. srand((unsigned)(time(&t)));
  114. /*init();
  115.  
  116. for (int i = 0; i < N; i++) {
  117. cout << config[i] << " ";
  118. }
  119. cout << "\nevaluarea configuratiei este " << Evaluare(config);
  120. //cout << "\npertubarea configuratiei este " << *Perturbare(config);
  121. c = Perturbare(config);
  122. cout <<"\nPasul schimbat "<< endl
  123.  
  124. for (int i = 0;i < N;i++) {
  125. cout << c[i]<<" ";
  126. }*/
  127.  
  128. HillClimbingRestartAleator();
  129. Afisare(config);
  130. cout << "\nevaluarea configuratiei este " << Evaluare(config);
  131. _getch();
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement