001 /*
002 * Java Genetic Algorithm Library (jenetics-1.6.0).
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><p>Single point crossover</p></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 * <div align="center">
040 * <img src="doc-files/SinglePointCrossover.svg" width="400" >
041 * </div>
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 * </p>
051 *
052 * @see MultiPointCrossover
053 *
054 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
055 * @since 1.0
056 * @version 1.2 — <em>$Date: 2014-02-27 $</em>
057 */
058 public class SinglePointCrossover<G extends Gene<?, G>>
059 extends MultiPointCrossover<G>
060 {
061
062 /**
063 * Constructs an alterer with a given recombination probability.
064 *
065 * @param probability the crossover probability.
066 * @throws IllegalArgumentException if the {@code probability} is not in the
067 * valid range of {@code [0, 1]}.
068 */
069 public SinglePointCrossover(final double probability) {
070 super(probability, 1);
071 }
072
073 /**
074 * Create a new single point crossover object with crossover probability of
075 * {@code 0.05}.
076 */
077 public SinglePointCrossover() {
078 this(0.05);
079 }
080
081
082
083 @Override
084 protected int crossover(final MSeq<G> that, final MSeq<G> other) {
085 assert (that.length() == other.length());
086
087 final Random random = RandomRegistry.getRandom();
088 crossover(that, other, random.nextInt(that.length()));
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 >= 0) : format(
099 "Crossover index must be within [0, %d) but was %d",
100 that.length(), index
101 );
102
103 that.swap(index, that.length(), other, index);
104 }
105
106 @Override
107 public int hashCode() {
108 return HashBuilder.of(getClass()).and(super.hashCode()).value();
109 }
110
111 @Override
112 public boolean equals(final Object obj) {
113 if (obj == this) {
114 return true;
115 }
116 if (obj == null || obj.getClass() != getClass()) {
117 return false;
118 }
119
120 return super.equals(obj);
121 }
122
123 @Override
124 public String toString() {
125 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
126 }
127
128 }
|