inline bool check_dist_and_prob(const char* function, const RealType& r, RealType p, RealType prob, RealType* result, const Policy& pol) { if(check_dist(function, r, p, result, pol) && detail::check_probability(function, prob, result, pol) == false) { return false; } return true; } // check_dist_and_prob
inline bool check_dist_and_prob(const char* function, RealType p, RealType prob, RealType* result, const Policy& /* pol */) { if(check_dist(function, p, result, Policy()) && detail::check_probability(function, prob, result, Policy()) == false) { return false; } return true; }
inline bool check_dist_and_prob(const char* function, RealType p, RealType prob, RealType* result, const Policy& /* pol */) { if((check_dist(function, p, result, Policy(), typename policies::method_error_check<Policy>::type()) && detail::check_probability(function, prob, result, Policy())) == false) { return false; } return true; }
inline bool check_dist_and_k(const char* function, const RealType& r, const RealType& p, RealType k, RealType* result, const Policy& pol) { if(check_dist(function, r, p, result, pol) == false) { return false; } if( !(lslboost::math::isfinite)(k) || (k < 0) ) { // Check k failures. *result = policies::raise_domain_error<RealType>( function, "Number of failures argument is %1%, but must be >= 0 !", k, pol); return false; } return true; } // Check_dist_and_k
inline bool check_dist_and_k(const char* function, const RealType& p, RealType k, RealType* result, const Policy& pol) { if(check_dist(function, p, result, Policy()) == false) { return false; } if(!(boost::math::isfinite)(k) || !((k == 0) || (k == 1))) { *result = policies::raise_domain_error<RealType>( function, "Number of successes argument is %1%, but must be 0 or 1 !", k, pol); return false; } return true; }
float count_dist(float xdep, float ydep, float xarr, float yarr) { float dist_long; float dist_larg; float hypothenuse; if (xdep == xarr || ydep == yarr) return (check_dist(xdep, ydep, xarr, yarr)); else { dist_long = yarr - ydep; dist_larg = xarr - ydep; hypothenuse = sqrt((dist_long * dist_long) + (dist_larg * dist_larg)); return (hypothenuse); } }
inline bool check_dist_and_k(const char* function, const RealType& N, const RealType& p, RealType k, RealType* result, const Policy& pol) { if(check_dist(function, N, p, result, pol) == false) return false; if((k < 0) || !(boost::math::isfinite)(k)) { *result = policies::raise_domain_error<RealType>( function, "Number of Successes argument is %1%, but must be >= 0 !", k, pol); return false; } if(k > N) { *result = policies::raise_domain_error<RealType>( function, "Number of Successes argument is %1%, but must be <= Number of Trials !", k, pol); return false; } return true; }
inline bool check_dist_and_prob(const char* function, const RealType& alpha, const RealType& beta, RealType p, RealType* result, const Policy& pol) { return check_dist(function, alpha, beta, result, pol) && check_prob(function, p, result, pol); } // bool check_dist_and_prob
inline bool check_dist(const char* function, const RealType& p, RealType* result, const Policy& /* pol */) { return check_dist(function, p, result, Policy(), typename policies::constructor_error_check<Policy>::type()); }
inline bool check_dist_and_x(const char* function, const RealType& alpha, const RealType& beta, RealType x, RealType* result, const Policy& pol) { return check_dist(function, alpha, beta, result, pol) && beta_detail::check_x(function, x, result, pol); } // bool check_dist_and_x
/** * Principal function of the Genetic Algorithm * \fn shooter_repartition genetic algorithm heuristic for selection of towers **/ void shooter_repartition(const char * input_file_name, const float mutation_prob, const int population_size, const int nb_children, const int nb_iteration, const int dist, float taux_var, bool sim) { vector<tower>* towers = new vector<tower>(); int k, n; tower_parse(input_file_name, *towers, k, n); srand((unsigned int) time(NULL)); if (!is_easy_solution(k, n)) { vector<vector<tower>*>* population = generate_initial_pop(towers, k, dist, taux_var); sort_population(population); vector<vector<tower>*>* children; // population growth until reaching population_size while ((int) population->size() < population_size) { children = reproduction_iteration(nb_children, mutation_prob, population, towers, dist, taux_var); sort_population(children); for (int i = 0; i < (int) children->size(); i++) { if ((int) population->size() < population_size) { //do not oversize the population_size && !find_indiv(population, (*children)[i]) population->push_back((*children)[i]); } else { break; } } } // reordering population sort_population(population); // iteration of genetic algorithm for (int j = 0; j < nb_iteration; j++) { children = reproduction_iteration(nb_children, mutation_prob, population, towers, dist, taux_var); for (int i = 0; i < (int) children->size(); i++) { child_insertion((*children)[i], population); } } // printing the results for (int i = 0; i < 10 ; i++) { cout << i+1 << " : "; print_indiv((*population)[i]); } if(sim) simulate_day(population->front(), towers, dist, taux_var); for (vector<vector<tower>*>::iterator it = population->begin(); it != population->end(); it++){ delete *it; } delete population; } else { // generate all the permutations and verify which is the best; int tmp_eval = 0, best_eval = 0; int nb_perm = binomial_coef(n,k); int t_size = towers->size(); int *perm_indexes = (int*) malloc( k * sizeof(int)); //build index tracker to avoid repeating indivs vector<tower> *tmp_indiv, *best_indiv; tmp_indiv = new vector<tower>(); best_indiv = new vector<tower>(); for(int l = 0; l<k; l++){ perm_indexes[l] = l; tmp_indiv->push_back((*towers)[l]); } // spread var for variance spread_indiv_var(tmp_indiv, towers, taux_var); *best_indiv = *tmp_indiv; best_eval = check_dist(best_indiv, dist) ? eval_indiv(best_indiv) : 0; for (int j = 0; j < nb_perm; j++){ //build and eval permutation for (int i = 0; i < k ; i++){ tmp_indiv->at(i) = towers->at(perm_indexes[i]); } spread_indiv_var(tmp_indiv, towers, taux_var); if(check_dist(tmp_indiv,dist)) { tmp_eval = eval_indiv(tmp_indiv); if (tmp_eval > best_eval) { best_eval = tmp_eval; *best_indiv = *tmp_indiv; } } //update of indexes tracker perm_indexes[k-1]++; for( int m = k-2; m >= 0; m--){ //go to next permutation if (perm_indexes[m+1] > t_size-(k-m-1)){ perm_indexes[m]++; for(int p = m+1; p < k; p++){//correcting overflow perm_indexes[p] = perm_indexes[p-1]+1; } } } } print_indiv(best_indiv); if (sim) simulate_day(best_indiv, towers, dist, taux_var); free(perm_indexes); delete best_indiv; delete tmp_indiv; } delete towers; }
/** * Reproduction between two individu * Mix : [1 2 3 4 5] + [15 16 17 18 19] = [1 2 17 18 19] and [15 16 3 4 5] **/ vector<tower>* mix(vector<tower>* parent1, vector<tower>* parent2, const int dist, vector<tower>* towers, float taux_var){ vector<tower>* child1 = new vector<tower>(); vector<tower>* child2 = new vector<tower>(); for(int i = 0; i < ((int) parent1->size()/2); i++){ child1->push_back((*parent1)[i]); child2->push_back((*parent2)[i]); } for(int i = ((int) parent1->size()/2); i < (int) parent1->size(); i++){ if (find(child1->begin(), child1->end(), (*parent2)[i]) != child1->end() || find(child2->begin(), child2->end(), (*parent1)[i]) != child2->end()) { // ensure the unicity of tower if (find(child1->begin(), child1->end(), (*parent1)[i]) != child1->end() || find(child2->begin(), child2->end(), (*parent2)[i]) != child2->end()) { // ensure the unicity of tower delete child1; delete child2; return new vector<tower>(); } else { child1->push_back((*parent1)[i]); child2->push_back((*parent2)[i]); } } else { child1->push_back((*parent2)[i]); child2->push_back((*parent1)[i]); } } if (*child1 == *parent1 || *child2 == *parent2 || *child1 == *parent2 || *child2 == *parent1) { delete child1; delete child2; return new vector<tower>(); } // reset child and spread var spread_indiv_var(child1, towers, taux_var); spread_indiv_var(child2, towers, taux_var); if(*child1 < *child2){ if (check_dist(child2, dist)) { delete child1; return child2; } else { if (check_dist(child1, dist)) {// if the first child don't respect the distance try it for the orher child delete child2; return child1; } else { delete child1; delete child2; return new vector<tower>(); } } } else { if (check_dist(child1, dist)) { delete child2; return child1; } else { if (check_dist(child2, dist)) {// if the first child don't respect the distance try it for the orher child delete child1; return child2; } else { delete child1; delete child2; return new vector<tower>(); } } } }