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

D:/Projekt/ECF_trunk/ECF/binary/BinaryMutSimple.cpp

00001 #include "../ECF_base.h"
00002 #include "Binary.h"
00003 #include <cmath>
00004 
00005 namespace Binary
00006 {
00007 
00008 void BinaryMutSimple::registerParameters(StateP state)
00009 {
00010     myGenotype_->registerParameter(state, "mut.simple", (voidP) new double(0), ECF::DOUBLE);
00011     myGenotype_->registerParameter(state, "mut.simple.bitprob", (voidP) new double(0.001), ECF::DOUBLE);
00012 }
00013 
00014 
00015 bool BinaryMutSimple::initialize(StateP state)
00016 {
00017     voidP sptr = myGenotype_->getParameterValue(state, "mut.simple");
00018     probability_ = *((double*)sptr.get());
00019 
00020     sptr = myGenotype_->getParameterValue(state, "mut.simple.bitprob");
00021     bitProb_ = *((double*)sptr.get());
00022 
00023     bUseBitProb_ = false;
00024     if(myGenotype_->isParameterDefined(state, "mut.simple.bitprob"))
00025         bUseBitProb_ = true;
00026 
00027     return true;
00028 }
00029 
00030 
00031 bool BinaryMutSimple::mutate(GenotypeP gene)
00032 {
00033     Binary* bin = (Binary*) (gene.get());
00034 
00035     // invert all bits with 'bitProb_' probability
00036     if(bUseBitProb_) {
00037         for(uint i = 0; i < bin->variables.size(); i++)
00038             for(uint j = 0; j < bin->getNumBits(); j++)
00039                 if(state_->getRandomizer()->getRandomDouble() < bitProb_)
00040                     bin->variables[i][j] = !(bin->variables[i][j]);
00041     }
00042     // invert a single random bit in the genotype
00043     else {
00044         uint iBit = state_->getRandomizer()->getRandomInteger((uint) bin->getNumBits());
00045         uint dimension = state_->getRandomizer()->getRandomInteger((uint) bin->variables.size());
00046         bin->variables[dimension][iBit] = !(bin->variables[dimension][iBit]);
00047     }
00048 
00049     bin->update();
00050 
00051 /*
00052 
00053     //
00054     // izvedba jednostavnog lokalnog operatora
00055     //
00056     IndividualP myInd = state_->getAlgorithm()->mutation_->currentInd;
00057     FitnessP current, modified;
00058     double precision = 1 / pow(10., (int) bin->nDecimal_);
00059 
00060     current = state_->getAlgorithm()->evalOp_->evaluate(myInd);
00061 
00062     for(uint i = 0; i < bin->variables.size(); i++) {
00063         double newVal = bin->realValue[i];
00064 
00065         bin->realValue[i] += precision;
00066         modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
00067         if(modified->isBetterThan(current)) {
00068             current = modified;
00069             newVal = bin->realValue[i];
00070         }
00071 
00072         bin->realValue[i] -= 2 * precision;
00073         modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
00074         if(modified->isBetterThan(current)) {
00075             current = modified;
00076             newVal = bin->realValue[i];
00077         }
00078 
00079         bin->realValue[i] = newVal;
00080     }
00081 
00082     // update genotype according to realValue
00083     for(uint iVar = 0; iVar < bin->nDimension_; iVar++) {
00084         bin->decValue[iVar] = static_cast<long int> ((bin->realValue[iVar] - bin->minValue_) / (bin->maxValue_ - bin->minValue_) * bin->potention_);
00085 
00086         long dec = bin->decValue[iVar];
00087         for (int iBit = bin->nBits_; iBit > 0; dec = dec/2, iBit--) {
00088             bin->vBool_[iBit - 1] = (dec % 2) ? true:false;
00089         }
00090         bin->variables[iVar] = bin->vBool_;
00091 
00092         if(bin->bRounding_) {
00093             bin->realValue[iVar] = bin->round(bin->realValue[iVar], bin->nDecimal_);
00094         }
00095     }
00096 
00097 */
00098     return true;
00099 }
00100 
00101 }

Generated on Tue Nov 4 2014 13:04:30 for ECF by  doxygen 1.7.1