-
Notifications
You must be signed in to change notification settings - Fork 150
/
WeaselSelector.java
99 lines (92 loc) · 3.01 KB
/
WeaselSelector.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
* Java Genetic Algorithm Library (@__identifier__@).
* Copyright (c) @__year__@ Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
*/
package io.jenetics.ext;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import io.jenetics.Gene;
import io.jenetics.Optimize;
import io.jenetics.Phenotype;
import io.jenetics.Selector;
import io.jenetics.stat.MinMax;
import io.jenetics.util.ISeq;
import io.jenetics.util.MSeq;
import io.jenetics.util.Seq;
/**
* Selector implementation which is part of the
* <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
* algorithm. The <i>Weasel program</i> is a thought experiment by Richard
* Dawkins to illustrate the functioning of the evolution: random <i>mutation</i>
* combined with non-random cumulative <i>selection</i>.
* <p>
* The selector always returns populations which only contains "{@code count}"
* instances of the <i>best</i> {@link Phenotype}.
* </p>
* {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
* {@snippet lang="java":
* final Engine<CharacterGene, Integer> engine = Engine.builder(problem)
* // Set the 'WeaselSelector'.
* .selector(new WeaselSelector<>())
* // Disable survivors selector.
* .offspringFraction(1)
* // Set the 'WeaselMutator'.
* .alterers(new WeaselMutator<>(0.05))
* .build();
* }
*
* @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
* @see WeaselMutator
*
* @param <G> the gene type
* @param <C> the fitness result type
*
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @since 3.5
* @version 5.0
*/
public class WeaselSelector<
G extends Gene<?, G>,
C extends Comparable<? super C>
>
implements Selector<G, C>
{
@Override
public ISeq<Phenotype<G, C>> select(
final Seq<Phenotype<G, C>> population,
final int count,
final Optimize opt
) {
requireNonNull(population, "Population");
requireNonNull(opt, "Optimization");
if (count < 0) {
throw new IllegalArgumentException(format(
"Selection count must be greater or equal then zero, but was %s",
count
));
}
final MinMax<Phenotype<G, C>> minMax = population.stream()
.collect(MinMax.toMinMax(opt.ascending()));
final MSeq<Phenotype<G, C>> result = MSeq.ofLength(count);
return result.fill(minMax::max).toISeq();
}
@Override
public String toString() {
return "WeaselSelector";
}
}