85 lines
2.8 KiB
C++
85 lines
2.8 KiB
C++
#include "InOutBreedingPopulationBreedingStrategy.h"
|
|
#include "../../../../processor/Settings.h"
|
|
#include "../../../Phenotype.h"
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
|
|
void InOutBreedingPopulationBreedingStrategy::breed(Population *_pop){
|
|
// N = const
|
|
|
|
AsexualPopulation* pop = dynamic_cast<AsexualPopulation*>(_pop);
|
|
if(pop == NULL){
|
|
std::cerr<<"Wrong dynamic cast to AsexualPopulation\n";
|
|
return;
|
|
}
|
|
|
|
srand((unsigned int)time(NULL));
|
|
|
|
int offsprings = Settings::OffspringsMean;
|
|
std::vector<Individual*> newIndivids;
|
|
|
|
long int inds;
|
|
for(inds = pop->individs.size();inds >=2; inds -= 2){
|
|
int motherIndex = rand()%inds;
|
|
int fatherIndex = rand()%inds;
|
|
if(fatherIndex == motherIndex){
|
|
fatherIndex = (fatherIndex+1)%inds;
|
|
}
|
|
|
|
|
|
// Äîáàâëÿåì ïîòîìêîâ â ýôôåêòèâíóþ ïîïóëÿöèþ
|
|
for(int i = 0; i < offsprings; i++){
|
|
Individual* ind = new Individual(*(pop->individs.at(motherIndex)), *(pop->individs.at(fatherIndex)));
|
|
|
|
// Ïðîâåðÿåì åãî ãåíîòèï è ïðèñïîñîáëåííîñòü
|
|
ind->calculatePhenotype();
|
|
Phenotype phen = ind->getPhenotype();
|
|
float coadapt = phen.getTraitByName("coadaptive").getTraitValueCont();
|
|
float disease = phen.getTraitByName("disease").getTraitValueCont();
|
|
|
|
if(disease > Settings::DiseaseThreshold){ // Ïî áîëåçíè ñðàçó îòñåêàåì
|
|
float probSurvive = 1.f;
|
|
if(coadapt < Settings::CoadaptiveThreshold){ // TODO: Áîëåå íåïðåðûâíî ñäåëàòü ïîçæå
|
|
probSurvive = Settings::CoadaptiveMinimumP;
|
|
}
|
|
else{
|
|
probSurvive = (1-Settings::CoadaptiveMinimumP)/(Settings::CoadaptiveMaximumTrait-Settings::CoadaptiveThreshold);
|
|
probSurvive *= coadapt - Settings::CoadaptiveMaximumTrait;
|
|
probSurvive += 1;
|
|
}
|
|
int order = 1000;
|
|
int prob = rand()%order;
|
|
if(prob < order*probSurvive){
|
|
// Çäåñü ïî íåîáõîäèìîñòè äîáàâëÿåì íàñòðîéêè ñðåäû è ò.ä.
|
|
newIndivids.push_back(ind);
|
|
}
|
|
}
|
|
} // (END) for(int i = 0; i < offsprings; i++)
|
|
|
|
// Óäàëÿåì ðîäèòåëåé èç ýôôåêòèâíîé ïîïóëÿöèè
|
|
delete pop->individs.at(motherIndex);
|
|
delete pop->individs.at(fatherIndex);
|
|
if(motherIndex > fatherIndex){
|
|
pop->individs.erase(pop->individs.begin()+motherIndex);
|
|
pop->individs.erase(pop->individs.begin()+fatherIndex);
|
|
}
|
|
else{
|
|
pop->individs.erase(pop->individs.begin()+fatherIndex);
|
|
pop->individs.erase(pop->individs.begin()+motherIndex);
|
|
}
|
|
|
|
// Óäàëÿåì îñòàâøèõñÿ ðîäèòåëåé
|
|
if(newIndivids.size() >= Settings::KMaxParameter){
|
|
for(unsigned int i = 0; i < pop->individs.size(); i++){
|
|
delete pop->individs.at(i);
|
|
}
|
|
pop->individs.clear();
|
|
break;
|
|
}
|
|
} // (END) for(inds = pop->individs.size();inds >=2; inds -= 2)
|
|
|
|
// Äåëàåì ïîòîìêîâ ýôôåêòèâíîé ïîïóëÿöèåé
|
|
pop->individs.insert(pop->individs.begin(), newIndivids.begin(), newIndivids.end());
|
|
} |