• Main Page
  • Modules
  • Classes
  • Files
  • File List

D:/Projekt/ECF_trunk/ECF/permutation/PermutationCrsOX.cpp

00001 #include "../ECF_base.h"
00002 #include "Permutation.h"
00003 #include <map>
00004 
00005 
00006 namespace Permutation
00007 {
00008 
00009 void PermutationCrsOX::registerParameters(StateP state)
00010 {
00011     myGenotype_->registerParameter(state, "crx.OX", (voidP) new double(0), ECF::DOUBLE);
00012 }
00013 
00014 
00015 bool PermutationCrsOX::initialize(StateP state)
00016 {
00017     voidP sptr = myGenotype_->getParameterValue(state, "crx.OX");
00018     probability_ = *((double*)sptr.get());
00019     return true;
00020 }
00021 
00022 
00023 bool PermutationCrsOX::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 start = state_->getRandomizer()->getRandomInteger(p1->getSize());
00030     int end = state_->getRandomizer()->getRandomInteger(start, p1->getSize() - 1);
00031 
00032     std::map<int, int> subSet;
00033     
00034     int ind1 = state_->getRandomizer()->getRandomInteger(p1->getSize() - 1);
00035     int ind2 = state_->getRandomizer()->getRandomInteger(ind1, p1->getSize() - 1);
00036 
00037     // copy subvector into child
00038     for(int i = ind1; i <= ind2; i++) {
00039         ch->variables[i] = p1->variables[i];
00040         subSet[p1->variables[i]] = 0;
00041     }
00042     int indexChild = (ind2 + 1) % p1->getSize();
00043     std::map<int, int>::iterator iter = subSet.begin();
00044 
00045     // krecemo od indeksa za jedan vise od indeksa zadnjeg elementa podniza
00046     // i kopiramo iz drugog roditelja u dijete
00047     // ako naidjemo na element koji se vec nalazi u podnizu idemo dalje 
00048     // postupak se zaustavlja kada popunimo cijelo dijete
00049     do {
00050         ind2 = (++ind2) % p1->getSize();
00051         if(subSet.find(p2->variables[ind2]) == subSet.end()) {
00052             ch->variables[indexChild] = p2->variables[ind2];
00053             indexChild = (++indexChild) % p1->getSize();
00054         }
00055     } while(indexChild != ind1);
00056 
00057     return true;
00058 }
00059 
00060 }

Generated on Tue Nov 4 2014 13:04:31 for ECF by  doxygen 1.7.1