1#include "../ECF_base.h"
2#include "Permutation.h"
11 myGenotype_->registerParameter(state,
"crx.UPMX", (voidP)
new double(0), ECF::DOUBLE);
17 voidP sptr =
myGenotype_->getParameterValue(state,
"crx.UPMX");
34 inverseIndexes =
new int[n];
36 for(
int i = 0; i < n; i++) {
37 inverseIndexes[array[i]] = i;
44 delete [] inverseIndexes;
48 void setIndexForValue(
int value,
int index) {
49 inverseIndexes[value] = index;
53 int getIndexForValue(
int value) {
54 return inverseIndexes[value];
60 void remove(
int* size,
int pos) {
65 array[*size] = array[pos];
69 t = inverseIndexes[array[pos]];
70 inverseIndexes[array[pos]] = inverseIndexes[array[*size]];
71 inverseIndexes[array[*size]] = t;
78 int t = p->variables[pos1];
79 p->variables[pos1] = p->variables[pos2];
80 p->variables[pos2] = t;
83 t = inverseIndexes[p->variables[pos1]];
84 inverseIndexes[p->variables[pos1]] = inverseIndexes[p->variables[pos2]];
85 inverseIndexes[p->variables[pos2]] = t;
96 int capacity = (int) p1->getSize();
101 int * legalPositions =
new int[capacity];
102 for(
int i = 0; i < capacity; i++) {
103 legalPositions[i] = i;
114 for(
int i = 0; i < capacity; i++) {
115 ch->variables[i] = p1->variables[i];
116 idxCh.setIndexForValue(p1->variables[i], i);
117 idxP2.setIndexForValue(p2->variables[i], i);
121 int attempts = capacity / 3;
124 int legalsCount = capacity;
127 for(
int attempt = 0; attempt < attempts; attempt++) {
129 int rand = state_->getRandomizer()->getRandomInteger(legalsCount);
130 int pos1 = legalPositions[rand];
131 idxLegal.remove(&legalsCount, rand);
134 int value1 = ch->variables[pos1];
135 int pos2 = idxP2.getIndexForValue(value1);
138 idxCh.swap(ch, pos1, pos2);
141 if(idxLegal.getIndexForValue(pos2) < legalsCount) {
142 idxLegal.remove(&legalsCount, idxLegal.getIndexForValue(pos2));
146 delete [] legalPositions;
double probability_
probability of usage of this crossover operator
GenotypeP myGenotype_
pointer to the Genotype that defines this CrossoverOp
bool initialize(StateP)
Initialize crossover operator. Called before first crossover operation.
void registerParameters(StateP)
Register parameters with the system. Called before CrossoverOp::initialize.
bool mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
Permutation class - implements genotype as a vector of indices 0..(n-1) (permutation of indices)