00001 #include "ECF_base.h"
00002
00003 const int RANDOM_GENOTYPE = 0;
00004 const int ALL_GENOTYPES = 1;
00005
00006
00010 double Mutation::getIndMutProb()
00011 {
00012 return indMutProb_;
00013 }
00014
00015
00019 double Mutation::setIndMutProb(double newProb)
00020 {
00021 if(newProb >= 0 && newProb <= 1)
00022 indMutProb_ = newProb;
00023 return indMutProb_;
00024 }
00025
00026
00030 void Mutation::registerParameters(StateP state)
00031 {
00032 state->getRegistry()->registerEntry("mutation.indprob", (voidP) new double(0.3), ECF::DOUBLE);
00033
00034 state->getRegistry()->registerEntry("mutation.genotypes", (voidP) new std::string("random"), ECF::STRING);
00035 state->getRegistry()->registerEntry("mutation.protected", (voidP) new std::string(""), ECF::STRING);
00036 }
00037
00038
00042 bool Mutation::initialize(StateP state)
00043 {
00044 state_ = state;
00045 protectedGenotypes_.clear();
00046 protectedGenotypes_.insert(protectedGenotypes_.begin(), operators.size(), false);
00047 opProb.clear();
00048
00049 voidP sptr = state->getRegistry()->getEntry("mutation.indprob");
00050 indMutProb_ = *((double*)sptr.get());
00051
00052 sptr = state->getRegistry()->getEntry("mutation.geneprob");
00053
00054
00055
00056
00057 sptr = state->getRegistry()->getEntry("mutation.genotypes");
00058 std::string mutGen = *((std::string*)sptr.get());
00059
00060 mutateGenotypes_ = RANDOM_GENOTYPE;
00061 if(mutGen == "random")
00062 mutateGenotypes_ = RANDOM_GENOTYPE;
00063 else if(mutGen == "all")
00064 mutateGenotypes_ = ALL_GENOTYPES;
00065 else
00066 ECF_LOG_ERROR(state, "Warning: invalid parameter value (key: mutation.genotypes)");
00067
00068
00069 std::stringstream ss;
00070 sptr = state->getRegistry()->getEntry("mutation.protected");
00071 ss << *((std::string*) sptr.get());
00072 uint genId;
00073 while(ss >> genId) {
00074 if(genId >= protectedGenotypes_.size()) {
00075 ECF_LOG_ERROR(state, "Error: invalid genotype index (key: mutation.protected)!");
00076 throw("");
00077 }
00078 protectedGenotypes_[genId] = true;
00079 }
00080
00081
00082 for(uint gen = 0; gen < operators.size(); gen++) {
00083 uint nOps = (uint) operators[gen].size();
00084
00085 if(nOps == 0) {
00086 protectedGenotypes_[gen] = true;
00087 std::vector<double> empty;
00088 opProb.push_back(empty);
00089 break;
00090 }
00091 for(uint i = 0; i < nOps; i++) {
00092 operators[gen][i]->state_ = state;
00093 operators[gen][i]->initialize(state);
00094 }
00095
00096 std::vector<double> probs(nOps);
00097 probs[0] = operators[gen][0]->probability_;
00098 for(uint i = 1; i < nOps; i++) {
00099 probs[i] = probs[i - 1] + operators[gen][i]->probability_;
00100 }
00101 if(probs[nOps - 1] == 0) {
00102 std::vector<double> none(1);
00103 none[0] = -1;
00104 opProb.push_back(none);
00105 } else {
00106 if(probs[nOps - 1] != 1) {
00107 double normal = probs[nOps - 1];
00108 ECF_LOG_ERROR(state, "Warning: " + operators[gen][0]->myGenotype_->getName() +
00109 " mutation operators: cumulative probability not equal to 1 (sum = " + dbl2str(normal) + ")");
00110 for(uint i = 0; i < probs.size(); i++)
00111 probs[i] /= normal;
00112 }
00113 opProb.push_back(probs);
00114 }
00115 }
00116 return true;
00117 }
00118
00119
00130 uint Mutation::mutation(const std::vector<IndividualP> &pool)
00131 {
00132 uint mutated = 0;
00133 for(uint i = 0; i < pool.size(); i++) {
00134 if(state_->getRandomizer()->getRandomDouble() <= indMutProb_) {
00135 mutated++;
00136 mutate(pool[i]);
00137 }
00138 }
00139
00140 return mutated;
00141 }
00142
00143
00150 bool Mutation::mutate(IndividualP ind)
00151 {
00152 ind->fitness->setInvalid();
00153
00154 state_->getContext()->mutatedIndividual = ind;
00155 ECF_LOG(state_, 5, "Mutating individual: " + ind->toString());
00156 currentInd = ind;
00157
00158
00159 if(mutateGenotypes_ == RANDOM_GENOTYPE) {
00160 uint iGenotype = state_->getRandomizer()->getRandomInteger((int)ind->size());
00161 if(protectedGenotypes_[iGenotype])
00162 return false;
00163
00164 uint iOperator;
00165 if(opProb[iGenotype][0] < 0)
00166 iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
00167 else {
00168 double random = state_->getRandomizer()->getRandomDouble();
00169 iOperator = 0;
00170 while(opProb[iGenotype][iOperator] < random)
00171 iOperator++;
00172 }
00173 operators[iGenotype][iOperator]->mutate(ind->at(iGenotype));
00174 }
00175
00176
00177 else if(mutateGenotypes_ == ALL_GENOTYPES) {
00178 for(uint iGenotype = 0; iGenotype < ind->size(); iGenotype++) {
00179 if(protectedGenotypes_[iGenotype])
00180 continue;
00181
00182 uint iOperator;
00183 if(opProb[iGenotype][0] < 0)
00184 iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
00185 else {
00186 double random = state_->getRandomizer()->getRandomDouble();
00187 iOperator = 0;
00188 while(opProb[iGenotype][iOperator] < random)
00189 iOperator++;
00190 }
00191 operators[iGenotype][0]->mutate(ind->at(iGenotype));
00192 }
00193 }
00194
00195 ECF_LOG(state_, 5, "Mutated individual: " + ind->toString());
00196
00197 return true;
00198 }
00199