ECF 1.5
BitStringMutSimple.cpp
1#include "../ECF_base.h"
2#include "BitString.h"
3
4namespace BitString
5{
6
8{
9 myGenotype_->registerParameter(state, "mut.simple", (voidP) new double(0), ECF::DOUBLE);
10 myGenotype_->registerParameter(state, "mut.simple.bitprob", (voidP) new double(0.001), ECF::DOUBLE);
11}
12
13
15{
16 voidP sptr = myGenotype_->getParameterValue(state, "mut.simple");
17 probability_ = *((double*)sptr.get());
18
19 sptr = myGenotype_->getParameterValue(state, "mut.simple.bitprob");
20 bitProb_ = *((double*)sptr.get());
21
22 bUseBitProb_ = false;
23 if(myGenotype_->isParameterDefined(state, "mut.simple.bitprob"))
24 bUseBitProb_ = true;
25
26 return true;
27}
28
29
30bool BitStringMutSimple::mutate(GenotypeP gene)
31{
32 BitString* bitstr = (BitString*) (gene.get());
33
34 // invert all bits with 'bitProb_' probability
35 if(bUseBitProb_) {
36 for(uint i = 0; i < bitstr->bits.size(); i++)
37 if(state_->getRandomizer()->getRandomDouble() < bitProb_)
38 bitstr->bits[i] = !bitstr->bits[i];
39 }
40 // invert a single random bit in the genotype
41 else {
42 uint iBit = state_->getRandomizer()->getRandomInteger(0, (int) bitstr->bits.size() - 1);
43 bitstr->bits[iBit] = !(bitstr->bits[iBit]);
44 }
45
46 return true;
47}
48
49}
BitString class - implements genotype as a series of bits.
Definition: BitString.h:24
bool bUseBitProb_
are we using bit mutation probabiltiy (instead of individual probability)
double bitProb_
probability of single bit mutation
bool mutate(GenotypeP gene)
Performs mutation of a genotype object. The genotype object must be initialized!
void registerParameters(StateP)
Register parameters with the system. Called before MutationOp::initialize.
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