001 /*
002 * Java Genetic Algorithm Library (jenetics-4.0.0).
003 * Copyright (c) 2007-2017 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 import static java.lang.String.format;
024
025 import java.util.Random;
026
027 import io.jenetics.internal.util.Equality;
028 import io.jenetics.internal.util.Hash;
029 import io.jenetics.util.MSeq;
030 import io.jenetics.util.RandomRegistry;
031
032 /**
033 * <strong>Single point crossover</strong>
034 *
035 * <p>
036 * One or two children are created by taking two parent strings and cutting
037 * them at some randomly chosen site. E.g.
038 * <p>
039 * <img src="doc-files/SinglePointCrossover.svg" width="400"
040 * alt="Single-point crossover" >
041 * <p>
042 * If we create a child and its complement we preserving the total number of
043 * genes in the population, preventing any genetic drift.
044 * Single-point crossover is the classic form of crossover. However, it produces
045 * very slow mixing compared with multi-point crossover or uniform crossover.
046 * For problems where the site position has some intrinsic meaning to the
047 * problem single-point crossover can lead to small disruption than multi-point
048 * or uniform crossover.
049 *
050 * @see MultiPointCrossover
051 *
052 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
053 * @since 1.0
054 * @version 4.0
055 */
056 public class SinglePointCrossover<
057 G extends Gene<?, G>,
058 C extends Comparable<? super C>
059 >
060 extends MultiPointCrossover<G, C>
061 {
062
063 /**
064 * Constructs an alterer with a given recombination probability.
065 *
066 * @param probability the crossover probability.
067 * @throws IllegalArgumentException if the {@code probability} is not in the
068 * valid range of {@code [0, 1]}.
069 */
070 public SinglePointCrossover(final double probability) {
071 super(probability, 1);
072 }
073
074 /**
075 * Create a new single point crossover object with crossover probability of
076 * {@code 0.05}.
077 */
078 public SinglePointCrossover() {
079 this(0.05);
080 }
081
082 @Override
083 protected int crossover(final MSeq<G> that, final MSeq<G> other) {
084 final Random random = RandomRegistry.getRandom();
085
086 final int index = random.nextInt(min(that.length(), other.length()));
087 crossover(that, other, index);
088 return 2;
089 }
090
091 // Package private for testing purpose.
092 static <T> void crossover(
093 final MSeq<T> that,
094 final MSeq<T> other,
095 final int index
096 ) {
097 assert index >= 0 :
098 format(
099 "Crossover index must be within [0, %d) but was %d",
100 that.length(), index
101 );
102
103 that.swap(index, min(that.length(), other.length()), other, index);
104 }
105
106 @Override
107 public int hashCode() {
108 return Hash.of(getClass()).and(super.hashCode()).value();
109 }
110
111 @Override
112 public boolean equals(final Object obj) {
113 return Equality.of(this, obj).test(super::equals);
114 }
115
116 @Override
117 public String toString() {
118 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
119 }
120
121 }
|