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

org.jenetics.SinglePointCrossover 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 static java.lang.String.format;

import java.util.Random;

import org.jenetics.internal.util.Equality;
import org.jenetics.internal.util.Hash;

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

/**
 * Single point crossover
 *
 * 

* One or two children are created by taking two parent strings and cutting * them at some randomly chosen site. E.g. *

* Single-point crossover *

* If we create a child and its complement we preserving the total number of * genes in the population, preventing any genetic drift. * Single-point crossover is the classic form of crossover. However, it produces * very slow mixing compared with multi-point crossover or uniform crossover. * For problems where the site position has some intrinsic meaning to the * problem single-point crossover can lead to small disruption than multi-point * or uniform crossover. * * @see MultiPointCrossover * * @author Franz Wilhelmstötter * @since 1.0 * @version 3.0 */ public class SinglePointCrossover< G extends Gene, C extends Comparable > extends MultiPointCrossover { /** * Constructs an alterer with a given recombination probability. * * @param probability the crossover probability. * @throws IllegalArgumentException if the {@code probability} is not in the * valid range of {@code [0, 1]}. */ public SinglePointCrossover(final double probability) { super(probability, 1); } /** * Create a new single point crossover object with crossover probability of * {@code 0.05}. */ public SinglePointCrossover() { this(0.05); } @Override protected int crossover(final MSeq that, final MSeq other) { assert that.length() == other.length(); final Random random = RandomRegistry.getRandom(); crossover(that, other, random.nextInt(that.length())); return 2; } // Package private for testing purpose. static void crossover( final MSeq that, final MSeq other, final int index ) { assert index >= 0 : format( "Crossover index must be within [0, %d) but was %d", that.length(), index ); that.swap(index, that.length(), other, index); } @Override public int hashCode() { return Hash.of(getClass()).and(super.hashCode()).value(); } @Override public boolean equals(final Object obj) { return Equality.of(this, obj).test(super::equals); } @Override public String toString() { return format("%s[p=%f]", getClass().getSimpleName(), _probability); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy