001 /*
002 * Java Genetic Algorithm Library (jenetics-2.0.2).
003 * Copyright (c) 2007-2014 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
024 import java.util.Random;
025
026 import org.jenetics.internal.util.HashBuilder;
027
028 import org.jenetics.util.IndexStream;
029 import org.jenetics.util.MSeq;
030 import org.jenetics.util.RandomRegistry;
031 import org.jenetics.util.math;
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 2.0 — <em>$Date: 2014-03-12 $</em>
050 */
051 public final class GaussianMutator<G extends NumericGene<?, G>>
052 extends Mutator<G>
053 {
054
055 public GaussianMutator() {
056 }
057
058 public GaussianMutator(final double probability) {
059 super(probability);
060 }
061
062 @Override
063 protected int mutate(final MSeq<G> genes, final double p) {
064 final Random random = RandomRegistry.getRandom();
065 final IndexStream stream = IndexStream.Random(genes.length(), p);
066
067 int alterations = 0;
068 for (int i = stream.next(); i != -1; i = stream.next()) {
069 genes.set(i, mutate(genes.get(i), random));
070
071 ++alterations;
072 }
073
074 return alterations;
075 }
076
077 G mutate(final G gene, final Random random) {
078 final double std = (
079 gene.getMax().doubleValue() - gene.getMin().doubleValue()
080 )*0.25;
081
082 return gene.newInstance(math.clamp(
083 random.nextGaussian()*std + gene.doubleValue(),
084 gene.getMin().doubleValue(),
085 gene.getMax().doubleValue()
086 ));
087 }
088
089 @Override
090 public int hashCode() {
091 return HashBuilder.of(getClass()).and(super.hashCode()).value();
092 }
093
094 @Override
095 public boolean equals(final Object obj) {
096 if (obj == this) {
097 return true;
098 }
099 if (obj == null || obj.getClass() != getClass()) {
100 return false;
101 }
102
103 return super.equals(obj);
104 }
105
106 @Override
107 public String toString() {
108 return format(
109 "%s[p=%f]",
110 getClass().getSimpleName(),
111 _probability
112 );
113 }
114
115 }
|