001/*
002 * Java Genetic Algorithm Library (jenetics-8.1.0).
003 * Copyright (c) 2007-2024 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 io.jenetics.internal.math.Randoms.indexes;
024
025import io.jenetics.internal.util.Requires;
026import io.jenetics.util.MSeq;
027import io.jenetics.util.RandomRegistry;
028
029/**
030 * The uniform crossover uses swaps single genes between two chromosomes, instead
031 * of whole ranges as in single- and multipoint crossover.
032 * <pre> {@code
033 * +---+---+---+---+---+---+---+
034 * | 1 | 2 | 3 | 4 | 6 | 7 | 8 |
035 * +-+-+---+-+-+-+-+---+-+-+---+
036 *   |       |   |       |        swapping
037 * +-+-+---+-+-+-+-+---+-+-+---+
038 * | a | b | c | d | e | f | g |
039 * +---+---+---+---+---+---+---+
040 * } </pre>
041 * The probability that two genes are swapped is controlled by the
042 * <i>swap-probability</i> ({@link #swapProbability()}), whereas the
043 * probability that a given individual is selected for crossover is defined by
044 * the <i>crossover-probability</i> ({@link #probability()}).
045 *
046 * @see <a href="https://en.wikipedia.org/wiki/Crossover_(genetic_algorithm)#Uniform_crossover_and_half_uniform_crossover">
047 *     Wikipedia: Uniform crossover</a>
048 *
049 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
050 * @version 6.0
051 * @since 3.7
052 */
053public class UniformCrossover<
054        G extends Gene<?, G>,
055        C extends Comparable<? super C>
056>
057        extends Crossover<G, C>
058{
059
060        private final double _swapProbability;
061
062        /**
063         * Create a new universal crossover instance.
064         *
065         * @param crossoverProbability the recombination probability as defined in
066         *        {@link Crossover#Crossover(double)}. This is the probability that
067         *        a given individual is selected for crossover.
068         * @param swapProbability the probability for swapping a given gene of
069         *         a chromosome
070         * @throws IllegalArgumentException if the probabilities are not in the
071         *         valid range of {@code [0, 1]}
072         */
073        public UniformCrossover(
074                final double crossoverProbability,
075                final double swapProbability
076        ) {
077                super(crossoverProbability);
078                _swapProbability = Requires.probability(swapProbability);
079        }
080
081        /**
082         * Create a new universal crossover instance. The {@code swapProbability} is
083         * set to {@link Alterer#DEFAULT_ALTER_PROBABILITY}.
084         *
085         * @param crossoverProbability the recombination probability as defined in
086         *        {@link Crossover#Crossover(double)}. This is the probability that
087         *        a given individual is selected for crossover.
088         * @throws IllegalArgumentException if the probabilities are not in the
089         *         valid range of {@code [0, 1]}
090         */
091        public UniformCrossover(final double crossoverProbability) {
092                this(crossoverProbability, DEFAULT_ALTER_PROBABILITY);
093        }
094
095        /**
096         * Create a new universal crossover instance. The probabilities are set to
097         * {@link Alterer#DEFAULT_ALTER_PROBABILITY}.
098         */
099        public UniformCrossover() {
100                this(DEFAULT_ALTER_PROBABILITY, DEFAULT_ALTER_PROBABILITY);
101        }
102
103        /**
104         * Return the probability for swapping genes of a chromosome.
105         *
106         * @return the probability for swapping genes of a chromosome
107         */
108        public double swapProbability() {
109                return _swapProbability;
110        }
111
112        @Override
113        protected int crossover(final MSeq<G> that, final MSeq<G> other) {
114                final int length = min(that.length(), other.length());
115                return (int)indexes(RandomRegistry.random(), length, _swapProbability)
116                        .peek(i -> that.swap(i, other))
117                        .count();
118        }
119
120}