#include "NeutralEvolutionBreedStrat.h" #include "../../individual/Phenotype.h" #include "../../processor/Settings.h" #include #include #include #include void NeutralEvolutionBreedingStrategy::breed(Population* _pop){ BisexualPopulation* pop = dynamic_cast(_pop); if(pop == NULL){ std::cerr<<"Wrong dynamic cast to BisexualPopulation\n"; return; } int numMales = pop->males.size(); int numFemales = pop->females.size(); int inds = numMales + numFemales; srand((unsigned int)time(NULL)); //srand(0); // Удаляем часть популяции int toDie = int (inds * deathRate); for(int i = 0; i < toDie; i++){ int indToDie = rand() % (inds-i); if(indToDie < numMales){ std::list::iterator it = pop->males.begin(); std::advance(it,indToDie); // it += indToDie; delete *it; pop->males.erase(it); numMales--; } else{ std::list::iterator it = pop->females.begin(); std::advance(it,indToDie-numMales); // it += indToDie; delete *it; pop->females.erase(it); numFemales--; } } //std::cout<<"PopulationBreedingStrategy::breed: "<males.size(); numFemales = pop->females.size(); inds = numMales + numFemales; for(int i = 0; i < toBorn; i++) { int fatherNum = rand() % numMales; int motherNum = rand() % numFemales; int gender = rand() % 2; std::list::iterator itFather = pop->males.begin(); std::advance(itFather, fatherNum); std::list::iterator itMother = pop->females.begin(); std::advance(itMother, motherNum); Genotype* genotype; double prob = Settings::ProbMtDNARecomb; if(prob > 0.0){ int invProb = (int)(1/prob); int recombine = rand() % invProb; if(recombine == 0){ // рекомбинируем мамин и папин геномы //std::cerr<<"Recomb\n"; ChromosomeRearrangementStrategy* recombinator = RecombinationStrategies::getInstance("D2015"); HaploidGenotype recombGenotype = recombinator->buildRecombinantGenotype( (*itFather)->getGenotype().getMotherGenome(), (*itMother)->getGenotype().getMotherGenome()); genotype = new Genotype(recombGenotype, recombGenotype); } else{ genotype = new Genotype( (*itMother)->getGenotype().getMotherGenome(), (*itMother)->getGenotype().getMotherGenome()); } } else{ genotype = new Genotype( (*itMother)->getGenotype().getMotherGenome(), (*itMother)->getGenotype().getMotherGenome()); // Можно сделать рекомбинационные стратегии папы и мамы разные } Phenotype* phenotype= new Phenotype((*itFather)->getPhenotype()); Individual* ind = new Individual(genotype, phenotype, 0, Individual::Gender(gender)); // Здесь по необходимости добавляем настройки среды и т.д. if(gender == 0){ pop->males.push_back(ind); } else{ pop->females.push_back(ind); } } }