SwapMutator.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-4.3.0).
03  * Copyright (c) 2007-2018 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 java.lang.String.format;
23 import static io.jenetics.internal.math.random.indexes;
24 
25 import java.util.Random;
26 
27 import io.jenetics.util.MSeq;
28 
29 /**
30  * The {@code SwapMutation} changes the order of genes in a chromosome, with the
31  * hope of bringing related genes closer together, thereby facilitating the
32  * production of building blocks. This mutation operator can also be used for
33  * combinatorial problems, where no duplicated genes within a chromosome are
34  * allowed, e.g. for the TSP.
35  *
36  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
37  @since 1.0
38  @version 4.0
39  */
40 public class SwapMutator<
41     extends Gene<?, G>,
42     extends Comparable<? super C>
43 >
44     extends Mutator<G, C>
45 {
46 
47     /**
48      * Constructs an alterer with a given recombination probability.
49      *
50      @param probability the crossover probability.
51      @throws IllegalArgumentException if the {@code probability} is not in the
52      *          valid range of {@code [0, 1]}.
53      */
54     public SwapMutator(final double probability) {
55         super(probability);
56     }
57 
58     /**
59      * Default constructor, with default mutation probability
60      * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}).
61      */
62     public SwapMutator() {
63         this(DEFAULT_ALTER_PROBABILITY);
64     }
65 
66     /**
67      * Swaps the genes in the given array, with the mutation probability of this
68      * mutation.
69      */
70     @Override
71     protected MutatorResult<Chromosome<G>> mutate(
72         final Chromosome<G> chromosome,
73         final double p,
74         final Random random
75     ) {
76         final MutatorResult<Chromosome<G>> result;
77         if (chromosome.length() 1) {
78             final MSeq<G> genes = chromosome.toSeq().copy();
79             final int mutations = (int)indexes(random, genes.length(), p)
80                 .peek(i -> genes.swap(i, random.nextInt(genes.length())))
81                 .count();
82             result = MutatorResult.of(
83                 chromosome.newInstance(genes.toISeq()),
84                 mutations
85             );
86         else {
87             result = MutatorResult.of(chromosome);
88         }
89 
90         return result;
91     }
92 
93     @Override
94     public String toString() {
95         return format("%s[p=%f]", getClass().getSimpleName(), _probability);
96     }
97 
98 }