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