Exemple #1
0
/**
 * Asexual reproduction:
 * 
 * I just randomly chose features of one or the other parent by a probabillity proportional
 * to the parents fitness function
 * 
 * The combination strategy is used in the make offspring method to create a child.
 */
individual individual::combine1(individual indiv){
	//individual child(indiv.getFeatureVector());
	std::vector<float> fv;
	std::vector<float> indivFV(indiv.getFeatureVector());
	
	std::uniform_real_distribution<> dint(0,1);
	
	float O1 = this->getObjectiveFunction();
	float O2 = indiv.getObjectiveFunction();
	
	float fittnessBias = O1>O2 ? 0.75 : 0.25;
	
	for(unsigned int i=0;i<ndim;i++){
		indivFV.at(i) = dint(this->rngEngine)<fittnessBias ? (this->featureVector).at(i) : (indiv.getFeatureVector()).at(i);
	}
	
	
	return individual(fv);
}
	individual combine(individual i1, individual i2){
		std::cerr<<"Warning: for sex aware objective function set the sexAware \n "
				 <<"boolean flag of the combination strategy to true! Otherwise\n"
				 <<" the sex of the individual has no effect."<<std::endl;
		std::vector<std::pair<float, float> > newGenotype(i2.getNdim());

		/* the get chromosome method gives a random selection of allele genes
		  * which is not neccesarrily the gene sequence that has been represented in the
		  * phenotype of the individual!
		  *
		  * This simulates the process of meiosis - an integral part of
		  * sexual reproduction.
		  *
		  * Possible improvements could inlcude gene methylation simulation and other
		  * acquired genetic traits other than simple mutation.
		  *
		  * When the strategy is sex aware the returned individual is returned with the objective function
		  * already calculated in order to reduce the number of objective function calculations.
		  *
		  * The objective function value of the male individual is estimated as
		  * the best objective function of his mother. That allows for possible hidden beneficial traits to
		  * pass to next generations and not wiped out by evolutionary pressure.
		  *
		  */

		std::vector<float > v1 = i1.getChromosome();
		std::vector<float > v2 = i2.getChromosome();
		
		std::uniform_real_distribution<float> dre(0.0,1.0);

		assert(REset == true);

		for(unsigned int i=0;i<i1.getNdim();i++){
			float rn = dre(re);
			std::pair<float, float> newGene;
			newGene.first = rn>0.5?v1.at(i):v2.at(i);
			newGene.second = rn>0.5?v2.at(i):v1.at(i); // saving the non expressed genes in the non expressed chromosome

			//Mutation in reality is most probable to happen during meiosis - here it goes:
			if(MRSet){
				//std::cout<<"mutating gene gene after recombination"<<std::endl;
				float rn2 = dre(re);
				newGene.first += (0.5-rn2)*mutRates.at(i);
				newGene.second += (0.5-rn2)*mutRates.at(i);
			}
			newGenotype.at(i) = newGene;
		}

		individual indd(newGenotype);
		indd.setSex(dre(re)>0.5?MALE:FEMALE);
		if(sexAware){
			if(indd.getSex()==MALE){
				/*
				 * If the created individual is a Male (objective function should not be calculated) then
				 * he inherits the objective function of his mother in order to reduce the objective function calcu
				 * lations and increase population variability. Hopefully beneficial genes are surviving generations
				 * when they are part of less fit individuals that way.
				 */
				indd.setEstimatedObjectiveFunction(i1.getSex()==FEMALE?i1.getObjectiveFunction():i2.getObjectiveFunction());
			}
		}
		return indd;
	}