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