001 /*
002 * Java Genetic Algorithm Library (jenetics-5.1.0).
003 * Copyright (c) 2007-2019 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
024 import java.util.Random;
025
026 import io.jenetics.AltererResult;
027 import io.jenetics.Chromosome;
028 import io.jenetics.Gene;
029 import io.jenetics.Genotype;
030 import io.jenetics.Mutator;
031 import io.jenetics.MutatorResult;
032 import io.jenetics.Phenotype;
033 import io.jenetics.util.ISeq;
034 import io.jenetics.util.RandomRegistry;
035 import io.jenetics.util.Seq;
036
037 /**
038 * Mutator implementation which is part of the
039 * <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
040 * algorithm. The <i>Weasel program</i> is an thought experiment by Richard
041 * Dawkins to illustrate the functioning of the evolution: random <i>mutation</i>
042 * combined with non-random cumulative <i>selection</i>.
043 * <p>
044 * The mutator mutates the genes of <i>every</i> chromosome of <i>every</i>
045 * genotype in the population with the given mutation probability.
046 * </p>
047 * {@link io.jenetics.engine.Engine} setup for the <i>Weasel program:</i>
048 * <pre>{@code
049 * final Engine<CharacterGene, Integer> engine = Engine
050 * .builder(fitness, gtf)
051 * // Set the 'WeaselSelector'.
052 * .selector(new WeaselSelector<>())
053 * // Disable survivors selector.
054 * .offspringFraction(1)
055 * // Set the 'WeaselMutator'.
056 * .alterers(new WeaselMutator<>(0.05))
057 * .build();
058 * }</pre>
059 *
060 * @see <a href="https://en.wikipedia.org/wiki/Weasel_program">Weasel program</a>
061 * @see WeaselSelector
062 *
063 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
064 * @since 3.5
065 * @version 5.0
066 */
067 public class WeaselMutator<
068 G extends Gene<?, G>,
069 C extends Comparable<? super C>
070 >
071 extends Mutator<G, C>
072 {
073
074 public WeaselMutator(final double probability) {
075 super(probability);
076 }
077
078 public WeaselMutator() {
079 this(0.05);
080 }
081
082 @Override
083 public AltererResult<G, C>
084 alter(final Seq<Phenotype<G, C>> population, final long generation) {
085 final Random random = RandomRegistry.getRandom();
086 final Seq<MutatorResult<Phenotype<G, C>>> result = population
087 .map(pt -> mutate(pt, generation, _probability, random));
088
089 return AltererResult.of(
090 result
091 .map(MutatorResult::getResult)
092 .asISeq(),
093 result.stream()
094 .mapToInt(MutatorResult::getMutations)
095 .sum()
096 );
097 }
098
099 @Override
100 protected MutatorResult<Genotype<G>> mutate(
101 final Genotype<G> genotype,
102 final double p,
103 final Random random
104 ) {
105 final ISeq<MutatorResult<Chromosome<G>>> result = genotype.toSeq()
106 .map(gt -> mutate(gt, p, random));
107
108 return MutatorResult.of(
109 Genotype.of(result.map(MutatorResult::getResult)),
110 result.stream().mapToInt(MutatorResult::getMutations).sum()
111 );
112 }
113
114 @Override
115 public String toString() {
116 return format("WeaselMutator[%f]", _probability);
117 }
118
119 }
|