source: java/ecj/cecj/sampling/RandomSamplingMethod.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: 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.