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.math.base;
028 import org.jenetics.internal.util.Equality;
029 import org.jenetics.internal.util.Hash;
030
031 import org.jenetics.util.MSeq;
032 import org.jenetics.util.RandomRegistry;
033
034 /**
035 * <p><strong>Multiple point crossover</strong></p>
036 *
037 * If the {@code MultiPointCrossover} is created with one crossover point, it
038 * behaves exactly like the {@link SinglePointCrossover}. The following picture
039 * shows how the {@code MultiPointCrossover} works with two crossover points,
040 * defined at index 1 and 4.
041 * <p>
042 * <img src="doc-files/2PointCrossover.svg" width="400" alt="2-point crossover">
043 * </p>
044 *
045 * If the number of crossover points is odd, the crossover looks like in the
046 * following figure.
047 *
048 * <p>
049 * <img src="doc-files/3PointCrossover.svg" width="400" alt="3-point crossover">
050 * </p>
051 *
052 * @see SinglePointCrossover
053 *
054 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
055 * @since 1.2
056 * @version 3.6
057 */
058 public class MultiPointCrossover<
059 G extends Gene<?, G>,
060 C extends Comparable<? super C>
061 >
062 extends Crossover<G, C>
063 {
064
065 private final int _n;
066
067 /**
068 * Create a new crossover instance.
069 *
070 * @param probability the recombination probability.
071 * @param n the number of crossover points.
072 * @throws IllegalArgumentException if the {@code probability} is not in the
073 * valid range of {@code [0, 1]} or {@code n < 1}.
074 */
075 public MultiPointCrossover(final double probability, final int n) {
076 super(probability);
077 if (n < 1) {
078 throw new IllegalArgumentException(format(
079 "n must be at least 1 but was %d.", n
080 ));
081 }
082 _n = n;
083 }
084
085 /**
086 * Create a new crossover instance with two crossover points.
087 *
088 * @param probability the recombination probability.
089 * @throws IllegalArgumentException if the {@code probability} is not in the
090 * valid range of {@code [0, 1]}.
091 */
092 public MultiPointCrossover(final double probability) {
093 this(probability, 2);
094 }
095
096 /**
097 * Create a new crossover instance with default crossover probability of
098 * 0.05.
099 *
100 * @param n the number of crossover points.
101 * @throws IllegalArgumentException if {@code n < 1}.
102 */
103 public MultiPointCrossover(final int n) {
104 this(0.05, n);
105 }
106
107 /**
108 * Create a new crossover instance with two crossover points and crossover
109 * probability 0.05.
110 */
111 public MultiPointCrossover() {
112 this(0.05, 2);
113 }
114
115 /**
116 * Return the number of crossover points.
117 *
118 * @return the number of crossover points.
119 */
120 public int getN() {
121 return _n;
122 }
123
124 @Override
125 protected int crossover(final MSeq<G> that, final MSeq<G> other) {
126 assert that.length() == other.length();
127
128 final int n = min(that.length(), other.length());
129 final int k = min(n, _n);
130
131 final Random random = RandomRegistry.getRandom();
132 final int[] points = k > 0 ? base.subset(n, k, random) : new int[0];
133
134 crossover(that, other, points);
135 return 2;
136 }
137
138 // Package private for testing purpose.
139 static <T> void crossover(
140 final MSeq<T> that,
141 final MSeq<T> other,
142 final int[] indexes
143 ) {
144
145 for (int i = 0; i < indexes.length - 1; i += 2) {
146 final int start = indexes[i];
147 final int end = indexes[i + 1];
148 that.swap(start, end, other, start);
149 }
150 if (indexes.length%2 == 1) {
151 final int index = indexes[indexes.length - 1];
152 that.swap(index, min(that.length(), other.length()), other, index);
153 }
154 }
155
156 @Override
157 public int hashCode() {
158 return Hash.of(getClass())
159 .and(super.hashCode())
160 .and(_n).value();
161 }
162
163 @Override
164 public boolean equals(final Object obj) {
165 return Equality.of(this, obj).test(mpc ->
166 _n == mpc._n &&
167 super.equals(obj)
168 );
169 }
170
171 @Override
172 public String toString() {
173 return format(
174 "%s[p=%f, n=%d]",
175 getClass().getSimpleName(), _probability, _n
176 );
177 }
178
179 //public static <G extends Gene<?, G>> MultiPointCrossover<G> zip() {
180 // return new MultiPointCrossover<>(Integer.MAX_VALUE);
181 //}
182
183 }
|