source: java/ecj/cecj/archive/BestOfGenerationArchive.java @ 814

Last change on this file since 814 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: 2.4 KB
RevLine 
[44]1/*
2  Copyright 2009 by Marcin Szubert
3  Licensed under the Academic Free License version 3.0
4 */
5
6package cecj.archive;
7
8import java.util.List;
9
10import ec.EvolutionState;
11import ec.Individual;
12import ec.util.Parameter;
13
14/**
15 * Represents the simplest archive type which just finds the best individual from the submitted
16 * population and appends it to the list of individuals found in previous generations. Note that, by
17 * default, the archive size is unbounded and grows steadily over time because no archival
18 * individuals are removed. By setting the <code>archive-size</code> parameter value to <i>x</i>,
19 * only the best competitors from last <i>x</i> generations are maintained.
20 *
21 * @author Marcin Szubert
22 *
23 */
24public class BestOfGenerationArchive extends CoevolutionaryArchive {
25
26        private static final int UNBOUNDED_ARCHIVE_SIZE = -1;
27
28        private static final String P_ARCHIVE_SIZE = "size";
29        private static final String P_SUBPOP = "subpop";
30
31        private int[] archiveSize;
32        private int[] currentArchivePosition;
33
34        @Override
35        public void setup(EvolutionState state, Parameter base) {
36                super.setup(state, base);
37
38                currentArchivePosition = new int[numSubpopulations];
39                archiveSize = new int[numSubpopulations];
40
41                for (int subpop = 0; subpop < numSubpopulations; subpop++) {
42                        Parameter archiveSizeParameter = base.push(P_SUBPOP).push("" + subpop).push(
43                                        P_ARCHIVE_SIZE);
44                        archiveSize[subpop] = state.parameters.getIntWithDefault(archiveSizeParameter, null,
45                                        UNBOUNDED_ARCHIVE_SIZE);
46                        if (archiveSize[subpop] <= 0 && archiveSize[subpop] != UNBOUNDED_ARCHIVE_SIZE) {
47                                state.output.fatal("Archive size must be > 0.\n", archiveSizeParameter);
48                        }
49                }
50        }
51
52        @Override
53        public void submit(EvolutionState state) {
54                for (int subpop = 0; subpop < numSubpopulations; subpop++) {
55                        Individual bestOfGeneration = findBestIndividual(getIndividuals(state, subpop));
56                        List<Individual> archive = getArchive(state, subpop);
57
58                        if ((archiveSize[subpop] == UNBOUNDED_ARCHIVE_SIZE)
59                                        || (archive.size() < archiveSize[subpop])) {
60                                archive.add(bestOfGeneration);
61                        } else {
62                                archive.set(currentArchivePosition[subpop], bestOfGeneration);
63                                currentArchivePosition[subpop]++;
64                                currentArchivePosition[subpop] %= archiveSize[subpop];
65                        }
66                }
67        }
68
69        private Individual findBestIndividual(List<Individual> inds) {
70                Individual result = inds.get(0);
71                for (Individual ind : inds) {
72                        if (ind.fitness.betterThan(result.fitness)) {
73                                result = ind;
74                        }
75                }
76                return result;
77        }
78}
Note: See TracBrowser for help on using the repository browser.