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

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

00001 #include "../ECF_base.h"
00002 #include "Permutation.h"
00003 #include <map>
00004 
00005 
00006 namespace Permutation
00007 {
00008 
00009 void PermutationCrsOPX::registerParameters(StateP state)
00010 {
00011     myGenotype_->registerParameter(state, "crx.OPX", (voidP) new double(0), ECF::DOUBLE);
00012 }
00013 
00014 
00015 bool PermutationCrsOPX::initialize(StateP state)
00016 {
00017     voidP sptr = myGenotype_->getParameterValue(state, "crx.OPX");
00018     probability_ = *((double*)sptr.get());
00019     return true;
00020 }
00021 
00022 
00023 bool PermutationCrsOPX::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 position = state_->getRandomizer()->getRandomInteger(p1->getSize()-1);
00030 
00031     // set representing values copied from first parent
00032     std::map<int, int> subSet;
00033 
00034     // copy first part of first parent (up to element at "position") into child
00035     for(int i = 0; i <= position; i++) {
00036         ch->variables[i] = p1->variables[i];
00037         // remember visited element
00038         subSet[p1->variables[i]] = 0;
00039     }
00040 
00041     // How many values are left?
00042     int left = p1->getSize() - position - 1;
00043     // Continue filling child from next position:
00044     int indexChild = position + 1;
00045 
00046     // Go through each value of second parent in correct order;
00047     // if value is not already visited, copy it to child in first available position
00048     for(int ind2 = 0; ind2 < (int) p1->getSize() && left>0; ind2++) {
00049         if(subSet.find(p2->variables[ind2]) == subSet.end()) {
00050             ch->variables[indexChild] = p2->variables[ind2];
00051             left--;
00052             indexChild++;
00053         }
00054     }
00055 
00056     return true;
00057 }
00058 
00059 }

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