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

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

00001 #include "../ECF_base.h"
00002 #include "Permutation.h"
00003 #include <map>
00004 
00005 
00006 namespace Permutation
00007 {
00008 
00009 void PermutationCrsDPX::registerParameters(StateP state)
00010 {
00011     myGenotype_->registerParameter(state, "crx.DPX", (voidP) new double(0), ECF::DOUBLE);
00012 }
00013 
00014 
00015 bool PermutationCrsDPX::initialize(StateP state)
00016 {
00017     voidP sptr = myGenotype_->getParameterValue(state, "crx.DPX");
00018     probability_ = *((double*)sptr.get());
00019     return true;
00020 }
00021 
00022 
00023 bool PermutationCrsDPX::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 capacity = p1->getSize();
00030 
00031     // Flag positions in child on which values are set from both parents
00032     bool* takenPositions = new bool[capacity];
00033 
00034     // A collection of values which are not copied from first parent;
00035     // it is growable collection of maximally "capacity" elements;
00036     // current size is given by "unusedNo" in which case elements occupy
00037     // positions from 0 to unusedNo-1
00038     int unusedNo = 0;
00039     int* unusedElements = new int[capacity];
00040 
00041     // Mark all positions as unused
00042     for(int i = 0; i < capacity; i++) {
00043         takenPositions[i] = false;
00044     }
00045 
00046     // Go through parents; if values at same index are the same,
00047     // copy value to child at that position and mark position as taken;
00048     // otherwise, add element from first parent to unused elements
00049     for(int ind = 0; ind < capacity; ind++) {
00050         if(p1->variables[ind]==p2->variables[ind]) {
00051             ch->variables[ind] = p1->variables[ind];
00052             takenPositions[ind] = true;
00053         } else {
00054             unusedElements[unusedNo] = p1->variables[ind];
00055             unusedNo++;
00056         }
00057     }
00058 
00059     // Candidate index for next empty position in child:
00060     int candidatePosition = 0;
00061 
00062     // While having at least two unused elements left, pick one randomly
00063     // and add it to child; remove it from unused collection and shrink it.
00064     while(unusedNo > 1) {
00065         int index = state_->getRandomizer()->getRandomInteger(unusedNo);
00066         while(takenPositions[candidatePosition]) {
00067             candidatePosition++;
00068         }
00069         ch->variables[candidatePosition] = unusedElements[index];
00070         unusedNo--;
00071         unusedElements[index] = unusedElements[unusedNo];
00072         candidatePosition++;
00073     }
00074     // If there is remaining unused element, copy it to child:
00075     if(unusedNo!=0) {
00076         while(takenPositions[candidatePosition]) {
00077             candidatePosition++;
00078         }
00079         ch->variables[candidatePosition] = unusedElements[0];
00080     }
00081 
00082     // Remove temporary arrays
00083     delete[] unusedElements;
00084     delete[] takenPositions;
00085 
00086     return true;
00087 }
00088 
00089 }

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