SwapMutator.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-6.3.0).
03  * Copyright (c) 2007-2021 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics;
21 
22 import static io.jenetics.internal.math.Randoms.indexes;
23 
24 import java.util.Random;
25 
26 import io.jenetics.util.MSeq;
27 
28 /**
29  * The {@code SwapMutation} changes the order of genes in a chromosome, with the
30  * hope of bringing related genes closer together, thereby facilitating the
31  * production of building blocks. This mutation operator can also be used for
32  * combinatorial problems, where no duplicated genes within a chromosome are
33  * allowed, e.g. for the TSP.
34  <p>
35  * This mutator is also known as <em>Partial Shuffle Mutator</em> (PSM).
36  *
37  @see <a href="https://arxiv.org/ftp/arxiv/papers/1203/1203.3099.pdf">
38  *     Analyzing the Performance of Mutation Operators to Solve the Travelling
39  *     Salesman Problem</a>
40  *
41  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
42  @since 1.0
43  @version 5.0
44  */
45 public class SwapMutator<
46     extends Gene<?, G>,
47     extends Comparable<? super C>
48 >
49     extends Mutator<G, C>
50 {
51 
52     /**
53      * Constructs an alterer with a given recombination probability.
54      *
55      @param probability the crossover probability.
56      @throws IllegalArgumentException if the {@code probability} is not in the
57      *          valid range of {@code [0, 1]}.
58      */
59     public SwapMutator(final double probability) {
60         super(probability);
61     }
62 
63     /**
64      * Default constructor, with default mutation probability
65      * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}).
66      */
67     public SwapMutator() {
68         this(DEFAULT_ALTER_PROBABILITY);
69     }
70 
71     /**
72      * Swaps the genes in the given array, with the mutation probability of this
73      * mutation.
74      */
75     @Override
76     protected MutatorResult<Chromosome<G>> mutate(
77         final Chromosome<G> chromosome,
78         final double p,
79         final Random random
80     ) {
81         final MutatorResult<Chromosome<G>> result;
82         if (chromosome.length() 1) {
83             final MSeq<G> genes = MSeq.of(chromosome);
84             final int mutations = (int)indexes(random, genes.length(), p)
85                 .peek(i -> genes.swap(i, random.nextInt(genes.length())))
86                 .count();
87             result = MutatorResult.of(
88                 chromosome.newInstance(genes.toISeq()),
89                 mutations
90             );
91         else {
92             result = MutatorResult.of(chromosome);
93         }
94 
95         return result;
96     }
97 
98 }