3const int RANDOM_GENOTYPE = 0;
4const int ALL_GENOTYPES = 1;
21 if(newProb >= 0 && newProb <= 1)
32 state->getRegistry()->registerEntry(
"mutation.indprob", (voidP)
new double(0.3), ECF::DOUBLE,
33 "individual mutation probability (unless the algorithm overrides it) (default: 0.3)");
35 state->getRegistry()->registerEntry(
"mutation.genotypes", (voidP)
new std::string(
"random"), ECF::STRING,
36 "if there are multiple genotypes, which to mutate? 'random': a random one, all: mutate all (default: random)");
37 state->getRegistry()->registerEntry(
"mutation.protected", (voidP)
new std::string(
""), ECF::STRING,
38 "indexes of genotypes (separated by spaces) that are excluded (protected) from mutation (default: none)");
52 voidP sptr = state->getRegistry()->getEntry(
"mutation.indprob");
55 sptr = state->getRegistry()->getEntry(
"mutation.geneprob");
60 sptr = state->getRegistry()->getEntry(
"mutation.genotypes");
61 std::string mutGen = *((std::string*)sptr.get());
64 if(mutGen ==
"random")
66 else if(mutGen ==
"all")
69 ECF_LOG_ERROR(state,
"Warning: invalid parameter value (key: mutation.genotypes)");
73 sptr = state->getRegistry()->getEntry(
"mutation.protected");
74 ss << *((std::string*) sptr.get());
78 ECF_LOG_ERROR(state,
"Error: invalid genotype index (key: mutation.protected)!");
85 for(uint gen = 0; gen <
operators.size(); gen++) {
90 std::vector<double> empty;
94 for(uint i = 0; i < nOps; i++) {
99 std::vector<double> probs(nOps);
100 probs[0] =
operators[gen][0]->probability_;
101 for(uint i = 1; i < nOps; i++) {
102 probs[i] = probs[i - 1] +
operators[gen][i]->probability_;
104 if(probs[nOps - 1] == 0) {
105 std::vector<double> none(1);
109 if(probs[nOps - 1] != 1) {
110 double normal = probs[nOps - 1];
111 ECF_LOG_ERROR(state,
"Warning: " +
operators[gen][0]->myGenotype_->getName() +
112 " mutation operators: cumulative probability not equal to 1 (sum = " + dbl2str(normal) +
")");
113 for(uint i = 0; i < probs.size(); i++)
136 for(uint i = 0; i < pool.size(); i++) {
137 if(state_->getRandomizer()->getRandomDouble() <=
indMutProb_) {
155 ind->fitness->setInvalid();
157 state_->getContext()->mutatedIndividual = ind;
158 ECF_LOG(state_, 5,
"Mutating individual: " + ind->toString());
163 uint iGenotype = state_->getRandomizer()->getRandomInteger((
int)ind->size());
168 if(
opProb[iGenotype][0] < 0)
169 iOperator = state_->getRandomizer()->getRandomInteger((
int)
operators[iGenotype].size());
171 double random = state_->getRandomizer()->getRandomDouble();
173 while(
opProb[iGenotype][iOperator] < random)
176 operators[iGenotype][iOperator]->mutate(ind->at(iGenotype));
181 for(uint iGenotype = 0; iGenotype < ind->size(); iGenotype++) {
186 if(
opProb[iGenotype][0] < 0)
187 iOperator = state_->getRandomizer()->getRandomInteger((
int)
operators[iGenotype].size());
189 double random = state_->getRandomizer()->getRandomDouble();
191 while(
opProb[iGenotype][iOperator] < random)
194 operators[iGenotype][0]->mutate(ind->at(iGenotype));
198 ECF_LOG(state_, 5,
"Mutated individual: " + ind->toString());
double getIndMutProb()
Return current individual mutation probability.
double setIndMutProb(double)
Set new individual mutation probability (ignored if not [0, 1])
uint mutateGenotypes_
what genotypes (if more than one) to mutate (random, all)
std::vector< bool > protectedGenotypes_
protected (non-mutatable) genotypes flags
bool initialize(StateP)
Initialize all mutation operators of all active genotypes.
uint mutation(const std::vector< IndividualP > &)
Perform mutation on a pool of individuals.
bool mutate(IndividualP ind)
Mutate an individual.
double indMutProb_
mutation probability of an individual
void registerParameters(StateP)
Register mutation related but Genotype unrelated parameters.
std::vector< std::vector< double > > opProb
usage probabilities for each MutationOp operator
std::vector< std::vector< MutationOpP > > operators
vectors of mutation operators for each genotype