int main() { const unsigned mutations=2; Genome::set_mutations_at_cloning(mutations); std::cout<<"[Cloning with "<<mutations<<" mutations.]"<<std::endl; Genome A; std::cout<<"HEALTHY GENOME: "<<std::endl<<" "<<A<<std::endl; std::cout<<" Bad mutations within first 10 years = "<<A.bad_mutations(10)<<std::endl; Genome B=A.clone(); std::cout<<"AFTER 1st CLONING: "<<std::endl<<" "<<B<<std::endl; std::cout<<" Bad mutations within the whole Genome = "<<B.bad_mutations(Genome::max_age-1)<<std::endl; for (unsigned i=0; i<5; ++i) { A=B.clone(); B=A.clone(); } std::cout<<"AFTER 10 MORE CLONINGS: "<<std::endl<<" "<<A<<std::endl; std::cout<<" Bad mutations within first 5 years = "<<A.bad_mutations(5)<<std::endl; std::cout<<"AFTER 1 MORE CLONING: "<<std::endl<<" "<<B<<std::endl; std::cout<<" Bad mutations within first 10 years = "<<A.bad_mutations(10)<<std::endl; for (unsigned i=0; i<50; ++i) { A=B.clone(); B=A.clone(); } std::cout<<"AFTER 100 MORE CLONINGS: "<<std::endl<<" "<<A<<std::endl; std::cout<<" Bad mutations within first 2 years = "<<A.bad_mutations(2)<<std::endl; std::cout<<"AFTER 1 MORE CLONING: "<<std::endl<<" "<<B<<std::endl; std::cout<<" Bad mutations within first 2 years = "<<A.bad_mutations(2)<<std::endl; return 0; }
int main() { Genome::set_mutations_at_cloning(Genome::max_age-Genome::max_age/4); Genome gene; Genome gene2=gene.clone(); cout<<gene; cout<<gene2; }
void CalculatorGenome::crossoverSexually(const Genome& father, const Genome& mother, shared_ptr<Genome>& brother, shared_ptr<Genome>& sister) { shared_ptr<CalculatorGenome> bro = dynamic_pointer_cast<CalculatorGenome>(father.clone()); shared_ptr<CalculatorGenome> sis = dynamic_pointer_cast<CalculatorGenome>(mother.clone()); // find the cut points shared_ptr<CalculatorGenomeNode> broSubtree = bro->_chooseRandomNode(); shared_ptr<CalculatorGenomeNode> sisSubtree = sis->_chooseRandomNode(); shared_ptr<CalculatorGenomeNode> broSubtreeParent = bro->_findParent(broSubtree); shared_ptr<CalculatorGenomeNode> sisSubtreeParent = sis->_findParent(sisSubtree); // move the sister subtree over to the bro if (broSubtreeParent == NULL) { bro->_root = sisSubtree; } else { std::string broLabel = broSubtreeParent->findInput(broSubtree); broSubtreeParent->setInput(broLabel, sisSubtree); } // move the brother subtree over to the bro if (sisSubtreeParent == NULL) { sis->_root = broSubtree; } else { std::string sisLabel = sisSubtreeParent->findInput(sisSubtree); sisSubtreeParent->setInput(sisLabel, broSubtree); } brother = bro; brother->setScore(-1); sister = sis; sister->setScore(-1); }
void SteadyStateGA::nextGeneration() { double cross_random; double mutate_random; // sort the population bases on the parameters pop->sortPopulation(); vector<Genome *> *new_genomes = new vector<Genome *>; // we only replace replace_percentage of the population. // decide which percentage to replace...the lowest scores... (-1) fitnesses // reverse interate through and delete the genomes. // copy the top (pop_size * replacement_percentage) genomes into the new genome vector for(int i = 0; i < (int)(pop->getPopSize() * replace_percentage); ++i) { new_genomes->push_back(pop->getGenome(i)->clone()); } // now i need to add 1 - pop->getPopSize() * replace_percentage more genomes for(int i = 0; i < (int)(pop->getPopSize() - (pop->getPopSize() * replace_percentage)); ++i) { // need to select a dad and mom .. select... Genome *dad = pop->select(); Genome *mom = pop->select(); // then create child for crossover. Genome *child; cross_random = objRand->randomPercentage(); // if crossover does not happen make the children = to the parents if(cross_random <= crossover_percentage) { // perform crossover child = dad->crossover(*mom); } else { // make child and dad equal to each other child = dad->clone(); } mutate_random = objRand->randomPercentage(); // chance of mutation to each of the children if(mutate_random <= mutation_percentage) { // perform crossover child->mutate(); } if((int)new_genomes->size() < pop->getPopSize()) new_genomes->push_back(child); } ++current_generation; pop->setPopGenomes(new_genomes); init(); // init the new population }