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
00011 name_ = "SteadyStateTournament";
00012
00013
00014
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
00030 selRandomOp->initialize(state);
00031 selWorstOp->initialize(state);
00032
00033
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
00056 tournament.push_back(selRandomOp->select(*deme));
00057 ECF_LOG(state, 5, uint2str(i) + ": " + tournament[i]->toString());
00058 }
00059
00060
00061 IndividualP worst = selWorstOp->select(tournament);
00062 ECF_LOG(state, 5, "The worst from the tournament: " + worst->toString());
00063
00064
00065 removeFrom(worst, tournament);
00066
00067
00068 mate(tournament[0], tournament[1], worst);
00069
00070
00071 mutate(worst);
00072
00073
00074 evaluate(worst);
00075 ECF_LOG(state, 5, "New individual: " + worst->toString());
00076 }
00077
00078 return true;
00079 }