001 /*
002 * Java Genetic Algorithm Library (jenetics-1.5.0).
003 * Copyright (c) 2007-2013 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 import static org.jenetics.util.object.hashCodeOf;
024
025 import java.util.Random;
026
027 import org.jenetics.util.MSeq;
028 import org.jenetics.util.RandomRegistry;
029
030
031 /**
032 * <strong><p>Single point crossover</p></strong>
033 *
034 * <p>
035 * One or two children are created by taking two parent strings and cutting
036 * them at some randomly chosen site. E.g.
037 * </p>
038 * <div align="center">
039 * <img src="doc-files/SinglePointCrossover.svg" width="400" >
040 * </div>
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 * </p>
050 *
051 * @see MultiPointCrossover
052 *
053 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
054 * @since 1.0
055 * @version 1.2 — <em>$Date: 2013-08-30 $</em>
056 */
057 public class SinglePointCrossover<G extends Gene<?, G>>
058 extends MultiPointCrossover<G>
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
081
082 @Override
083 protected int crossover(final MSeq<G> that, final MSeq<G> other) {
084 assert (that.length() == other.length());
085
086 final Random random = RandomRegistry.getRandom();
087 crossover(that, other, random.nextInt(that.length()));
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) : format(
098 "Crossover index must be within [0, %d) but was %d",
099 that.length(), index
100 );
101
102 that.swap(index, that.length(), other, index);
103 }
104
105 @Override
106 public int hashCode() {
107 return hashCodeOf(getClass()).and(super.hashCode()).value();
108 }
109
110 @Override
111 public boolean equals(final Object obj) {
112 if (obj == this) {
113 return true;
114 }
115 if (obj == null || obj.getClass() != getClass()) {
116 return false;
117 }
118
119 return super.equals(obj);
120 }
121
122 @Override
123 public String toString() {
124 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
125 }
126
127 }
|