template <class ARRAY_TYPE> int GA3DArrayGenome<ARRAY_TYPE>::SwapMutator(GAGenome & c, float pmut) { GA3DArrayGenome<ARRAY_TYPE> &child=(GA3DArrayGenome<ARRAY_TYPE> &)c; register int n, i; if(pmut <= 0.0) return(0); float nMut = pmut * (float)(child.size()); int size = child.size()-1; if(nMut < 1.0){ // we have to do a flip test on each bit nMut = 0; for(i=size; i>=0; i--){ if(GAFlipCoin(pmut)){ child.GAArray<ARRAY_TYPE>::swap(i, GARandomInt(0, size)); nMut++; } } } else{ // only flip the number of bits we need to flip for(n=0; n<nMut; n++) child.GAArray<ARRAY_TYPE>::swap(GARandomInt(0, size), GARandomInt(0, size)); } return((int)nMut); }
template <class ARRAY_TYPE> int GA3DArrayAlleleGenome<ARRAY_TYPE>::FlipMutator(GAGenome & c, float pmut) { GA3DArrayAlleleGenome<ARRAY_TYPE> &child= (GA3DArrayAlleleGenome<ARRAY_TYPE> &)c; register int n, m, d, i, j, k; if(pmut <= 0.0) return(0); float nMut = pmut * (float)(child.size()); if(nMut < 1.0){ // we have to do a flip test on each bit nMut = 0; for(i=child.width()-1; i>=0; i--){ for(j=child.height()-1; j>=0; j--){ for(k=child.depth()-1; k>=0; k--){ if(GAFlipCoin(pmut)){ child.gene(i, j, k, child.alleleset().allele()); nMut++; } } } } } else{ // only flip the number of bits we need to flip for(n=0; n<nMut; n++){ m = GARandomInt(0, child.size()-1); d = child.height() * child.depth(); i = m / d; j = (m % d) / child.depth(); k = (m % d) % child.depth(); child.gene(i, j, k, child.alleleset().allele()); } } return((int)nMut); }
int GA3DBinaryStringGenome::FlipMutator(GAGenome & c, float pmut) { GA3DBinaryStringGenome &child=(GA3DBinaryStringGenome &)c; register int n, m, i, j, k, d; if(pmut <= 0.0) return(0); float nMut = pmut * (float)(child.size()); if(nMut < 1.0){ // we have to do a flip test on each bit nMut = 0; for(i=child.width()-1; i>=0; i--){ for(j=child.height()-1; j>=0; j--){ for(k=child.depth()-1; k>=0; k--){ if(GAFlipCoin(pmut)){ child.gene(i, j, k, ((child.gene(i,j,k) == 0) ? 1 : 0)); nMut++; } } } } } else{ // only flip the number of bits we need to flip for(n=0; n<nMut; n++){ m = GARandomInt(0, child.size()-1); d = child.height() * child.depth(); i = m / d; j = (m % d) / child.depth(); k = (m % d) % child.depth(); child.gene(i, j, k, ((child.gene(i,j,k) == 0) ? 1 : 0)); } } return((int)nMut); }
// Randomly pick bits in the bit string then flip their values. We try to be // smart about the number of times we have to call any random functions. If // the requested likliehood is small enough (relative to the number of bits in // the genome) then we must do a weighted coin toss on each bit in the genome. // Otherwise, we just do the expected number of flips (note that this will not // guarantee the requested mutation rate, but it will come close when the // length of the bit string is long enough). int BitStringGenome::UniformMutator(GAGenome & c, float pmut) { BitStringGenome &genome=(BitStringGenome &)c; register int n, i; if(pmut <= 0.0) return(0); float nMut = pmut * (float)(genome.length()); if(nMut < 1.0){ // we have to do a flip test on each bit nMut = 0; for(i=genome.length()-1; i>=0; i--){ if(GAFlipCoin(pmut)){ genome.gene(i, genome.gene(i) ? 0 : 1); nMut++; } } } else{ // only flip the number of bits we need to flip for(n=1; n<nMut; n++){ i = GARandomInt(0, genome.length()-1); // the index of the bit to flip genome.gene(i, genome.gene(i) ? 0 : 1); } } return (int)nMut; }
// Evolve a new generation of genomes. A steady-state GA has no 'old' // and 'new' populations - we pick from the current population and replace its // members with the new ones we create. We replace the worst members of the // preceeding population. If a genome in the tmp population is worse than // one in the main population, the genome in the main population will be // replaced regardless of its better score. void GASteadyStateGA::step() { int i, mut, c1, c2; GAGenome *mom, *dad; // tmp holders for selected genomes // Generate the individuals in the temporary population from individuals in // the main population. for(i=0; i<tmpPop->size()-1; i+=2){ // takes care of odd population mom = &(pop->select()); dad = &(pop->select()); stats.numsel += 2; // keep track of number of selections c1 = c2 = 0; if(GAFlipCoin(pCrossover())){ stats.numcro += (*scross)(*mom, *dad, &tmpPop->individual(i), &tmpPop->individual(i+1)); c1 = c2 = 1; } else{ tmpPop->individual( i ).copy(*mom); tmpPop->individual(i+1).copy(*dad); } stats.nummut += (mut = tmpPop->individual( i ).mutate(pMutation())); if(mut > 0) c1 = 1; stats.nummut += (mut = tmpPop->individual(i+1).mutate(pMutation())); if(mut > 0) c2 = 1; stats.numeval += c1 + c2; } if(tmpPop->size() % 2 != 0){ // do the remaining population member mom = &(pop->select()); dad = &(pop->select()); stats.numsel += 2; // keep track of number of selections c1 = 0; if(GAFlipCoin(pCrossover())){ stats.numcro += (*scross)(*mom, *dad, &tmpPop->individual(i), (GAGenome*)0); c1 = 1; } else{ if(GARandomBit()) tmpPop->individual( i ).copy(*mom); else tmpPop->individual( i ).copy(*dad); } stats.nummut += (mut = tmpPop->individual( i ).mutate(pMutation())); if(mut > 0) c1 = 1; stats.numeval += c1; } // Replace the worst genomes in the main population with all of the individuals // we just created. Notice that we invoke the population's add member with a // genome pointer rather than reference. This way we don't force a clone of // the genome - we just let the population take over. Then we take it back by // doing a remove then a replace in the tmp population. for(i=0; i<tmpPop->size(); i++) pop->add(&tmpPop->individual(i)); pop->evaluate(); // get info about current pop for next time pop->scale(); // remind the population to do its scaling // the individuals in tmpPop are all owned by pop, but tmpPop does not know // that. so we use replace to take the individuals from the pop and stick // them back into tmpPop for(i=0; i<tmpPop->size(); i++) tmpPop->replace(pop->remove(GAPopulation::WORST, GAPopulation::SCALED), i); stats.numrep += tmpPop->size(); stats.update(*pop); // update the statistics by one generation }
// Evolve a new generation of genomes. A steady-state GA has no 'old' // and 'new' populations - we pick from the current population and replace its // members with the new ones we create. We generate either one or two children // each 'generation'. The replacement strategy is set by the GA. void GAIncrementalGA::step() { int mut, c1, c2; GAGenome *mom, *dad; // tmp holders for selected genomes mom = &(pop->select()); dad = &(pop->select()); stats.numsel += 2; // keep track of the number of selections if(noffspr == 1){ c1 = 0; if(GAFlipCoin(pCrossover())){ stats.numcro += (*scross)(*mom, *dad, child1, (GAGenome*)0); c1 = 1; } else{ if(GARandomBit()) child1->copy(*mom); else child1->copy(*dad); } stats.nummut += (mut = child1->mutate(pMutation())); if(mut > 0) c1 = 1; stats.numeval += c1; if(rs == PARENT) child1 = pop->replace(child1, mom); else if(rs == CUSTOM) child1 = pop->replace(child1, &(rf(*child1, *pop))); else child1 = pop->replace(child1, rs); stats.numrep += 1; } else{ c1 = c2 = 0; if(GAFlipCoin(pCrossover())){ stats.numcro += (*scross)(*mom, *dad, child1, child2); c1 = c2 = 1; } else{ child1->copy(*mom); child2->copy(*dad); } stats.nummut += (mut = child1->mutate(pMutation())); if(mut > 0) c1 = 1; stats.nummut += (mut = child2->mutate(pMutation())); if(mut > 0) c2 = 1; stats.numeval += c1 + c2; if(rs == PARENT){ child1 = pop->replace(child1, mom); if(mom == dad) // this is a possibility, if so do worst child2 = pop->replace(child2, GAPopulation::WORST); else child2 = pop->replace(child2, dad); } else if(rs == CUSTOM){ child1 = pop->replace(child1, &(rf(*child1, *pop))); child2 = pop->replace(child2, &(rf(*child2, *pop))); } else{ child1 = pop->replace(child1, rs); child2 = pop->replace(child2, rs); } stats.numrep += 2; } pop->evaluate(gaTrue); // allow pop-based evaluators to do their thing stats.update(*pop); // update the statistics for this generation }
// Evolve a new generation of genomes. When we start this routine, pop // contains the current generation. When we finish, pop contains the new // generation and oldPop contains the (no longer) current generation. The // previous old generation is lost. We don't deallocate any memory, we just // reset the contents of the genomes. // The selection routine must return a pointer to a genome from the old // population. void GASimpleGA::step() { int i, mut, c1, c2; GAGenome *mom, *dad; // tmp holders for selected genomes GAPopulation *tmppop; // Swap the old population with the new pop. tmppop = oldPop; // When we finish the ++ we want the newly oldPop = pop; // generated population to be current (for pop = tmppop; // references to it from member functions). // Generate the individuals in the temporary population from individuals in // the main population. for(i=0; i<pop->size()-1; i+=2){ // takes care of odd population mom = &(oldPop->select()); dad = &(oldPop->select()); stats.numsel += 2; // keep track of number of selections c1 = c2 = 0; if(GAFlipCoin(pCrossover())){ stats.numcro += (*scross)(*mom, *dad, &pop->individual(i), &pop->individual(i+1)); c1 = c2 = 1; } else{ pop->individual( i ).copy(*mom); pop->individual(i+1).copy(*dad); } stats.nummut += (mut = pop->individual( i ).mutate(pMutation())); if(mut > 0) c1 = 1; stats.nummut += (mut = pop->individual(i+1).mutate(pMutation())); if(mut > 0) c2 = 1; stats.numeval += c1 + c2; } if(pop->size() % 2 != 0){ // do the remaining population member mom = &(oldPop->select()); dad = &(oldPop->select()); stats.numsel += 2; // keep track of number of selections c1 = 0; if(GAFlipCoin(pCrossover())){ stats.numcro += (*scross)(*mom, *dad, &pop->individual(i), (GAGenome*)0); c1 = 1; } else{ if(GARandomBit()) pop->individual( i ).copy(*mom); else pop->individual( i ).copy(*dad); } stats.nummut += (mut = pop->individual( i ).mutate(pMutation())); if(mut > 0) c1 = 1; stats.numeval += c1; } // Pass mpi_tasks and mpi_rank to the population clas pop->mpi_tasks(vmpi_tasks); pop->mpi_rank(vmpi_rank); stats.numrep += pop->size(); pop->evaluate(gaTrue); // get info about current pop for next time // If we are supposed to be elitist, carry the best individual from the old // population into the current population. Be sure to check whether we are // supposed to minimize or maximize. if(minimaxi() == GAGeneticAlgorithm::MAXIMIZE) { if(el && oldPop->best().score() > pop->best().score()) oldPop->replace(pop->replace(&(oldPop->best()), GAPopulation::WORST), GAPopulation::BEST); } else { if(el && oldPop->best().score() < pop->best().score()) oldPop->replace(pop->replace(&(oldPop->best()), GAPopulation::WORST), GAPopulation::BEST); } stats.update(*pop); // update the statistics by one generation }
// To evolve the genetic algorithm, we loop through all of our populations and // evolve each one of them. Then allow the migrator to do its thing. Assumes // that the tmp pop is at least as big as the largest nrepl that we'll use. // The master population maintains the best n individuals from each of the // populations, and it is based on those that we keep the statistics for the // entire genetic algorithm run. void GADemeGA::step() { int i, mut, c1, c2; GAGenome *mom, *dad; float pc; if(!scross) pc = 0.0; else pc = pCrossover(); for(unsigned int ii=0; ii<npop; ii++) { for(i=0; i<nrepl[ii]-1; i+=2){ // takes care of odd population mom = &(deme[ii]->select()); dad = &(deme[ii]->select()); pstats[ii].numsel += 2; c1 = c2 = 0; if(GAFlipCoin(pc)){ pstats[ii].numcro += (*scross)(*mom, *dad, &tmppop->individual(i), &tmppop->individual(i+1)); c1 = c2 = 1; } else{ tmppop->individual( i ).copy(*mom); tmppop->individual(i+1).copy(*dad); } pstats[ii].nummut += (mut=tmppop->individual( i ).mutate(pMutation())); if(mut > 0) c1 = 1; pstats[ii].nummut += (mut=tmppop->individual(i+1).mutate(pMutation())); if(mut > 0) c2 = 1; pstats[ii].numeval += c1 + c2; } if(nrepl[ii] % 2 != 0){ // do the remaining population member mom = &(deme[ii]->select()); dad = &(deme[ii]->select()); pstats[ii].numsel += 2; c1 = 0; if(GAFlipCoin(pc)){ pstats[ii].numcro += (*scross)(*mom, *dad, &tmppop->individual(i), (GAGenome*)0); c1 = 1; } else{ if(GARandomBit()) tmppop->individual(i).copy(*mom); else tmppop->individual(i).copy(*dad); } pstats[ii].nummut += (mut=tmppop->individual(i).mutate(pMutation())); if(mut > 0) c1 = 1; pstats[ii].numeval += c1; } for(i=0; i<nrepl[ii]; i++) deme[ii]->add(&tmppop->individual(i)); deme[ii]->evaluate(); deme[ii]->scale(); for(i=0; i<nrepl[ii]; i++) tmppop->replace(deme[ii]->remove(GAPopulation::WORST, GAPopulation::SCALED), i); pstats[ii].numrep += nrepl[ii]; } migrate(); for(unsigned int jj=0; jj<npop; jj++) { deme[jj]->evaluate(); pstats[jj].update(*deme[jj]); } stats.numsel = stats.numcro = stats.nummut = stats.numrep = stats.numeval=0; for(unsigned int kk=0; kk<npop; kk++) { pop->individual(kk).copy(deme[kk]->best()); stats.numsel += pstats[kk].numsel; stats.numcro += pstats[kk].numcro; stats.nummut += pstats[kk].nummut; stats.numrep += pstats[kk].numrep; stats.numeval += pstats[kk].numeval; } pop->touch(); stats.update(*pop); for(unsigned int ll=0; ll<npop; ll++) stats.numpeval += pstats[ll].numpeval; }