Projet du cours MPRI 2.24.2 "Résolution de problèmes d'optimisation avec heuristiques de recherche" : https://wikimpri.dptinfo.ens-cachan.fr/doku.php?id=cours:c-2-24-2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 6.4KB

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <numeric>
  5. #include <algorithm>
  6. #include <vector>
  7. #include <random>
  8. #include <chrono>
  9. #include <cmath>
  10. #include <windows.h>
  11. #include <limits>
  12. #include "EA.h"
  13. #include "RLS.h"
  14. #include "analysis.h"
  15. #include "scripts.h"
  16. using namespace std;
  17. int main() {
  18. int nb_colors = 16;
  19. int nb_cases = 16;
  20. srand(time(NULL));
  21. std::default_random_engine randomizer{static_cast<long unsigned int>(time(0))};
  22. auto start2 = start_chrono();
  23. RLS algo1;
  24. Statistic stat1 = avg_calls(algo1, MasterMind(nb_colors, nb_cases, randomizer), 4500);
  25. cout << "Average RLS: " << stat1.average << endl;
  26. EA algo2(1./nb_cases, 1, 1, randomizer);
  27. Statistic stat2 = avg_calls(algo2, MasterMind(nb_colors, nb_cases, randomizer), 4500);
  28. cout << "Average EA: " << stat2.average << endl;
  29. auto stop2 = stop_chrono();
  30. int elapsed_milliseconds2 = interval_chrono(start2, stop2);
  31. cout << " ... executed in " << elapsed_milliseconds2/1000. << " seconds" << endl;
  32. for(int u=0; u<3; u++)
  33. Beep(1568, 200);
  34. cout << "=========================" << endl;
  35. //optimal_proba(5,15, randomizer);
  36. double p_init=0.05;
  37. double p_final=0.5;
  38. double p_step=0.01;
  39. int nb_diff_proba = floor((p_final-p_init)/p_step);
  40. int exploration_phase = 1000;
  41. int exploitation_phase = 10000;
  42. double initial_value = 50;
  43. int nb_test = 20;
  44. for(int n=2; n<15; n++) {
  45. auto start_n = start_chrono();
  46. double* tab = new double[nb_diff_proba];
  47. for(int i=0; i<nb_diff_proba; i++)
  48. tab[i] = initial_value;
  49. double max_weight = initial_value;
  50. int max_weight_ind_proba = 0;
  51. double second_weight = initial_value;
  52. double second_weight_ind_proba = 1;
  53. for(int num_loop=1; second_weight + 2*initial_value > max_weight; num_loop++) {
  54. auto start_loop = start_chrono();
  55. int nb_solved_problems = 0;
  56. for(int k=0; k<20; k++) {
  57. double min_avg = -1;
  58. double min_proba;
  59. for(double p=p_init; p<p_final; p+=p_step) {
  60. int ind = round((p - p_init)/p_step);
  61. double execution_proba = 0;
  62. if(ind > 0 && ind < nb_diff_proba-1)
  63. execution_proba = ((tab[ind-1]+tab[ind]+tab[ind+1])/3)/max_weight;
  64. else if(ind > 0)
  65. execution_proba = ((tab[ind-1] + tab[ind])/2)/max_weight;
  66. else if(ind < nb_diff_proba-1)
  67. execution_proba = ((tab[ind] + tab[ind+1])/2)/max_weight;
  68. execution_proba = (2*execution_proba > 1) ? -4*pow(execution_proba, 2)+8*execution_proba-3 : 0;
  69. std::bernoulli_distribution distribution(execution_proba);
  70. if(distribution(randomizer)) {
  71. EA algo(p, 1, 1, randomizer);
  72. Statistic stat = avg_calls(algo, MasterMind(n, n, randomizer), nb_test);
  73. double avg = stat.average;
  74. if(min_avg < 0 || min_avg > avg) {
  75. min_avg = avg;
  76. min_proba = p;
  77. }
  78. nb_solved_problems += nb_test;
  79. }
  80. }
  81. int ind_p = round((min_proba - p_init)/p_step);
  82. tab[ind_p]++;
  83. if(tab[ind_p] > max_weight) {
  84. max_weight = tab[ind_p];
  85. max_weight_ind_proba = ind_p;
  86. }
  87. }
  88. auto stop_loop = stop_chrono();
  89. int elapsed_milliseconds_loop = interval_chrono(start_n, stop_loop);
  90. second_weight = -1;
  91. for(int i=0; i<nb_diff_proba; i++)
  92. if(i != max_weight_ind_proba)
  93. if(second_weight < 0 || second_weight < tab[i]) {
  94. second_weight = tab[i];
  95. second_weight_ind_proba = i;
  96. }
  97. cout << "\r";
  98. cout << "n=" << n << ", num_loop=" << num_loop << ": ";
  99. cout << p_init+max_weight_ind_proba*p_step << " (" << max_weight << "), ";
  100. cout << p_init+second_weight_ind_proba*p_step << " (" << second_weight << "), ";
  101. cout << "executed in " <<elapsed_milliseconds_loop/1000. << " seconds [" << nb_solved_problems << "]";
  102. cout << " ";
  103. }
  104. //cout << endl;
  105. //for(int i=0; i<nb_diff_proba; i++)
  106. // cout << p_init+i*p_step << ": " << tab[i]-initial_value << endl;
  107. auto stop_n = stop_chrono();
  108. int elapsed_milliseconds_n = interval_chrono(start_n, stop_n);
  109. cout << '\r' << "n=" << n << ", p_best=" << p_init+max_weight_ind_proba*p_step << ", executed in " << elapsed_milliseconds_n/1000. << " seconds";
  110. cout << " " << endl;
  111. for(int u=0; u<3; u++)
  112. Beep(1568, 200);
  113. }
  114. cout << "FINISHED !" << endl;
  115. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  116. std::cin.get();
  117. return 0;
  118. cout << "=========================" << endl;
  119. for(int n=5; n<6; n++) {
  120. vector<double> avg_list;
  121. vector<double> lambdas;
  122. int lambda_init = 1;
  123. int lambda_final = 20;
  124. int nb_tests = 10000;
  125. auto start = start_chrono();
  126. for(int lambda=lambda_init; lambda<lambda_final; lambda++) {
  127. EA algo(1./n, lambda, 1, randomizer);
  128. Statistic stat = avg_calls(algo, MasterMind(n, n, randomizer), nb_tests);
  129. double avg = stat.average;
  130. avg_list.push_back(avg);
  131. lambdas.push_back(lambda);
  132. cout << " -> lambda=" << lambda << ", nbcalls=" << avg << endl;
  133. }
  134. auto stop = stop_chrono();
  135. int elapsed_milliseconds = interval_chrono(start, stop);
  136. int min_ind = 0;
  137. for(int i=1; i<avg_list.size(); i++)
  138. if(avg_list[min_ind] > avg_list[i])
  139. min_ind = i;
  140. double lambda_best = lambdas[min_ind];
  141. cout << "n=" << n << ": " << lambda_best << " in " << elapsed_milliseconds/1000. << " seconds " << "(" << nb_tests*(lambda_final-lambda_init) << " problems)" << endl;
  142. }
  143. return 0;
  144. }