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