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