ECF 1.5
BinaryMutSimple.cpp
1#include "../ECF_base.h"
2#include "Binary.h"
3#include <cmath>
4
5namespace Binary
6{
7
9{
10 myGenotype_->registerParameter(state, "mut.simple", (voidP) new double(0), ECF::DOUBLE);
11 myGenotype_->registerParameter(state, "mut.simple.bitprob", (voidP) new double(0.001), ECF::DOUBLE);
12}
13
14
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 return true;
28}
29
30
31bool BinaryMutSimple::mutate(GenotypeP gene)
32{
33 Binary* bin = (Binary*) (gene.get());
34
35 // invert all bits with 'bitProb_' probability
36 if(bUseBitProb_) {
37 for(uint i = 0; i < bin->variables.size(); i++)
38 for(uint j = 0; j < bin->getNumBits(); j++)
39 if(state_->getRandomizer()->getRandomDouble() < bitProb_)
40 bin->variables[i][j] = !(bin->variables[i][j]);
41 }
42 // invert a single random bit in the genotype
43 else {
44 uint iBit = state_->getRandomizer()->getRandomInteger((uint) bin->getNumBits());
45 uint dimension = state_->getRandomizer()->getRandomInteger((uint) bin->variables.size());
46 bin->variables[dimension][iBit] = !(bin->variables[dimension][iBit]);
47 }
48
49 bin->update();
50
51/*
52
53 //
54 // izvedba jednostavnog lokalnog operatora
55 //
56 IndividualP myInd = state_->getAlgorithm()->mutation_->currentInd;
57 FitnessP current, modified;
58 double precision = 1 / pow(10., (int) bin->nDecimal_);
59
60 current = state_->getAlgorithm()->evalOp_->evaluate(myInd);
61
62 for(uint i = 0; i < bin->variables.size(); i++) {
63 double newVal = bin->realValue[i];
64
65 bin->realValue[i] += precision;
66 modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
67 if(modified->isBetterThan(current)) {
68 current = modified;
69 newVal = bin->realValue[i];
70 }
71
72 bin->realValue[i] -= 2 * precision;
73 modified = state_->getAlgorithm()->evalOp_->evaluate(myInd);
74 if(modified->isBetterThan(current)) {
75 current = modified;
76 newVal = bin->realValue[i];
77 }
78
79 bin->realValue[i] = newVal;
80 }
81
82 // update genotype according to realValue
83 for(uint iVar = 0; iVar < bin->nDimension_; iVar++) {
84 bin->decValue[iVar] = static_cast<long int> ((bin->realValue[iVar] - bin->minValue_) / (bin->maxValue_ - bin->minValue_) * bin->potention_);
85
86 long dec = bin->decValue[iVar];
87 for (int iBit = bin->nBits_; iBit > 0; dec = dec/2, iBit--) {
88 bin->vBool_[iBit - 1] = (dec % 2) ? true:false;
89 }
90 bin->variables[iVar] = bin->vBool_;
91
92 if(bin->bRounding_) {
93 bin->realValue[iVar] = bin->round(bin->realValue[iVar], bin->nDecimal_);
94 }
95 }
96
97*/
98 return true;
99}
100
101}
Binary class - implements genotype as a vector of binary coded real values with variable interval and...
Definition: Binary.h:38
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
double probability_
probability of usage of this mutation operator
Definition: Mutation.h:40
GenotypeP myGenotype_
pointer to the Genotype that defines this MutationOp
Definition: Mutation.h:41