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

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

00001 #include "../ECF_base.h"
00002 #include "Permutation.h"
00003 #include <map>
00004 
00005 
00006 namespace Permutation
00007 {
00008 
00009 void PermutationCrsSPX::registerParameters(StateP state)
00010 {
00011     myGenotype_->registerParameter(state, "crx.SPX", (voidP) new double(0), ECF::DOUBLE);
00012 }
00013 
00014 
00015 bool PermutationCrsSPX::initialize(StateP state)
00016 {
00017     voidP sptr = myGenotype_->getParameterValue(state, "crx.SPX");
00018     probability_ = *((double*)sptr.get());
00019     return true;
00020 }
00021 
00022 
00023 bool PermutationCrsSPX::mate(GenotypeP gen1, GenotypeP gen2, GenotypeP child)
00024 {
00025     Permutation* p1 = (Permutation*) (gen1.get());
00026     Permutation* p2 = (Permutation*) (gen2.get());
00027 
00028     // uzmimo radije smart pointer, da ne moramo rucno brisati visak
00029     PermutationP ch = boost::static_pointer_cast<Permutation> (child);
00030 
00031     // stvori kopiju genotipa
00032     PermutationP ch2(ch->copy());
00033     // dohvati jedinku dijete
00034     IndividualP myInd = state_->getContext()->child;
00035     // dohvati redni broj genotipa
00036     uint gId = child->getGenotypeId();
00037 
00038     // definiraj novi ch i ch2
00039 
00040     //Make ch clone of parent1, ch2 clone of parent2
00041     int capacity = ch->getSize();
00042     for(int i = 0; i < capacity;  i++) {
00043         ch->variables[i] = p1->variables[i];
00044         ch2->variables[i] = p2->variables[i];
00045     }
00046 
00047     // Pick two different positions, ensure pos1 < pos2:
00048     int pos1 = state_->getRandomizer()->getRandomInteger(capacity);
00049     int pos2 = state_->getRandomizer()->getRandomInteger(capacity);
00050     if(pos1==pos2) {
00051         if(pos1==0) {
00052             pos2++;
00053         } else {
00054             pos1--;
00055         }
00056     } else if(pos2<pos1) {
00057         int t = pos1;
00058         pos1 = pos2;
00059         pos2 = t;
00060     }
00061 
00062     // Swap selected element in first child
00063     int elem1 = p1->variables[pos1];
00064     int elem2 = p1->variables[pos2];
00065     ch->variables[pos1] = elem2;
00066     ch->variables[pos2] = elem1;
00067 
00068     // Find locations of selected elements in second child:
00069     for(int i = 0; i < capacity;  i++) {
00070         if(p2->variables[i]==elem1) {
00071             pos1 = i;
00072             break;
00073         }
00074     }
00075     for(int i = 0; i < capacity;  i++) {
00076         if(p2->variables[i]==elem2) {
00077             pos2 = i;
00078             break;
00079         }
00080     }
00081 
00082     // Swap selected element in second child
00083     ch2->variables[pos1] = elem2;
00084     ch2->variables[pos2] = elem1;
00085 
00086     FitnessP chFitness, ch2Fitness;
00087     // evaluiraj dijete uz ch
00088     state_->getAlgorithm()->evaluate(myInd);
00089     chFitness = myInd->fitness;
00090 
00091     // umetni ch2 u jedinku dijete
00092     myInd->at(gId) = (GenotypeP) ch2;
00093     // 'rucno' smo promijenili jedinku, pa to oznaci (za svaki slucaj...)
00094     myInd->fitness->setInvalid();
00095     // evaluiraj dijete uz ch2
00096     state_->getAlgorithm()->evaluate(myInd);
00097     ch2Fitness = myInd->fitness;
00098 
00099     // ako je ch bio bolji, trebamo ga vratiti
00100     if(chFitness->isBetterThan(ch2Fitness)) {
00101         myInd->at(gId) = (GenotypeP) ch;
00102         // ne zaboravimo fitness! (da se ne evaluira ponovo)
00103         myInd->fitness = chFitness;
00104     }
00105 
00106     return true;
00107 }
00108 
00109 }

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