#ifndef SCRIPTS_H_INCLUDED #define SCRIPTS_H_INCLUDED #include #include "analysis.h" #include "ParameterOptimizer.h" using namespace std; class ProbaOptimizer : public ParameterOptimizer { public: ProbaOptimizer(int initial_n, std::default_random_engine& randomizer) : ParameterOptimizer(randomizer), m_initial_n{initial_n} {}; SearchHeuristic* get_context(unsigned int i, double parameter, std::default_random_engine& randomizer) { EA* algo = new EA(1, 1, PLUS, parameter, randomizer); MasterMind* mm = new MasterMind(m_initial_n+i, m_initial_n+i, randomizer); algo->set_problem(mm); return algo; } virtual std::string display_context(unsigned int context) { std::stringstream ss; ss << "n=" << context+m_initial_n; return ss.str(); } std::string get_context_for_csvfile(unsigned int num_context) { return std::to_string(m_initial_n+num_context); } private: int m_initial_n; }; void optimal_proba_vmath3(int n_initial, int n_final, std::default_random_engine& randomizer, double p_init=0.05, double p_final=0.5, double p_step=0.01, vector nb_tests_by_depth={}, vector stq_by_depth={}) { ProbaOptimizer po(n_initial, randomizer); po.set_nb_contexts(n_final-n_initial); po.set_study_interval(p_init, p_final, p_step); for(int depth=0; depth optimal_proba_vmath2(int n_initial, int n_final, std::default_random_engine& randomizer, double p_init=0.05, double p_final=0.5, double p_step=0.01, int nb_tests=10, double student_law_quartile=-1, bool displaying=true) { int nb_diff_proba = floor((p_final-p_init)/p_step); // Initialize result file ofstream probafile("probas-vmath.csv"); probafile << "n;"; for(int i=0; i res; for(int n=n_initial; n potential_thresold) thresold = potential_thresold; auto stop_loop = stop_chrono(); int elapsed_milliseconds_loop = interval_chrono(start_n, stop_loop); if(displaying) cout << '\r' << "n=" << n << ", p=" << p << "... executed in " << elapsed_milliseconds_loop/1000. << " s"; } // Calculate the confidence interval of 95% if(displaying) cout << '\r' << "n=" << n << ". Thresold=" << thresold << ". Keep best... "; int min_possible = -1; int max_possible = -1; for(int i=0; i optimal_proba_vmath2_rec(int n_initial, int n_final, std::default_random_engine& randomizer, double base_p_init=0.05, double base_p_final=0.5, double p_step=0.01, vector nb_tests_by_depth={}, vector stq_by_depth={}) { vector res; for(int n=n_initial; n cis; int nb_tests = 0; for(unsigned int depth=0; depth= 0) ? ind2 : old_min_ind; double p = p_init+ind*p_step; EA algo(1, 1, PLUS, p, randomizer); MasterMind mm(n, n, randomizer); algo.set_problem(&mm); Statistic stat = avg_calls(&algo, nb_tests, min_avg+1); nb_solved_problems += stat.nb; double avg_nb_calls = stat.average; if(min_avg < 0 || min_avg > avg_nb_calls) { min_avg = avg_nb_calls; min_ind = ind; } } // Store the result of the experience nb_best[min_ind]++; if(nb_best[min_ind] > nb_best[max_min_ind]) max_min_ind = min_ind; old_min_ind = min_ind; // Display temporary results auto stop_loop = stop_chrono(); int elapsed_milliseconds_loop = interval_chrono(start_n, stop_loop); cout << "\r"; cout << "n=" << n << ", num_loop=" << num_loop << ": "; cout << p_init+max_min_ind*p_step << " (" << nb_best[max_min_ind] << "), "; cout << "executed in " <=0; j--) { for(int i=0; i= j/10.) ? '#' : ' '); } cout << endl; } cout << endl; // Calculate the confidence interval of 95% double pbest = p_init+max_min_ind*p_step; double x = (double) nb_best[max_min_ind]/nb_loops; cout << "x=" << x << ',' << nb_loops << ',' << nb_best[max_min_ind] << endl; double up_bound = calculate_up_bound(x, nb_loops); double down_bound = calculate_down_bound(x, nb_loops); cout << "\\mu_{p_best} \\in [" << down_bound << ", " << up_bound << "]" << endl; cout << "Keep best..." << endl; int min_possible = -1; int max_possible = -1; for(int i=0; i= down_bound) min_possible = i; if(max_bound >= down_bound) max_possible = i; } double pmin = p_init+min_possible*p_step; double pmax = p_init+max_possible*p_step; // Store the results in a file probafile << n << ";"; for(int i=0; i0; depth/=2) { double start_with_weight = max_weight; for(int num_loop=1; second_weight + 2*initial_value > max_weight && max_weight < 20*initial_value+start_with_weight; num_loop++) { int nb_new_problems = 0; for(int k=0; k<20; k++) { double min_avg = -1; double min_proba; for(int ind2=-2; ind2= 0) ? ind2 : ((ind2 == -2) ? max_weight_ind_proba: second_weight_ind_proba); if(ind % depth != 0) continue; double p = p_init+ind*p_step; double weight = tab[ind].weight; double execution_proba = weight/max_weight; if(ind-depth >= 0 && ind+depth < nb_diff_proba) execution_proba = ((tab[ind-depth].weight+weight+tab[ind+depth].weight)/3)/max_weight; else if(ind-depth >= 0) execution_proba = ((tab[ind-depth].weight + weight)/2)/max_weight; else if(ind+depth < nb_diff_proba) execution_proba = ((weight + tab[ind+depth].weight)/2)/max_weight; execution_proba = (2*execution_proba > 1) ? pa*pow(execution_proba, 2)+pb*execution_proba+pc : 0; std::bernoulli_distribution distribution(execution_proba); if(distribution(randomizer)) { tab[ind].nb_executed++; EA algo(1, 1, PLUS, p, randomizer); MasterMind mm(n, n, randomizer); algo.set_problem(&mm); Statistic stat = avg_calls(&algo, nb_test, min_avg+1); double avg = stat.average; if(min_avg < 0 || min_avg > avg) { min_avg = avg; min_proba = p; } nb_new_problems += stat.nb; } } int ind_p = round((min_proba - p_init)/p_step); tab[ind_p].weight++; if(tab[ind_p].weight > max_weight) { max_weight = tab[ind_p].weight; max_weight_ind_proba = ind_p; } tab[max_weight_ind_proba].nb_best++; } auto stop_loop = stop_chrono(); int elapsed_milliseconds_loop = interval_chrono(start_n, stop_loop); second_weight = -1; for(int i=0; i avg_list; vector probas; auto start = start_chrono(); for(double p=p_init; p avg_list[i]) min_ind = i; double p_best = probas[min_ind]; cout << "n=" << n << ": " << p_best << " in " << elapsed_milliseconds/1000. << " seconds " << "(" << 100*floor((p_final-p_init)/p_step) << " problems)" << endl; } } #endif // SCRIPTS_H_INCLUDED