[28] | 1 | package cecj.eval; |
---|
| 2 | |
---|
| 3 | import java.util.ArrayList; |
---|
| 4 | import java.util.List; |
---|
| 5 | |
---|
| 6 | import cecj.fitness.FitnessAggregateMethod; |
---|
| 7 | import cecj.interaction.InteractionResult; |
---|
| 8 | import cecj.interaction.InteractionScheme; |
---|
| 9 | import cecj.sampling.SamplingMethod; |
---|
| 10 | import cecj.statistics.CoevolutionaryStatistics; |
---|
| 11 | |
---|
| 12 | import ec.EvolutionState; |
---|
| 13 | import ec.Individual; |
---|
| 14 | import ec.util.Parameter; |
---|
| 15 | |
---|
| 16 | /** |
---|
| 17 | * Simple coevolutionary evaluator without any additional mechanisms. |
---|
| 18 | * |
---|
| 19 | * It evaluates individuals according to outcomes of its interactions with other individuals. |
---|
| 20 | * Interactions are not restricted to intraspecific or interspecific type, i.e. opponents can be |
---|
| 21 | * choosen from the same population or any other coevolving population. |
---|
| 22 | * |
---|
| 23 | * In contrast to <code>TournamentCoevolutionaryEvaluator</code> all interactions can be simulated |
---|
| 24 | * in any order. There are no sequantial dependencies between interactions. |
---|
| 25 | * |
---|
| 26 | * @author Marcin Szubert |
---|
| 27 | * |
---|
| 28 | */ |
---|
| 29 | public class SimpleCoevolutionaryEvaluator extends CoevolutionaryEvaluator { |
---|
| 30 | |
---|
| 31 | protected static final String P_SUBPOP = "subpop"; |
---|
| 32 | private static final String P_STATISTICS = "statistics"; |
---|
| 33 | private static final String P_FITNESS_METHOD = "fitness-method"; |
---|
| 34 | private static final String P_POP_INDS_WEIGHT = "pop-inds-weight"; |
---|
| 35 | private static final String P_SAMPLING_METHOD = "sampling-method"; |
---|
| 36 | private static final String P_INTERACTION_SCHEME = "interaction-scheme"; |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * Tests used to interact with candidate solutions. |
---|
| 40 | */ |
---|
| 41 | protected List<List<Individual>> opponents; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | * Methods of sampling the opponents from particular populations. |
---|
| 45 | */ |
---|
| 46 | protected SamplingMethod[] samplingMethod; |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * Method of aggregating multiple interaction outcomes into single value. |
---|
| 50 | */ |
---|
| 51 | protected FitnessAggregateMethod[] fitnessAggregateMethod; |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * Specifies how interactions between populations look like. |
---|
| 55 | */ |
---|
| 56 | protected InteractionScheme interactionScheme; |
---|
| 57 | |
---|
| 58 | protected CoevolutionaryStatistics statistics; |
---|
| 59 | |
---|
| 60 | private int popIndsWeight; |
---|
| 61 | |
---|
| 62 | @Override |
---|
| 63 | public void setup(final EvolutionState state, final Parameter base) { |
---|
| 64 | super.setup(state, base); |
---|
| 65 | |
---|
| 66 | Parameter interactionSchemeParam = base.push(P_INTERACTION_SCHEME); |
---|
| 67 | interactionScheme = (InteractionScheme) (state.parameters |
---|
| 68 | .getInstanceForParameter(interactionSchemeParam, null, InteractionScheme.class)); |
---|
| 69 | interactionScheme.setup(state, interactionSchemeParam); |
---|
| 70 | |
---|
| 71 | Parameter popIndsWeightParam = base.push(P_POP_INDS_WEIGHT); |
---|
| 72 | popIndsWeight = state.parameters.getIntWithDefault(popIndsWeightParam, null, 1); |
---|
| 73 | |
---|
| 74 | Parameter statisticsParam = base.push(P_STATISTICS); |
---|
| 75 | if (state.parameters.exists(statisticsParam)) { |
---|
| 76 | statistics = (CoevolutionaryStatistics) (state.parameters |
---|
| 77 | .getInstanceForParameter(statisticsParam, null, CoevolutionaryStatistics.class)); |
---|
| 78 | statistics.setup(state, statisticsParam); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | opponents = new ArrayList<List<Individual>>(numSubpopulations); |
---|
| 82 | samplingMethod = new SamplingMethod[numSubpopulations]; |
---|
| 83 | fitnessAggregateMethod = new FitnessAggregateMethod[numSubpopulations]; |
---|
| 84 | |
---|
| 85 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
| 86 | opponents.add(new ArrayList<Individual>()); |
---|
| 87 | setupSubpopulation(state, base, subpop); |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /** |
---|
| 92 | * Sets up fitness aggregate methods and sampling method for the given subpopulation. |
---|
| 93 | * |
---|
| 94 | * @param state |
---|
| 95 | * the current evolution state |
---|
| 96 | * @param base |
---|
| 97 | * the base parameter |
---|
| 98 | * @param subpop |
---|
| 99 | * the subpopulation index |
---|
| 100 | */ |
---|
| 101 | private void setupSubpopulation(EvolutionState state, Parameter base, int subpop) { |
---|
| 102 | Parameter samplingMethodParam = base.push(P_SUBPOP).push("" + subpop) |
---|
| 103 | .push(P_SAMPLING_METHOD); |
---|
| 104 | samplingMethod[subpop] = (SamplingMethod) (state.parameters |
---|
| 105 | .getInstanceForParameter(samplingMethodParam, null, SamplingMethod.class)); |
---|
| 106 | samplingMethod[subpop].setup(state, samplingMethodParam); |
---|
| 107 | |
---|
| 108 | Parameter fitnessMethodParam = base.push(P_SUBPOP).push("" + subpop).push(P_FITNESS_METHOD); |
---|
| 109 | fitnessAggregateMethod[subpop] = (FitnessAggregateMethod) (state.parameters |
---|
| 110 | .getInstanceForParameter(fitnessMethodParam, null, FitnessAggregateMethod.class)); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | @Override |
---|
| 114 | public void evaluatePopulation(EvolutionState state) { |
---|
| 115 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
| 116 | opponents.set(subpop, findOpponentsFromSubpopulation(state, subpop)); |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
| 120 | List<List<InteractionResult>> subpopulationResults = interactionScheme |
---|
| 121 | .performInteractions(state, subpop, opponents); |
---|
| 122 | |
---|
| 123 | fitnessAggregateMethod[subpop].prepareToAggregate(state, subpop); |
---|
| 124 | fitnessAggregateMethod[subpop].addToAggregate(state, subpop, subpopulationResults, |
---|
| 125 | popIndsWeight); |
---|
| 126 | fitnessAggregateMethod[subpop].assignFitness(state, subpop); |
---|
| 127 | |
---|
| 128 | if (statistics != null) { |
---|
| 129 | statistics.printInteractionResults(state, subpopulationResults, subpop); |
---|
| 130 | } |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | /** |
---|
| 135 | * Samples subpopulation to choose a reference set of individuals. Other individuals can be |
---|
| 136 | * evaluated on the basis of interactions with this reference set. It may happen that |
---|
| 137 | * individuals from the same subpopulation are tested int this way - it depends on |
---|
| 138 | * |
---|
| 139 | * @param subpop |
---|
| 140 | */ |
---|
| 141 | private List<Individual> findOpponentsFromSubpopulation(EvolutionState state, int subpop) { |
---|
| 142 | return samplingMethod[subpop].sample(state, state.population.subpops[subpop].individuals); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | public InteractionScheme getInteractionScheme() { |
---|
| 146 | return interactionScheme; |
---|
| 147 | } |
---|
| 148 | } |
---|