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