00001 #include "../ECF_base.h"
00002 #include "Binary.h"
00003 #include <cmath>
00004
00005 uint extraTime;
00006
00007 void BinaryMutSimple::registerParameters(StateP state)
00008 {
00009 myGenotype_->registerParameter(state, "mut.simple", (voidP) new double(0), DOUBLE);
00010 myGenotype_->registerParameter(state, "mut.simple.bitprob", (voidP) new double(0.001), DOUBLE);
00011 myGenotype_->registerParameter(state, "mut.simple.extra", (voidP) new uint(1), UINT);
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 sptr = myGenotype_->getParameterValue(state, "mut.simple.extra");
00028 extraTime = *((uint*)sptr.get());
00029
00030 return true;
00031 }
00032
00033
00034 bool BinaryMutSimple::mutate(GenotypeP gene)
00035 {
00036 Binary* bin = (Binary*) (gene.get());
00037
00038
00039
00040
00041 bool bit = true;
00042 for(uint i = 0; i < extraTime * bin->variables.size() * bin->getNumBits(); i++)
00043 if(state_->getRandomizer()->getRandomDouble() < bitProb_)
00044 bit = !bit;
00045
00046
00047
00048 if(bUseBitProb_) {
00049 for(uint i = 0; i < bin->variables.size(); i++)
00050 for(uint j = 0; j < bin->getNumBits(); j++)
00051 if(state_->getRandomizer()->getRandomDouble() < bitProb_)
00052 bin->variables[i][j] = !(bin->variables[i][j]);
00053 }
00054
00055 else {
00056 uint iBit = state_->getRandomizer()->getRandomInteger((uint) bin->getNumBits());
00057 uint dimension = state_->getRandomizer()->getRandomInteger((uint) bin->variables.size());
00058 bin->variables[dimension][iBit] = !(bin->variables[dimension][iBit]);
00059 }
00060
00061 bin->update();
00062
00063
00064
00065
00066 IndividualP myInd = state_->getAlgorithm()->mutation_->currentInd;
00067 FitnessP current, modified;
00068 double precision = 1 / pow(10., (int) bin->nDecimal_);
00069
00070 current = state_->getAlgorithm()->evalOp_->evaluate(myInd);
00071
00072 for(uint i = 0; i < bin->variables.size(); i++) {
00073 double newVal = bin->realValue[i];
00074
00075 bin->realValue[i] += precision;
00076 modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
00077 if(modified->isBetterThan(current)) {
00078 current = modified;
00079 newVal = bin->realValue[i];
00080 }
00081
00082 bin->realValue[i] -= 2 * precision;
00083 modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
00084 if(modified->isBetterThan(current)) {
00085 current = modified;
00086 newVal = bin->realValue[i];
00087 }
00088
00089 bin->realValue[i] = newVal;
00090 }
00091
00092
00093 for(uint iVar = 0; iVar < bin->nDimension_; iVar++) {
00094 bin->decValue[iVar] = static_cast<long int> ((bin->realValue[iVar] - bin->minValue_) / (bin->maxValue_ - bin->minValue_) * bin->potention_);
00095
00096 long dec = bin->decValue[iVar];
00097 for (int iBit = bin->nBits_; iBit > 0; dec = dec/2, iBit--) {
00098 bin->vBool_[iBit - 1] = (dec % 2) ? true:false;
00099 }
00100 bin->variables[iVar] = bin->vBool_;
00101
00102 if(bin->bRounding_) {
00103 bin->realValue[iVar] = bin->round(bin->realValue[iVar], bin->nDecimal_);
00104 }
00105 }
00106
00107 return true;
00108 }
00109