ECF 1.5
PermutationCrsDPX.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.DPX", (voidP) new double(0), ECF::DOUBLE);
12}
13
14
16{
17 voidP sptr = myGenotype_->getParameterValue(state, "crx.DPX");
18 probability_ = *((double*)sptr.get());
19 return true;
20}
21
22
23bool PermutationCrsDPX::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 capacity = (int) p1->getSize();
30
31 // Flag positions in child on which values are set from both parents
32 bool* takenPositions = new bool[capacity];
33
34 // A collection of values which are not copied from first parent;
35 // it is growable collection of maximally "capacity" elements;
36 // current size is given by "unusedNo" in which case elements occupy
37 // positions from 0 to unusedNo-1
38 int unusedNo = 0;
39 int* unusedElements = new int[capacity];
40
41 // Mark all positions as unused
42 for(int i = 0; i < capacity; i++) {
43 takenPositions[i] = false;
44 }
45
46 // Go through parents; if values at same index are the same,
47 // copy value to child at that position and mark position as taken;
48 // otherwise, add element from first parent to unused elements
49 for(int ind = 0; ind < capacity; ind++) {
50 if(p1->variables[ind]==p2->variables[ind]) {
51 ch->variables[ind] = p1->variables[ind];
52 takenPositions[ind] = true;
53 } else {
54 unusedElements[unusedNo] = p1->variables[ind];
55 unusedNo++;
56 }
57 }
58
59 // Candidate index for next empty position in child:
60 int candidatePosition = 0;
61
62 // While having at least two unused elements left, pick one randomly
63 // and add it to child; remove it from unused collection and shrink it.
64 while(unusedNo > 1) {
65 int index = state_->getRandomizer()->getRandomInteger(unusedNo);
66 while(takenPositions[candidatePosition]) {
67 candidatePosition++;
68 }
69 ch->variables[candidatePosition] = unusedElements[index];
70 unusedNo--;
71 unusedElements[index] = unusedElements[unusedNo];
72 candidatePosition++;
73 }
74 // If there is remaining unused element, copy it to child:
75 if(unusedNo!=0) {
76 while(takenPositions[candidatePosition]) {
77 candidatePosition++;
78 }
79 ch->variables[candidatePosition] = unusedElements[0];
80 }
81
82 // Remove temporary arrays
83 delete[] unusedElements;
84 delete[] takenPositions;
85
86 return true;
87}
88
89}
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 initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)
Definition: Permutation.h:37