ECF 1.5
FloatingPointCrsSbx.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.sbx", (voidP) new double(0), ECF::DOUBLE);
12 state->getRegistry()->registerEntry("crx.sbx.ni", (voidP) new uint(1), ECF::UINT);
13}
14
15
17{
18 voidP sptr = myGenotype_->getParameterValue(state, "crx.sbx");
19 probability_ = *((double*)sptr.get());
20
21 voidP par = state->getRegistry()->getEntry("crx.sbx.ni");
22 ni = *((uint*) par.get());
23
24 return true;
25}
26
27
28bool FloatingPointCrsSbx::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
29{
30 FloatingPoint* p1 = (FloatingPoint*) (gen1.get());
31 FloatingPoint* p2 = (FloatingPoint*) (gen2.get());
32 FloatingPoint* ch = (FloatingPoint*) (child.get());
33
34 // repeat for each variable
35 for(uint i = 0; i < p1->realValue.size(); i++) {
36 double p1x = p1->realValue[i];
37 double p2x = p2->realValue[i];
38
39 // determine smaller and greater parent values
40 double low = p1x, high = p2x;
41 if(p2x < p1x) {
42 low = p2x;
43 high = p1x;
44 }
45
46 // check for close or same parents
47 if(fabs(high - low) < 1.e-12) {
48 ch->realValue[i] = (high + low) / 2;
49 continue;
50 }
51
52 // determine min[(low - LBound), (UBound - high)]
53 double min = low - p1->getLBound();
54 if((p1->getUBound() - high) < min)
55 min = p1->getUBound() - high;
56
57 // determine beta and alpha
58 double beta = 1 + 2 * min / (high - low);
59 double alpha = 2 - 1 / pow(beta, 1. + ni);
60
61 double u = state_->getRandomizer()->getRandomDouble();
62 // scale down to avoid u == 1
63 u *= 0.999;
64
65 double beta_dash;
66 // if u is smaller than 1/alpha perform a contracting crossover
67 if (u <= (1. / alpha)) {
68 beta_dash = pow(alpha * u, 1.0 / (ni + 1.0));
69 }
70 // otherwise perform an expanding crossover
71 else {
72 beta_dash = pow(1. / (2. - alpha * u), 1.0 / (ni + 1.0));
73 }
74
75 // apply beta_dash
76 switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
77 case 0: ch->realValue[i] = ((p1->realValue[i] + p2->realValue[i])/2.0) - beta_dash * 0.5 * fabs(p1->realValue[i] - p2->realValue[i]);
78 break;
79 case 1: ch->realValue[i] = ((p1->realValue[i] + p2->realValue[i])/2.0) + beta_dash * 0.5 * fabs(p1->realValue[i] - p2->realValue[i]);
80 }
81 }
82
83 return true;
84}
85
86}
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