001    /*
002     * Copyright 2011 Christian Kumpe http://kumpe.de/christian/java
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package de.kumpe.hadooptimizer.impl;
017    
018    import org.apache.commons.logging.Log;
019    import org.apache.commons.logging.LogFactory;
020    import org.apache.commons.math.random.RandomGenerator;
021    
022    import de.kumpe.hadooptimizer.EsWrappableMutator;
023    import de.kumpe.hadooptimizer.NeedsRandomGenerator;
024    
025    /**
026     * Base class for Mutators based on gaussian random number.
027     * 
028     * @author <a href="http://kumpe.de/christian/java">Christian Kumpe</a>
029     */
030    public abstract class GaussionMutatorBase implements EsWrappableMutator,
031                    NeedsRandomGenerator {
032            private static final long serialVersionUID = 1L;
033            private static final Log log = LogFactory.getLog(GaussionMutatorBase.class);
034    
035            protected transient RandomGenerator randomGenerator;
036            protected final double standardDeviation;
037    
038            public GaussionMutatorBase(final double standardDeviation) {
039                    this.standardDeviation = standardDeviation;
040            }
041    
042            @Override
043            public void setRandomGenerator(final RandomGenerator randomGenerator) {
044                    if (log.isTraceEnabled()) {
045                            log.trace("Received new RandomGenerator-instance: "
046                                            + randomGenerator);
047                    }
048    
049                    this.randomGenerator = randomGenerator;
050            }
051    
052            @Override
053            public double[] mutate(final double[] individual,
054                            final double standardDeviation) {
055                    final double[] newIndividual = new double[individual.length];
056                    for (int i = 0; i < newIndividual.length; i++) {
057                            newIndividual[i] = individual[i] + nextGaussian(standardDeviation);
058                    }
059                    return newIndividual;
060            }
061    
062            /**
063             * Produces a normal distributed random number with the given standard
064             * deviation.
065             */
066            protected final double nextGaussian(final double standardDeviation) {
067                    return randomGenerator.nextGaussian() * standardDeviation;
068            }
069    
070            @Override
071            public double getMinIncrement() {
072                    return standardDeviation;
073            }
074    }