Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/SimulatedBinaryCrossover.cs @ 2918

Last change on this file since 2918 was 2918, checked in by abeham, 14 years ago

Added a new crossover for real vectors: SimulatedBinaryCrossover (SBX) #890

File size: 5.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Encodings.RealVector {
29  /// <summary>
30  /// Performs the simulated binary crossover (SBX) on a vector of real values such that each position is either crossed contracted or expanded with a certain probability.
31  /// The probability distribution is designed such that the children will lie closer to their parents as is the case with the single point binary crossover.
32  /// </summary>
33  /// <remarks>
34  /// It is implemented as described in Deb, K. and Agrawal, R. B. 1995. Simulated binary crossover for continuous search space. Complex Systems, 9, pp. 115-148.
35  /// </remarks>
36  [Item("SimulatedBinaryCrossover", "The simulated binary crossover (SBX) is implemented as described in Deb, K. and Agrawal, R. B. 1995. Simulated binary crossover for continuous search space. Complex Systems, 9, pp. 115-148.")]
37  [EmptyStorableClass]
38  public class SimulatedBinaryCrossover : RealVectorCrossover {
39    /// <summary>
40    /// The parameter must be greater or equal than 0. Common values are in the range [0;5] and more often just [2;5].
41    /// </summary>
42    public ValueLookupParameter<DoubleData> ContiguityParameter {
43      get { return (ValueLookupParameter<DoubleData>)Parameters["Contiguity"]; }
44    }
45
46    public SimulatedBinaryCrossover()
47      : base() {
48      Parameters.Add(new ValueLookupParameter<DoubleData>("Contiguity", "Specifies whether the crossover should produce very different (small value) or very similar (large value) children. Valid values must be greater or equal to 0.", new DoubleData(2)));
49    }
50
51    /// <summary>
52    /// Performs the simulated binary crossover on a real vector. Each position is crossed with a probability of 50% and if crossed either a contracting crossover or an expanding crossover is performed, again with equal probability.
53    /// For more details refer to the paper by Deb and Agrawal.
54    /// </summary>
55    /// <exception cref="ArgumentException">Thrown when the parents' vectors are of unequal length or when <paramref name="contiguity"/> is smaller than 0.</exception>
56    /// <param name="random">The random number generator to use.</param>
57    /// <param name="parent1">The first parent vector.</param>
58    /// <param name="parent2">The second parent vector.</param>
59    /// <param name="contiguity">The contiguity value that specifies how close a child should be to its parents (larger value means closer). The value must be greater or equal than 0. Typical values are in the range [2;5].</param>
60    /// <returns>The vector resulting from the crossover.</returns>
61    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2, DoubleData contiguity) {
62      if (parent1.Length != parent2.Length) throw new ArgumentException("SinglePointCrossover: Parents are of unequal length");
63      if (contiguity.Value < 0) throw new ArgumentException("SimulatedBinaryCrossover: Contiguity value is smaller than 0", "contiguity");
64      int length = parent1.Length;
65      DoubleArrayData result = new DoubleArrayData(length);
66      for (int i = 0; i < length; i++) {
67        if (random.NextDouble() < 0.5) { // cross this variable
68          double u = random.NextDouble();
69          double beta = 0;
70          if (u < 0.5) { // if u is smaller than 0.5 perform a contracting crossover
71            beta = Math.Pow(2 * u, 1.0 / (contiguity.Value + 1));
72          } else { // otherwise perform an expanding crossover
73            beta = Math.Pow(1.0 / (2 - 2 * u), (contiguity.Value + 1));
74          }
75          if (random.NextDouble() < 0.5)
76            result[i] = ((parent1[i] + parent2[i]) / 2.0) - beta * 0.5 * Math.Abs(parent1[i] - parent2[i]);
77          else
78            result[i] = ((parent1[i] + parent2[i]) / 2.0) + beta * 0.5 * Math.Abs(parent1[i] - parent2[i]);
79        }
80      }
81      return result;
82    }
83
84    /// <summary>
85    /// Checks number of parents, availability of the parameters and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData, DoubleData)"/>.
86    /// </summary>
87    /// <exception cref="InvalidOperationException">Thrown when there are not exactly 2 parents or when the contiguity parameter could not be found.</exception>
88    /// <param name="random">The random number generator.</param>
89    /// <param name="parents">The collection of parents (must be of size 2).</param>
90    /// <returns>The real vector resulting from the crossover.</returns>
91    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
92      if (parents.Length != 2) throw new InvalidOperationException("SinglePointCrossover: The number of parents is not equal to 2");
93      if (ContiguityParameter.ActualValue == null) throw new InvalidOperationException("SimulatedBinaryCrossover: Parameter " + ContiguityParameter.ActualName + " could not be found.");
94      return Apply(random, parents[0], parents[1], ContiguityParameter.ActualValue);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.