ECF 1.5
CartesianCrxOnePoint.cpp
1#include "CartesianCrxOnePoint.h"
2#include "Cartesian_genotype.h"
3namespace cartesian{
4
6 {
7 myGenotype_->registerParameter(state, "crx.onepoint", (voidP) new double(0), ECF::DOUBLE);
8 }
9
11 {
12 voidP sptr = myGenotype_->getParameterValue(state, "crx.onepoint");
13 probability_ = *((double*)sptr.get());
14 return true;
15 }
16
17 bool CartesianCrxOnePoint::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 double whichParent = p1->get_random_double(0,1.0);
29 uint index = p1->get_random_int(0, minimumSize);
30 if(whichParent < 0.5) {
31 for(uint i = 0; i < index; i++) {
32 ch->push_back(p1->operator[](i));
33 }
34 for(uint i = index; i < minimumSize; i++) {
35 ch->push_back(p2->operator[](i));
36 }
37 }
38 else {
39 for(uint i = 0; i < index; i++) {
40 ch->push_back(p2->operator[](i));
41 }
42 for(uint i = index; i < minimumSize; i++) {
43 ch->push_back(p1->operator[](i));
44 }
45 }
46 if(parent1IsLonger) {
47 for(uint i = parent2Size; i < parent1Size; i++) {
48 ch->push_back(p1->operator[](i));
49 }
50 }
51 else{
52 for(uint i = parent1Size; i < parent2Size; i++) {
53 ch->push_back(p1->operator[](i));
54 }
55 }
56 for(uint i = 0; i < p1->nOutputs; i++) {
57 if(whichParent < 0.5) {
58 ch->push_back(p1->operator[](parent1Size + i));
59 }
60 else {
61 ch->push_back(p1->operator[](parent2Size + i));
62 }
63 }
64 return true;
65 }
66}
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.
void registerParameters(StateP state)
Register parameters with the system. Called before CrossoverOp::initialize.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
uint nOutputs
number of final outputs