[44] | 1 | package cecj.fitness; |
---|
| 2 | |
---|
| 3 | import java.util.Arrays; |
---|
| 4 | import java.util.List; |
---|
| 5 | |
---|
| 6 | import cecj.interaction.InteractionResult; |
---|
| 7 | |
---|
| 8 | import ec.EvolutionState; |
---|
| 9 | import ec.Individual; |
---|
| 10 | import ec.simple.SimpleFitness; |
---|
| 11 | |
---|
| 12 | public class SimpleSumFitness implements FitnessAggregateMethod { |
---|
| 13 | |
---|
| 14 | private float[] fitnesses; |
---|
| 15 | |
---|
| 16 | public void prepareToAggregate(EvolutionState state, int subpop) { |
---|
| 17 | fitnesses = new float[state.population.subpops[subpop].individuals.length]; |
---|
| 18 | Arrays.fill(fitnesses, 0.0f); |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | public void addToAggregate(EvolutionState state, int subpop, |
---|
| 22 | List<List<InteractionResult>> subpopulationResults, int weight) { |
---|
| 23 | |
---|
| 24 | Individual[] inds = state.population.subpops[subpop].individuals; |
---|
| 25 | if (subpopulationResults.size() != inds.length) { |
---|
| 26 | throw new IllegalArgumentException( |
---|
| 27 | "Results list's size must be equal to subpopulation size."); |
---|
| 28 | } |
---|
| 29 | |
---|
| 30 | for (int ind = 0; ind < inds.length; ind++) { |
---|
| 31 | float fitness = 0; |
---|
| 32 | for (InteractionResult result : subpopulationResults.get(ind)) { |
---|
| 33 | fitness += result.getNumericValue(); |
---|
| 34 | } |
---|
| 35 | fitnesses[ind] += fitness * weight; |
---|
| 36 | } |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | public void assignFitness(EvolutionState state, int subpop) { |
---|
| 40 | Individual[] inds = state.population.subpops[subpop].individuals; |
---|
| 41 | for (int ind = 0; ind < inds.length; ind++) { |
---|
| 42 | ((SimpleFitness) inds[ind].fitness).setFitness(state, fitnesses[ind], false); |
---|
| 43 | } |
---|
| 44 | } |
---|
| 45 | } |
---|