Crossover.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-1.6.0).
003  * Copyright (c) 2007-2014 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@gmx.at)
019  */
020 package org.jenetics;
021 
022 import java.util.Random;
023 
024 import org.jenetics.util.MSeq;
025 import org.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}.
031  </p>
032  <p>
033  * The order ({@link #getOrder()}) of this Recombination implementation is two.
034  </p>
035  *
036  @param <G> the gene type.
037  *
038  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
039  @since 1.0
040  @version 1.0 &mdash; <em>$Date: 2014-02-15 $</em>
041  */
042 public abstract class Crossover<G extends Gene<?, G>> extends Recombinator<G> {
043 
044     /**
045      * Constructs an alterer with a given recombination probability.
046      *
047      @param probability The recombination probability.
048      @throws IllegalArgumentException if the {@code probability} is not in the
049      *          valid range of {@code [0, 1]}.
050      */
051     protected Crossover(final double probability) {
052         super(probability, 2);
053     }
054 
055     @Override
056     protected final <C extends Comparable<? super C>> int recombine(
057         final Population<G, C> population,
058         final int[] individuals,
059         final int generation
060     ) {
061         final Random random = RandomRegistry.getRandom();
062 
063         final Phenotype<G, C> pt1 = population.get(individuals[0]);
064         final Phenotype<G, C> pt2 = population.get(individuals[1]);
065         final Genotype<G> gt1 = pt1.getGenotype();
066         final Genotype<G> gt2 = pt2.getGenotype();
067 
068         //Choosing the Chromosome for crossover.
069         final int chIndex = random.nextInt(gt1.length());
070 
071         final MSeq<Chromosome<G>> chroms1 = gt1.toSeq().copy();
072         final MSeq<Chromosome<G>> chroms2 = gt2.toSeq().copy();
073         final MSeq<G> genes1 = chroms1.get(chIndex).toSeq().copy();
074         final MSeq<G> genes2 = chroms2.get(chIndex).toSeq().copy();
075 
076         crossover(genes1, genes2);
077 
078         chroms1.set(chIndex, chroms1.get(chIndex).newInstance(genes1.toISeq()));
079         chroms2.set(chIndex, chroms2.get(chIndex).newInstance(genes2.toISeq()));
080 
081         //Creating two new Phenotypes and exchanging it with the old.
082         population.set(
083             individuals[0],
084             pt1.newInstance(gt1.newInstance(chroms1.toISeq()), generation)
085         );
086         population.set(
087             individuals[1],
088             pt2.newInstance(gt1.newInstance(chroms2.toISeq()), generation)
089         );
090 
091         return getOrder();
092     }
093 
094 
095     /**
096      * Template method which performs the crossover. The arguments given are
097      * mutable non null arrays of the same length.
098      */
099     protected abstract int crossover(final MSeq<G> that, final MSeq<G> other);
100 
101 
102 }