source: java/ecj/interaction/LearnerTeacherInteractionScheme.java @ 42

Last change on this file since 42 was 42, checked in by mszubert, 14 years ago

refactored cecj; framsticks package added

File size: 3.0 KB
Line 
1package cecj.interaction;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import cecj.problems.TestBasedProblem;
7
8import ec.EvolutionState;
9import ec.Individual;
10import ec.util.Parameter;
11
12/**
13 *
14 * @author Marcin Szubert
15 *
16 */
17public class LearnerTeacherInteractionScheme implements InteractionScheme {
18
19        private static final String P_POP = "pop";
20        private static final String P_ROLE = "role";
21        private static final String P_SIZE = "subpops";
22        private static final String P_SUBPOP = "subpop";
23
24        /**
25         * Number of subpopulations.
26         */
27        private int numSubpopulations;
28
29        private TestBasedProblem problem;
30
31        public enum Role {
32                LEARNER, TEACHER
33        }
34
35        private Role[] subpopulationRoles;
36
37        public void setup(EvolutionState state, Parameter base) {
38                if (!(state.evaluator.p_problem instanceof TestBasedProblem)) {
39                        state.output.fatal("Learner-teacher interactions need asymmetric problem definition\n");
40                } else {
41                        problem = (TestBasedProblem) state.evaluator.p_problem;
42                }
43
44                Parameter popSizeParameter = new Parameter(P_POP).push(P_SIZE);
45                numSubpopulations = state.parameters.getInt(popSizeParameter, null, 0);
46                if (numSubpopulations <= 0) {
47                        state.output.fatal("Population size must be > 0.\n", popSizeParameter);
48                }
49
50                for (int subpop = 0; subpop < numSubpopulations; subpop++) {
51                        Parameter subpopRoleParam = base.push(P_SUBPOP).push("" + subpop).push(P_ROLE);
52                        String role = state.parameters.getString(subpopRoleParam, null);
53                        if (role == null) {
54                                state.output.fatal("Subpopulation role must be specified for the learner-teacher "
55                                                + "interactions scheme\n", subpopRoleParam);
56                        } else {
57                                try {
58                                        subpopulationRoles[subpop] = Enum.valueOf(Role.class, role.toUpperCase());
59                                } catch (IllegalArgumentException ex) {
60                                        state.output.fatal("Subpopulation role " + role
61                                                        + " does not exist in the learner-teacher interactions scheme");
62                                }
63                        }
64                }
65        }
66
67        public List<List<InteractionResult>> performInteractions(EvolutionState state, int subpop,
68                        List<List<Individual>> opponents) {
69
70                List<List<InteractionResult>> subpopulationResults = new ArrayList<List<InteractionResult>>();
71                Individual[] inds = state.population.subpops[subpop].individuals;
72
73                for (Individual ind : inds) {
74                        List<InteractionResult> results = new ArrayList<InteractionResult>();
75                        for (int subpop2 = 0; subpop2 < numSubpopulations; subpop2++) {
76                                if (subpopulationRoles[subpop2] != subpopulationRoles[subpop]) {
77                                        List<Individual> curOpponents = opponents.get(subpop2);
78                                        for (Individual opponent : curOpponents) {
79                                                if (subpopulationRoles[subpop] == Role.LEARNER) {
80                                                        results.add(problem.test(state, ind, opponent).first);
81                                                } else {
82                                                        results.add(problem.test(state, opponent, ind).second);
83                                                }
84                                        }
85                                }
86                        }
87                        subpopulationResults.add(results);
88                }
89
90                return subpopulationResults;
91        }
92
93        public List<Integer> getSubpopulationIndices(Role role) {
94                List<Integer> result = new ArrayList<Integer>();
95                for (int subpop = 0; subpop < numSubpopulations; subpop++) {
96                        if (subpopulationRoles[subpop] == role) {
97                                result.add(subpop);
98                        }
99                }
100                return result;
101        }
102}
Note: See TracBrowser for help on using the repository browser.