GaussianMutator.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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.util.object.hashCodeOf;
024 
025 import java.util.Random;
026 
027 import javolution.lang.Immutable;
028 
029 import org.jenetics.util.IndexStream;
030 import org.jenetics.util.MSeq;
031 import org.jenetics.util.RandomRegistry;
032 import org.jenetics.util.math;
033 
034 /**
035  * The GaussianMutator class performs the mutation of a {@link NumberGene}.
036  * This mutator picks a new value based on a Gaussian distribution around the
037  * current value of the gene. The variance of the new value (before clipping to
038  * the allowed gene range) will be
039  <p>
040  <img
041  *     src="doc-files/gaussian-mutator-var.gif"
042  *     alt="\hat{\sigma }^2 = \left ( \frac{ g_{max} - g_{min} }{4}\right )^2"
043  * >
044  </p>
045  * The new value will be cropped to the gene's boundaries.
046  *
047  *
048  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
049  @since 1.0
050  @version 1.0 &mdash; <em>$Date: 2013-11-28 $</em>
051  */
052 public final class GaussianMutator<G extends NumberGene<?, G>>
053     extends Mutator<G>
054     implements Immutable
055 {
056 
057     public GaussianMutator() {
058     }
059 
060     public GaussianMutator(final double probability) {
061         super(probability);
062     }
063 
064     @Override
065     protected int mutate(final MSeq<G> genes, final double p) {
066         final Random random = RandomRegistry.getRandom();
067         final IndexStream stream = IndexStream.Random(genes.length(), p);
068 
069         int alterations = 0;
070         for (int i = stream.next(); i != -1; i = stream.next()) {
071             genes.set(i, mutate(genes.get(i), random));
072 
073             ++alterations;
074         }
075 
076         return alterations;
077     }
078 
079     G mutate(final G gene, final Random random) {
080         final double std = (
081             gene.getMax().doubleValue() - gene.getMin().doubleValue()
082         )*0.25;
083 
084         return gene.newInstance(math.clamp(
085             random.nextGaussian()*std + gene.doubleValue(),
086             gene.getMin().doubleValue(),
087             gene.getMax().doubleValue()
088         ));
089     }
090 
091     @Override
092     public int hashCode() {
093         return hashCodeOf(getClass()).and(super.hashCode()).value();
094     }
095 
096     @Override
097     public boolean equals(final Object obj) {
098         if (obj == this) {
099             return true;
100         }
101         if (obj == null || obj.getClass() != getClass()) {
102             return false;
103         }
104 
105         return super.equals(obj);
106     }
107 
108     @Override
109     public String toString() {
110         return format(
111             "%s[p=%f]",
112             getClass().getSimpleName(),
113             _probability
114         );
115     }
116 
117 }
118 
119 
120 
121