001 /*
002 * Java Genetic Algorithm Library (jenetics-3.8.0).
003 * Copyright (c) 2007-2017 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@gmx.at)
019 */
020 package org.jenetics;
021
022 import static java.lang.String.format;
023 import static org.jenetics.internal.math.random.indexes;
024
025 import java.util.Random;
026
027 import org.jenetics.internal.util.Equality;
028 import org.jenetics.internal.util.Hash;
029
030 import org.jenetics.util.MSeq;
031 import org.jenetics.util.RandomRegistry;
032
033 /**
034 * The {@code SwapMutation} changes the order of genes in a chromosome, with the
035 * hope of bringing related genes closer together, thereby facilitating the
036 * production of building blocks. This mutation operator can also be used for
037 * combinatorial problems, where no duplicated genes within a chromosome are
038 * allowed, e.g. for the TSP.
039 *
040 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
041 * @since 1.0
042 * @version 3.0
043 */
044 public class SwapMutator<
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 SwapMutator(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 SwapMutator() {
067 this(DEFAULT_ALTER_PROBABILITY);
068 }
069
070 /**
071 * Swaps the genes in the given array, with the mutation probability of this
072 * mutation.
073 */
074 @Override
075 protected int mutate(final MSeq<G> genes, final double p) {
076 final Random random = RandomRegistry.getRandom();
077
078 return genes.length() > 1
079 ? (int)indexes(random, genes.length(), p)
080 .peek(i -> genes.swap(i, random.nextInt(genes.length())))
081 .count()
082 : 0;
083 }
084
085 @Override
086 public int hashCode() {
087 return Hash.of(getClass()).and(super.hashCode()).value();
088 }
089
090 @Override
091 public boolean equals(final Object obj) {
092 return Equality.of(this, obj).test(super::equals);
093 }
094
095 @Override
096 public String toString() {
097 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
098 }
099
100 }
|