All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jenetics.Crossover Maven / Gradle / Ivy

There is a newer version: 3.6.0
Show newest version
/*
 * Java Genetic Algorithm Library (jenetics-3.4.0).
 * Copyright (c) 2007-2016 Franz Wilhelmstötter
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:
 *    Franz Wilhelmstötter ([email protected])
 */
package org.jenetics;

import java.util.Random;

import org.jenetics.util.MSeq;
import org.jenetics.util.RandomRegistry;

/**
 * 

* Performs a * Crossover of two {@link Chromosome}. *

*

* The order ({@link #getOrder()}) of this Recombination implementation is two. *

* * @param the gene type. * * @author Franz Wilhelmstötter * @since 1.0 * @version 3.0 */ public abstract class Crossover< G extends Gene, C extends Comparable > extends Recombinator { /** * Constructs an alterer with a given recombination probability. * * @param probability The recombination probability. * @throws IllegalArgumentException if the {@code probability} is not in the * valid range of {@code [0, 1]}. */ protected Crossover(final double probability) { super(probability, 2); } @Override protected final int recombine( final Population population, final int[] individuals, final long generation ) { final Random random = RandomRegistry.getRandom(); final Phenotype pt1 = population.get(individuals[0]); final Phenotype pt2 = population.get(individuals[1]); final Genotype gt1 = pt1.getGenotype(); final Genotype gt2 = pt2.getGenotype(); //Choosing the Chromosome for crossover. final int chIndex = random.nextInt(gt1.length()); final MSeq> c1 = gt1.toSeq().copy(); final MSeq> c2 = gt2.toSeq().copy(); final MSeq genes1 = c1.get(chIndex).toSeq().copy(); final MSeq genes2 = c2.get(chIndex).toSeq().copy(); crossover(genes1, genes2); c1.set(chIndex, c1.get(chIndex).newInstance(genes1.toISeq())); c2.set(chIndex, c2.get(chIndex).newInstance(genes2.toISeq())); //Creating two new Phenotypes and exchanging it with the old. population.set( individuals[0], pt1.newInstance(gt1.newInstance(c1.toISeq()), generation) ); population.set( individuals[1], pt2.newInstance(gt1.newInstance(c2.toISeq()), generation) ); return getOrder(); } /** * Template method which performs the crossover. The arguments given are * mutable non null arrays of the same length. * * @param that the genes of the first chromosome * @param other the genes of the other chromosome * @return the number of altered genes */ protected abstract int crossover(final MSeq that, final MSeq other); }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy