1 | package cecj.statistics; |
---|
2 | |
---|
3 | import ec.EvolutionState; |
---|
4 | import ec.display.chart.XYSeriesChartStatistics; |
---|
5 | import ec.util.Parameter; |
---|
6 | |
---|
7 | public class AverageSubjectiveFitnessChartStatistics extends XYSeriesChartStatistics { |
---|
8 | |
---|
9 | private static final String P_POP = "pop"; |
---|
10 | private static final String P_SIZE = "subpops"; |
---|
11 | |
---|
12 | private int numSubpopulations; |
---|
13 | private int[] seriesID; |
---|
14 | |
---|
15 | @Override |
---|
16 | public void setup(EvolutionState state, Parameter base) { |
---|
17 | super.setup(state, base); |
---|
18 | |
---|
19 | Parameter popSizeParameter = new Parameter(P_POP).push(P_SIZE); |
---|
20 | numSubpopulations = state.parameters.getInt(popSizeParameter, null, 0); |
---|
21 | seriesID = new int[numSubpopulations]; |
---|
22 | |
---|
23 | for (int i = 0; i < numSubpopulations; ++i) { |
---|
24 | seriesID[i] = addSeries("SubPop " + i); |
---|
25 | } |
---|
26 | } |
---|
27 | |
---|
28 | @Override |
---|
29 | public void postEvaluationStatistics(EvolutionState state) { |
---|
30 | super.postEvaluationStatistics(state); |
---|
31 | |
---|
32 | for (int subPop = 0; subPop < numSubpopulations; ++subPop) { |
---|
33 | double averageFitness = 0; |
---|
34 | for (int i = 0; i < state.population.subpops[subPop].individuals.length; ++i) { |
---|
35 | averageFitness += state.population.subpops[subPop].individuals[i].fitness.fitness(); |
---|
36 | } |
---|
37 | averageFitness /= state.population.subpops[subPop].individuals.length; |
---|
38 | |
---|
39 | addDataPoint(seriesID[subPop], state.generation, averageFitness); |
---|
40 | } |
---|
41 | } |
---|
42 | } |
---|