ECF 1.5
PermutationCrsOPX.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.OPX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.OPX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsOPX::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 position = state_->getRandomizer()->getRandomInteger(p1->getSize()-1);
30
31 // set representing values copied from first parent
32 std::map<int, int> subSet;
33
34 // copy first part of first parent (up to element at "position") into child
35 for(int i = 0; i <= position; i++) {
36 ch->variables[i] = p1->variables[i];
37 // remember visited element
38 subSet[p1->variables[i]] = 0;
39 }
40
41 // How many values are left?
42 int left = (int) p1->getSize() - position - 1;
43 // Continue filling child from next position:
44 int indexChild = position + 1;
45
46 // Go through each value of second parent in correct order;
47 // if value is not already visited, copy it to child in first available position
48 for(int ind2 = 0; ind2 < (int) p1->getSize() && left>0; ind2++) {
49 if(subSet.find(p2->variables[ind2]) == subSet.end()) {
50 ch->variables[indexChild] = p2->variables[ind2];
51 left--;
52 indexChild++;
53 }
54 }
55
56 return true;
57}
58
59}
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)
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
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