ECF 1.5
PermutationCrsOX2.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.OX2", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.OX2");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsOX2::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 int start = state_->getRandomizer()->getRandomInteger(p1->getSize());
30 int end = state_->getRandomizer()->getRandomInteger(start, (int) p1->getSize() - 1);
31
32 std::map<int, int> subSet;
33
34 int ind1 = state_->getRandomizer()->getRandomInteger(p1->getSize() - 1);
35 int ind2 = state_->getRandomizer()->getRandomInteger(ind1, (int) p1->getSize() - 1);
36
37 // copy subvector into child
38 for(int i = ind1; i <= ind2; i++) {
39 ch->variables[i] = p1->variables[i];
40 subSet[p1->variables[i]] = 1; //bilo je 0
41 }
42 int indexChild = (ind2 + 1) % p1->getSize();
43 std::map<int, int>::iterator iter = subSet.begin();
44
45 // krecemo od pocetka
46
47 int index = 0;
48 for (int i = 0; i < (int) p1->getSize(); i++) { // copy remaining part of second permutation
49 if (index == ind1) { // skip already copied part
50 index = ind2 + 1;
51 }
52 if (!subSet[p2->variables[i]]) {
53 ch->variables[index] = p2->variables[i];
54 index++;
55 }
56 }
57
58 return true;
59}
60
61}
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 initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37