IntermediateCrossover.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 a new points are
042  * generated, until they lie in the valid range. The strategy on how
043  * out-of-range points are handled, is the difference to the very similar
044  {@link LineCrossover}.
045  *
046  @see <a href="https://cs.gmu.edu/~sean/book/metaheuristics/"><em>
047  *       Essentials of Metaheuristic, page 42</em></a>
048  @see LineCrossover
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 IntermediateCrossover<
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 intermediate-crossover with the given recombination
065      * probability and the line-scaling factor <em>p</em>.
066      <p>
067      <b>When the value for <em>p</em> is greater then 0, the crossover point
068      * generation must be repeated until the points lie within the allowed
069      * range. Values greater then 10 are usually not recommended, since this
070      * leads to unnecessary crossover point generation.</b>
071      *
072      @param probability the recombination probability.
073      @param p defines the possible location of the recombined chromosomes. If
074      *        <em>p</em> = 0 then the children will be located along the line
075      *        within the hypercube between the two points. If <em>p</em> &gt; 0
076      *        then the children may be located anywhere on the line, even
077      *        somewhat outside of the hypercube.
078      @throws IllegalArgumentException if the {@code probability} is not in the
079      *         valid range of {@code [0, 1]} or if {@code p} is smaller then zero
080      */
081     public IntermediateCrossover(final double probability, final double p) {
082         super(probability);
083         _p = Requires.nonNegative(p, "p");
084     }
085 
086     /**
087      * Creates a new intermediate-crossover with the given recombination
088      * probability. The parameter <em>p</em> is set to zero, which restricts the
089      * recombined chromosomes within the hypercube of the selected chromosomes
090      * (vectors).
091      *
092      @param probability the recombination probability.
093      @throws IllegalArgumentException if the {@code probability} is not in the
094      *         valid range of {@code [0, 1]}
095      */
096     public IntermediateCrossover(final double probability) {
097         this(probability, 0);
098     }
099 
100     /**
101      * Creates a new intermediate-crossover with default recombination
102      * probability ({@link #DEFAULT_ALTER_PROBABILITY}) and a <em>p</em> value
103      * of zero, which restricts the recombined chromosomes within the hypercube
104      * of the selected chromosomes (vectors).
105      */
106     public IntermediateCrossover() {
107         this(DEFAULT_ALTER_PROBABILITY, 0);
108     }
109 
110     @Override
111     protected int crossover(final MSeq<G> v, final MSeq<G> w) {
112         final Random random = RandomRegistry.random();
113 
114         final double min = v.get(0).min().doubleValue();
115         final double max = v.get(0).max().doubleValue();
116 
117         for (int i = 0, n = min(v.length(), w.length()); i < n; ++i) {
118             final var g1 = v.get(i);
119             final var g2 = w.get(i);
120 
121             if (g1.isValid() && g2.isValid()) {
122                 final double vi = g1.doubleValue();
123                 final double wi = g2.doubleValue();
124 
125                 double t, s;
126                 do {
127                     final double a = nextDouble(-_p, + _p, random);
128                     final double b = nextDouble(-_p, + _p, random);
129 
130                     t = a*vi + (- a)*wi;
131                     s = b*wi + (- b)*vi;
132                 while (t < min || s < min || t >= max || s >= max);
133 
134                 v.set(i, v.get(i).newInstance(t));
135                 w.set(i, w.get(i).newInstance(s));
136             }
137         }
138 
139         return 2;
140     }
141 
142     @Override
143     public String toString() {
144         return format("%s[p=%f]", getClass().getSimpleName(), _probability);
145     }
146 
147 }