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 7.3KB

  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 <fstream>
  13. #include <sstream>
  14. #include <stdlib.h>
  15. #include "EA.h"
  16. #include "RLS.h"
  17. #include "analysis.h"
  18. #include "crossover-rate.h"
  19. #include "scripts.h"
  20. #include "scripts2.h"
  21. #include "bestPGA.h"
  22. #include "algo-complexity.h"
  23. using namespace std;
  24. int main(int argc, char** argv) {
  25. srand(time(NULL));
  26. std::default_random_engine randomizer{static_cast<long unsigned int>(time(0))};
  27. if(argc >= 2) {
  28. int num_experiment = atoi(argv[1]);
  29. switch(num_experiment) {
  30. case 1:
  31. cout << "Experiment: Optimal Crossover probability" << endl;
  32. switch(atoi(argv[2])) {
  33. case 0: // Start from beginning
  34. script_crossover_proba_global_optim(randomizer, "global-crossover-proba.csv", false, 5, 5);
  35. break;
  36. case 1: // Restart from a specific point
  37. {
  38. stringstream ss;
  39. ss << "global-crossover-proba-" << atoi(argv[3]) << "-" << atoi(argv[4]) << ".csv";
  40. script_crossover_proba_global_optim(randomizer, ss.str(), true, atoi(argv[3]), atoi(argv[4]));
  41. }break;
  42. case 2: // Filtering
  43. crossover_model_filtering(argv[3], argv[4], argv[5], atoi(argv[6]), atoi(argv[7]));
  44. break;
  45. case 3: // Local optimization of model
  46. crossover_model_optimization(argv[3]);
  47. break;
  48. case 4: // Validation of model
  49. crossover_model_validation(argv[3], atof(argv[4]), atof(argv[5]), atof(argv[6]), atof(argv[7]));
  50. break;
  51. }
  52. return 0;
  53. }
  54. }
  55. ////////////////////////////////////////////////////
  56. RLS algo;
  57. vector<Statistic> stats = compute_complexity(randomizer, &algo, 10, 1000, 10, 100, 1.9842169516, true);
  58. save_in_file("RLS-complexity", stats);
  59. return 0;
  60. //script_optimal_proba(randomizer);
  61. pga_optimisation(10, 20, randomizer);
  62. return 0;
  63. int nb_colors = 50;
  64. int nb_cases = 50;
  65. MasterMind mm(nb_colors, nb_cases, randomizer);
  66. RLS algo1;
  67. EA algo2(100, 100, PLUS, 1./nb_colors, randomizer);
  68. GA algo3(100, 100, PLUS, 1./nb_cases, cr(nb_colors, 100), UNIFORM_CROSSOVER, GA_V1, randomizer);
  69. cout << cr(nb_colors, 100) << endl;
  70. EA algo4(500, 500, PLUS, 1./nb_colors, randomizer);
  71. GA algo5(500, 500, PLUS, 1./nb_cases, cr(nb_colors, 500), UNIFORM_CROSSOVER, GA_V1, randomizer);
  72. cout << cr(nb_colors, 500) << endl;
  73. GA algo6(100, 100, PLUS, 1./nb_cases, 0.8, UNIFORM_CROSSOVER, GA_V1, randomizer);
  74. algo1.set_problem(&mm);
  75. algo2.set_problem(&mm);
  76. algo3.set_problem(&mm);
  77. algo4.set_problem(&mm);
  78. algo5.set_problem(&mm);
  79. algo6.set_problem(&mm);
  80. vector<SearchHeuristic*> algos = {&algo1, &algo2, &algo3, &algo4, &algo5, &algo6};
  81. Statistic stat = avg_calls(&algo1, 20, -1, 3.883);
  82. int max_nb_calls = ceil(stat.average+stat.alea)*2;
  83. cmp_evolution(algos, mm, max_nb_calls, 100);
  84. return 0;
  85. /*
  86. Statistic* curves[6];
  87. Statistic* dcurves[6];
  88. int length = 0;
  89. for(int i=0; i<algos.size(); i++) {
  90. curves[i] = avg_evolution(algos[i], max_nb_calls, 20);
  91. dcurves[i] = derivate(curves[i], max_nb_calls);
  92. length = max_nb_calls-1;
  93. for(int k=0; k<7; k++) {
  94. Statistic* cdcurve = compress(dcurves[i], length);
  95. delete[] dcurves[i];
  96. dcurves[i] = cdcurve;
  97. length = ceil(length/2.)-1;
  98. }
  99. for(int k=0; k<15; k++) {
  100. Statistic* sdcurve = smooth(dcurves[i], length);
  101. delete[] dcurves[i];
  102. dcurves[i] = sdcurve;
  103. }
  104. }
  105. ofstream testfile("42.csv");
  106. for(int i=0; i<length; i++) {
  107. for(int j=0; j<algos.size(); j++)
  108. testfile << dcurves[j][i].average << ";";
  109. testfile << endl;
  110. }
  111. ofstream testfile2("43.csv");
  112. for(int i=0; i<max_nb_calls; i++) {
  113. for(int j=0; j<algos.size(); j++)
  114. testfile2 << curves[j][i].average << ";";
  115. testfile2 << endl;
  116. }
  117. for(int j=0; j<algos.size(); j++) {
  118. delete[] dcurves[j];
  119. delete[] curves[j];
  120. }
  121. return 0;
  122. int initial_n = 3;
  123. int final_n = 25;
  124. /// Mu-Lambda Optimisation
  125. MuLambdaOptimizer mlo(initial_n, randomizer);
  126. mlo.set_nb_contexts(final_n - initial_n);
  127. mlo.set_study_interval(1, 100, 4);
  128. mlo.add_test_wave(100, 2.4751374205, 0);
  129. mlo.add_test_wave(1000, 2.4365918443, 1);
  130. mlo.add_test_wave(10000, 2.4327997145, 2);
  131. //mlo.run();
  132. /*
  133. //optimal_mulambda(3, 16, randomizer, 1, 50, 1, 100, 1.9842169516);
  134. return 0;
  135. optimal_lambda(20, 1, 2, randomizer, 1, 100, 1, 50, 1.9842169516, true);
  136. return 0;
  137. */
  138. /* int nb_tests = 100;
  139. MasterMind mm(nb_colors, nb_cases, randomizer);
  140. RLS algo1;
  141. GA algo3(10, 10, PLUS, 1./nb_cases, 2./nb_cases, UNIFORM_CROSSOVER, GA_V1, randomizer);
  142. algo1.set_problem(&mm);
  143. Statistic stat = avg_calls(&algo1, 20, -1, 3.883);
  144. int max_nb_calls = ceil(stat.average);
  145. cmp_evolution({&algo1, &algo3}, mm, max_nb_calls, nb_tests);
  146. return 0;
  147. */
  148. //cout << "=========================" << endl;
  149. //optimal_proba(5,15, randomizer);
  150. //optimal_proba_v2(5,15, randomizer);
  151. //optimal_proba_vmath2_rec(3, 16, randomizer, 0.05, 0.5, 0.01, {20, 200, 2000}, {2.3362421597,2.3451370823,3.2953981367});
  152. //optimal_proba_vmath2_rec(16, 25, randomizer, 0.02, 0.2, 0.005, {20, 200, 2000}, {2.3362421597,2.3451370823,3.2953981367});
  153. cout << "FINISHED !" << endl;
  154. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  155. std::cin.get();
  156. return 0;
  157. cout << "=========================" << endl;
  158. for(int n=5; n<6; n++) {
  159. vector<double> avg_list;
  160. vector<double> lambdas;
  161. int lambda_init = 1;
  162. int lambda_final = 20;
  163. int nb_tests = 10000;
  164. auto start = start_chrono();
  165. for(int lambda=lambda_init; lambda<lambda_final; lambda++) {
  166. EA algo(1, lambda, PLUS, 1./n, randomizer);
  167. MasterMind mm(n, n, randomizer);
  168. algo.set_problem(&mm);
  169. Statistic stat = avg_calls(&algo, nb_tests);
  170. double avg = stat.average;
  171. avg_list.push_back(avg);
  172. lambdas.push_back(lambda);
  173. cout << " -> lambda=" << lambda << ", nbcalls=" << avg << endl;
  174. }
  175. auto stop = stop_chrono();
  176. int elapsed_milliseconds = interval_chrono(start, stop);
  177. int min_ind = 0;
  178. for(unsigned int i=1; i<avg_list.size(); i++)
  179. if(avg_list[min_ind] > avg_list[i])
  180. min_ind = i;
  181. double lambda_best = lambdas[min_ind];
  182. cout << "n=" << n << ": " << lambda_best << " in " << elapsed_milliseconds/1000. << " seconds " << "(" << nb_tests*(lambda_final-lambda_init) << " problems)" << endl;
  183. }
  184. return 0;
  185. }