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