source: java/ecj/cecj/sampling/RandomSamplingMethod.java @ 1254

Last change on this file since 1254 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: 1.0 KB
Line 
1package cecj.sampling;
2
3import java.util.ArrayList;
4import java.util.List;
5
6import ec.EvolutionState;
7import ec.Individual;
8import ec.util.Parameter;
9
10/**
11 * A random sampling method samples randomly with repetitions given individuals collection. It can
12 * be used for so called "k-random opponents" evaluation scheme.
13 *
14 * @author Marcin Szubert
15 *
16 */
17public class RandomSamplingMethod extends SamplingMethod {
18
19        private static final String P_SAMPLE_SIZE = "sample-size";
20
21        private int sampleSize;
22
23        public void setup(EvolutionState state, Parameter base) {
24                Parameter sampleSizeParameter = base.push(P_SAMPLE_SIZE);
25                sampleSize = state.parameters.getIntWithDefault(sampleSizeParameter, null, 1);
26        }
27
28        @Override
29        public List<Individual> sample(EvolutionState state, List<Individual> source) {
30                List<Individual> result = new ArrayList<Individual>(sampleSize);
31                if (!source.isEmpty()) {
32                        for (int i = 0; i < sampleSize; i++) {
33                                result.add(source.get(state.random[0].nextInt(source.size())));
34                        }
35                }
36
37                return result;
38        }
39}
Note: See TracBrowser for help on using the repository browser.