00001 #include "ECF_base.h"
00002
00003 const int RANDOM_GENOTYPE = 0;
00004 const int ALL_GENOTYPES = 1;
00005
00006
00007 void Mutation::registerParameters(StateP state)
00008 {
00009 state->getRegistry()->registerEntry("mutation.indprob", (voidP) new double(0.3), ECF::DOUBLE);
00010 state->getRegistry()->registerEntry("mutation.geneprob", (voidP) new double(0.01), ECF::DOUBLE);
00011 state->getRegistry()->registerEntry("mutation.genotypes", (voidP) new std::string("random"), ECF::STRING);
00012 state->getRegistry()->registerEntry("mutation.protected", (voidP) new std::string(""), ECF::STRING);
00013 }
00014
00015
00016 bool Mutation::initialize(StateP state)
00017 {
00018 state_ = state;
00019 protectedGenotypes_.clear();
00020 protectedGenotypes_.insert(protectedGenotypes_.begin(), operators.size(), false);
00021 opProb.clear();
00022
00023 voidP sptr = state->getRegistry()->getEntry("mutation.indprob");
00024 indMutProb_ = *((double*)sptr.get());
00025
00026 sptr = state->getRegistry()->getEntry("mutation.geneprob");
00027 geneMutProb_ = *((double*)sptr.get());
00028 if(state->getRegistry()->isModified("mutation.geneprob") == false)
00029 geneMutProb_ = 0;
00030
00031 sptr = state->getRegistry()->getEntry("mutation.genotypes");
00032 std::string mutGen = *((std::string*)sptr.get());
00033
00034 mutateGenotypes_ = RANDOM_GENOTYPE;
00035 if(mutGen == "random")
00036 mutateGenotypes_ = RANDOM_GENOTYPE;
00037 else if(mutGen == "all")
00038 mutateGenotypes_ = ALL_GENOTYPES;
00039 else
00040 state->getLogger()->log(1, "Warning: invalid parameter value (key: mutation.genotypes)");
00041
00042
00043 std::stringstream ss;
00044 sptr = state->getRegistry()->getEntry("mutation.protected");
00045 ss << *((std::string*) sptr.get());
00046 uint genId;
00047 while(ss >> genId) {
00048 if(genId >= protectedGenotypes_.size()) {
00049 state->getLogger()->log(1, "Error: invalid genotype index (key: mutation.protected)!");
00050 throw("");
00051 }
00052 protectedGenotypes_[genId] = true;
00053 }
00054
00055
00056 for(uint gen = 0; gen < operators.size(); gen++) {
00057 uint nOps = (uint) operators[gen].size();
00058
00059 if(nOps == 0) {
00060 protectedGenotypes_[gen] = true;
00061 std::vector<double> empty;
00062 opProb.push_back(empty);
00063 break;
00064 }
00065 for(uint i = 0; i < nOps; i++) {
00066 operators[gen][i]->state_ = state;
00067 operators[gen][i]->initialize(state);
00068 }
00069
00070 std::vector<double> probs(nOps);
00071 probs[0] = operators[gen][0]->probability_;
00072 for(uint i = 1; i < nOps; i++) {
00073 probs[i] = probs[i - 1] + operators[gen][i]->probability_;
00074 }
00075 if(probs[nOps - 1] == 0) {
00076 std::vector<double> none(1);
00077 none[0] = -1;
00078 opProb.push_back(none);
00079 } else {
00080 if(probs[nOps - 1] != 1) {
00081 double normal = probs[nOps - 1];
00082 state->getLogger()->log(1, "Warning: " + operators[gen][0]->myGenotype_->getName() +
00083 " mutation operators: cumulative probability not equal to 1 (sum = " + dbl2str(normal) + ")");
00084 for(uint i = 0; i < probs.size(); i++)
00085 probs[i] /= normal;
00086 }
00087 opProb.push_back(probs);
00088 }
00089 }
00090 return true;
00091 }
00092
00093
00104 uint Mutation::mutation(const std::vector<IndividualP> &pool)
00105 {
00106 uint mutated = 0;
00107 for(uint i = 0; i < pool.size(); i++) {
00108 if(state_->getRandomizer()->getRandomDouble() <= indMutProb_) {
00109 mutated++;
00110 mutate(pool[i]);
00111 }
00112 }
00113
00114 return mutated;
00115 }
00116
00117
00124 bool Mutation::mutate(IndividualP ind)
00125 {
00126 ind->fitness->setInvalid();
00127 currentInd = ind;
00128
00129 if(mutateGenotypes_ == RANDOM_GENOTYPE) {
00130 uint iGenotype = state_->getRandomizer()->getRandomInteger((int)ind->size());
00131 if(protectedGenotypes_[iGenotype])
00132 return false;
00133
00134 uint iOperator;
00135 if(opProb[iGenotype][0] < 0)
00136 iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
00137 else {
00138 double random = state_->getRandomizer()->getRandomDouble();
00139 iOperator = 0;
00140 while(opProb[iGenotype][iOperator] < random)
00141 iOperator++;
00142 }
00143 operators[iGenotype][iOperator]->mutate(ind->at(iGenotype));
00144 }
00145 else if(mutateGenotypes_ == ALL_GENOTYPES) {
00146 for(uint iGenotype = 0; iGenotype < ind->size(); iGenotype++) {
00147 if(protectedGenotypes_[iGenotype])
00148 continue;
00149
00150 uint iOperator;
00151 if(opProb[iGenotype][0] < 0)
00152 iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
00153 else {
00154 double random = state_->getRandomizer()->getRandomDouble();
00155 iOperator = 0;
00156 while(opProb[iGenotype][iOperator] < random)
00157 iOperator++;
00158 }
00159 operators[iGenotype][0]->mutate(ind->at(iGenotype));
00160 }
00161 }
00162
00163 return true;
00164 }
00165