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