ECF 1.5
PermutationCrsULX.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.ULX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.ULX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsULX::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> numCopied;
30 std::vector<int> free;
31
32 for (int i = 0; i < (int) p1->getSize(); i++) {
33 bool copied = true;
34 if (p1->variables[i] == p2->variables[i]) {
35 ch->variables[i] = p1->variables[i];
36 } else if (!numCopied[p1->variables[i]] && !numCopied[p2->variables[i]]) {
37 ch->variables[i] = state_->getRandomizer()->getRandomInteger(0, 1) ? p1->variables[i] : p2->variables[i];
38 } else if (!numCopied[p1->variables[i]]) {
39 ch->variables[i] = p1->variables[i];
40 } else if (!numCopied[p2->variables[i]]) {
41 ch->variables[i] = p2->variables[i];
42 } else {
43 copied = false;
44 free.push_back(i);
45 }
46 if (copied)
47 numCopied[ch->variables[i]] = 1;
48 }
49
50 if (free.size() > 0) {
51 for (int i = 0; i < (int) p1->getSize(); i++) {
52 if (!numCopied[i]) {
53 int r = state_->getRandomizer()->getRandomInteger((int) free.size());
54
55 ch->variables[free[r]] = i;
56 free.erase(free.begin() + r);
57 }
58 if (free.size() == 0) break;
59 }
60 }
61
62 return true;
63}
64
65}
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