void population::evolve(int gen) { //Wright-Fisher Model - Population size is held constant. Parental genotypes are chosen randomly. for (int j=0; j<gen; j++){ vector<genome> newPop (N); avgFit = 0; for(int i=0; i<N; i++) { if (r == 1) { newPop[i] = progeny(selectParents()); avgFit += newPop[i].get_fit(); } else if ((r != 0) && gsl_rng_uniform(rng) <= r) { newPop[i] = progeny(selectParents()); avgFit += newPop[i].get_fit(); } else { if (neutral) {newPop[i] = pop[(int)gsl_rng_uniform_int(rng,N)];} else { double fit_rand = selective_weight[N-1]*gsl_rng_uniform(rng); newPop[i] = pop[binarySearch_int(selective_weight, 0, N-1, fit_rand)]; avgFit += newPop[i].get_fit(); } } } avgFit/= N; pop = newPop; gen_pop++; update_weights(); //updateBlockSizes(); if (!neutral) {update_selection_weights();} } update_weights(); updateBlockSizes(); //if (WRITEHIST) {writeBlockHist();} }
void Popu::createNewGen(){ ///Apply Ellitism by keeping the 2 first chromosomes newGen[0]=content[0]; newGen[1]=content[1]; ///qDebug()<<"Ellistism performed"<<content[0].fitness<<content[1].fitness; for (int i=2; i<popSize;i=i+2){ selectParents(&parentOne,&parentTwo,i); ///child=cross(parentOne,parentTwo); As crossover is a failure for now simply copy one parent as is ///child=parentOne; ///child=cross(parentOne,parentTwo); ///content[i]=child; ///child=cross(parentOne,parentTwo); ///content[i+1]=child; //qDebug()<<"Generated"<<child.elements<<" route="<< child.routeLength; //qDebug()<<"from parents"<< parentOne.elements<<parentTwo.elements; ///qDebug()<<"Generated"<< child.routeLength<<"from parents"<<parentOne.routeLength<<parentTwo.routeLength; ///if (child.routeLength >parentOne.routeLength) {child=parentOne;} ///if (child.routeLength >parentTwo.routeLength) {child=parentTwo;} //qDebug()<<"Starting Mutation"; //qDebug()<<"Starting Mutation"; ///mutate(i); mutateImprove(i); } ///qDebug()<<"New generation created"; content=newGen; //newGen.clear(); sortContent(); }
pair<int,int> population::selectParents(){ //Parents are selected at random from population pair<int,int> ancestors; if (neutral) { //Neutral Evolution ancestors.first = (int)gsl_rng_uniform_int(rng,N); ancestors.second = (int)gsl_rng_uniform_int(rng,N); } else { //Selection! double fit_rand1 = selective_weight[N-1]*gsl_rng_uniform(rng); double fit_rand2 = selective_weight[N-1]*gsl_rng_uniform(rng); ancestors.first = binarySearch_int(selective_weight, 0, N-1, fit_rand1); ancestors.second = binarySearch_int(selective_weight, 0, N-1, fit_rand2); } //Recursive definition to ensure unique parents. if (ancestors.second == ancestors.first) { return selectParents(); } else { return ancestors; } }
/// Choisit n parents, cree un enfant, l'intensifie et met a jour la population void runIteration(){ // Selection des parents int nbParents=2; selectParents(nbParents); buildChildN(nbParents); tColor=tChild; initConflict(); bestFitnessValue=99999; for (int i=0; i<nbLocalSearch && nbEdgesConflict > 0 ; i++) { determineBestImprove(); if (nbEdgesConflict<bestFitnessValue) { bestFitnessValue=nbEdgesConflict; bestIter=i; for (int j=0; j<nbSommets; j++) { tBestSol[j]=tColor[j]; } } } bestIterMean=((nbIterationsCross-1)*bestIterMean + bestIter)/nbIterationsCross; printNewChild(nbParents); saveFitnessValue(); // remplacement d'un parent si la solution n'est pas trouvee if (nbEdgesConflict>0) { updatePopulation(); } }