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