Crossover.java
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 
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 #getOrder()}) 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     @SuppressWarnings("deprecation")
067     protected final int recombine(
068         final MSeq<Phenotype<G, C>> population,
069         final int[] individuals,
070         final long generation
071     ) {
072         assert individuals.length == "Required order of 2";
073         final Random random = RandomRegistry.getRandom();
074 
075         final Phenotype<G, C> pt1 = population.get(individuals[0]);
076         final Phenotype<G, C> pt2 = population.get(individuals[1]);
077         final Genotype<G> gt1 = pt1.getGenotype();
078         final Genotype<G> gt2 = pt2.getGenotype();
079 
080         //Choosing the Chromosome index for crossover.
081         final int chIndex = random.nextInt(min(gt1.length(), gt2.length()));
082 
083         final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy();
084         final MSeq<Chromosome<G>> c2 = gt2.toSeq().copy();
085         final MSeq<G> genes1 = c1.get(chIndex).toSeq().copy();
086         final MSeq<G> genes2 = c2.get(chIndex).toSeq().copy();
087 
088         crossover(genes1, genes2);
089 
090         c1.set(chIndex, c1.get(chIndex).newInstance(genes1.toISeq()));
091         c2.set(chIndex, c2.get(chIndex).newInstance(genes2.toISeq()));
092 
093         //Creating two new Phenotypes and exchanging it with the old.
094         population.set(
095             individuals[0],
096             pt1.newInstance(Genotype.of(c1), generation)
097         );
098         population.set(
099             individuals[1],
100             pt2.newInstance(Genotype.of(c2), generation)
101         );
102 
103         return getOrder();
104     }
105 
106     /**
107      * Template method which performs the crossover. The arguments given are
108      * mutable non null arrays of the same length.
109      *
110      @param that the genes of the first chromosome
111      @param other the genes of the other chromosome
112      @return the number of altered genes
113      */
114     protected abstract int crossover(final MSeq<G> that, final MSeq<G> other);
115 
116 }