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;
023
024import io.jenetics.util.MSeq;
025import io.jenetics.util.RandomRegistry;
026
027/**
028 * <p>
029 * Performs a <a href="http://en.wikipedia.org/wiki/Crossover_%28genetic_algorithm%29">
030 * Crossover</a> of two {@link Chromosome}. This crossover implementation can
031 * handle genotypes with different length (different number of chromosomes). It
032 * is guaranteed that chromosomes with the the same (genotype) index are chosen
033 * for <em>crossover</em>.
034 * </p>
035 * <p>
036 * The order ({@link #order()}) of this Recombination implementation is two.
037 * </p>
038 *
039 * @param <G> the gene type.
040 *
041 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
042 * @since 1.0
043 * @version 4.0
044 */
045public abstract class Crossover<
046        G extends Gene<?, G>,
047        C extends Comparable<? super C>
048>
049        extends Recombinator<G, C>
050{
051
052        /**
053         * Constructs an alterer with a given recombination probability.
054         *
055         * @param probability the recombination probability
056         * @throws IllegalArgumentException if the {@code probability} is not in the
057         *          valid range of {@code [0, 1]}
058         */
059        protected Crossover(final double probability) {
060                super(probability, 2);
061        }
062
063        @Override
064        protected final int recombine(
065                final MSeq<Phenotype<G, C>> population,
066                final int[] individuals,
067                final long generation
068        ) {
069                assert individuals.length == 2 : "Required order of 2";
070
071                final var pt1 = population.get(individuals[0]);
072                final var pt2 = population.get(individuals[1]);
073                final var gt1 = pt1.genotype();
074                final var gt2 = pt2.genotype();
075
076                //Choosing the Chromosome index for crossover.
077                final int chIndex = RandomRegistry.random()
078                        .nextInt(min(gt1.length(), gt2.length()));
079
080                final var c1 = MSeq.of(gt1);
081                final var c2 = MSeq.of(gt2);
082                final var genes1 = MSeq.of(c1.get(chIndex));
083                final var genes2 = MSeq.of(c2.get(chIndex));
084
085                crossover(genes1, genes2);
086
087                c1.set(chIndex, c1.get(chIndex).newInstance(genes1.toISeq()));
088                c2.set(chIndex, c2.get(chIndex).newInstance(genes2.toISeq()));
089
090                //Creating two new Phenotypes and exchanging it with the old.
091                population.set(
092                        individuals[0],
093                        Phenotype.of(Genotype.of(c1), generation)
094                );
095                population.set(
096                        individuals[1],
097                        Phenotype.of(Genotype.of(c2), generation)
098                );
099
100                return order();
101        }
102
103        /**
104         * Template method which performs the crossover. The arguments given are
105         * mutable non-null arrays of the same length.
106         *
107         * @param that the genes of the first chromosome
108         * @param other the genes of the other chromosome
109         * @return the number of altered genes
110         */
111        protected abstract int crossover(final MSeq<G> that, final MSeq<G> other);
112
113}