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 104526).cpp 13KB

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