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