ECF 1.5
PermutationCrsPBX.cpp
1#include "../ECF_base.h"
2#include "Permutation.h"
3#include <map>
4
5
6namespace Permutation
7{
8
10{
11 myGenotype_->registerParameter(state, "crx.PBX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.PBX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsPBX::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
24{
25 Permutation* p1 = (Permutation*) (gen1.get());
26 Permutation* p2 = (Permutation*) (gen2.get());
27 Permutation* ch = (Permutation*) (child.get());
28
29 // storage of elements copied into child (true ==> copied)
30 std::vector<bool> taged;
31 taged.resize(p1->getSize());
32
33 int count=0;
34 for(uint i = 0; i < ch->getSize(); i++) {
35 if(state_->getRandomizer()->getRandomInteger(2) == 0) {
36 ch->variables[i] = p1->variables[i];
37 taged[p1->variables[i]] = 1;
38 count++;
39 }
40 else {
41 ch->variables[i] = -1;
42 }
43 }
44
45 int indCh = 0;
46 count = (int) ch->getSize() - count;
47 for(uint i = 0; i < ch->getSize() && count > 0; i++) {
48 //trazimo prazno mjesto u djetetu
49 while(ch->variables[indCh] != -1)
50 indCh++;
51 //ako element iz drugog roditelja nije vec u djetetu onda ga kopiramo
52 if(taged[p2->variables[i]] != 1) {
53 ch->variables[indCh] = p2->variables[i];
54 count--;
55 }
56 }
57 return true;
58}
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
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37