1 | package cecj.interaction; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import cecj.problems.SymmetricCompetitionProblem; |
---|
7 | |
---|
8 | import ec.EvolutionState; |
---|
9 | import ec.Individual; |
---|
10 | import ec.util.Parameter; |
---|
11 | |
---|
12 | public 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 | } |
---|