// Use the ratio between the minimum and maximum to determine whether the // population has converged. This method will not work if the values cross // zero! // Note that this is significantly different than the definition (and the // bug-laden implementation) that was in version of GAlib prior to 2.4.5. // // For historical purposes, here is the old definition of this method: // // Use the ratio of the population mean divided by the population maximum to // determine whether the population has converged. If we are maximizing, then // check to see if the ratio exceeds the convergence. If we are minimizing, // then check to see if the ratio has dropped below the convergence. GABoolean GAGeneticAlgorithm::TerminateUponPopConvergence(GAGeneticAlgorithm & ga){ GABoolean val = gaFalse; if(ga.statistics().current(GAStatistics::Maximum) == 0) { return val; } float ratio = ga.statistics().current(GAStatistics::Minimum) / ga.statistics().current(GAStatistics::Maximum); if(ga.minimaxi() == GAGeneticAlgorithm::MINIMIZE) { if(ratio <= ga.pConvergence()) val = gaTrue; else val = gaFalse; } else { if(ratio >= ga.pConvergence()) val = gaTrue; else val = gaFalse; } return val; }
GABoolean GAGeneticAlgorithm::TerminateUponPopConvergence(GAGeneticAlgorithm & ga){ if(ga.statistics().current(GAStatistics::Mean) / ga.statistics().current(GAStatistics::Maximum) < ga.pConvergence()) return gaTrue; else return gaFalse; }
// Send the specified number of individuals from the current population to // the specified task. int SendMigration(int toid, GAGeneticAlgorithm& ga, int count) { int status = 0; status = pvm_initsend(PvmDataDefault); status = pvm_pkint(&count, 1, 1); for(int i=0; i<count && status>=0; i++) status = PackIndividual(ga.population().best(i)); status = pvm_send(toid, MSG_INCOMING_MIGRATION); return status; }
// Receive a bunch of individuals from a task. To do this, we clone the GA's // population, stuff the immigrants into the population, trash the worst // individuals to bring the population size back down to what it was, then // stick the population back into the GA. // This implementation is really inefficient, but you get the idea of how // to do this... int RecvMigration(GAGeneticAlgorithm& ga) { int status = 0; GAPopulation pop(ga.population()); GAGenome *tmpind = ga.population().individual(0).clone(); int count = 0; status = pvm_upkint(&count, 1, 1); for(int i=0; i<count && status>=0; i++) { status = UnpackIndividual(*tmpind); pop.add(*tmpind); } for(int j=0; j<count; j++) pop.destroy(); // default behavior is to destroy worst ga.population(pop); delete tmpind; return status; }
// If we are maximizing, then terminate when the convergence has exceeded the // specified convergence. If we are minimizing, then terminate when the // convergence has dropped below the specified convergence. GABoolean GAGeneticAlgorithm::TerminateUponConvergence(GAGeneticAlgorithm & ga){ GABoolean val = gaFalse; if(ga.minimaxi() == GAGeneticAlgorithm::MINIMIZE) { if(ga.convergence() == 0 || ga.convergence() > ga.pConvergence()) val = gaFalse; else val = gaTrue; } else { if(ga.convergence() < ga.pConvergence()) val = gaFalse; else val = gaTrue; } return val; }
// Here are a few termination functions that you can use. Terminators return // gaTrue if the algorithm should finish, gaFalse otherwise. GABoolean GAGeneticAlgorithm::TerminateUponGeneration(GAGeneticAlgorithm & ga){ return(ga.generation() < ga.nGenerations() ? gaFalse : gaTrue); }
void TwitterAnnounce::action(const GAGeneticAlgorithm & ga) { std::string generation = stringFrom(ga.generation()); // todo get from config announcer_->announceGeneration("Generation " + generation + " created! #megapolis", "","http://nynex.hydrogenproject.com/rate.php"); }