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 javolution.lang.Immutable;
028
029 import org.jenetics.util.MSeq;
030 import org.jenetics.util.RandomRegistry;
031 import org.jenetics.util.math;
032
033 /**
034 * <p>
035 * The {@code PartiallyMatchedCrossover} (PMX) guarantees that all {@link Gene}s
036 * are found exactly once in each chromosome. No gene is duplicated by this
037 * crossover. The PMX can be applied usefully in the TSP or other permutation
038 * problem encodings. Permutation encoding is useful for all problems where the
039 * fitness only depends on the ordering of the genes within the chromosome. This
040 * is the case in many combinatorial optimization problems. Other crossover
041 * operators for combinatorial optimization are:
042 * <ul type="square">
043 * <li>order crossover</li>
044 * <li>cycle crossover</li>
045 * <li>edge recombination crossover</li>
046 * <li>edge assembly crossover</li>
047 * </ul>
048 * </p>
049 * The PMX is similar to the two-point crossover. A crossing region is chosen
050 * by selecting two crossing points.
051 * <pre>
052 * C1 = 012|345|6789
053 * C2 = 987|654|3210
054 * </pre>
055 * After performing the crossover we normally got two invalid chromosomes.
056 * <pre>
057 * C1 = 012|654|6789
058 * C2 = 987|345|3210
059 * </pre>
060 * Chromosome {@code C1} contains the value 6 twice and misses the value
061 * 3. On the other side chromosome {@code C2} contains the value 3 twice and
062 * misses the value 6. We can observe that this crossover is equivalent
063 * to the exchange of the values 3 -> 6, 4 -> 5 and 5 -> 4. To repair the two
064 * chromosomes we have to apply this exchange outside the crossing region.
065 * <pre>
066 * C1 = 012|654|3789
067 * C2 = 987|345|6210
068 * </pre>
069 *
070 * @see PermutationChromosome
071 *
072 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
073 * @since 1.0
074 * @version 1.0 — <em>$Date: 2013-09-01 $</em>
075 */
076 public final class PartiallyMatchedCrossover<T>
077 extends Crossover<EnumGene<T>>
078 implements Immutable
079 {
080
081 public PartiallyMatchedCrossover(final double probability) {
082 super(probability);
083 }
084
085 @Override
086 protected int crossover(
087 final MSeq<EnumGene<T>> that,
088 final MSeq<EnumGene<T>> other
089 ) {
090 assert (that.length() == other.length());
091
092 if (that.length() >= 2) {
093 final Random random = RandomRegistry.getRandom();
094 final int[] points = math.subset(that.length(), 2, random);
095
096 that.swap(points[0], points[1], other, points[0]);
097 repair(that, other, points[0], points[1]);
098 repair(other, that, points[0], points[1]);
099 }
100
101 return 1;
102 }
103
104 private static <T> void repair(
105 final MSeq<T> that, final MSeq<T> other,
106 final int begin, final int end
107 ) {
108 for (int i = 0; i < begin; ++i) {
109 int index = that.indexOf(that.get(i), begin, end);
110 while (index != -1) {
111 that.set(i, other.get(index));
112 index = that.indexOf(that.get(i), begin, end);
113 }
114 }
115 for (int i = end, n = that.length(); i < n; ++i) {
116 int index = that.indexOf(that.get(i), begin, end);
117 while (index != -1) {
118 that.set(i, other.get(index));
119 index = that.indexOf(that.get(i), begin, end);
120 }
121 }
122 }
123
124 @Override
125 public int hashCode() {
126 return hashCodeOf(getClass()).and(super.hashCode()).value();
127 }
128
129 @Override
130 public boolean equals(final Object obj) {
131 if (obj == this) {
132 return true;
133 }
134 if (obj == null || obj.getClass() != getClass()) {
135 return false;
136 }
137
138 return super.equals(obj);
139 }
140
141 @Override
142 public String toString() {
143 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
144 }
145
146 }
147
148
|