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