Ejemplo n.º 1
0
/**
 * \brief Mutate an individual.
 *
 * May mutate one or more genotypes in given individual.
 * Determines which MutationOp to use on each genotype.
 */
bool Mutation::mutate(IndividualP ind)
{	
	ind->fitness->setInvalid();
	// set mutation context
	state_->getContext()->mutatedIndividual = ind;
	ECF_LOG(state_, 5, "Mutating individual: " + ind->toString());
	currentInd = ind;

	// if mutating a random genotype
	if(mutateGenotypes_ == RANDOM_GENOTYPE) {
		uint iGenotype = state_->getRandomizer()->getRandomInteger((int)ind->size());
		if(protectedGenotypes_[iGenotype])
			return false;
		// choose operator
		uint iOperator;
		if(opProb[iGenotype][0] < 0)
			iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
		else {
			double random = state_->getRandomizer()->getRandomDouble();
			iOperator = 0;
			while(opProb[iGenotype][iOperator] < random)
				iOperator++;
		}
		operators[iGenotype][iOperator]->mutate(ind->at(iGenotype));
	}

	// if mutating all genotypes in the individual
	else if(mutateGenotypes_ == ALL_GENOTYPES) {
		for(uint iGenotype = 0; iGenotype < ind->size(); iGenotype++) {
			if(protectedGenotypes_[iGenotype])
				continue;
			// choose operator
			uint iOperator;
			if(opProb[iGenotype][0] < 0)
				iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
			else {
				double random = state_->getRandomizer()->getRandomDouble();
				iOperator = 0;
				while(opProb[iGenotype][iOperator] < random)
					iOperator++;
			}
			operators[iGenotype][0]->mutate(ind->at(iGenotype));
		}
	}

	ECF_LOG(state_, 5, "Mutated individual: " + ind->toString());

	return true;
}