• 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 }
00024 
00025 
00026 bool SteadyStateTournament::initialize(StateP state)
00027 {
00028     // initialize all operators
00029     selRandomOp->initialize(state);
00030     selWorstOp->initialize(state);
00031 
00032     // read parameter values 
00033     voidP tsizep = getParameterValue(state, "tsize");
00034     nTournament_ = *((uint*) tsizep.get());
00035 
00036     if(nTournament_ < 3) {
00037         ECF_LOG(state, 1, "Error: SteadyStateTournament algorithm requires minimum tournament size of 3!");
00038         throw "";
00039     }
00040 
00041     return true;
00042 }
00043 
00044 
00045 bool SteadyStateTournament::advanceGeneration(StateP state, DemeP deme)
00046 {
00048     for(uint iIter = 0; iIter < deme->size(); iIter++) {
00049 
00050         ECF_LOG(state, 5, "Individuals in tournament: ");
00051 
00052         std::vector<IndividualP> tournament;
00053         for (uint i = 0; i < nTournament_; ++i) {
00054             // select a random individual for the tournament
00055             tournament.push_back(selRandomOp->select(*deme));
00056             ECF_LOG(state, 5, uint2str(i) + ": " + tournament[i]->toString());
00057         }
00058 
00059         // select the worst from the tournament
00060         IndividualP worst = selWorstOp->select(tournament);
00061         ECF_LOG(state, 5, "The worst from the tournament: " + worst->toString());
00062 
00063         // remove pointer to 'worst' individual from vector 'tournament'
00064         removeFrom(worst, tournament);
00065 
00066         // crossover the first two (remaining) individuals in the tournament
00067         mate(tournament[0], tournament[1], worst);
00068 
00069         // perform mutation on new individual
00070         mutate(worst);
00071 
00072         // create new fitness
00073         evaluate(worst);
00074         ECF_LOG(state, 5, "New individual: " + worst->toString());
00075     }
00076 
00077     return true;
00078 }

Generated on Fri Jul 5 2013 09:34:23 for ECF by  doxygen 1.7.1