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