001 /*
002 * Java Genetic Algorithm Library (jenetics-4.4.0).
003 * Copyright (c) 2007-2019 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 io.jenetics.internal.math.random.indexes;
024
025 import io.jenetics.internal.util.require;
026 import io.jenetics.util.MSeq;
027 import 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 multi-point crossover.
032 * <pre>
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 #getSwapProbability()}), whereas the
043 * probability that a given individual is selected for crossover is defined by
044 * the <i>crossover-probability</i> ({@link #getProbability()}).
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 3.7
051 * @since 3.7
052 */
053 public 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 = require.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 getSwapProbability() {
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.getRandom(), length, _swapProbability)
116 .peek(i -> that.swap(i, other))
117 .count();
118 }
119
120 }
|