ECF 1.5
PermutationCrsCyclic.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.cyclic", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.cyclic");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsCyclic::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 std::map<int, int> invP1, invP2, index;
30
31 //radim inverzno mapiranje (broj -> index)
32
33 for (int i = 0; i < (int) p1->getSize(); i++) {
34 invP1[p1->variables[i]] = i;
35 invP2[p2->variables[i]] = i;
36 }
37
38 //slučajnim odabirom odabrati da li se kopiranje počima od roditelja 1 ili 2
39 int rand = state_->getRandomizer()->getRandomInteger(0,1);
40
41 int j = 0;
42 do {
43 do {
44 if (rand) {
45 ch->variables[j] = p1->variables[j];
46 index[j] = 1;
47 j = invP2[ch->variables[j]]; // postavi poziciju j na poziciju kopiranog broja u p2
48 } else {
49 ch->variables[j] = p2->variables[j];
50 index[j] = 1;
51 j = invP1[ch->variables[j]]; // postavi poziciju j na poziciju kopiranog broja u p1
52 }
53 } while (!index[j]);
54 rand = !rand;
55 j = 0;
56 while (j < (int) p1->getSize() && index[j])
57 j++;
58 } while (j < (int) p1->getSize());
59
60 return true;
61}
62
63}
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)
Register parameters with the system. Called before CrossoverOp::initialize.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37