GaussianMutator.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.9.0).
003  * Copyright (c) 2007-2017 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@gmx.at)
019  */
020 package org.jenetics;
021 
022 import static java.lang.String.format;
023 import static org.jenetics.internal.math.base.clamp;
024 import static org.jenetics.internal.math.random.indexes;
025 
026 import java.util.Random;
027 
028 import org.jenetics.internal.util.Hash;
029 
030 import org.jenetics.util.MSeq;
031 import org.jenetics.util.RandomRegistry;
032 
033 /**
034  * The GaussianMutator class performs the mutation of a {@link NumericGene}.
035  * This mutator picks a new value based on a Gaussian distribution around the
036  * current value of the gene. The variance of the new value (before clipping to
037  * the allowed gene range) will be
038  <p>
039  <img
040  *     src="doc-files/gaussian-mutator-var.gif"
041  *     alt="\hat{\sigma }^2 = \left ( \frac{ g_{max} - g_{min} }{4}\right )^2"
042  * >
043  </p>
044  * The new value will be cropped to the gene's boundaries.
045  *
046  *
047  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
048  @since 1.0
049  @version 3.0
050  */
051 public final class GaussianMutator<
052     extends NumericGene<?, G>,
053     extends Comparable<? super C>
054 >
055     extends Mutator<G, C>
056 {
057 
058     public GaussianMutator(final double probability) {
059         super(probability);
060     }
061 
062     public GaussianMutator() {
063         this(DEFAULT_ALTER_PROBABILITY);
064     }
065 
066     @Override
067     protected int mutate(final MSeq<G> genes, final double p) {
068         final Random random = RandomRegistry.getRandom();
069 
070         return (int)indexes(random, genes.length(), p)
071             .peek(i -> genes.set(i, mutate(genes.get(i), random)))
072             .count();
073     }
074 
075     G mutate(final G gene, final Random random) {
076         final double min = gene.getMin().doubleValue();
077         final double max = gene.getMax().doubleValue();
078         final double std = (max - min)*0.25;
079 
080         final double value = gene.doubleValue();
081         final double gaussian = random.nextGaussian();
082         return gene.newInstance(clamp(gaussian*std + value, min, max));
083     }
084 
085     @Override
086     public int hashCode() {
087         return Hash.of(getClass()).and(super.hashCode()).value();
088     }
089 
090     @Override
091     public boolean equals(final Object obj) {
092         return obj instanceof GaussianMutator && super.equals(obj);
093     }
094 
095     @Override
096     public String toString() {
097         return format(
098             "%s[p=%f]",
099             getClass().getSimpleName(),
100             _probability
101         );
102     }
103 
104 }