• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/ECF/Mutation.cpp

00001 #include "ECF_base.h"
00002 
00003 const int RANDOM_GENOTYPE = 0;
00004 const int ALL_GENOTYPES = 1;
00005 
00006 
00010 void Mutation::registerParameters(StateP state)
00011 {
00012     state->getRegistry()->registerEntry("mutation.indprob", (voidP) new double(0.3), ECF::DOUBLE);
00013 //  state->getRegistry()->registerEntry("mutation.geneprob", (voidP) new double(0.01), ECF::DOUBLE);
00014     state->getRegistry()->registerEntry("mutation.genotypes", (voidP) new std::string("random"), ECF::STRING);
00015     state->getRegistry()->registerEntry("mutation.protected", (voidP) new std::string(""), ECF::STRING);
00016 }
00017 
00018 
00022 bool Mutation::initialize(StateP state)
00023 {
00024     state_ = state;
00025     protectedGenotypes_.clear();
00026     protectedGenotypes_.insert(protectedGenotypes_.begin(), operators.size(), false);
00027     opProb.clear();
00028 
00029     voidP sptr = state->getRegistry()->getEntry("mutation.indprob");
00030     indMutProb_ = *((double*)sptr.get());
00031 
00032     sptr = state->getRegistry()->getEntry("mutation.geneprob");
00033 //  geneMutProb_ = *((double*)sptr.get());
00034 //  if(state->getRegistry()->isModified("mutation.geneprob") == false)
00035 //      geneMutProb_ = 0;
00036 
00037     sptr = state->getRegistry()->getEntry("mutation.genotypes");
00038     std::string mutGen = *((std::string*)sptr.get());
00039 
00040     mutateGenotypes_ = RANDOM_GENOTYPE;
00041     if(mutGen == "random")
00042         mutateGenotypes_ = RANDOM_GENOTYPE;
00043     else if(mutGen == "all")
00044         mutateGenotypes_ = ALL_GENOTYPES;
00045     else
00046         ECF_LOG_ERROR(state, "Warning: invalid parameter value (key: mutation.genotypes)");
00047 
00048     // read protected genotypes
00049     std::stringstream ss;
00050     sptr = state->getRegistry()->getEntry("mutation.protected");
00051     ss << *((std::string*) sptr.get());
00052     uint genId;
00053     while(ss >> genId) {    // read all the data from string
00054         if(genId >= protectedGenotypes_.size()) {
00055             ECF_LOG_ERROR(state, "Error: invalid genotype index (key: mutation.protected)!");
00056             throw("");
00057         }
00058         protectedGenotypes_[genId] = true;
00059     }
00060 
00061     // initialize operators for all genotypes
00062     for(uint gen = 0; gen < operators.size(); gen++) {
00063         uint nOps = (uint) operators[gen].size();
00064         // if the genotype doesn't define mutation operators
00065         if(nOps == 0) {
00066             protectedGenotypes_[gen] = true;
00067             std::vector<double> empty;
00068             opProb.push_back(empty);
00069             break;
00070         }
00071         for(uint i = 0; i < nOps; i++) {
00072             operators[gen][i]->state_ = state;
00073             operators[gen][i]->initialize(state);
00074         }
00075         // calculate cumulative operator probabilities
00076         std::vector<double> probs(nOps);
00077         probs[0] = operators[gen][0]->probability_;
00078         for(uint i = 1; i < nOps; i++) {
00079             probs[i] = probs[i - 1] + operators[gen][i]->probability_;
00080         }
00081         if(probs[nOps - 1] == 0) {
00082             std::vector<double> none(1);
00083             none[0] = -1;
00084             opProb.push_back(none);
00085         } else {
00086             if(probs[nOps - 1] != 1) {
00087                 double normal = probs[nOps - 1];
00088                 ECF_LOG_ERROR(state, "Warning: " + operators[gen][0]->myGenotype_->getName() +
00089                     " mutation operators: cumulative probability not equal to 1 (sum = " + dbl2str(normal) + ")");
00090                 for(uint i = 0; i < probs.size(); i++)
00091                     probs[i] /= normal;
00092             }
00093             opProb.push_back(probs);
00094         }
00095     }
00096     return true;
00097 }
00098 
00099 
00110 uint Mutation::mutation(const std::vector<IndividualP> &pool)
00111 {
00112     uint mutated = 0;
00113     for(uint i = 0; i < pool.size(); i++) {
00114         if(state_->getRandomizer()->getRandomDouble() <= indMutProb_) {
00115             mutated++;
00116             mutate(pool[i]);
00117         }
00118     }
00119 
00120     return mutated;
00121 }
00122 
00123 
00130 bool Mutation::mutate(IndividualP ind)
00131 {   
00132     ind->fitness->setInvalid();
00133     // set mutation context
00134     state_->getContext()->mutatedIndividual = ind;
00135     ECF_LOG(state_, 5, "Mutating individual: " + ind->toString());
00136     currentInd = ind;
00137 
00138     // if mutating a random genotype
00139     if(mutateGenotypes_ == RANDOM_GENOTYPE) {
00140         uint iGenotype = state_->getRandomizer()->getRandomInteger((int)ind->size());
00141         if(protectedGenotypes_[iGenotype])
00142             return false;
00143         // choose operator
00144         uint iOperator;
00145         if(opProb[iGenotype][0] < 0)
00146             iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
00147         else {
00148             double random = state_->getRandomizer()->getRandomDouble();
00149             iOperator = 0;
00150             while(opProb[iGenotype][iOperator] < random)
00151                 iOperator++;
00152         }
00153         operators[iGenotype][iOperator]->mutate(ind->at(iGenotype));
00154     }
00155 
00156     // if mutating all genotypes in the individual
00157     else if(mutateGenotypes_ == ALL_GENOTYPES) {
00158         for(uint iGenotype = 0; iGenotype < ind->size(); iGenotype++) {
00159             if(protectedGenotypes_[iGenotype])
00160                 continue;
00161             // choose operator
00162             uint iOperator;
00163             if(opProb[iGenotype][0] < 0)
00164                 iOperator = state_->getRandomizer()->getRandomInteger((int)operators[iGenotype].size());
00165             else {
00166                 double random = state_->getRandomizer()->getRandomDouble();
00167                 iOperator = 0;
00168                 while(opProb[iGenotype][iOperator] < random)
00169                     iOperator++;
00170             }
00171             operators[iGenotype][0]->mutate(ind->at(iGenotype));
00172         }
00173     }
00174 
00175     ECF_LOG(state_, 5, "Mutated individual: " + ind->toString());
00176 
00177     return true;
00178 }
00179 

Generated on Thu Oct 6 2011 13:41:01 for ECF by  doxygen 1.7.1