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 | public void setup(EvolutionState state, Parameter base) { |
---|
21 | super.setup(state, base); |
---|
22 | |
---|
23 | Parameter fitnessCalcParameter = base.push(P_FITNESS_CALCULATOR); |
---|
24 | fitnessCalc = (ObjectiveFitnessCalculator) state.parameters |
---|
25 | .getInstanceForParameter(fitnessCalcParameter, null, ObjectiveFitnessCalculator.class); |
---|
26 | fitnessCalc.setup(state, fitnessCalcParameter); |
---|
27 | |
---|
28 | Parameter frequencyParam = base.push(P_FREQUENCY); |
---|
29 | frequency = state.parameters.getIntWithDefault(frequencyParam, null, 1); |
---|
30 | |
---|
31 | Parameter popSizeParameter = new Parameter(P_POP).push(P_SIZE); |
---|
32 | numSubpopulations = state.parameters.getInt(popSizeParameter, null, 0); |
---|
33 | seriesID = new int[numSubpopulations]; |
---|
34 | for (int i = 0; i < numSubpopulations; ++i) { |
---|
35 | seriesID[i] = addSeries("SubPop " + i); |
---|
36 | } |
---|
37 | } |
---|
38 | |
---|
39 | public void postEvaluationStatistics(EvolutionState state) { |
---|
40 | super.postEvaluationStatistics(state); |
---|
41 | |
---|
42 | if (state.generation % frequency != 0) { |
---|
43 | return; |
---|
44 | } |
---|
45 | |
---|
46 | for (int subPop = 0; subPop < numSubpopulations; ++subPop) { |
---|
47 | double averageFitness = 0; |
---|
48 | for (int i = 0; i < state.population.subpops[subPop].individuals.length; ++i) { |
---|
49 | averageFitness += fitnessCalc |
---|
50 | .calculateObjectiveFitness(state, |
---|
51 | state.population.subpops[subPop].individuals[i]); |
---|
52 | } |
---|
53 | averageFitness /= state.population.subpops[subPop].individuals.length; |
---|
54 | |
---|
55 | addDataPoint(seriesID[subPop], state.generation, averageFitness); |
---|
56 | } |
---|
57 | } |
---|
58 | } |
---|