ECF 1.5
FloatingPointCrsBga.cpp
1#include "../ECF_base.h"
2#include "FloatingPoint.h"
3#include <math.h>
4
5
6namespace FloatingPoint
7{
8
10{
11 myGenotype_->registerParameter(state, "crx.bga", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.bga");
18 probability_ = *((double*)sptr.get());
19
20 return true;
21}
22
23
24bool FloatingPointCrsBga::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
25{
26 FloatingPoint* p1 = (FloatingPoint*) (gen1.get());
27 FloatingPoint* p2 = (FloatingPoint*) (gen2.get());
28 FloatingPoint* ch = (FloatingPoint*) (child.get());
29
30 int a;
31 uint size = (uint) p1->realValue.size();
32 // upper - lower bound
33 double range = 0.5 * (p1->getUBound() - p1->getLBound());
34 double gama = 0, lambda = 0, b;
35
36 for (int i = 0; i <= 15; i++) {
37 a = state_->getRandomizer()->getRandomInteger(1, 16);
38 // 1/16 probability for 1 = 1
39 if (a == 16)
40 a = 1;
41 else
42 a = 0;
43 gama = gama + a * pow((double) 2., -i);
44 }
45
46 double norm = 0;
47 for(uint i = 0; i < size; i++)
48 norm += pow(p1->realValue[i] - p2->realValue[i], 2);
49 norm = sqrt(norm);
50
51 // scaling safeguard
52 if(norm < 10e-9)
53 norm = 1;
54
55 // determine better parent
56 FloatingPoint *better, *worse;
57 FitnessP parent2 = state_->getContext()->secondParent->fitness;
58 if(state_->getContext()->firstParent->fitness->isBetterThan(parent2)) {
59 better = p1;
60 worse = p2;
61 } else {
62 better = p2;
63 worse = p1;
64 }
65
66 // build child
67 for (uint i = 0; i < size; i++) {
68 // worse gene minus better gene divided with norm
69 lambda = (worse->realValue[i] - better->realValue[i]) / norm;
70
71 b = state_->getRandomizer()->getRandomDouble();
72 // minus with probability 0.9
73 if (b <= 0.9)
74 ch->realValue[i] = better->realValue[i] - range * gama * lambda;
75 else
76 ch->realValue[i] = better->realValue[i] + range * gama * lambda;
77
78 // check for bounds; if outside the interval, bring closer to better parent
79 if(ch->realValue[i] > ch->getUBound())
80 ch->realValue[i] = better->realValue[i] + state_->getRandomizer()->getRandomDouble() * (ch->getUBound() - better->realValue[i]);
81 else if(ch->realValue[i] < ch->getLBound())
82 ch->realValue[i] = better->realValue[i] - state_->getRandomizer()->getRandomDouble() * (better->realValue[i] - ch->getLBound());
83 }
84
85 return true;
86}
87
88}
double probability_
probability of usage of this crossover operator
Definition: Crossover.h:42
GenotypeP myGenotype_
pointer to the Genotype that defines this CrossoverOp
Definition: Crossover.h:43
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
FloatingPoint class - implements genotype as a vector of floating point values.
Definition: FloatingPoint.h:39