WeaselSelector.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-5.1.0).
03  * Copyright (c) 2007-2019 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
47  *     .builder(fitness, gtf)
48  *      // Set the 'WeaselSelector'.
49  *     .selector(new WeaselSelector<>())
50  *      // Disable survivors selector.
51  *     .offspringFraction(1)
52  *      // Set the 'WeaselMutator'.
53  *     .alterers(new WeaselMutator<>(0.05))
54  *     .build();
55  * }</pre>
56  *
57  @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
58  @see WeaselMutator
59  *
60  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
61  @since 3.5
62  @version 5.0
63  */
64 public class WeaselSelector<
65     extends Gene<?, G>,
66     extends Comparable<? super C>
67 >
68     implements Selector<G, C>
69 {
70     @Override
71     public ISeq<Phenotype<G, C>> select(
72         final Seq<Phenotype<G, C>> population,
73         final int count,
74         final Optimize opt
75     ) {
76         requireNonNull(population, "Population");
77         requireNonNull(opt, "Optimization");
78         if (count < 0) {
79             throw new IllegalArgumentException(format(
80                 "Selection count must be greater or equal then zero, but was %s",
81                 count
82             ));
83         }
84 
85         final MinMax<Phenotype<G, C>> minMax = population.stream()
86             .collect(MinMax.toMinMax(opt.ascending()));
87 
88         final MSeq<Phenotype<G, C>> result = MSeq.ofLength(count);
89         return result.fill(minMax::getMax).toISeq();
90     }
91 
92     @Override
93     public String toString() {
94         return "WeaselSelector";
95     }
96 
97 }