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