MonteCarloSelector.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-1.5.0).
03  * Copyright (c) 2007-2013 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics;
21 
22 import static java.lang.String.format;
23 import static java.util.Objects.requireNonNull;
24 import static org.jenetics.util.object.hashCodeOf;
25 
26 import java.util.Random;
27 
28 import javolution.lang.Immutable;
29 
30 import org.jenetics.util.RandomRegistry;
31 
32 /**
33  * The Monte Carlo selector selects the individuals from a given population
34  * randomly. This selector can be used to measure the performance of a other
35  * selectors. In general, the performance of a selector should be better than
36  * the selection performance of the Monte Carlo selector.
37  *
38  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
39  @since 1.0
40  @version 1.0 &mdash; <em>$Date: 2013-12-02 $</em>
41  */
42 public final class MonteCarloSelector<
43     extends Gene<?, G>,
44     extends Comparable<? super C>
45 >
46     implements
47         Selector<G, C>,
48         Immutable
49 {
50 
51     public MonteCarloSelector() {
52     }
53 
54     @Override
55     public Population<G, C> select(
56         final Population<G, C> population,
57         final int count,
58         final Optimize opt
59     ) {
60         requireNonNull(population, "Population");
61         requireNonNull(opt, "Optimization");
62         if (count < 0) {
63             throw new IllegalArgumentException(format(
64                 "Selection count must be greater or equal then zero, but was %d.",
65                 count
66             ));
67         }
68 
69         final Population<G, C> selection = new Population<>(count);
70 
71         if (count > 0) {
72             final Random random = RandomRegistry.getRandom();
73             final int size = population.size();
74             for (int i = 0; i < count; ++i) {
75                 final int pos = random.nextInt(size);
76                 selection.add(population.get(pos));
77             }
78         }
79 
80         return selection;
81     }
82 
83     @Override
84     public int hashCode() {
85         return hashCodeOf(getClass()).value();
86     }
87 
88     @Override
89     public boolean equals(final Object obj) {
90         return obj == this || obj instanceof MonteCarloSelector<?, ?>;
91     }
92 
93     @Override
94     public String toString() {
95         return format("%s", getClass().getSimpleName());
96     }
97 
98 }