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