| 
001 /*002  * Java Genetic Algorithm Library (jenetics-3.0.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 3.0 — <em>$Date: 2014-12-28 $</em>
 041  */
 042 public abstract class Crossover<
 043     G extends Gene<?, G>,
 044     C extends Comparable<? super C>
 045 >
 046     extends Recombinator<G, C>
 047 {
 048
 049     /**
 050      * Constructs an alterer with a given recombination probability.
 051      *
 052      * @param probability The recombination probability.
 053      * @throws IllegalArgumentException if the {@code probability} is not in the
 054      *          valid range of {@code [0, 1]}.
 055      */
 056     protected Crossover(final double probability) {
 057         super(probability, 2);
 058     }
 059
 060     @Override
 061     protected final int recombine(
 062         final Population<G, C> population,
 063         final int[] individuals,
 064         final long generation
 065     ) {
 066         final Random random = RandomRegistry.getRandom();
 067
 068         final Phenotype<G, C> pt1 = population.get(individuals[0]);
 069         final Phenotype<G, C> pt2 = population.get(individuals[1]);
 070         final Genotype<G> gt1 = pt1.getGenotype();
 071         final Genotype<G> gt2 = pt2.getGenotype();
 072
 073         //Choosing the Chromosome for crossover.
 074         final int chIndex = random.nextInt(gt1.length());
 075
 076         final MSeq<Chromosome<G>> c1 = gt1.toSeq().copy();
 077         final MSeq<Chromosome<G>> c2 = gt2.toSeq().copy();
 078         final MSeq<G> genes1 = c1.get(chIndex).toSeq().copy();
 079         final MSeq<G> genes2 = c2.get(chIndex).toSeq().copy();
 080
 081         crossover(genes1, genes2);
 082
 083         c1.set(chIndex, c1.get(chIndex).newInstance(genes1.toISeq()));
 084         c2.set(chIndex, c2.get(chIndex).newInstance(genes2.toISeq()));
 085
 086         //Creating two new Phenotypes and exchanging it with the old.
 087         population.set(
 088             individuals[0],
 089             pt1.newInstance(gt1.newInstance(c1.toISeq()), generation)
 090         );
 091         population.set(
 092             individuals[1],
 093             pt2.newInstance(gt1.newInstance(c2.toISeq()), generation)
 094         );
 095
 096         return getOrder();
 097     }
 098
 099
 100     /**
 101      * Template method which performs the crossover. The arguments given are
 102      * mutable non null arrays of the same length.
 103      *
 104      * @param that the genes of the first chromosome
 105      * @param other the genes of the other chromosome
 106      * @return the number of altered genes
 107      */
 108     protected abstract int crossover(final MSeq<G> that, final MSeq<G> other);
 109
 110
 111 }
 |