ECF 1.5
Copy of BinaryMutSimple.cpp
1#include "../ECF_base.h"
2#include "Binary.h"
3#include <cmath>
4
5uint extraTime;
6
7void BinaryMutSimple::registerParameters(StateP state)
8{
9 myGenotype_->registerParameter(state, "mut.simple", (voidP) new double(0), DOUBLE);
10 myGenotype_->registerParameter(state, "mut.simple.bitprob", (voidP) new double(0.001), DOUBLE);
11 myGenotype_->registerParameter(state, "mut.simple.extra", (voidP) new uint(1), UINT);
12}
13
14
15bool BinaryMutSimple::initialize(StateP state)
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "mut.simple");
18 probability_ = *((double*)sptr.get());
19
20 sptr = myGenotype_->getParameterValue(state, "mut.simple.bitprob");
21 bitProb_ = *((double*)sptr.get());
22
23 bUseBitProb_ = false;
24 if(myGenotype_->isParameterDefined(state, "mut.simple.bitprob"))
25 bUseBitProb_ = true;
26
27 sptr = myGenotype_->getParameterValue(state, "mut.simple.extra");
28 extraTime = *((uint*)sptr.get());
29
30 return true;
31}
32
33
34bool BinaryMutSimple::mutate(GenotypeP gene)
35{
36 Binary* bin = (Binary*) (gene.get());
37
38 //
39 // trosenje vremena - simulacija Marinove mutacije koja prolazi sve bitove
40 //
41 bool bit = true;
42 for(uint i = 0; i < extraTime * bin->variables.size() * bin->getNumBits(); i++)
43 if(state_->getRandomizer()->getRandomDouble() < bitProb_)
44 bit = !bit;
45
46
47 // invert all bits with 'bitProb_' probability
48 if(bUseBitProb_) {
49 for(uint i = 0; i < bin->variables.size(); i++)
50 for(uint j = 0; j < bin->getNumBits(); j++)
51 if(state_->getRandomizer()->getRandomDouble() < bitProb_)
52 bin->variables[i][j] = !(bin->variables[i][j]);
53 }
54 // invert a single random bit in the genotype
55 else {
56 uint iBit = state_->getRandomizer()->getRandomInteger((uint) bin->getNumBits());
57 uint dimension = state_->getRandomizer()->getRandomInteger((uint) bin->variables.size());
58 bin->variables[dimension][iBit] = !(bin->variables[dimension][iBit]);
59 }
60
61 bin->update();
62
63 //
64 // izvedba jednostavnog lokalnog operatora
65 //
66 IndividualP myInd = state_->getAlgorithm()->mutation_->currentInd;
67 FitnessP current, modified;
68 double precision = 1 / pow(10., (int) bin->nDecimal_);
69
70 current = state_->getAlgorithm()->evalOp_->evaluate(myInd);
71
72 for(uint i = 0; i < bin->variables.size(); i++) {
73 double newVal = bin->realValue[i];
74
75 bin->realValue[i] += precision;
76 modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
77 if(modified->isBetterThan(current)) {
78 current = modified;
79 newVal = bin->realValue[i];
80 }
81
82 bin->realValue[i] -= 2 * precision;
83 modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
84 if(modified->isBetterThan(current)) {
85 current = modified;
86 newVal = bin->realValue[i];
87 }
88
89 bin->realValue[i] = newVal;
90 }
91
92 // update genotype according to realValue
93 for(uint iVar = 0; iVar < bin->nDimension_; iVar++) {
94 bin->decValue[iVar] = static_cast<long int> ((bin->realValue[iVar] - bin->minValue_) / (bin->maxValue_ - bin->minValue_) * bin->potention_);
95
96 long dec = bin->decValue[iVar];
97 for (int iBit = bin->nBits_; iBit > 0; dec = dec/2, iBit--) {
98 bin->vBool_[iBit - 1] = (dec % 2) ? true:false;
99 }
100 bin->variables[iVar] = bin->vBool_;
101
102 if(bin->bRounding_) {
103 bin->realValue[iVar] = bin->round(bin->realValue[iVar], bin->nDecimal_);
104 }
105 }
106
107 return true;
108}
109