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