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

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

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

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