#include "ChromosomeRearrangementStrategy.h" #include #include HaploidGenotype ChromosomeRearrangementStrategy::buildRecombinantGenotype(const Genotype* parentGenotype){ std::vector chromosomes; const std::vector* fGenome = &parentGenotype->fatherGenome.chromosomes; const std::vector* mGenome = &parentGenotype->motherGenome.chromosomes; //srand((unsigned int)time(NULL)); for (unsigned int i = 0; i < fGenome->size(); i++){ int flag = rand() % 2; const std::vector* first; const std::vector* second; if(flag == 0){ first = &fGenome->at(i).genes; second= &mGenome->at(i).genes; } else{ first = &mGenome->at(i).genes; second= &fGenome->at(i).genes; } Chromosome childChromosome(fGenome->at(i).name, *first); // Для каждой хромосомы выбираем свою "точку перегиба" int bound = rand() % first->size(); for(unsigned int j = bound; j < first->size(); j++){ childChromosome.genes.at(j) = second->at(j); } chromosomes.push_back(childChromosome); } // Тут также можно учесть, что могут быть негомологичные хромосомы (напр. Х и У), // которые будут наследоваться, определяя пол особи по геному return HaploidGenotype(chromosomes); } HaploidGenotype ChromosomeRearrangementStrategy:: buildRecombinantGenotype(const HaploidGenotype& fatherGenome, const HaploidGenotype& motherGenome){ std::vector chromosomes; const std::vector fGenome = fatherGenome.chromosomes; const std::vector mGenome = motherGenome.chromosomes; //srand((unsigned int)time(NULL)); for (unsigned int i = 0; i < fGenome.size(); i++){ int flag = rand() % 2; std::vector first; std::vector second; if(flag == 0){ first = fGenome.at(i).genes; second= mGenome.at(i).genes; } else{ first = mGenome.at(i).genes; second= fGenome.at(i).genes; } Chromosome childChromosome(fGenome.at(i).name, first); // Для каждой хромосомы выбираем свою "точку перегиба" int bound = rand() % first.size(); for(unsigned int j = bound; j < first.size(); j++){ childChromosome.genes.at(j) = second.at(j); } chromosomes.push_back(childChromosome); } // Тут также можно учесть, что могут быть негомологичные хромосомы (напр. Х и У), // которые будут наследоваться, определяя пол особи по геному return HaploidGenotype(chromosomes); } ChromosomeRearrangementStrategy* RecombinationStrategies::getInstance(std::string _name){ return new ChromosomeRearrangementStrategy(); }