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