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