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