MultiPointCrossover.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-4.3.0).
003  * Copyright (c) 2007-2018 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.Math.min;
023 import static java.lang.String.format;
024 import static io.jenetics.internal.util.Hashes.hash;
025 
026 import java.util.Random;
027 
028 import io.jenetics.internal.math.comb;
029 import io.jenetics.util.MSeq;
030 import io.jenetics.util.RandomRegistry;
031 
032 /**
033  <p><strong>Multiple point crossover</strong></p>
034  *
035  * If the {@code MultiPointCrossover} is created with one crossover point, it
036  * behaves exactly like the {@link SinglePointCrossover}. The following picture
037  * shows how the {@code MultiPointCrossover} works with two crossover points,
038  * defined at index 1 and 4.
039  <p>
040  *    <img src="doc-files/2PointCrossover.svg" width="400" alt="2-point crossover">
041  </p>
042  *
043  * If the number of crossover points is odd, the crossover looks like in the
044  * following figure.
045  *
046  <p>
047  *    <img src="doc-files/3PointCrossover.svg" width="400" alt="3-point crossover">
048  </p>
049  *
050  @see SinglePointCrossover
051  *
052  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
053  @since 1.2
054  @version 4.0
055  */
056 public class MultiPointCrossover<
057     extends Gene<?, G>,
058     extends Comparable<? super C>
059 >
060     extends Crossover<G, C>
061 {
062 
063     private final int _n;
064 
065     /**
066      * Create a new crossover instance.
067      *
068      @param probability the recombination probability.
069      @param n the number of crossover points.
070      @throws IllegalArgumentException if the {@code probability} is not in the
071      *         valid range of {@code [0, 1]} or {@code n &lt; 1}.
072      */
073     public MultiPointCrossover(final double probability, final int n) {
074         super(probability);
075         if (n < 1) {
076             throw new IllegalArgumentException(format(
077                 "n must be at least 1 but was %d.", n
078             ));
079         }
080         _n = n;
081     }
082 
083     /**
084      * Create a new crossover instance with two crossover points.
085      *
086      @param probability the recombination probability.
087      @throws IllegalArgumentException if the {@code probability} is not in the
088      *         valid range of {@code [0, 1]}.
089      */
090     public MultiPointCrossover(final double probability) {
091         this(probability, 2);
092     }
093 
094     /**
095      * Create a new crossover instance with default crossover probability of
096      * 0.05.
097      *
098      @param n the number of crossover points.
099      @throws IllegalArgumentException if {@code n &lt; 1}.
100      */
101     public MultiPointCrossover(final int n) {
102         this(0.05, n);
103     }
104 
105     /**
106      * Create a new crossover instance with two crossover points and crossover
107      * probability 0.05.
108      */
109     public MultiPointCrossover() {
110         this(0.052);
111     }
112 
113     /**
114      * Return the number of crossover points.
115      *
116      @return the number of crossover points.
117      */
118     public int getN() {
119         return _n;
120     }
121 
122     @Override
123     protected int crossover(final MSeq<G> that, final MSeq<G> other) {
124         assert that.length() == other.length();
125 
126         final int n = min(that.length(), other.length());
127         final int k = min(n, _n);
128 
129         final Random random = RandomRegistry.getRandom();
130         final int[] points = k > ? comb.subset(n, k, randomnew int[0];
131 
132         crossover(that, other, points);
133         return 2;
134     }
135 
136     // Package private for testing purpose.
137     static <T> void crossover(
138         final MSeq<T> that,
139         final MSeq<T> other,
140         final int[] indexes
141     ) {
142 
143         for (int i = 0; i < indexes.length - 1; i += 2) {
144             final int start = indexes[i];
145             final int end = indexes[i + 1];
146             that.swap(start, end, other, start);
147         }
148         if (indexes.length%== 1) {
149             final int index = indexes[indexes.length - 1];
150             that.swap(index, min(that.length(), other.length()), other, index);
151         }
152     }
153 
154     @Override
155     public int hashCode() {
156         return hash(super.hashCode(), hash(_n));
157     }
158 
159     @Override
160     public boolean equals(final Object obj) {
161         return obj == this ||
162             obj != null &&
163             getClass() == obj.getClass() &&
164             _n == ((MultiPointCrossover)obj)._n &&
165             super.equals(obj);
166     }
167 
168     @Override
169     public String toString() {
170         return format(
171             "%s[p=%f, n=%d]",
172             getClass().getSimpleName(), _probability, _n
173         );
174     }
175 
176 }