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

Last change on this file since 35 was 28, checked in by mszubert, 16 years ago

cecj - coEvolutionary Computation in Java with additional games package

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