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

D:/Projekt/ECF_trunk/ECF/binary/BinaryCrsShuffle.cpp

00001 #include "../ECF_base.h"
00002 #include "Binary.h"
00003 #include <algorithm>
00004 
00005 namespace Binary
00006 {
00007 
00008 void BinaryCrsShuffle::registerParameters(StateP state)
00009 {
00010     myGenotype_->registerParameter(state, "crx.shuffle", (voidP) new double(0), ECF::DOUBLE);
00011 }
00012 
00013 
00014 bool BinaryCrsShuffle::initialize(StateP state)
00015 {
00016     voidP sptr = myGenotype_->getParameterValue(state, "crx.shuffle");
00017     probability_ = *((double*)sptr.get());
00018     return true;
00019 }
00020     
00021 bool BinaryCrsShuffle::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00022 {
00023     Binary* p1 = (Binary*) (gen1.get());
00024     Binary* p2 = (Binary*) (gen2.get());
00025     Binary* ch = (Binary*) (child.get());
00026 
00027     Binary* p1copy = (Binary*) (gen1->copy());
00028     Binary* p2copy = (Binary*) (gen2->copy());
00029     Binary* chcopy = (Binary*) (child->copy());
00030 
00031     //uint rand1, rand2;
00032     //bool temp;
00033     typedef std::vector<uint> v_uint;
00034 
00035     //v_uint pFrom;
00036     //v_uint pTo;
00037 
00038     v_uint mapiranje;
00039 
00040     for (uint i = 0; i < p1->getNumBits(); i++) { //this is initial arrangement
00041         mapiranje.push_back(i);
00042     }
00043 
00044     std::random_shuffle(mapiranje.begin(), mapiranje.end()); //jel moze ovo
00045 
00046     uint bitCrs = state_->getRandomizer()->getRandomInteger(p1->getNumBits());
00047 
00048 
00049     //sada citam redom sto pise u vectoru mapa i u kopije roditelja spremam na novu poziciju vrijednosti iz originalnih roditelja
00050 
00051     for (uint dimension = 0; dimension < p1->variables.size(); dimension++) {
00052     //shuffle parents
00053         for (uint i = 0; i < p1->getNumBits(); i++) {
00054             
00055             p1copy->variables[dimension][i] = p1->variables[dimension][mapiranje[i]];
00056             p2copy->variables[dimension][i] = p2->variables[dimension][mapiranje[i]];
00057         }
00058 
00059         //shuffle done, now just one-point crossover
00060         switch (state_->getRandomizer()->getRandomInteger(0, 1)) {
00061             case 0: for (uint i = 0; i < bitCrs; i++) {
00062                         chcopy->variables[dimension][i] = p1copy->variables[dimension][i];
00063                     }
00064                     for (uint i = bitCrs; i < p2->getNumBits(); i++) {
00065                         chcopy->variables[dimension][i] = p2copy->variables[dimension][i];
00066                     }
00067                     break;
00068             case 1: for (uint i = 0; i < bitCrs; i++) {
00069                         chcopy->variables[dimension][i] = p2copy->variables[dimension][i];
00070                     }
00071                     for (uint i = bitCrs; i < p1->getNumBits(); i++) {
00072                         chcopy->variables[dimension][i] = p1copy->variables[dimension][i];
00073                     }
00074         }
00075         //unshuffle child
00076         for (uint i = 0; i < p1->getNumBits(); i++) {
00077             ch->variables[dimension][mapiranje[i]] = chcopy->variables[dimension][i];
00078         }
00079     }
00080 
00081     // update real domain representation
00082     ch->update();
00083 
00084     delete p1copy;
00085     delete p2copy;
00086     delete chcopy;
00087 
00088     return true;
00089 }
00090 
00091 }

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