Files
DEC/DEC_GUI/DEC-0.0/population/BreedingStrategies/NeutralEvolutionBreedStrat.cpp
2024-10-03 18:43:04 +07:00

99 lines
3.0 KiB
C++

#include "NeutralEvolutionBreedStrat.h"
#include "../../individual/Phenotype.h"
#include "../../processor/Settings.h"
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <ctime>
void NeutralEvolutionBreedingStrategy::breed(Population* _pop){
BisexualPopulation* pop = dynamic_cast<BisexualPopulation*>(_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<Individual*>::iterator it = pop->males.begin();
std::advance(it,indToDie); // it += indToDie;
delete *it;
pop->males.erase(it);
numMales--;
}
else{
std::list<Individual*>::iterator it = pop->females.begin();
std::advance(it,indToDie-numMales); // it += indToDie;
delete *it;
pop->females.erase(it);
numFemales--;
}
}
//std::cout<<"PopulationBreedingStrategy::breed: "<<toDie<<" individs have died\n";
// Ðîæàåì íîâûõ îñîáåé
int toBorn = int (inds * birthRate);
numMales = pop->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<Individual*>::iterator itFather = pop->males.begin();
std::advance(itFather, fatherNum);
std::list<Individual*>::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("dummy");
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);
}
}
}