SwapMutator.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.5.0).
003  * Copyright (c) 2007-2013 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.util.object.hashCodeOf;
024 
025 import java.util.Random;
026 
027 import org.jenetics.util.IndexStream;
028 import org.jenetics.util.MSeq;
029 import org.jenetics.util.RandomRegistry;
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@gmx.at">Franz Wilhelmstötter</a>
039  @since 1.0
040  @version 1.0 &mdash; <em>$Date: 2013-08-30 $</em>
041  */
042 public class SwapMutator<G extends Gene<?, G>> extends Mutator<G> {
043 
044     /**
045      * Constructs an alterer with a given recombination probability.
046      *
047      @param probability the crossover probability.
048      @throws IllegalArgumentException if the {@code probability} is not in the
049      *          valid range of {@code [0, 1]}.
050      */
051     public SwapMutator(final double probability) {
052         super(probability);
053     }
054 
055     /**
056      * Default constructor, with default mutation probability
057      * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}).
058      */
059     public SwapMutator() {
060         this(DEFAULT_ALTER_PROBABILITY);
061     }
062 
063     /**
064      * Swaps the genes in the given array, with the mutation probability of this
065      * mutation.
066      */
067     @Override
068     protected int mutate(final MSeq<G> genes, final double p) {
069         int alterations = 0;
070 
071         if (genes.length() 1) {
072             final Random random = RandomRegistry.getRandom();
073             final IndexStream stream = IndexStream.Random(
074                 genes.length(), p, random
075             );
076 
077             for (int i = stream.next(); i != -1; i = stream.next()) {
078                 final int j = random.nextInt(genes.length());
079                 genes.swap(i, j);
080 
081                 ++alterations;
082             }
083         }
084 
085         return alterations;
086     }
087 
088     @Override
089     public int hashCode() {
090         return hashCodeOf(getClass()).and(super.hashCode()).value();
091     }
092 
093     @Override
094     public boolean equals(final Object obj) {
095         if (obj == this) {
096             return true;
097         }
098         if (obj == null || obj.getClass() != getClass()) {
099             return false;
100         }
101 
102         return super.equals(obj);
103     }
104 
105     @Override
106     public String toString() {
107         return format("%s[p=%f]", getClass().getSimpleName(), _probability);
108     }
109 
110 }