source: java/ecj/cecj/interaction/InterPopulationInteractionScheme.java @ 1114

Last change on this file since 1114 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.8 KB
Line 
1package cecj.interaction;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import cecj.problems.SymmetricCompetitionProblem;
7
8import ec.EvolutionState;
9import ec.Individual;
10import ec.util.Parameter;
11
12public class InterPopulationInteractionScheme implements InteractionScheme {
13
14        private static final String P_POP = "pop";
15        private static final String P_SIZE = "subpops";
16
17        /**
18         * Number of subpopulations.
19         */
20        private int numSubpopulations;
21
22        private SymmetricCompetitionProblem problem;
23
24        public void setup(EvolutionState state, Parameter base) {
25                if (!(state.evaluator.p_problem instanceof SymmetricCompetitionProblem)) {
26                        state.output.fatal("Intrapopulation interactions need symmetric problem definition\n");
27                } else {
28                        problem = (SymmetricCompetitionProblem) state.evaluator.p_problem;
29                }
30
31                Parameter popSizeParameter = new Parameter(P_POP).push(P_SIZE);
32                numSubpopulations = state.parameters.getInt(popSizeParameter, null, 0);
33                if (numSubpopulations <= 0) {
34                        state.output.fatal("Population size must be > 0.\n", popSizeParameter);
35                }
36        }
37
38        public List<List<InteractionResult>> performInteractions(EvolutionState state, int subpop,
39                        List<List<Individual>> opponents) {
40                List<List<InteractionResult>> subpopulationResults = new ArrayList<List<InteractionResult>>();
41                Individual[] competitors = state.population.subpops[subpop].individuals;
42
43                for (Individual competitor : competitors) {
44                        List<InteractionResult> results = new ArrayList<InteractionResult>();
45
46                        for (int subpop2 = 0; subpop2 < numSubpopulations; subpop2++) {
47                                if (subpop2 == subpop) {
48                                        continue;
49                                }
50
51                                List<Individual> curOpponents = opponents.get(subpop2);
52                                for (Individual opponent : curOpponents) {
53                                        results.add(problem.compete(state, competitor, opponent).first);
54                                }
55                        }
56
57                        subpopulationResults.add(results);
58                }
59
60                return subpopulationResults;
61        }
62}
Note: See TracBrowser for help on using the repository browser.