source: java/ecj/cecj/archive/ArchiveRandomSelection.java @ 44

Last change on this file since 44 was 44, checked in by mszubert, 14 years ago

cecj, framsticks and games packages imported

File size: 2.0 KB
Line 
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.SelectionMethod;
13import ec.select.SelectDefaults;
14import ec.util.Parameter;
15
16public class ArchiveRandomSelection extends SelectionMethod {
17
18        /** default base */
19        private static final String P_ARCHIVAL_RANDOM = "archival-random";
20
21        private static final String P_SIZE = "size";
22        private static final int ENTIRE_ARCHIVE = -1;
23
24        private int size;
25
26        public Parameter defaultBase() {
27                return SelectDefaults.base().push(P_ARCHIVAL_RANDOM);
28        }
29
30        @Override
31        public void setup(EvolutionState state, Parameter base) {
32                super.setup(state, base);
33               
34                Parameter sizeParam = base.push(P_SIZE);
35                size = state.parameters.getIntWithDefault(sizeParam, null, ENTIRE_ARCHIVE);
36        }
37
38        @Override
39        public int produce(int subpopulation, EvolutionState state, int thread) {
40                throw new UnsupportedOperationException(
41                        "Archival selection methods do not support supopulation index-based produce method.");
42        }
43
44        @Override
45        public int produce(int min, int max, int start, int subpop, Individual[] inds,
46                        EvolutionState state, int thread) {
47                int n = 1;
48                if (n > max)
49                        n = max;
50                if (n < min)
51                        n = min;
52
53                List<Individual> archive = ((ArchivingSubpopulation) state.population.subpops[subpop])
54                        .getArchivalIndividuals();
55                List<Individual> popInds = ((ArchivingSubpopulation) state.population.subpops[subpop])
56                        .getIndividuals();
57
58                if (!archive.isEmpty()) {
59                        for (int q = 0; q < n; q++) {
60                                int selected;
61                                if (size <= 0) {
62                                        selected = state.random[thread].nextInt(archive.size());
63                                } else {
64                                        selected = archive.size() - state.random[thread].nextInt(size) - 1;
65                                        selected = Math.max(selected, 0);
66                                }
67                                inds[start + q] = archive.get(selected);
68                        }
69                } else {
70                        for (int q = 0; q < n; q++) {
71                                inds[start + q] = popInds.get(state.random[thread].nextInt(popInds.size()));
72                        }
73                }
74
75                return n;
76        }
77}
Note: See TracBrowser for help on using the repository browser.