/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics;

import static java.lang.Math.min;
import static org.jenetics.internal.math.random.indexes;

import org.jenetics.internal.util.require;

import org.jenetics.util.MSeq;
import org.jenetics.util.RandomRegistry;

/**
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @version !__version__!
 * @since !__version__!
 */
public class UniformCrossover<
	G extends Gene<?, G>,
	C extends Comparable<? super C>
>
	extends Crossover<G, C>
{

	private final double _swapProbability;

	/**
	 * Create a new universal crossover instance.
	 *
	 * @param crossoverProbability the recombination probability as defined in
	 *        {@link Crossover#Crossover(double)}. This is the probability that
	 *        a given individual is selected for crossover.
	 * @param swapProbability the probability for swapping genes a given gene of
	 *         a chromosome
	 * @throws IllegalArgumentException if the probabilities are not in the
	 *         valid range of {@code [0, 1]}
	 */
	public UniformCrossover(
		final double crossoverProbability,
		final double swapProbability
	) {
		super(crossoverProbability);
		_swapProbability = require.probability(swapProbability);
	}

	/**
	 * Create a new universal crossover instance. The {@code swapProbability} is
	 * set to {@link Alterer#DEFAULT_ALTER_PROBABILITY}.
	 *
	 * @param crossoverProbability the recombination probability as defined in
	 *        {@link Crossover#Crossover(double)}. This is the probability that
	 *        a given individual is selected for crossover.
	 * @throws IllegalArgumentException if the probabilities are not in the
	 *         valid range of {@code [0, 1]}
	 */
	public UniformCrossover(final double crossoverProbability) {
		this(crossoverProbability, DEFAULT_ALTER_PROBABILITY);
	}

	/**
	 * Create a new universal crossover instance. The probabilities are set to
	 * {@link Alterer#DEFAULT_ALTER_PROBABILITY}.
	 */
	public UniformCrossover() {
		this(DEFAULT_ALTER_PROBABILITY, DEFAULT_ALTER_PROBABILITY);
	}

	/**
	 * Return the probability for swapping genes of a chromosome.
	 *
	 * @return the probability for swapping genes of a chromosome
	 */
	public double getSwapProbability() {
		return _swapProbability;
	}

	@Override
	protected int crossover(final MSeq<G> that, final MSeq<G> other) {
		final int length = min(that.length(), other.length());
		return (int)indexes(RandomRegistry.getRandom(), length, _swapProbability)
			.peek(i -> swap(i, that, other))
			.count();
	}

	private static <T> void swap(
		final int index,
		final MSeq<T> that,
		final MSeq<T> other
	) {
		final T temp = that.get(index);
		that.set(index, other.get(index));
		other.set(index, temp);
	}

}

@@ -970,16 +970,752 @@
 y;%0A%0A/**%0A
+ * The uniform crossover uses swaps single genes between two chromosomes, instead%0A * of whole ranges as in single- and multi-point crossover.%0A * %3Cpre%3E%0A * +---+---+---+---+---+---+---+%0A * %7C 1 %7C 2 %7C 3 %7C 4 %7C 6 %7C 7 %7C 8 %7C%0A * +-+-+---+-+-+-+-+---+-+-+---+%0A *   %E2%88%A7       %E2%88%A7   %E2%88%A7       %E2%88%A7%0A *   %7C       %7C   %7C       %7C  swapping%0A *   %E2%88%A8       %E2%88%A8   %E2%88%A8       %E2%88%A8%0A * +-+-+---+-+-+-+-+---+-+-+---+%0A * %7C a %7C b %7C c %7C d %7C e %7C f %7C g %7C%0A * +---+---+---+---+---+---+---+%0A * %3C/pre%3E%0A * The probability that two genes are swapped is controlled by the%0A * %3Ci%3Eswap-probability%3C/i%3E (%7B@link #getSwapProbability()%7D), whereas the%0A * probability that a given individual is selected for crossover is defined by%0A * the %3Ci%3Ecrossover-probability%3C/i%3E (%7B@link #getProbability()%7D).%0A *%0A
  * @auth

Add Javadoc.