WeaselMutator.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.2.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 
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 3.5
066  */
067 public class WeaselMutator<
068     extends Gene<?, G>,
069     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.map(MutatorResult::getResult).asISeq(),
091             result.stream().mapToInt(MutatorResult::getMutations).sum()
092         );
093     }
094 
095     @Override
096     protected MutatorResult<Genotype<G>> mutate(
097         final Genotype<G> genotype,
098         final double p,
099         final Random random
100     ) {
101         final ISeq<MutatorResult<Chromosome<G>>> result = genotype.toSeq()
102             .map(gt -> mutate(gt, p, random));
103 
104         return MutatorResult.of(
105             Genotype.of(result.map(MutatorResult::getResult)),
106             result.stream().mapToInt(MutatorResult::getMutations).sum()
107         );
108     }
109 
110     @Override
111     public int hashCode() {
112         int hash = 17;
113         hash += 31*WeaselMutator.class.hashCode() 37;
114         hash += 31*Double.hashCode(_probability37;
115         return hash;
116     }
117 
118     @Override
119     public boolean equals(final Object obj) {
120         return obj == this ||
121             obj instanceof WeaselMutator &&
122             Double.compare(
123                 ((WeaselMutatorobj)._probability, _probability== 0;
124     }
125 
126     @Override
127     public String toString() {
128         return format("WeaselMutator[%f]", _probability);
129     }
130 
131 }