01 /*
02 * Java Genetic Algorithm Library (jenetics-6.3.0).
03 * Copyright (c) 2007-2021 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@gmail.com)
19 */
20 package io.jenetics.ext;
21
22 import static java.lang.String.format;
23 import static java.util.Objects.requireNonNull;
24
25 import io.jenetics.Gene;
26 import io.jenetics.Optimize;
27 import io.jenetics.Phenotype;
28 import io.jenetics.Selector;
29 import io.jenetics.stat.MinMax;
30 import io.jenetics.util.ISeq;
31 import io.jenetics.util.MSeq;
32 import io.jenetics.util.Seq;
33
34 /**
35 * Selector implementation which is part of the
36 * <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
37 * algorithm. The <i>Weasel program</i> is an thought experiment by Richard
38 * Dawkins to illustrate the functioning of the evolution: random <i>mutation</i>
39 * combined with non-random cumulative <i>selection</i>.
40 * <p>
41 * The selector always returns populations which only contains "{@code count}"
42 * instances of the <i>best</i> {@link Phenotype}.
43 * </p>
44 * {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
45 * <pre>{@code
46 * final Engine<CharacterGene, Integer> engine = Engine.builder(problem)
47 * // Set the 'WeaselSelector'.
48 * .selector(new WeaselSelector<>())
49 * // Disable survivors selector.
50 * .offspringFraction(1)
51 * // Set the 'WeaselMutator'.
52 * .alterers(new WeaselMutator<>(0.05))
53 * .build();
54 * }</pre>
55 *
56 * @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
57 * @see WeaselMutator
58 *
59 * @param <G> the gene type
60 * @param <C> the fitness result type
61 *
62 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
63 * @since 3.5
64 * @version 5.0
65 */
66 public class WeaselSelector<
67 G extends Gene<?, G>,
68 C extends Comparable<? super C>
69 >
70 implements Selector<G, C>
71 {
72 @Override
73 public ISeq<Phenotype<G, C>> select(
74 final Seq<Phenotype<G, C>> population,
75 final int count,
76 final Optimize opt
77 ) {
78 requireNonNull(population, "Population");
79 requireNonNull(opt, "Optimization");
80 if (count < 0) {
81 throw new IllegalArgumentException(format(
82 "Selection count must be greater or equal then zero, but was %s",
83 count
84 ));
85 }
86
87 final MinMax<Phenotype<G, C>> minMax = population.stream()
88 .collect(MinMax.toMinMax(opt.ascending()));
89
90 final MSeq<Phenotype<G, C>> result = MSeq.ofLength(count);
91 return result.fill(minMax::max).toISeq();
92 }
93
94 @Override
95 public String toString() {
96 return "WeaselSelector";
97 }
98
99 }
|