00001 #include "../ECF_base.h"
00002 #include "Permutation.h"
00003 #include <map>
00004
00005
00006 namespace Permutation
00007 {
00008
00009 void PermutationCrsCyclic::registerParameters(StateP state)
00010 {
00011 myGenotype_->registerParameter(state, "crx.cyclic", (voidP) new double(0), ECF::DOUBLE);
00012 }
00013
00014
00015 bool PermutationCrsCyclic::initialize(StateP state)
00016 {
00017 voidP sptr = myGenotype_->getParameterValue(state, "crx.cyclic");
00018 probability_ = *((double*)sptr.get());
00019 return true;
00020 }
00021
00022
00023 bool PermutationCrsCyclic::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 std::map<int, int> invP1, invP2, index;
00030
00031
00032
00033 for (int i = 0; i < (int) p1->getSize(); i++) {
00034 invP1[p1->variables[i]] = i;
00035 invP2[p2->variables[i]] = i;
00036 }
00037
00038
00039 int rand = state_->getRandomizer()->getRandomInteger(0,1);
00040
00041 int j = 0;
00042 do {
00043 do {
00044 if (rand) {
00045 ch->variables[j] = p1->variables[j];
00046 index[j] = 1;
00047 j = invP2[ch->variables[j]];
00048 } else {
00049 ch->variables[j] = p2->variables[j];
00050 index[j] = 1;
00051 j = invP1[ch->variables[j]];
00052 }
00053 } while (!index[j]);
00054 rand = !rand;
00055 j = 0;
00056 while (j < (int) p1->getSize() && index[j])
00057 j++;
00058 } while (j < (int) p1->getSize());
00059
00060 return true;
00061 }
00062
00063 }