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