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