001/* 002 * Java Genetic Algorithm Library (jenetics-7.2.0). 003 * Copyright (c) 2007-2023 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@gmail.com) 019 */ 020package io.jenetics.ext; 021 022import java.util.random.RandomGenerator; 023 024import io.jenetics.AbstractAlterer; 025import io.jenetics.Chromosome; 026import io.jenetics.Gene; 027import io.jenetics.Mutator; 028import io.jenetics.MutatorResult; 029import io.jenetics.internal.math.Subset; 030import io.jenetics.util.MSeq; 031 032/** 033 * The reverse sequence mutation, two positions i and j are randomly chosen The 034 * gene order in a chromosome will then be reversed between this two points. 035 * This mutation operator can also be used for combinatorial problems, where no 036 * duplicated genes within a chromosome are allowed, e.g., for the TSP. 037 * 038 * @see io.jenetics.SwapMutator 039 * 040 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a> 041 * @version 5.0 042 * @since 5.0 043 */ 044public class RSMutator< 045 G extends Gene<?, G>, 046 C extends Comparable<? super C> 047> 048 extends Mutator<G, C> 049{ 050 051 /** 052 * Constructs an alterer with a given recombination probability. 053 * 054 * @param probability the crossover probability. 055 * @throws IllegalArgumentException if the {@code probability} is not in the 056 * valid range of {@code [0, 1]}. 057 */ 058 public RSMutator(final double probability) { 059 super(probability); 060 } 061 062 /** 063 * Default constructor, with default mutation probability 064 * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}). 065 */ 066 public RSMutator() { 067 this(DEFAULT_ALTER_PROBABILITY); 068 } 069 070 @Override 071 protected MutatorResult<Chromosome<G>> mutate( 072 final Chromosome<G> chromosome, 073 final double p, 074 final RandomGenerator random 075 ) { 076 final MutatorResult<Chromosome<G>> result; 077 if (chromosome.length() > 1) { 078 final int[] points = Subset.next(random, chromosome.length() + 1, 2); 079 final MSeq<G> genes = MSeq.of(chromosome); 080 genes.subSeq(points[0], points[1]).reverse(); 081 082 result = new MutatorResult<>( 083 chromosome.newInstance(genes.toISeq()), 084 points[1] - points[0] - 1 085 ); 086 } else { 087 result = new MutatorResult<>(chromosome, 0); 088 } 089 090 return result; 091 } 092 093}