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