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