1 | package cecj.eval; |
---|
2 | |
---|
3 | import ec.EvolutionState; |
---|
4 | import ec.Individual; |
---|
5 | import ec.util.Parameter; |
---|
6 | import games.ec.TDLImprover; |
---|
7 | |
---|
8 | public class TDLImprovingEvaluator extends CoevolutionaryEvaluator { |
---|
9 | |
---|
10 | private static final String P_INNER_EVALUATOR = "inner-evaluator"; |
---|
11 | private static final String P_TDL_IMPROVER = "tdl-improver"; |
---|
12 | |
---|
13 | private CoevolutionaryEvaluator innerEvaluator; |
---|
14 | private TDLImprover temporalDifferenceImprover; |
---|
15 | private boolean firstEvaluation = true; |
---|
16 | |
---|
17 | @Override |
---|
18 | public void setup(EvolutionState state, Parameter base) { |
---|
19 | super.setup(state, base); |
---|
20 | |
---|
21 | Parameter innerEvaluatorParam = base.push(P_INNER_EVALUATOR); |
---|
22 | innerEvaluator = (CoevolutionaryEvaluator) (state.parameters |
---|
23 | .getInstanceForParameter(innerEvaluatorParam, null, CoevolutionaryEvaluator.class)); |
---|
24 | innerEvaluator.setup(state, innerEvaluatorParam); |
---|
25 | |
---|
26 | Parameter tdlImproverParam = base.push(P_TDL_IMPROVER); |
---|
27 | temporalDifferenceImprover = (TDLImprover) (state.parameters |
---|
28 | .getInstanceForParameter(tdlImproverParam, null, TDLImprover.class)); |
---|
29 | temporalDifferenceImprover.setup(state, tdlImproverParam); |
---|
30 | } |
---|
31 | |
---|
32 | @Override |
---|
33 | public void evaluatePopulation(EvolutionState state) { |
---|
34 | if (firstEvaluation) { |
---|
35 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
36 | Individual[] inds = state.population.subpops[subpop].individuals; |
---|
37 | for (Individual ind : inds) { |
---|
38 | temporalDifferenceImprover.prepareForImproving(state, ind); |
---|
39 | } |
---|
40 | } |
---|
41 | firstEvaluation = false; |
---|
42 | } |
---|
43 | |
---|
44 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
45 | Individual[] inds = state.population.subpops[subpop].individuals; |
---|
46 | for (Individual ind : inds) { |
---|
47 | temporalDifferenceImprover.improve(state, ind); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | innerEvaluator.evaluatePopulation(state); |
---|
52 | } |
---|
53 | } |
---|