ECF 1.5
BitStringMutMix.cpp
1#include "../ECF_base.h"
2#include "BitString.h"
3
4namespace BitString
5{
6
8{
9 myGenotype_->registerParameter(state, "mut.mix", (voidP) new double(0), ECF::DOUBLE);
10}
11
12
14{
15 voidP sptr = myGenotype_->getParameterValue(state, "mut.mix");
16 probability_ = *((double*)sptr.get());
17
18 sptr = myGenotype_->getParameterValue(state, "size");
19 if(*((uint*)sptr.get()) == 1) {
20 ECF_LOG_ERROR(state, "Warning: mixing mutation not applicable on BitString genotype of size 1!");
21 }
22 return true;
23}
24
25
26bool BitStringMutMix::mutate(GenotypeP gene)
27{
28 BitString* bitstr = (BitString*) (gene.get());
29
30 if(bitstr->bits.size() == 1)
31 return false;
32
33 // determine random left and right bit bound
34 uint bitIndexSmaller = state_->getRandomizer()->getRandomInteger((uint) bitstr->bits.size());
35 uint bitIndexBigger;
36
37 do {
38 bitIndexBigger = state_->getRandomizer()->getRandomInteger((uint) bitstr->bits.size());
39 } while (bitIndexSmaller == bitIndexBigger);
40
41 // make sure smaller < bigger
42 uint tmp = bitIndexSmaller;
43 if (bitIndexSmaller > bitIndexBigger) {
44 bitIndexSmaller = bitIndexBigger;
45 bitIndexBigger = tmp;
46 }
47
48 // count zeros and ones in chosen segment
49 int counter0 = 0;
50 int counter1 = 0;
51
52 for(uint i = bitIndexSmaller; i <= bitIndexBigger; i++) {
53 if(bitstr->bits[i]) counter1++;
54 else counter0++;
55 }
56
57 int fairness0 = counter0;
58 int fairness1 = counter1;
59
60 // mutate chosen segment
61 for(uint i = bitIndexSmaller; i <= bitIndexBigger; i++) {
62 int random = state_->getRandomizer()->getRandomInteger(1, fairness0 + fairness1);
63
64 if(random <= fairness1) {
65 if (counter1 > 0) {
66 bitstr->bits[i] = true;
67 counter1--;
68 }
69 else {
70 bitstr->bits[i] = false;
71 counter0--;
72 }
73 }
74 else {
75 if (counter0 > 0) {
76 bitstr->bits[i] = false;
77 counter0--;
78 }
79 else {
80 bitstr->bits[i] = true;
81 counter1--;
82 }
83 }
84 }
85
86 return true;
87}
88
89}
BitString class - implements genotype as a series of bits.
Definition: BitString.h:24
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!
bool initialize(StateP)
Initialize mutation operator. Called before first mutation operation.
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