source: java/ecj/cecj/eval/ArchivingCoevolutionaryEvaluator.java @ 313

Last change on this file since 313 was 193, checked in by Maciej Komosinski, 11 years ago

Set svn:eol-style native for all textual files

  • Property svn:eol-style set to native
File size: 4.4 KB
Line 
1/*
2  Copyright 2009 by Marcin Szubert
3  Licensed under the Academic Free License version 3.0
4 */
5
6package cecj.eval;
7
8import java.util.ArrayList;
9import java.util.List;
10
11import cecj.archive.ArchivingSubpopulation;
12import cecj.archive.CoevolutionaryArchive;
13import cecj.interaction.InteractionResult;
14import cecj.sampling.SamplingMethod;
15
16import ec.EvolutionState;
17import ec.Individual;
18import ec.util.Parameter;
19
20/**
21 * Extends the simple evaluation process with an archiving mechanism.
22 *
23 * The evaluation procedure is realized in the following manner. Firstly, after taking simple
24 * evaluation steps as in the superclass, additional opponents are selected among archival
25 * individuals (an archive is maintained for each subpopulation). Outcomes of the interactions with
26 * such opponents are added to results obtained by the superclass and aggregated together.
27 * Eventually, subpopulation individuals are submitted to the archive which decides if any of them
28 * is worth keeping. While interaction scheme and fitness aggregation method are inherited from the
29 * <code>SimpleCoevolutionaryEvaluator</code>, archival sampling methods must be defined separately
30 * for each of the archives.
31 *
32 * Often, opponents sampled from the population are less competent than these from the archive; to
33 * address this issue, additional parameters were created that specify the relative importance of
34 * opponents from both sources - <code>archive-inds-weight</code> and <code>pop-inds-weight</code>.
35 *
36 * @author Marcin Szubert
37 *
38 */
39public class ArchivingCoevolutionaryEvaluator extends SimpleCoevolutionaryEvaluator {
40
41        private static final String P_ARCHIVE = "archive";
42        private static final String P_ARCHIVE_INDS_WEIGHT = "archive-inds-weight";
43        private static final String P_ARCHIVE_SAMPLING_METHOD = "archive-sampling-method";
44
45        private CoevolutionaryArchive archive;
46
47        private List<List<Individual>> archiveOpponents;
48
49        private SamplingMethod[] archiveSamplingMethod;
50
51        private int archiveIndsWeight;
52
53        @Override
54        public void setup(final EvolutionState state, final Parameter base) {
55                super.setup(state, base);
56
57                Parameter archiveParam = base.push(P_ARCHIVE);
58                archive = (CoevolutionaryArchive) (state.parameters.getInstanceForParameter(archiveParam,
59                                null, CoevolutionaryArchive.class));
60                archive.setup(state, base.push(P_ARCHIVE));
61
62                Parameter archiveIndsWeightParam = base.push(P_ARCHIVE_INDS_WEIGHT);
63                archiveIndsWeight = state.parameters.getIntWithDefault(archiveIndsWeightParam, null, 1);
64
65                archiveOpponents = new ArrayList<List<Individual>>(numSubpopulations);
66                archiveSamplingMethod = new SamplingMethod[numSubpopulations];
67
68                for (int subpop = 0; subpop < numSubpopulations; subpop++) {
69                        archiveOpponents.add(new ArrayList<Individual>());
70                        setupArchivingSubpopulation(state, base, subpop);
71                }
72        }
73
74        private void setupArchivingSubpopulation(EvolutionState state, Parameter base, int subpop) {
75                Parameter samplingMethodParam = base.push(P_SUBPOP).push("" + subpop).push(
76                                P_ARCHIVE_SAMPLING_METHOD);
77                archiveSamplingMethod[subpop] = (SamplingMethod) (state.parameters.getInstanceForParameter(
78                                samplingMethodParam, null, SamplingMethod.class));
79                archiveSamplingMethod[subpop].setup(state, samplingMethodParam);
80        }
81
82        @Override
83        public void evaluatePopulation(EvolutionState state) {
84                if (!(state.population.subpops[0] instanceof ArchivingSubpopulation)) {
85                        state.output.fatal("Archiving evaluator requires archiving subpopulation");
86                }
87
88                super.evaluatePopulation(state);
89
90                for (int subpop = 0; subpop < numSubpopulations; subpop++) {
91                        archiveOpponents.set(subpop, findOpponentsFromArchive(state, subpop));
92                }
93
94                for (int subpop = 0; subpop < numSubpopulations; subpop++) {
95                        List<List<InteractionResult>> subpopulationResults = interactionScheme
96                                        .performInteractions(state, subpop, archiveOpponents);
97
98                        fitnessAggregateMethod[subpop].addToAggregate(state, subpop, subpopulationResults,
99                                        archiveIndsWeight);
100                        fitnessAggregateMethod[subpop].assignFitness(state, subpop);
101
102                        if (statistics != null) {
103                                statistics.printInteractionResults(state, subpopulationResults, subpop);
104                        }
105                }
106
107                archive.submit(state);
108        }
109
110        private List<Individual> findOpponentsFromArchive(EvolutionState state, int subpop) {
111                List<Individual> archivalInds = ((ArchivingSubpopulation) state.population.subpops[subpop])
112                                .getArchivalIndividuals();
113                return archiveSamplingMethod[subpop].sample(state, archivalInds);
114        }
115}
Note: See TracBrowser for help on using the repository browser.