• Main Page
  • Classes
  • Files
  • File List

D:/Radagast_D/Projekt/ECF_trunk/ECF/AlgSteadyStateTournament.cpp

00001 #include "ECF_base.h"
00002 #include "ECF_derived.h"
00003 #include "ECF_macro.h"
00004 
00005 
00006 SteadyStateTournament::SteadyStateTournament()
00007 {
00008     // define algorithm name
00009     name_ = "SteadyStateTournament";
00010 
00011     // create selection operators needed
00012     // in this case, SelRandomOp and SelWorstOp
00013     selRandomOp = static_cast<SelectionOperatorP> (new SelRandomOp);
00014     selWorstOp = static_cast<SelectionOperatorP> (new SelWorstOp);
00015 }
00016 
00017 
00018 void SteadyStateTournament::registerParameters(StateP state)
00019 {
00020     registerParameter(state, "tsize", (voidP) new uint(3), ECF::UINT);
00021 }
00022 
00023 
00024 bool SteadyStateTournament::initialize(StateP state)
00025 {
00026     // initialize all operators
00027     selRandomOp->initialize(state);
00028     selWorstOp->initialize(state);
00029 
00030     // read parameter values 
00031     voidP tsizep = getParameterValue(state, "tsize");
00032     nTournament_ = *((uint*) tsizep.get());
00033 
00034     if(nTournament_ < 3) {
00035         state->getLogger()->log(1, "Error: SteadyStateTournament algorithm requires minimum tournament size of 3!");
00036         throw "";
00037     }
00038 
00039     return true;
00040 }
00041 
00042 
00043 bool SteadyStateTournament::advanceGeneration(StateP state, DemeP deme)
00044 {
00046     for(uint iIter = 0; iIter < deme->size(); iIter++) {
00047 
00048         state->getLogger()->log(5, "Individuals in tournament: ");
00049 
00050         std::vector<IndividualP> tournament;
00051         for (uint i = 0; i < nTournament_; ++i) {
00052             // select a random individual for the tournament
00053             tournament.push_back(selRandomOp->select(*deme));
00054             state->getLogger()->log(5, "\t" + dbl2str(tournament[i]->fitness->getValue()));
00055         }
00056 
00057         // select the worst from the tournament
00058         IndividualP worst = selWorstOp->select(tournament);
00059         state->getLogger()->log(5, "The worst from the tournament: " + dbl2str(worst->fitness->getValue()));
00060 
00061         // remove pointer to 'worst' individual from vector 'tournament'
00062         removeFrom(worst, tournament);
00063 
00064         // crossover the first two individuals in the tournament
00065         mate(tournament[0], tournament[1], worst);
00066 
00067         // perform mutation on new individual
00068         mutate(worst);
00069 
00070         // create new fitness
00071         evaluate(worst);
00072         state->getLogger()->log(5, "New individual: " + dbl2str(worst->fitness->getValue()));
00073     }
00074 
00075     for(uint i = 0; i < deme->size(); i++){
00076         state->getLogger()->log(5, "deme[" + uint2str(i) + "]: " + dbl2str(deme->at(i)->fitness->getValue()) + "\t" + uint2str(deme->at(i)->index));
00077     }
00078 
00079     return true;
00080 }

Generated on Wed Sep 1 2010 14:31:21 for ECF by  doxygen 1.7.1