01 /*
02 * Java Genetic Algorithm Library (jenetics-5.1.0).
03 * Copyright (c) 2007-2019 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.random.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 G extends Gene<?, G>,
47 C extends Comparable<? super C>
48 >
49 extends Mutator<G, C>
50
51 {
52
53 /**
54 * Constructs an alterer with a given recombination probability.
55 *
56 * @param probability the crossover probability.
57 * @throws IllegalArgumentException if the {@code probability} is not in the
58 * valid range of {@code [0, 1]}.
59 */
60 public SwapMutator(final double probability) {
61 super(probability);
62 }
63
64 /**
65 * Default constructor, with default mutation probability
66 * ({@link AbstractAlterer#DEFAULT_ALTER_PROBABILITY}).
67 */
68 public SwapMutator() {
69 this(DEFAULT_ALTER_PROBABILITY);
70 }
71
72 /**
73 * Swaps the genes in the given array, with the mutation probability of this
74 * mutation.
75 */
76 @Override
77 protected MutatorResult<Chromosome<G>> mutate(
78 final Chromosome<G> chromosome,
79 final double p,
80 final Random random
81 ) {
82 final MutatorResult<Chromosome<G>> result;
83 if (chromosome.length() > 1) {
84 final MSeq<G> genes = chromosome.toSeq().copy();
85 final int mutations = (int)indexes(random, genes.length(), p)
86 .peek(i -> genes.swap(i, random.nextInt(genes.length())))
87 .count();
88 result = MutatorResult.of(
89 chromosome.newInstance(genes.toISeq()),
90 mutations
91 );
92 } else {
93 result = MutatorResult.of(chromosome);
94 }
95
96 return result;
97 }
98
99 }
|