00001 #include "ECF_base.h"
00002 #include "TermStagnationOp.h"
00003 #include <cmath>
00004
00005
00006 void TermStagnationOp::registerParameters(StateP state)
00007 {
00008 uint *value = new uint(0);
00009 state->getRegistry()->registerEntry("term.stagnation", (voidP) value, ECF::UINT);
00010 }
00011
00012
00013 bool TermStagnationOp::initialize(StateP state)
00014 {
00015 voidP sptr = state->getRegistry()->getEntry("term.stagnation");
00016 termStagnation_ = *((uint*) sptr.get());
00017
00018
00019 if(termStagnation_ == 0) {
00020 voidP sptr = state->getRegistry()->getEntry("population.size");
00021 uint demeSize = *((uint*) sptr.get());
00022 termStagnation_ = 5000 / demeSize;
00023 if(termStagnation_ < 10)
00024 termStagnation_ = 5;
00025 if(termStagnation_ > 200)
00026 termStagnation_ = 200;
00027 }
00028
00029 if(!state->getRegistry()->isModified("term.stagnation"))
00030 return false;
00031
00032 return true;
00033 }
00034
00035
00036 bool TermStagnationOp::operate(StateP state)
00037 {
00038 uint currentGen = state->getGenerationNo();
00039 if(currentGen - state->getPopulation()->getHof()->getLastChange() > termStagnation_) {
00040 state->setTerminateCond();
00041 ECF_LOG(state, 1, "Termination: maximum number of generations without improvement ("
00042 + uint2str(termStagnation_) + ") reached");
00043 }
00044
00045 return true;
00046 }