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