72 lines
2.5 KiB
C++
72 lines
2.5 KiB
C++
#include "PopulationBreedingStrategy.h"
|
|
#include "VerhulstBreedingStrategy.h"
|
|
#include "NeutralEvolutionBreedStrat.h"
|
|
#include "../../individual/genome/strategies/InOutBreeding/InOutBreedingPopulationBreedingStrategy.h"
|
|
#include "../../individual/genome/strategies/KolchShindyal/KolchShindyalBreedingStrategy.h"
|
|
|
|
#include "../../processor/Settings.h"
|
|
//#include <vector>
|
|
#include <algorithm>
|
|
#include <iostream>
|
|
#include <cstdlib>
|
|
#include <ctime>
|
|
|
|
void PopulationBreedingStrategy::breed(Population* _pop){
|
|
float deathRate = Settings::NaturalDeathRate;//0.01f; // Êîýôôèöèåíò ñìåðòíîñòè
|
|
|
|
// Âûáèðàåì ëóçåðîâ è óäàëÿåì èõ
|
|
AsexualPopulation* pop = dynamic_cast<AsexualPopulation*>(_pop);
|
|
if(pop == NULL){
|
|
std::cerr<<"Wrong dynamic cast to AsexualPopulation\n";
|
|
return;
|
|
}
|
|
std::sort(pop->individs.begin(), pop->individs.end(), compareOnFitness);
|
|
int inds = pop->individs.size();
|
|
|
|
// Ïî èäåå, ñìåðòíîñòü íàäî ìîäåëèðîâàòü â ðàçäåëå SelectionAll
|
|
// à òóò íàäî êàêîé-òî ìåõàíèçì äèôôåðåíöèðîâàííîé ïëîäîâèòîñòè
|
|
// îïèñàòü
|
|
int toDie = int (inds * deathRate);
|
|
std::vector<Individual*>::iterator start = pop->individs.begin();
|
|
std::vector<Individual*>::iterator end = pop->individs.begin()+toDie;
|
|
//advance(end,toDie);
|
|
for(std::vector<Individual*>::iterator it = start; it != end; it++){
|
|
delete *it;
|
|
}
|
|
pop->individs.erase(start, end);
|
|
|
|
std::cout<<"PopulationBreedingStrategy::breed: "<<toDie<<" individs have died\n";
|
|
|
|
// Ðîæàåì íîâûõ îñîáåé
|
|
float birthRate = Settings::BirthRate;//0.02f;
|
|
int toBorn = int (inds * birthRate);
|
|
inds = pop->individs.size();
|
|
srand((unsigned int)time(NULL));
|
|
for(int i = 0; i < toBorn; i++) {
|
|
int male = rand() % inds;
|
|
int female = rand() % inds;
|
|
female = (female != male ? female : (female + 1) % inds);
|
|
|
|
Individual* ind = new Individual(*(pop->individs.at(male)), *(pop->individs.at(female)));
|
|
// Çäåñü ïî íåîáõîäèìîñòè äîáàâëÿåì íàñòðîéêè ñðåäû è ò.ä.
|
|
pop->individs.push_back(ind);
|
|
}
|
|
|
|
}
|
|
|
|
PopulationBreedingStrategy* PopulationBreedingStrategy::getInstance(std::string name){
|
|
if(name.compare("Verhulst") == 0){
|
|
return new VerhulstBreedingStrategy();
|
|
}
|
|
if(name.compare("inoutbreeding") == 0){
|
|
return new InOutBreedingPopulationBreedingStrategy();
|
|
}
|
|
if(name.compare("kolchan_shindyal_breeding") == 0){
|
|
return new KolchShindyalBreedingStrategy();
|
|
}
|
|
if(name.compare("neutral") == 0){
|
|
return new NeutralEvolutionBreedingStrategy();
|
|
}
|
|
return new PopulationBreedingStrategy();
|
|
}
|