SimulatedBinaryCrossover.java
001 /*
002  * Java Genetic Algorithm Library (jenetics-5.1.0).
003  * Copyright (c) 2007-2019 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.ext;
021 
022 import static java.lang.Math.abs;
023 import static java.lang.Math.pow;
024 import static java.lang.String.format;
025 import static io.jenetics.internal.math.base.clamp;
026 
027 import java.util.Random;
028 
029 import io.jenetics.Crossover;
030 import io.jenetics.NumericGene;
031 import io.jenetics.internal.math.random;
032 import io.jenetics.internal.util.require;
033 import io.jenetics.util.MSeq;
034 import io.jenetics.util.RandomRegistry;
035 
036 /**
037  * Performs the simulated binary crossover (SBX) on a {@code Chromosome} of
038  {@link NumericGene}s such that each position is either crossed contracted or
039  * expanded with a certain probability. The probability distribution is designed
040  * such that the children will lie closer to their parents as is the case with
041  * the single point binary crossover.
042  <p>
043  * It is implemented as described in Deb, K. and Agrawal, R. B. 1995. Simulated
044  * binary crossover for continuous search space. Complex Systems, 9, pp. 115-148.
045  *
046  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
047  @since 3.5
048  @version 5.0
049  */
050 public class SimulatedBinaryCrossover<
051     extends NumericGene<?, G>,
052     extends Comparable<? super C>
053 >
054     extends Crossover<G, C>
055 {
056     private final double _contiguity;
057 
058     /**
059      * Create a new <i>simulated binary crossover</i> alterer with the given
060      * parameters.
061      *
062      @param probability the recombination probability
063      @param contiguity the contiguity value that specifies how close a child
064      *       should be to its parents (larger value means closer). The value
065      *       must be greater or equal than 0. Typical values are in the range
066      *       [2..5].
067      @throws IllegalArgumentException if the {@code probability} is not in the
068      *          valid range of {@code [0, 1]}
069      @throws IllegalArgumentException if {@code contiguity} is smaller than
070      *         zero
071      */
072     public SimulatedBinaryCrossover(
073         final double probability,
074         final double contiguity
075     ) {
076         super(probability);
077         _contiguity = require.nonNegative(contiguity);
078     }
079 
080     /**
081      * Create a new <i>simulated binary crossover</i> alterer with the given
082      * parameters. The <i>contiguity</i> value is set to {@code 2.5}.
083      *
084      @param probability the recombination probability
085      @throws IllegalArgumentException if the {@code probability} is not in the
086      *          valid range of {@code [0, 1]}
087      @throws IllegalArgumentException if {@code contiguity} is smaller than
088      *         zero
089      */
090     public SimulatedBinaryCrossover(final double probability) {
091         this(probability, 2.5);
092     }
093 
094     /**
095      * Return the <i>contiguity</i> value of the crossover.
096      *
097      @return the <i>contiguity</i> value of the crossover
098      */
099     public double getContiguity() {
100         return _contiguity;
101     }
102 
103     @Override
104     protected int crossover(final MSeq<G> that, final MSeq<G> other) {
105         return (int)random.indexes(RandomRegistry.getRandom(), that.length()0.5)
106             .peek(i -> crossover(that, other, i))
107             .count();
108     }
109 
110     private void crossover(final MSeq<G> that, final MSeq<G> other, final int i) {
111         final Random random = RandomRegistry.getRandom();
112 
113         final double u = random.nextDouble();
114         final double beta;
115         if (u < 0.5) {
116             // If u is smaller than 0.5 perform a contracting crossover.
117             beta = pow(2*u, 1.0/(_contiguity + 1));
118         else if (u > 0.5) {
119             // Otherwise perform an expanding crossover.
120             beta = pow(0.5 (1.0 - u)1.0/(_contiguity + 1));
121         else if (u == 0.5) {
122             beta = 1;
123         else {
124             beta = 0;
125         }
126 
127         final double v1 = that.get(i).doubleValue();
128         final double v2 = other.get(i).doubleValue();
129         final double v = random.nextBoolean()
130             ((v1 - v2)*0.5- beta*0.5*abs(v1 - v2)
131             ((v1 - v2)*0.5+ beta*0.5*abs(v1 - v2);
132 
133         final double min = that.get(i).getMin().doubleValue();
134         final double max = that.get(i).getMax().doubleValue();
135         that.set(i, that.get(i).newInstance(clamp(v, min, max)));
136     }
137 
138     @Override
139     public String toString() {
140         return format(
141             "SimulatedBinaryCrossover[p=%f, c=%f]",
142             _probability, _contiguity
143         );
144     }
145 
146 }