1 | package cecj.eval; |
---|
2 | |
---|
3 | import java.util.ArrayList; |
---|
4 | import java.util.List; |
---|
5 | |
---|
6 | import cecj.archive.ArchivingSubpopulation; |
---|
7 | import cecj.archive.CoevolutionaryArchive; |
---|
8 | import cecj.interaction.InteractionResult; |
---|
9 | import cecj.sampling.SamplingMethod; |
---|
10 | |
---|
11 | import ec.EvolutionState; |
---|
12 | import ec.Individual; |
---|
13 | import ec.util.Parameter; |
---|
14 | |
---|
15 | public class ArchivingCoevolutionaryEvaluator extends SimpleCoevolutionaryEvaluator { |
---|
16 | |
---|
17 | private static final String P_ARCHIVE = "archive"; |
---|
18 | private static final String P_ARCHIVE_INDS_WEIGHT = "archive-inds-weight"; |
---|
19 | private static final String P_ARCHIVE_SAMPLING_METHOD = "archive-sampling-method"; |
---|
20 | |
---|
21 | private CoevolutionaryArchive archive; |
---|
22 | |
---|
23 | private List<List<Individual>> archiveOpponents; |
---|
24 | |
---|
25 | private SamplingMethod[] archiveSamplingMethod; |
---|
26 | |
---|
27 | private int archiveIndsWeight; |
---|
28 | |
---|
29 | @Override |
---|
30 | public void setup(final EvolutionState state, final Parameter base) { |
---|
31 | super.setup(state, base); |
---|
32 | |
---|
33 | Parameter archiveParam = base.push(P_ARCHIVE); |
---|
34 | archive = (CoevolutionaryArchive) (state.parameters |
---|
35 | .getInstanceForParameter(archiveParam, null, CoevolutionaryArchive.class)); |
---|
36 | archive.setup(state, base.push(P_ARCHIVE)); |
---|
37 | |
---|
38 | Parameter archiveIndsWeightParam = base.push(P_ARCHIVE_INDS_WEIGHT); |
---|
39 | archiveIndsWeight = state.parameters.getIntWithDefault(archiveIndsWeightParam, null, 1); |
---|
40 | |
---|
41 | archiveOpponents = new ArrayList<List<Individual>>(numSubpopulations); |
---|
42 | archiveSamplingMethod = new SamplingMethod[numSubpopulations]; |
---|
43 | |
---|
44 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
45 | archiveOpponents.add(new ArrayList<Individual>()); |
---|
46 | setupArchivingSubpopulation(state, base, subpop); |
---|
47 | } |
---|
48 | } |
---|
49 | |
---|
50 | private void setupArchivingSubpopulation(EvolutionState state, Parameter base, int subpop) { |
---|
51 | Parameter samplingMethodParam = base.push(P_SUBPOP).push("" + subpop) |
---|
52 | .push(P_ARCHIVE_SAMPLING_METHOD); |
---|
53 | archiveSamplingMethod[subpop] = (SamplingMethod) (state.parameters |
---|
54 | .getInstanceForParameter(samplingMethodParam, null, SamplingMethod.class)); |
---|
55 | archiveSamplingMethod[subpop].setup(state, samplingMethodParam); |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | public void evaluatePopulation(EvolutionState state) { |
---|
60 | if (!(state.population.subpops[0] instanceof ArchivingSubpopulation)) { |
---|
61 | state.output.fatal("Archiving evaluator requires archiving subpopulation"); |
---|
62 | } |
---|
63 | |
---|
64 | super.evaluatePopulation(state); |
---|
65 | |
---|
66 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
67 | archiveOpponents.set(subpop, findOpponentsFromArchive(state, subpop)); |
---|
68 | } |
---|
69 | |
---|
70 | for (int subpop = 0; subpop < numSubpopulations; subpop++) { |
---|
71 | List<List<InteractionResult>> subpopulationResults = interactionScheme |
---|
72 | .performInteractions(state, subpop, archiveOpponents); |
---|
73 | |
---|
74 | fitnessAggregateMethod[subpop].addToAggregate(state, subpop, subpopulationResults, |
---|
75 | archiveIndsWeight); |
---|
76 | fitnessAggregateMethod[subpop].assignFitness(state, subpop); |
---|
77 | |
---|
78 | if (statistics != null) { |
---|
79 | statistics.printInteractionResults(state, subpopulationResults, subpop); |
---|
80 | } |
---|
81 | } |
---|
82 | |
---|
83 | archive.submit(state); |
---|
84 | } |
---|
85 | |
---|
86 | private List<Individual> findOpponentsFromArchive(EvolutionState state, int subpop) { |
---|
87 | List<Individual> archive = ((ArchivingSubpopulation) state.population.subpops[subpop]) |
---|
88 | .getArchivalIndividuals(); |
---|
89 | return archiveSamplingMethod[subpop].sample(state, archive); |
---|
90 | } |
---|
91 | } |
---|