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