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 the original
044 * value are used instead. The strategy on how out-of-range points are handled,
045 * is the difference to the very similar {@link IntermediateCrossover}.
046 *
047 * @see <a href="https://cs.gmu.edu/~sean/book/metaheuristics/"><em>
048 * Essentials of Metaheuristic, page 42</em></a>
049 * @see IntermediateCrossover
050 *
051 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
052 * @version 3.8
053 * @since 3.8
054 */
055 public class LineCrossover<
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 linear-crossover with the given recombination
066 * probability and the line-scaling factor <em>p</em>.
067 *
068 * @param probability the recombination probability.
069 * @param p defines the possible location of the recombined chromosomes. If
070 * <em>p</em> = 0 then the children will be located along the line
071 * within the hypercube between the two points. If <em>p</em> > 0
072 * then the children may be located anywhere on the line, even
073 * somewhat outside of the hypercube.
074 * @throws IllegalArgumentException if the {@code probability} is not in the
075 * valid range of {@code [0, 1]} or if {@code p} is smaller then zero
076 */
077 public LineCrossover(final double probability, final double p) {
078 super(probability);
079 _p = require.nonNegative(p, "p");
080 }
081
082 /**
083 * Creates a new linear-crossover with the given recombination
084 * probability. The parameter <em>p</em> is set to zero, which restricts the
085 * recombined chromosomes within the hypercube of the selected chromosomes
086 * (vectors).
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 LineCrossover(final double probability) {
093 this(probability, 0);
094 }
095
096 /**
097 * Creates a new linear-crossover with default recombination
098 * probability ({@link #DEFAULT_ALTER_PROBABILITY}) and a <em>p</em> value
099 * of zero, which restricts the recombined chromosomes within the hypercube
100 * of the selected chromosomes (vectors).
101 */
102 public LineCrossover() {
103 this(DEFAULT_ALTER_PROBABILITY, 0);
104 }
105
106 @Override
107 protected int crossover(final MSeq<G> v, final MSeq<G> w) {
108 final Random random = RandomRegistry.getRandom();
109
110 final double min = v.get(0).getMin().doubleValue();
111 final double max = v.get(0).getMax().doubleValue();
112
113 final double a = nextDouble(random, -_p, 1 + _p);
114 final double b = nextDouble(random, -_p, 1 + _p);
115
116 boolean changed = false;
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 final double t = a*vi + (1 - a)*wi;
122 final double s = b*wi + (1 - b)*vi;
123
124 if (t >= min && s >= min && t < max && s < max) {
125 v.set(i, v.get(i).newInstance(t));
126 w.set(i, w.get(i).newInstance(s));
127 changed = true;
128 }
129 }
130
131 return changed ? 2 : 0;
132 }
133
134 @Override
135 public int hashCode() {
136 return Hash.of(getClass()).and(super.hashCode()).value();
137 }
138
139 @Override
140 public boolean equals(final Object obj) {
141 return obj instanceof LineCrossover && super.equals(obj);
142 }
143
144 @Override
145 public String toString() {
146 return format("%s[p=%f]", getClass().getSimpleName(), _probability);
147 }
148
149 }
|