ECF 1.5
WriteBest.h
1//
2// pomocni operator za ispis trenutno najboljeg rjesenja
3//
4
5bool evaluateVerbose;
6
7class WriteBest : public Operator
8{
9private:
10 StateP state_;
11 uint freq, maxGen, eval;
12 double fitnessVal;
13 bool termEval, termFitness, termStagn, termGen;
14
15public:
16
17 bool initialize(StateP state)
18 {
19 termEval = termFitness = termStagn = termGen = false;
20
21 if(state->getRegistry()->isModified("term.eval"))
22 termEval = true;
23 if(state->getRegistry()->isModified("term.stagnation"))
24 termStagn = true;
25 if(state->getRegistry()->isModified("term.fitnessval"))
26 termFitness = true;
27 if(state->getRegistry()->isModified("term.maxgen"))
28 termGen = true;
29
30 state_ = state;
31 voidP sptr = state->getRegistry()->getEntry("term.stagnation");
32 freq = *((uint*)sptr.get());
33
34 sptr = state->getRegistry()->getEntry("term.maxgen");
35 maxGen = *((uint*)sptr.get());
36
37 sptr = state->getRegistry()->getEntry("term.eval");
38 eval = *((uint*)sptr.get());
39
40 sptr = state->getRegistry()->getEntry("term.fitnessval");
41 fitnessVal = *((double*)sptr.get());
42
43 return true;
44 }
45
46 bool operate(StateP state)
47 {
48 ECF_LOG(state, 3, "Best in " + uint2str(state->getGenerationNo()));
49 IndividualP bestInd = state->getPopulation()->getHof()->getBest().at(0);
50 ECF_LOG(state, 3, bestInd->toString());
51
52 bool output = false;
53 uint currentGen = state->getGenerationNo();
54 double minFitness = state->getStats()->getFitnessMin();
55 double maxFitness = state->getStats()->getFitnessMax();
56
57 if(termGen && currentGen >= maxGen)
58 output = true;
59
60 if(termStagn && (currentGen - state->getPopulation()->getHof()->getLastChange()) > freq)
61 output = true;
62
63 if(termEval && state->getEvaluations() >= eval)
64 output = true;
65
66 if(termFitness && (fitnessVal >= minFitness && fitnessVal <= maxFitness))
67 output = true;
68
69 if (output) {
70 std::vector<IndividualP> hof = state->getHoF()->getBest();
71 IndividualP ind = hof[0];
72
73 evaluateVerbose = true;
74 state->getAlgorithm()->evaluate(ind);
75 evaluateVerbose = false;
76
77 ECF_LOG(state, 1, ind->toString());
78 }
79
80 return true;
81 }
82};
Abstract operator class.
Definition: Operator.h:11
bool operate(StateP state)
perform the designated operation
Definition: WriteBest.h:46
bool initialize(StateP state)
Perform initialization. Called before Operator::operate. By default, if the return value is false,...
Definition: WriteBest.h:17