PartiallyMatchedCrossover.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-3.9.0).
003  * Copyright (c) 2007-2017 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  <em>The {@code PartiallyMatchedCrossover} class requires chromosomes with the
071  * same length. An {@code IllegalArgumentException} is thrown at runtime if this
072  * requirement is not fulfilled.</em>
073  *
074  @see PermutationChromosome
075  *
076  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
077  @since 1.0
078  @version 3.6
079  */
080 public final class PartiallyMatchedCrossover<T, C extends Comparable<? super C>>
081     extends Crossover<EnumGene<T>, C>
082 {
083 
084     public PartiallyMatchedCrossover(final double probability) {
085         super(probability);
086     }
087 
088     @Override
089     protected int crossover(
090         final MSeq<EnumGene<T>> that,
091         final MSeq<EnumGene<T>> other
092     ) {
093         if (that.length() != other.length()) {
094             throw new IllegalArgumentException(format(
095                 "Required chromosomes with same length: %s != %s",
096                 that.length(), other.length()
097             ));
098         }
099 
100         if (that.length() >= 2) {
101             final Random random = RandomRegistry.getRandom();
102             final int[] points = base.subset(that.length()2, random);
103 
104             that.swap(points[0], points[1], other, points[0]);
105             repair(that, other, points[0], points[1]);
106             repair(other, that, points[0], points[1]);
107         }
108 
109         return 1;
110     }
111 
112     private static <T> void repair(
113         final MSeq<T> that, final MSeq<T> other,
114         final int begin, final int end
115     ) {
116         for (int i = 0; i < begin; ++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         for (int i = end, n = that.length(); i < n; ++i) {
124             int index = that.indexOf(that.get(i), begin, end);
125             while (index != -1) {
126                 that.set(i, other.get(index));
127                 index = that.indexOf(that.get(i), begin, end);
128             }
129         }
130     }
131 
132     @Override
133     public int hashCode() {
134         return Hash.of(getClass()).and(super.hashCode()).value();
135     }
136 
137     @Override
138     public boolean equals(final Object obj) {
139         return Equality.of(this, obj).test(super::equals);
140     }
141 
142     @Override
143     public String toString() {
144         return format("%s[p=%f]", getClass().getSimpleName(), _probability);
145     }
146 
147 }