SwapMutator.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.0.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@gmail.com)
019  */
020 package io.jenetics;
021 
022 import static java.lang.String.format;
023 import static io.jenetics.internal.math.random.indexes;
024 
025 import java.util.Random;
026 
027 import io.jenetics.internal.util.Equality;
028 import io.jenetics.internal.util.Hash;
029 import io.jenetics.util.MSeq;
030 
031 /**
032  * The {@code SwapMutation} changes the order of genes in a chromosome, with the
033  * hope of bringing related genes closer together, thereby facilitating the
034  * production of building blocks. This mutation operator can also be used for
035  * combinatorial problems, where no duplicated genes within a chromosome are
036  * allowed, e.g. for the TSP.
037  *
038  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
039  @since 1.0
040  @version 4.0
041  */
042 public class SwapMutator<
043     extends Gene<?, G>,
044     extends Comparable<? super C>
045 >
046     extends Mutator<G, C>
047 {
048 
049     /**
050      * Constructs an alterer with a given recombination probability.
051      *
052      @param probability the crossover probability.
053      @throws IllegalArgumentException if the {@code probability} is not in the
054      *          valid range of {@code [0, 1]}.
055      */
056     public SwapMutator(final double probability) {
057         super(probability);
058     }
059 
060     /**
061      * Default constructor, with default mutation probability
062      * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}).
063      */
064     public SwapMutator() {
065         this(DEFAULT_ALTER_PROBABILITY);
066     }
067 
068     /**
069      * Swaps the genes in the given array, with the mutation probability of this
070      * mutation.
071      */
072     @Override
073     protected MutatorResult<Chromosome<G>> mutate(
074         final Chromosome<G> chromosome,
075         final double p,
076         final Random random
077     ) {
078         final MutatorResult<Chromosome<G>> result;
079         if (chromosome.length() 1) {
080             final MSeq<G> genes = chromosome.toSeq().copy();
081             final int mutations = (int)indexes(random, genes.length(), p)
082                 .peek(i -> genes.swap(i, random.nextInt(genes.length())))
083                 .count();
084             result = MutatorResult.of(
085                 chromosome.newInstance(genes.toISeq()),
086                 mutations
087             );
088         else {
089             result = MutatorResult.of(chromosome);
090         }
091 
092         return result;
093     }
094 
095     @Override
096     public int hashCode() {
097         return Hash.of(getClass()).and(super.hashCode()).value();
098     }
099 
100     @Override
101     public boolean equals(final Object obj) {
102         return Equality.of(this, obj).test(super::equals);
103     }
104 
105     @Override
106     public String toString() {
107         return format("%s[p=%f]", getClass().getSimpleName(), _probability);
108     }
109 
110 }