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