001 /*
002 * Java Genetic Algorithm Library (jenetics-4.3.0).
003 * Copyright (c) 2007-2018 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.ext;
021
022 import static java.lang.String.format;
023 import static java.util.Objects.requireNonNull;
024
025 import io.jenetics.Gene;
026 import io.jenetics.Optimize;
027 import io.jenetics.Phenotype;
028 import io.jenetics.Selector;
029 import io.jenetics.stat.MinMax;
030 import io.jenetics.util.ISeq;
031 import io.jenetics.util.MSeq;
032 import io.jenetics.util.Seq;
033
034 /**
035 * Selector implementation which is part of the
036 * <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
037 * algorithm. The <i>Weasel program</i> is an thought experiment by Richard
038 * Dawkins to illustrate the functioning of the evolution: random <i>mutation</i>
039 * combined with non-random cumulative <i>selection</i>.
040 * <p>
041 * The selector always returns populations which only contains "{@code count}"
042 * instances of the <i>best</i> {@link Phenotype}.
043 * </p>
044 * {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
045 * <pre>{@code
046 * final Engine<CharacterGene, Integer> engine = Engine
047 * .builder(fitness, gtf)
048 * // Set the 'WeaselSelector'.
049 * .selector(new WeaselSelector<>())
050 * // Disable survivors selector.
051 * .offspringFraction(1)
052 * // Set the 'WeaselMutator'.
053 * .alterers(new WeaselMutator<>(0.05))
054 * .build();
055 * }</pre>
056 *
057 * @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
058 * @see WeaselMutator
059 *
060 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
061 * @since 3.5
062 * @version 3.5
063 */
064 public class WeaselSelector<
065 G extends Gene<?, G>,
066 C extends Comparable<? super C>
067 >
068 implements Selector<G, C>
069 {
070 @Override
071 public ISeq<Phenotype<G, C>> select(
072 final Seq<Phenotype<G, C>> population,
073 final int count,
074 final Optimize opt
075 ) {
076 requireNonNull(population, "Population");
077 requireNonNull(opt, "Optimization");
078 if (count < 0) {
079 throw new IllegalArgumentException(format(
080 "Selection count must be greater or equal then zero, but was %s",
081 count
082 ));
083 }
084
085 final MinMax<Phenotype<G, C>> minMax = population.stream()
086 .collect(MinMax.toMinMax(opt.ascending()));
087
088 final MSeq<Phenotype<G, C>> result = MSeq.ofLength(count);
089 return result.fill(minMax::getMax).toISeq();
090 }
091
092 @Override
093 public int hashCode() {
094 return WeaselMutator.class.hashCode();
095 }
096
097 @Override
098 public boolean equals(final Object obj) {
099 return obj == this || obj instanceof WeaselMutator;
100 }
101
102 @Override
103 public String toString() {
104 return "WeaselSelector";
105 }
106
107 }
|