All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jenetics.GaussianMutator Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-3.2.0).
 * Copyright (c) 2007-2015 Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package org.jenetics;

import static java.lang.String.format;
import static org.jenetics.internal.math.random.indexes;

import java.util.Random;

import org.jenetics.internal.math.base;
import org.jenetics.internal.util.Equality;
import org.jenetics.internal.util.Hash;

import org.jenetics.util.MSeq;
import org.jenetics.util.RandomRegistry;

/**
 * The GaussianMutator class performs the mutation of a {@link NumericGene}.
 * This mutator picks a new value based on a Gaussian distribution around the
 * current value of the gene. The variance of the new value (before clipping to
 * the allowed gene range) will be
 * 

* \hat{\sigma }^2 = \left ( \frac{ g_{max} - g_{min} }{4}\right )^2 *

* The new value will be cropped to the gene's boundaries. * * * @author Franz Wilhelmstötter * @since 1.0 * @version 3.0 */ public final class GaussianMutator< G extends NumericGene, C extends Comparable > extends Mutator { public GaussianMutator(final double probability) { super(probability); } public GaussianMutator() { this(DEFAULT_ALTER_PROBABILITY); } @Override protected int mutate(final MSeq genes, final double p) { final Random random = RandomRegistry.getRandom(); return (int)indexes(random, genes.length(), p) .peek(i -> genes.set(i, mutate(genes.get(i), random))) .count(); } G mutate(final G gene, final Random random) { final double std = (gene.getMax().doubleValue() - gene.getMin().doubleValue())*0.25; return gene.newInstance(base.clamp( random.nextGaussian()*std + gene.doubleValue(), gene.getMin().doubleValue(), gene.getMax().doubleValue() )); } @Override public int hashCode() { return Hash.of(getClass()).and(super.hashCode()).value(); } @Override public boolean equals(final Object obj) { return obj instanceof GaussianMutator && super.equals(obj); } @Override public String toString() { return format( "%s[p=%f]", getClass().getSimpleName(), _probability ); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy