ECF 1.5
BinaryCrsTwoPoint.cpp
1#include "../ECF_base.h"
2#include "Binary.h"
3
4namespace Binary
5{
6
8{
9 myGenotype_->registerParameter(state, "crx.twopoint", (voidP) new double(0), ECF::DOUBLE);
10}
11
12
14{
15 voidP sptr = myGenotype_->getParameterValue(state, "crx.twopoint");
16 probability_ = *((double*)sptr.get());
17 return true;
18}
19
20
21bool BinaryCrsTwoPoint::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
22{
23 Binary* p1 = (Binary*) (gen1.get());
24 Binary* p2 = (Binary*) (gen2.get());
25 Binary* ch = (Binary*) (child.get());
26
27 uint bitCrsFirst = state_->getRandomizer()->getRandomInteger(p1->getNumBits());
28 uint bitCrsSecond, tmp;
29 do
30 {
31 bitCrsSecond = state_->getRandomizer()->getRandomInteger(p1->getNumBits());
32 }while (bitCrsFirst==bitCrsSecond);
33
34 if (bitCrsFirst>bitCrsSecond)
35 {
36 tmp=bitCrsFirst;
37 bitCrsFirst=bitCrsSecond;
38 bitCrsSecond=tmp;
39 }
40
41 for (uint dimension = 0; dimension < p1->variables.size(); dimension++) {
42 switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
43 case 0: for (uint i = 0; i < bitCrsFirst; i++) {
44 ch->variables[dimension][i] = p1->variables[dimension][i];
45 }
46 for (uint i = bitCrsFirst; i < bitCrsSecond; i++) {
47 ch->variables[dimension][i] = p2->variables[dimension][i];
48 }
49 for (uint i = bitCrsSecond; i < p1->getNumBits(); i++) {
50 ch->variables[dimension][i] = p1->variables[dimension][i];
51 }
52 break;
53 case 1: for (uint i = 0; i < bitCrsFirst; i++) {
54 ch->variables[dimension][i] = p2->variables[dimension][i];
55 }
56 for (uint i = bitCrsFirst; i < bitCrsSecond; i++) {
57 ch->variables[dimension][i] = p1->variables[dimension][i];
58 }
59 for (uint i = bitCrsSecond; i < p2->getNumBits(); i++) {
60 ch->variables[dimension][i] = p2->variables[dimension][i];
61 }
62 }
63 }
64
65 // update integer and real domain representation
66 ch->update();
67
68 return true;
69}
70
71}
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
Binary class - implements genotype as a vector of binary coded real values with variable interval and...
Definition: Binary.h:38
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