00001 #include "../ECF_base.h"
00002 #include "Permutation.h"
00003 #include <map>
00004
00005
00006 namespace Permutation
00007 {
00008
00009 void PermutationCrsDPX::registerParameters(StateP state)
00010 {
00011 myGenotype_->registerParameter(state, "crx.DPX", (voidP) new double(0), ECF::DOUBLE);
00012 }
00013
00014
00015 bool PermutationCrsDPX::initialize(StateP state)
00016 {
00017 voidP sptr = myGenotype_->getParameterValue(state, "crx.DPX");
00018 probability_ = *((double*)sptr.get());
00019 return true;
00020 }
00021
00022
00023 bool PermutationCrsDPX::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00024 {
00025 Permutation* p1 = (Permutation*) (gen1.get());
00026 Permutation* p2 = (Permutation*) (gen2.get());
00027 Permutation* ch = (Permutation*) (child.get());
00028
00029 int capacity = p1->getSize();
00030
00031
00032 bool* takenPositions = new bool[capacity];
00033
00034
00035
00036
00037
00038 int unusedNo = 0;
00039 int* unusedElements = new int[capacity];
00040
00041
00042 for(int i = 0; i < capacity; i++) {
00043 takenPositions[i] = false;
00044 }
00045
00046
00047
00048
00049 for(int ind = 0; ind < capacity; ind++) {
00050 if(p1->variables[ind]==p2->variables[ind]) {
00051 ch->variables[ind] = p1->variables[ind];
00052 takenPositions[ind] = true;
00053 } else {
00054 unusedElements[unusedNo] = p1->variables[ind];
00055 unusedNo++;
00056 }
00057 }
00058
00059
00060 int candidatePosition = 0;
00061
00062
00063
00064 while(unusedNo > 1) {
00065 int index = state_->getRandomizer()->getRandomInteger(unusedNo);
00066 while(takenPositions[candidatePosition]) {
00067 candidatePosition++;
00068 }
00069 ch->variables[candidatePosition] = unusedElements[index];
00070 unusedNo--;
00071 unusedElements[index] = unusedElements[unusedNo];
00072 candidatePosition++;
00073 }
00074
00075 if(unusedNo!=0) {
00076 while(takenPositions[candidatePosition]) {
00077 candidatePosition++;
00078 }
00079 ch->variables[candidatePosition] = unusedElements[0];
00080 }
00081
00082
00083 delete[] unusedElements;
00084 delete[] takenPositions;
00085
00086 return true;
00087 }
00088
00089 }