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 (conflicted copy 2020-02-03 100957).cpp 11KB

  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 "PEA.h"
  19. #include "ea-complexity.h"
  20. #include "crossover-rate.h"
  21. #include "scripts.h"
  22. #include "scripts2.h"
  23. #include "bestPGA.h"
  24. #include "bestPGA.h"
  25. #include "bestPEA.h"
  26. #include "algo-complexity.h"
  27. using namespace std;
  28. struct Data {
  29. Data() : nb_goods{0} {};
  30. double r;
  31. double s;
  32. double t;
  33. double u;
  34. double a;
  35. double b;
  36. double c;
  37. double d;
  38. double alpha;
  39. double beta;
  40. double gamma;
  41. double lambda;
  42. int nb_goods;
  43. };
  44. int main(int argc, char** argv) {
  45. srand(time(NULL));
  46. std::default_random_engine randomizer{static_cast<long unsigned int>(time(0))};
  47. if(argc >= 2) {
  48. int num_experiment = atoi(argv[1]);
  49. switch(num_experiment) {
  50. case 1:
  51. cout << "Experiment: Optimal Crossover probability" << endl;
  52. switch(atoi(argv[2])) {
  53. case 0: // Start from beginning
  54. script_crossover_proba_global_optim(randomizer, "global-crossover-proba.csv", false, 5, 5);
  55. break;
  56. case 1: // Restart from a specific point
  57. {
  58. stringstream ss;
  59. ss << "global-crossover-proba-" << atoi(argv[3]) << "-" << atoi(argv[4]) << ".csv";
  60. script_crossover_proba_global_optim(randomizer, ss.str(), true, atoi(argv[3]), atoi(argv[4]));
  61. }break;
  62. case 2: // Filtering
  63. crossover_model_filtering(argv[3], argv[4], argv[5], atoi(argv[6]), atoi(argv[7]));
  64. break;
  65. case 3: // Local optimization of model
  66. crossover_model_optimization(argv[3]);
  67. break;
  68. case 4: // Validation of model
  69. crossover_model_validation(argv[3], atof(argv[4]), atof(argv[5]), atof(argv[6]), atof(argv[7]));
  70. break;
  71. }
  72. return 0;
  73. case 2:
  74. cout << "Experiment: EA Complexity" << endl;
  75. switch(atoi(argv[2])) {
  76. case 1:
  77. {
  78. int n_initial = (argc>=4) ? atoi(argv[3]) : 3;
  79. stringstream ss;
  80. ss << "complexity-ea-n-" << n_initial << ".csv";
  81. ofstream outfile(ss.str());
  82. for(int n=n_initial; n<30; n++) {
  83. MasterMind mm(n, n, randomizer);
  84. for(int mu=1; mu<50; mu+=2) {
  85. cout << " n=" << n << ": mu=" << mu << endl;
  86. for(int lambda=1; lambda<50; lambda+=2) {
  87. EA algo(mu, lambda, PLUS, 1./n, randomizer);
  88. algo.set_problem(&mm);
  89. Statistic stat = avg_calls(&algo, 1000, -1, 1.9623414611);
  90. outfile << n << ";" << mu << ";" << lambda << ";" << stat.average << ";" << stat.alea << endl;
  91. }
  92. }
  93. }
  94. }
  95. break;
  96. case 2: // Filtering
  97. ea_complexity_filtering(argv[3], argv[4], atoi(argv[5]), atoi(argv[6]), atoi(argv[7]));
  98. break;
  99. }
  100. return 0;
  101. }
  102. }
  103. ////////////////////////////////////////////////////
  104. Data datas[30];
  105. std::ifstream infile("complexity-ea.csv");
  106. std::string line;
  107. while(getline(infile, line)) {
  108. int n, mu, lda;
  109. double avg, alea;
  110. sscanf(line.c_str(), "%d;%d;%d;%lf;%lf", &n, &mu, &lda, &avg, &alea);
  111. if((mu==5 || mu==45) && (lda==5 || lda==45)) {
  112. if(mu==5) {
  113. if(lda==5)
  114. datas[n].r = avg;
  115. else
  116. datas[n].s = avg;
  117. } else {
  118. if(lda==5)
  119. datas[n].t = avg;
  120. else
  121. datas[n].u = avg;
  122. }
  123. }
  124. }
  125. for(int n=5; n<30; n++) {
  126. Data dat = datas[n];
  127. datas[n].a = (dat.s - dat.r) / (45. - 5.);
  128. datas[n].b = dat.s - 45.*datas[n].a;
  129. datas[n].c = (dat.u - dat.t) / (45. - 5.);
  130. datas[n].b = dat.u - 45.*datas[n].c;
  131. }
  132. for(int n=5; n<30; n++) {
  133. Data dat = datas[n];
  134. datas[n].alpha = (dat.c - dat.a) / (45. - 5.);
  135. datas[n].gamma = dat.a - 5*datas[n].alpha;
  136. datas[n].beta = (dat.d - dat.b) / (45. - 5.);
  137. datas[n].lambda = dat.b - 5*datas[n].beta;
  138. }
  139. std::ifstream in2file("complexity-ea.csv");
  140. while(getline(in2file, line)) {
  141. int n, mu, lda;
  142. double avg, alea;
  143. sscanf(line.c_str(), "%d;%d;%d;%lf;%lf", &n, &mu, &lda, &avg, &alea);
  144. Data d = datas[n];
  145. double prev = d.alpha*mu*lda + d.beta*mu + d.gamma*lda + d.lambda;
  146. if(abs(avg-prev) <= alea)
  147. datas[n].nb_goods++;
  148. }
  149. for(int n=5; n<30; n++) {
  150. Data d = datas[n];
  151. cout << n << ": " << d.alpha << "*mu*lda + " << d.beta << "*mu + " << d.gamma << "*lda + " << d.lambda << " => " << 100*d.nb_goods/625. << "%" << endl;
  152. }
  153. return 0;
  154. /* RLS algo;
  155. vector<Statistic> stats = compute_complexity(randomizer, &algo, 10, 1000, 10, 100, 1.9842169516, true);
  156. save_in_file("RLS-complexity", stats);
  157. return 0;
  158. */
  159. //script_optimal_proba(randomizer);
  160. /*
  161. pea_optimisation(10, 20, randomizer);
  162. return 0;
  163. */
  164. int nb_colors = 20;
  165. int nb_cases = 20;
  166. int n=20;
  167. /*
  168. MasterMind mm(nb_colors, nb_cases, randomizer);
  169. RLS algo1;
  170. EA algo2(1, 1, PLUS, 1./n, randomizer);
  171. EA algo3(2, 1, PLUS, 1./n, randomizer);
  172. EA algo4(4, 1, PLUS, 1./n, randomizer);
  173. EA algo5(8, 1, PLUS, 1./n, randomizer);
  174. EA algo6(16, 1, PLUS, 1./n, randomizer);
  175. algo1.set_problem(&mm);
  176. algo2.set_problem(&mm);
  177. algo3.set_problem(&mm);
  178. algo4.set_problem(&mm);
  179. algo5.set_problem(&mm);
  180. algo6.set_problem(&mm);
  181. vector<SearchHeuristic*> algos = {&algo1, &algo2, &algo3, &algo4, &algo5, &algo6};
  182. */
  183. MasterMind mm(nb_colors, nb_cases, randomizer);
  184. RLS algo1;
  185. EA algo2(1, 1, PLUS, 1./n, randomizer);
  186. PEA algo3(1, 1, PLUS, {}, randomizer);
  187. algo1.set_problem(&mm);
  188. algo2.set_problem(&mm);
  189. algo3.set_problem(&mm);
  190. vector<SearchHeuristic*> algos = {&algo1, &algo2, &algo3};
  191. Statistic stat = avg_calls(&algo1, 20, -1, 3.883);
  192. int max_nb_calls = ceil(stat.average+stat.alea)*2;
  193. cmp_evolution(algos, mm, max_nb_calls, 100);
  194. return 0;
  195. /*
  196. Statistic* curves[6];
  197. Statistic* dcurves[6];
  198. int length = 0;
  199. for(int i=0; i<algos.size(); i++) {
  200. curves[i] = avg_evolution(algos[i], max_nb_calls, 20);
  201. dcurves[i] = derivate(curves[i], max_nb_calls);
  202. length = max_nb_calls-1;
  203. for(int k=0; k<7; k++) {
  204. Statistic* cdcurve = compress(dcurves[i], length);
  205. delete[] dcurves[i];
  206. dcurves[i] = cdcurve;
  207. length = ceil(length/2.)-1;
  208. }
  209. for(int k=0; k<15; k++) {
  210. Statistic* sdcurve = smooth(dcurves[i], length);
  211. delete[] dcurves[i];
  212. dcurves[i] = sdcurve;
  213. }
  214. }
  215. ofstream testfile("42.csv");
  216. for(int i=0; i<length; i++) {
  217. for(int j=0; j<algos.size(); j++)
  218. testfile << dcurves[j][i].average << ";";
  219. testfile << endl;
  220. }
  221. ofstream testfile2("43.csv");
  222. for(int i=0; i<max_nb_calls; i++) {
  223. for(int j=0; j<algos.size(); j++)
  224. testfile2 << curves[j][i].average << ";";
  225. testfile2 << endl;
  226. }
  227. for(int j=0; j<algos.size(); j++) {
  228. delete[] dcurves[j];
  229. delete[] curves[j];
  230. }
  231. return 0;
  232. int initial_n = 3;
  233. int final_n = 25;
  234. /// Mu-Lambda Optimisation
  235. MuLambdaOptimizer mlo(initial_n, randomizer);
  236. mlo.set_nb_contexts(final_n - initial_n);
  237. mlo.set_study_interval(1, 100, 4);
  238. mlo.add_test_wave(100, 2.4751374205, 0);
  239. mlo.add_test_wave(1000, 2.4365918443, 1);
  240. mlo.add_test_wave(10000, 2.4327997145, 2);
  241. //mlo.run();
  242. /*
  243. //optimal_mulambda(3, 16, randomizer, 1, 50, 1, 100, 1.9842169516);
  244. return 0;
  245. optimal_lambda(20, 1, 2, randomizer, 1, 100, 1, 50, 1.9842169516, true);
  246. return 0;
  247. */
  248. /* int nb_tests = 100;
  249. MasterMind mm(nb_colors, nb_cases, randomizer);
  250. RLS algo1;
  251. GA algo3(10, 10, PLUS, 1./nb_cases, 2./nb_cases, UNIFORM_CROSSOVER, GA_V1, randomizer);
  252. algo1.set_problem(&mm);
  253. Statistic stat = avg_calls(&algo1, 20, -1, 3.883);
  254. int max_nb_calls = ceil(stat.average);
  255. cmp_evolution({&algo1, &algo3}, mm, max_nb_calls, nb_tests);
  256. return 0;
  257. */
  258. //cout << "=========================" << endl;
  259. //optimal_proba(5,15, randomizer);
  260. //optimal_proba_v2(5,15, randomizer);
  261. //optimal_proba_vmath2_rec(3, 16, randomizer, 0.05, 0.5, 0.01, {20, 200, 2000}, {2.3362421597,2.3451370823,3.2953981367});
  262. //optimal_proba_vmath2_rec(16, 25, randomizer, 0.02, 0.2, 0.005, {20, 200, 2000}, {2.3362421597,2.3451370823,3.2953981367});
  263. cout << "FINISHED !" << endl;
  264. std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
  265. std::cin.get();
  266. return 0;
  267. cout << "=========================" << endl;
  268. for(int n=5; n<6; n++) {
  269. vector<double> avg_list;
  270. vector<double> lambdas;
  271. int lambda_init = 1;
  272. int lambda_final = 20;
  273. int nb_tests = 10000;
  274. auto start = start_chrono();
  275. for(int lambda=lambda_init; lambda<lambda_final; lambda++) {
  276. EA algo(1, lambda, PLUS, 1./n, randomizer);
  277. MasterMind mm(n, n, randomizer);
  278. algo.set_problem(&mm);
  279. Statistic stat = avg_calls(&algo, nb_tests);
  280. double avg = stat.average;
  281. avg_list.push_back(avg);
  282. lambdas.push_back(lambda);
  283. cout << " -> lambda=" << lambda << ", nbcalls=" << avg << endl;
  284. }
  285. auto stop = stop_chrono();
  286. int elapsed_milliseconds = interval_chrono(start, stop);
  287. int min_ind = 0;
  288. for(unsigned int i=1; i<avg_list.size(); i++)
  289. if(avg_list[min_ind] > avg_list[i])
  290. min_ind = i;
  291. double lambda_best = lambdas[min_ind];
  292. cout << "n=" << n << ": " << lambda_best << " in " << elapsed_milliseconds/1000. << " seconds " << "(" << nb_tests*(lambda_final-lambda_init) << " problems)" << endl;
  293. }
  294. return 0;
  295. }