[44] | 1 | package cecj.interaction; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.List; |
---|
| 5 | |
---|
| 6 | import cecj.problems.TestBasedProblem; |
---|
| 7 | |
---|
| 8 | import ec.EvolutionState; |
---|
| 9 | import ec.Individual; |
---|
| 10 | import ec.util.Parameter; |
---|
| 11 | |
---|
| 12 | /** |
---|
| 13 | * |
---|
| 14 | * @author Marcin Szubert |
---|
| 15 | * |
---|
| 16 | */ |
---|
| 17 | public 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 | } |
---|
[60] | 49 | |
---|
| 50 | subpopulationRoles = new Role[numSubpopulations]; |
---|
[44] | 51 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
| 52 | Parameter subpopRoleParam = base.push(P_SUBPOP).push("" + subpop).push(P_ROLE); |
---|
| 53 | String role = state.parameters.getString(subpopRoleParam, null); |
---|
| 54 | if (role == null) { |
---|
| 55 | state.output.fatal("Subpopulation role must be specified for the learner-teacher " |
---|
| 56 | + "interactions scheme\n", subpopRoleParam); |
---|
| 57 | } else { |
---|
| 58 | try { |
---|
| 59 | subpopulationRoles[subpop] = Enum.valueOf(Role.class, role.toUpperCase()); |
---|
| 60 | } catch (IllegalArgumentException ex) { |
---|
| 61 | state.output.fatal("Subpopulation role " + role |
---|
| 62 | + " does not exist in the learner-teacher interactions scheme"); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | } |
---|
| 66 | } |
---|
| 67 | |
---|
| 68 | public List<List<InteractionResult>> performInteractions(EvolutionState state, int subpop, |
---|
| 69 | List<List<Individual>> opponents) { |
---|
| 70 | |
---|
| 71 | List<List<InteractionResult>> subpopulationResults = new ArrayList<List<InteractionResult>>(); |
---|
| 72 | Individual[] inds = state.population.subpops[subpop].individuals; |
---|
| 73 | |
---|
| 74 | for (Individual ind : inds) { |
---|
| 75 | List<InteractionResult> results = new ArrayList<InteractionResult>(); |
---|
| 76 | for (int subpop2 = 0; subpop2 < numSubpopulations; subpop2++) { |
---|
| 77 | if (subpopulationRoles[subpop2] != subpopulationRoles[subpop]) { |
---|
| 78 | List<Individual> curOpponents = opponents.get(subpop2); |
---|
| 79 | for (Individual opponent : curOpponents) { |
---|
| 80 | if (subpopulationRoles[subpop] == Role.LEARNER) { |
---|
| 81 | results.add(problem.test(state, ind, opponent).first); |
---|
| 82 | } else { |
---|
| 83 | results.add(problem.test(state, opponent, ind).second); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | } |
---|
| 88 | subpopulationResults.add(results); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return subpopulationResults; |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | public List<Integer> getSubpopulationIndices(Role role) { |
---|
| 95 | List<Integer> result = new ArrayList<Integer>(); |
---|
| 96 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
| 97 | if (subpopulationRoles[subpop] == role) { |
---|
| 98 | result.add(subpop); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | return result; |
---|
| 102 | } |
---|
| 103 | } |
---|