ECF 1.5
BinaryCrsRandomRespectful.cpp
1#include "../ECF_base.h"
2#include "Binary.h"
3
4namespace Binary
5{
6
8{
9 myGenotype_->registerParameter(state, "crx.randomrespectful", (voidP) new double(0), ECF::DOUBLE);
10}
11
12
14{
15 voidP sptr = myGenotype_->getParameterValue(state, "crx.randomrespectful");
16 probability_ = *((double*)sptr.get());
17 return true;
18}
19
20
21bool BinaryCrsRandomRespectful::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 double rand;
28 typedef std::vector<uint> v_uint;
29
30 v_uint similarity;
31
32 for (uint dimension = 0; dimension < p1->variables.size(); dimension++) {
33 for(uint i = 0; i < p1->getNumBits(); i++) {
34 if (p1->variables[dimension][i] == p2->variables[dimension][i])
35 similarity.push_back(p2->variables[dimension][i]);
36 else
37 similarity.push_back(2);
38 }
39 }
40
41 for (uint dimension = 0; dimension < p1->variables.size(); dimension++) {
42 for(uint i = 0; i < p1->getNumBits(); i++) {
43 if (similarity[dimension*p1->getNumBits()+i]==1)
44 ch->variables[dimension][i]= 1;
45 else if (similarity[dimension*p1->getNumBits()+i]==0)
46 ch->variables[dimension][i]= 0;
47 else {
48 rand = state_->getRandomizer()->getRandomDouble();
49 if (rand <= 0.5)
50 ch->variables[dimension][i]= 1;
51 else
52 ch->variables[dimension][i]= 0;
53 }
54 }
55 }
56
57 ch->update();
58
59 return true;
60}
61
62}
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
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
std::vector< v_bool > variables
vector of bit vectors
Definition: Binary.h:53
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