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

io.jenetics.ext.RSMutator 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 java.util.random.RandomGenerator;

import io.jenetics.AbstractAlterer;
import io.jenetics.Chromosome;
import io.jenetics.Gene;
import io.jenetics.Mutator;
import io.jenetics.MutatorResult;
import io.jenetics.internal.math.Subsets;
import io.jenetics.util.MSeq;

/**
 * The reverse sequence mutation, two positions i and j are randomly chosen The
 * gene order in a chromosome will then be reversed between this two points.
 * This mutation operator can also be used for combinatorial problems, where no
 * duplicated genes within a chromosome are allowed, e.g., for the TSP.
 *
 * @see io.jenetics.SwapMutator
 *
 * @author Franz Wilhelmstötter
 * @version 5.0
 * @since 5.0
 */
public class RSMutator<
	G extends Gene,
	C extends Comparable
>
	extends Mutator
{

	/**
	 * Constructs an alterer with a given recombination probability.
	 *
	 * @param probability the crossover probability.
	 * @throws IllegalArgumentException if the {@code probability} is not in the
	 *          valid range of {@code [0, 1]}.
	 */
	public RSMutator(final double probability) {
		super(probability);
	}

	/**
	 * Default constructor, with default mutation probability
	 * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}).
	 */
	public RSMutator() {
		this(DEFAULT_ALTER_PROBABILITY);
	}

	@Override
	protected MutatorResult> mutate(
		final Chromosome chromosome,
		final double p,
		final RandomGenerator random
	) {
		final MutatorResult> result;
		if (chromosome.length() > 1) {
			final int[] points = Subsets.next(random, chromosome.length() + 1, 2);
			final MSeq genes = MSeq.of(chromosome);
			genes.subSeq(points[0], points[1]).reverse();

			result = new MutatorResult<>(
				chromosome.newInstance(genes.toISeq()),
				points[1] - points[0] - 1
			);
		} else {
			result = new MutatorResult<>(chromosome, 0);
		}

		return result;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy