// 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; }