1 | package cecj.statistics; |
---|
2 | |
---|
3 | import ec.EvolutionState; |
---|
4 | import ec.display.chart.XYSeriesChartStatistics; |
---|
5 | import ec.util.Parameter; |
---|
6 | |
---|
7 | public class AverageObjectiveFitnessChartStatistics extends XYSeriesChartStatistics { |
---|
8 | |
---|
9 | private static final String P_POP = "pop"; |
---|
10 | private static final String P_SIZE = "subpops"; |
---|
11 | private static final String P_FREQUENCY = "frequency"; |
---|
12 | private static final String P_FITNESS_CALCULATOR = "fitness-calc"; |
---|
13 | |
---|
14 | private int numSubpopulations; |
---|
15 | private int[] seriesID; |
---|
16 | private int frequency; |
---|
17 | |
---|
18 | private ObjectiveFitnessCalculator fitnessCalc; |
---|
19 | |
---|
20 | @Override |
---|
21 | public void setup(EvolutionState state, Parameter base) { |
---|
22 | super.setup(state, base); |
---|
23 | |
---|
24 | Parameter fitnessCalcParameter = base.push(P_FITNESS_CALCULATOR); |
---|
25 | fitnessCalc = (ObjectiveFitnessCalculator) state.parameters |
---|
26 | .getInstanceForParameter(fitnessCalcParameter, null, ObjectiveFitnessCalculator.class); |
---|
27 | fitnessCalc.setup(state, fitnessCalcParameter); |
---|
28 | |
---|
29 | Parameter frequencyParam = base.push(P_FREQUENCY); |
---|
30 | frequency = state.parameters.getIntWithDefault(frequencyParam, null, 1); |
---|
31 | |
---|
32 | Parameter popSizeParameter = new Parameter(P_POP).push(P_SIZE); |
---|
33 | numSubpopulations = state.parameters.getInt(popSizeParameter, null, 0); |
---|
34 | seriesID = new int[numSubpopulations]; |
---|
35 | for (int i = 0; i < numSubpopulations; ++i) { |
---|
36 | seriesID[i] = addSeries("SubPop " + i); |
---|
37 | } |
---|
38 | } |
---|
39 | |
---|
40 | @Override |
---|
41 | public void postEvaluationStatistics(EvolutionState state) { |
---|
42 | super.postEvaluationStatistics(state); |
---|
43 | |
---|
44 | if (state.generation % frequency != 0) { |
---|
45 | return; |
---|
46 | } |
---|
47 | |
---|
48 | for (int subPop = 0; subPop < numSubpopulations; ++subPop) { |
---|
49 | double averageFitness = 0; |
---|
50 | for (int i = 0; i < state.population.subpops[subPop].individuals.length; ++i) { |
---|
51 | averageFitness += fitnessCalc |
---|
52 | .calculateObjectiveFitness(state, |
---|
53 | state.population.subpops[subPop].individuals[i]); |
---|
54 | } |
---|
55 | averageFitness /= state.population.subpops[subPop].individuals.length; |
---|
56 | |
---|
57 | addDataPoint(seriesID[subPop], state.generation, averageFitness); |
---|
58 | } |
---|
59 | } |
---|
60 | } |
---|