SinglePointCrossover.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-2.0.2).
003  * Copyright (c) 2007-2014 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.String.format;
023 
024 import java.util.Random;
025 
026 import org.jenetics.internal.util.HashBuilder;
027 
028 import org.jenetics.util.MSeq;
029 import org.jenetics.util.RandomRegistry;
030 
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@gmx.at">Franz Wilhelmstötter</a>
053  @since 1.0
054  @version 2.0 &mdash; <em>$Date: 2014-03-28 $</em>
055  */
056 public class SinglePointCrossover<G extends Gene<?, G>>
057     extends MultiPointCrossover<G>
058 {
059 
060     /**
061      * Constructs an alterer with a given recombination probability.
062      *
063      @param probability the crossover probability.
064      @throws IllegalArgumentException if the {@code probability} is not in the
065      *         valid range of {@code [0, 1]}.
066      */
067     public SinglePointCrossover(final double probability) {
068         super(probability, 1);
069     }
070 
071     /**
072      * Create a new single point crossover object with crossover probability of
073      * {@code 0.05}.
074      */
075     public SinglePointCrossover() {
076         this(0.05);
077     }
078 
079 
080 
081     @Override
082     protected int crossover(final MSeq<G> that, final MSeq<G> other) {
083         assert (that.length() == other.length());
084 
085         final Random random = RandomRegistry.getRandom();
086         crossover(that, other, random.nextInt(that.length()));
087         return 2;
088     }
089 
090     // Package private for testing purpose.
091     static <T> void crossover(
092         final MSeq<T> that,
093         final MSeq<T> other,
094         final int index
095     ) {
096         assert (index >= 0: format(
097             "Crossover index must be within [0, %d) but was %d",
098             that.length(), index
099         );
100 
101         that.swap(index, that.length(), other, index);
102     }
103 
104     @Override
105     public int hashCode() {
106         return HashBuilder.of(getClass()).and(super.hashCode()).value();
107     }
108 
109     @Override
110     public boolean equals(final Object obj) {
111         if (obj == this) {
112             return true;
113         }
114         if (obj == null || obj.getClass() != getClass()) {
115             return false;
116         }
117 
118         return super.equals(obj);
119     }
120 
121     @Override
122     public String toString() {
123         return format("%s[p=%f]", getClass().getSimpleName(), _probability);
124     }
125 
126 }