ECF 1.5
BinaryMutMix.cpp
1#include "../ECF_base.h"
2#include "Binary.h"
3
4namespace Binary
5{
6
8{
9 myGenotype_->registerParameter(state, "mut.mix", (voidP) new double(0), ECF::DOUBLE);
10}
11
12
13bool BinaryMutMix::initialize(StateP state)
14{
15 voidP sptr = myGenotype_->getParameterValue(state, "mut.mix");
16 probability_ = *((double*)sptr.get());
17 return true;
18}
19
20
21bool BinaryMutMix::mutate(GenotypeP gene)
22{
23 Binary* bin = (Binary*) (gene.get());
24
25 uint dimension = state_->getRandomizer()->getRandomInteger((uint) bin->variables.size());
26
27 // make sure indexSmaller < indexBigger
28 uint bitIndexSmaller = state_->getRandomizer()->getRandomInteger((uint) bin->getNumBits());
29 uint bitIndexBigger;
30 do {
31 bitIndexBigger = state_->getRandomizer()->getRandomInteger((uint) bin->getNumBits());
32 }while(bitIndexBigger == bitIndexSmaller);
33
34 uint tmp = bitIndexSmaller;
35 if(bitIndexSmaller > bitIndexBigger) {
36 bitIndexSmaller = bitIndexBigger;
37 bitIndexBigger = tmp;
38 }
39
40 // count 1's and 0's
41 int counter0 = 0;
42 int counter1 = 0;
43
44 for(uint i = bitIndexSmaller; i <= bitIndexBigger; i++) {
45 if(bin->variables[dimension][i]) counter1++;
46 else counter0++;
47 }
48
49 int fairness0 = counter0;
50 int fairness1 = counter1;
51
52 // mix bits in [indexSmaller, indexBigger]
53 for(uint i = bitIndexSmaller; i <= bitIndexBigger; i++) {
54 int random = state_->getRandomizer()->getRandomInteger(1, fairness0 + fairness1);
55 if(random <= fairness1) {
56 if(counter1 > 0) {
57 bin->variables[dimension][i] = true;
58 counter1--;
59 }
60 else {
61 bin->variables[dimension][i] = false;
62 counter0--;
63 }
64 }
65 else {
66 if(counter0 > 0) {
67 bin->variables[dimension][i] = false;
68 counter0--;
69 }
70 else {
71 bin->variables[dimension][i] = true;
72 counter1--;
73 }
74 }
75 }
76
77 bin->update();
78
79 return true;
80}
81
82}
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.
Definition: BinaryMutMix.cpp:7
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