source: java/ecj/cecj/fitness/SimpleSumFitness.java @ 223

Last change on this file since 223 was 193, checked in by Maciej Komosinski, 11 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 1.3 KB
Line 
1package cecj.fitness;
2
3import java.util.Arrays;
4import java.util.List;
5
6import cecj.interaction.InteractionResult;
7
8import ec.EvolutionState;
9import ec.Individual;
10import ec.simple.SimpleFitness;
11
12public 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}
Note: See TracBrowser for help on using the repository browser.