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