001 /*
002 * Java Genetic Algorithm Library (jenetics-6.0.0).
003 * Copyright (c) 2007-2020 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 import static io.jenetics.internal.math.Randoms.nextDouble;
025
026 import java.util.Random;
027
028 import io.jenetics.internal.util.Requires;
029 import io.jenetics.util.MSeq;
030 import io.jenetics.util.RandomRegistry;
031
032 /**
033 * This alterer takes two chromosome (treating it as vectors) and creates a
034 * linear combination of this vectors as result. The line-recombination depends
035 * on a variable <em>p</em> which determines how far out along the line (defined
036 * by the two multidimensional points/vectors) the children are allowed to be.
037 * If <em>p</em> = 0 then the children will be located along the line within the
038 * hypercube between the two points. If <em>p</em> > 0 then the children may
039 * be located anywhere on the line, even somewhat outside of the hypercube.
040 * <p>
041 * Points outside of the allowed numeric range are rejected and a new points are
042 * generated, until they lie in the valid range. The strategy on how
043 * out-of-range points are handled, is the difference to the very similar
044 * {@link LineCrossover}.
045 *
046 * @see <a href="https://cs.gmu.edu/~sean/book/metaheuristics/"><em>
047 * Essentials of Metaheuristic, page 42</em></a>
048 * @see LineCrossover
049 *
050 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
051 * @version 3.8
052 * @since 3.8
053 */
054 public class IntermediateCrossover<
055 G extends NumericGene<?, G>,
056 C extends Comparable<? super C>
057 >
058 extends Crossover<G, C>
059 {
060
061 private final double _p;
062
063 /**
064 * Creates a new intermediate-crossover with the given recombination
065 * probability and the line-scaling factor <em>p</em>.
066 * <p>
067 * <b>When the value for <em>p</em> is greater then 0, the crossover point
068 * generation must be repeated until the points lie within the allowed
069 * range. Values greater then 10 are usually not recommended, since this
070 * leads to unnecessary crossover point generation.</b>
071 *
072 * @param probability the recombination probability.
073 * @param p defines the possible location of the recombined chromosomes. If
074 * <em>p</em> = 0 then the children will be located along the line
075 * within the hypercube between the two points. If <em>p</em> > 0
076 * then the children may be located anywhere on the line, even
077 * somewhat outside of the hypercube.
078 * @throws IllegalArgumentException if the {@code probability} is not in the
079 * valid range of {@code [0, 1]} or if {@code p} is smaller then zero
080 */
081 public IntermediateCrossover(final double probability, final double p) {
082 super(probability);
083 _p = Requires.nonNegative(p, "p");
084 }
085
086 /**
087 * Creates a new intermediate-crossover with the given recombination
088 * probability. The parameter <em>p</em> is set to zero, which restricts the
089 * recombined chromosomes within the hypercube of the selected chromosomes
090 * (vectors).
091 *
092 * @param probability the recombination probability.
093 * @throws IllegalArgumentException if the {@code probability} is not in the
094 * valid range of {@code [0, 1]}
095 */
096 public IntermediateCrossover(final double probability) {
097 this(probability, 0);
098 }
099
100 /**
101 * Creates a new intermediate-crossover with default recombination
102 * probability ({@link #DEFAULT_ALTER_PROBABILITY}) and a <em>p</em> value
103 * of zero, which restricts the recombined chromosomes within the hypercube
104 * of the selected chromosomes (vectors).
105 */
106 public IntermediateCrossover() {
107 this(DEFAULT_ALTER_PROBABILITY, 0);
108 }
109
110 @Override
111 protected int crossover(final MSeq<G> v, final MSeq<G> w) {
112 final Random random = RandomRegistry.random();
113
114 final double min = v.get(0).min().doubleValue();
115 final double max = v.get(0).max().doubleValue();
116
117 for (int i = 0, n = min(v.length(), w.length()); i < n; ++i) {
118 final double vi = v.get(i).doubleValue();
119 final double wi = w.get(i).doubleValue();
120
121 double t, s;
122 do {
123 final double a = nextDouble(-_p, 1 + _p, random);
124 final double b = nextDouble(-_p, 1 + _p, random);
125
126 t = a*vi + (1 - a)*wi;
127 s = b*wi + (1 - b)*vi;
128 } while (t < min || s < min || t >= max || s >= max);
129
130 v.set(i, v.get(i).newInstance(t));
131 w.set(i, w.get(i).newInstance(s));
132 }
133
134 return 2;
135 }
136
137 @Override
138 public String toString() {
139 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
140 }
141
142 }
|