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 | /** |
---|
13 | * |
---|
14 | * |
---|
15 | * @author Marcin Szubert |
---|
16 | * |
---|
17 | */ |
---|
18 | public class IntraPopulationInteractionScheme implements InteractionScheme { |
---|
19 | |
---|
20 | private SymmetricCompetitionProblem problem; |
---|
21 | |
---|
22 | public void setup(EvolutionState state, Parameter base) { |
---|
23 | if (!(state.evaluator.p_problem instanceof SymmetricCompetitionProblem)) { |
---|
24 | state.output.fatal("Intrapopulation interactions need symmetric problem definition\n"); |
---|
25 | } else { |
---|
26 | problem = (SymmetricCompetitionProblem) state.evaluator.p_problem; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | public List<List<InteractionResult>> performInteractions(EvolutionState state, int subpop, |
---|
31 | List<List<Individual>> opponents) { |
---|
32 | |
---|
33 | List<List<InteractionResult>> subpopulationResults = new ArrayList<List<InteractionResult>>(); |
---|
34 | Individual[] competitors = state.population.subpops[subpop].individuals; |
---|
35 | List<Individual> curOpponents = opponents.get(subpop); |
---|
36 | |
---|
37 | for (Individual competitor : competitors) { |
---|
38 | List<InteractionResult> results = new ArrayList<InteractionResult>(); |
---|
39 | for (Individual opponent : curOpponents) { |
---|
40 | results.add(problem.compete(state, competitor, opponent).first); |
---|
41 | results.add(problem.compete(state, opponent, competitor).second); |
---|
42 | } |
---|
43 | subpopulationResults.add(results); |
---|
44 | } |
---|
45 | |
---|
46 | return subpopulationResults; |
---|
47 | } |
---|
48 | } |
---|