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