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