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

io.jenetics.ext.WeaselMutator Maven / Gradle / Ivy

The newest version!
/*
 * Java Genetic Algorithm Library (jenetics-8.1.0).
 * Copyright (c) 2007-2024 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 io.jenetics.ext;

import static java.lang.String.format;

import java.util.random.RandomGenerator;

import io.jenetics.AltererResult;
import io.jenetics.Gene;
import io.jenetics.Genotype;
import io.jenetics.Mutator;
import io.jenetics.MutatorResult;
import io.jenetics.Phenotype;
import io.jenetics.util.ISeq;
import io.jenetics.util.RandomRegistry;
import io.jenetics.util.Seq;

/**
 * Mutator implementation which is part of the
 * Weasel program
 * algorithm. The Weasel program is a thought experiment by Richard
 * Dawkins to illustrate the functioning of the evolution: random mutation
 * combined with non-random cumulative selection.
 * 

* The mutator mutates the genes of every chromosome of every * genotype in the population with the given mutation probability. *

* {@link io.jenetics.engine.Engine} setup for the Weasel program: * {@snippet lang="java": * final Engine engine = Engine.builder(problem) * // Set the 'WeaselSelector'. * .selector(new WeaselSelector<>()) * // Disable survivors selector. * .offspringFraction(1) * // Set the 'WeaselMutator'. * .alterers(new WeaselMutator<>(0.05)) * .build(); * } * * @see Weasel program * @see WeaselSelector * * @param the gene type * @param the fitness result type * * @author Franz Wilhelmstötter * @since 3.5 * @version 5.0 */ public class WeaselMutator< G extends Gene, C extends Comparable > extends Mutator { /** * Create a new weasel mutator with the given mutation probability. * * @param probability the mutation probability * @throws IllegalArgumentException if the {@code probability} is not in the * valid range of {@code [0, 1]}. */ public WeaselMutator(final double probability) { super(probability); } /** * Create a new weasel mutator with the default mutation probability * of {@code 0.05}. */ public WeaselMutator() { this(0.05); } @Override public AltererResult alter(final Seq> population, final long generation) { final var random = RandomRegistry.random(); final var result = population .map(pt -> mutate(pt, generation, _probability, random)); return new AltererResult<>( result .map(MutatorResult::result) .asISeq(), result.stream() .mapToInt(MutatorResult::mutations) .sum() ); } @Override protected MutatorResult> mutate( final Genotype genotype, final double p, final RandomGenerator random ) { final var result = genotype.stream() .map(gt -> mutate(gt, p, random)) .collect(ISeq.toISeq()); return new MutatorResult<>( Genotype.of(result.map(MutatorResult::result)), result.stream().mapToInt(MutatorResult::mutations).sum() ); } @Override public String toString() { return format("WeaselMutator[%f]", _probability); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy