char *print_indiv( char *buffer ) { if ( *buffer < FSET_START ) { printf( " X%d", *buffer ); return( ++buffer ); } printf(" (%c",prim_print[*buffer - FSET_START]); buffer = print_indiv( print_indiv( ++buffer ) ); printf(")"); return( buffer ); }
/** * Simulate a day * Print the result of each hour **/ void simulate_day(vector<tower>* solution, vector<tower>* towers, const int dist, float taux_var) { cout << "DAY: " << endl; int index, n = (int) towers->size(); for (int i = 0; i < 10; i++) { cout << i << "h: "; for (vector<tower>::iterator it = solution->begin(); it != solution->end(); it++) { if (it->var_est < 1) { do { index = rand() % n; } while (find(solution->begin(), solution->end(), (*towers)[index]) != solution->end() || !check_dist_with_specific_tower(solution, (*towers)[index], dist)); change_var(towers, index, taux_var); *it = (*towers)[index]; } } print_indiv(solution); } }
void stats( float *fitness, char **pop, int gen ) { int i, node_count; node_count = best = 0; favgpop = 0.0f; for ( i = 0; i < POPSIZE; i ++ ) { node_count += length( pop[i] ); favgpop += fitness[i]; if ( fitness[i] > fitness[best] ) best = i; } avg_len = (float) node_count / POPSIZE; favgpop /= POPSIZE; printf("\nGen=%d AvgFit=%g BestFit=%g Nodes=%lu Avg_size=%g\n", gen, -favgpop, -fitness[best],nodes_evaluated,avg_len); print_indiv( pop[best] ); }
/** * 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; }