ECF 1.5
CartesianCrxHalfUniform.cpp
1#include "CartesianCrxHalfUniform.h"
2#include "Cartesian_genotype.h"
3namespace cartesian{
4
6 {
7 myGenotype_->registerParameter(state, "crx.halfuniform", (voidP) new double(0), ECF::DOUBLE);
8 }
9
11 {
12 voidP sptr = myGenotype_->getParameterValue(state, "crx.halfuniform");
13 probability_ = *((double*)sptr.get());
14 return true;
15 }
16
17 bool CartesianCrxHalfUniform::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
18 {
19 Cartesian* p1 = (Cartesian*) gen1.get();
20 Cartesian* p2 = (Cartesian*) gen2.get();
21 Cartesian* ch = (Cartesian*) child.get();
22
23 uint parent1Size = p1->size() - p1->nOutputs;
24 uint parent2Size = p2->size() - p2->nOutputs;
25 uint minimumSize = parent1Size > parent2Size ? parent1Size : parent2Size;
26 bool parent1IsLonger = parent1Size > parent2Size;
27 ch->clear();
28
29 double whichParent = p1->get_random_double(0,1);
30 double whichPart = p1->get_random_double(0,1);
31 uint crossoverStart = 0;
32 uint crossoverEnd = minimumSize/2;
33 uint start = crossoverEnd;
34 uint end = minimumSize;
35 bool keepFirst = false;
36 if(whichPart >= 0.5) {
37 start = 0;
38 end = minimumSize/2;
39 crossoverStart = end;
40 crossoverEnd = minimumSize;
41 keepFirst = true;
42 }
43 if(whichParent < 0.5) {
44 if(keepFirst) {
45 for(uint i = start; i < end; i++){
46 ch->push_back(p1->operator[](i));
47 }
48 for(uint i = crossoverStart; i < crossoverEnd; i++) {
49 double randomResult = p1->get_random_double(0,1);
50 if(randomResult < 0.5) {
51 ch->push_back(p1->operator[](i));
52 }
53 else {
54 ch->push_back(p2->operator[](i));
55 }
56 }
57 }
58 else {
59 for(uint i = crossoverStart; i < crossoverEnd; i++) {
60 double randomResult = p1->get_random_double(0,1);
61 if(randomResult < 0.5) {
62 ch->push_back(p1->operator[](i));
63 }
64 else {
65 ch->push_back(p2->operator[](i));
66 }
67 }
68 for(uint i = start; i < end; i++){
69 ch->push_back(p1->operator[](i));
70 }
71 }
72 }
73 else {
74 if(keepFirst) {
75 for(uint i = start; i < end; i++){
76 ch->push_back(p2->operator[](i));
77 }
78 for(uint i = crossoverStart; i < crossoverEnd; i++) {
79 double randomResult = p1->get_random_double(0,1);
80 if(randomResult < 0.5) {
81 ch->push_back(p1->operator[](i));
82 }
83 else {
84 ch->push_back(p2->operator[](i));
85 }
86 }
87 }
88 else {
89 for(uint i = crossoverStart; i < crossoverEnd; i++) {
90 double randomResult = p1->get_random_double(0,1);
91 if(randomResult < 0.5) {
92 ch->push_back(p1->operator[](i));
93 }
94 else {
95 ch->push_back(p2->operator[](i));
96 }
97 }
98 for(uint i = start; i < end; i++){
99 ch->push_back(p2->operator[](i));
100 }
101 }
102 }
103 if(parent1IsLonger) {
104 for(uint i = parent2Size; i < parent1Size; i++) {
105 ch->push_back(p1->operator[](i));
106 }
107 }
108 else{
109 for(uint i = parent1Size; i < parent2Size; i++) {
110 ch->push_back(p1->operator[](i));
111 }
112 }
113 //Resolve outputs.
114 for(uint i = 0; i < p1->nOutputs; i++) {
115 if(whichParent < 0.5) {
116 ch->push_back(p1->operator[](parent1Size + i));
117 }
118 else {
119 ch->push_back(p1->operator[](parent2Size + i));
120 }
121 }
122 return true;
123 }
124}
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 initialize(StateP state)
Initialize crossover operator. Called before first crossover operation.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
void registerParameters(StateP state)
Register parameters with the system. Called before CrossoverOp::initialize.
uint nOutputs
number of final outputs