Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/RoundedBlendAlphaBetaCrossover.cs @ 17218

Last change on this file since 17218 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 10.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HEAL.Attic;
29
30namespace HeuristicLab.Encodings.IntegerVectorEncoding {
31  /// <summary>
32  /// Blend alpha-beta crossover for integer vectors (BLX-a-b). Creates a new offspring by selecting a
33  /// random value from the interval between the two alleles of the parent solutions and rounds the
34  /// result to the nearest feasible value. The interval is increased in both directions as follows:
35  /// Into the direction of the 'better' solution by the factor alpha, into the direction of the
36  /// 'worse' solution by the factor beta.
37  /// </summary>
38  [Item("RoundedBlendAlphaBetaCrossover", "The rounded blend alpha beta crossover (BLX-a-b) for integer vectors is similar to the blend alpha crossover (BLX-a), but distinguishes between the better and worse of the parents. The interval from which to choose the new offspring can be extended beyond the better parent by specifying a higher alpha value, and beyond the worse parent by specifying a higher beta value. The new offspring is sampled uniformly in the extended range and rounded to the next feasible integer.")]
39  [StorableType("E9786DA1-C713-44EA-AD2A-BBE371712BA2")]
40  public class RoundedBlendAlphaBetaCrossover : BoundedIntegerVectorCrossover, ISingleObjectiveOperator {
41    /// <summary>
42    /// Whether the problem is a maximization or minimization problem.
43    /// </summary>
44    public ValueLookupParameter<BoolValue> MaximizationParameter {
45      get { return (ValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
46    }
47    /// <summary>
48    /// The quality of the parents.
49    /// </summary>
50    public ScopeTreeLookupParameter<DoubleValue> QualityParameter {
51      get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
52    }
53    /// <summary>
54    /// The Alpha parameter controls the extension of the range beyond the better parent. The value must be >= 0 and does not depend on Beta.
55    /// </summary>
56    public ValueLookupParameter<DoubleValue> AlphaParameter {
57      get { return (ValueLookupParameter<DoubleValue>)Parameters["Alpha"]; }
58    }
59    /// <summary>
60    /// The Beta parameter controls the extension of the range beyond the worse parent. The value must be >= 0 and does not depend on Alpha.
61    /// </summary>
62    public ValueLookupParameter<DoubleValue> BetaParameter {
63      get { return (ValueLookupParameter<DoubleValue>)Parameters["Beta"]; }
64    }
65
66    [StorableConstructor]
67    protected RoundedBlendAlphaBetaCrossover(StorableConstructorFlag _) : base(_) { }
68    protected RoundedBlendAlphaBetaCrossover(RoundedBlendAlphaBetaCrossover original, Cloner cloner) : base(original, cloner) { }
69    /// <summary>
70    /// Initializes a new instance of <see cref="RoundedBlendAlphaBetaCrossover"/> with four additional parameters
71    /// (<c>Maximization</c>, <c>Quality</c>, <c>Alpha</c> and <c>Beta</c>).
72    /// </summary>
73    public RoundedBlendAlphaBetaCrossover()
74      : base() {
75      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "Whether the problem is a maximization problem or not."));
76      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality values of the parents."));
77      Parameters.Add(new ValueLookupParameter<DoubleValue>("Alpha", "The Alpha parameter controls the extension of the range beyond the better parent. The value must be >= 0 and does not depend on Beta.", new DoubleValue(0.75)));
78      Parameters.Add(new ValueLookupParameter<DoubleValue>("Beta", "The Beta parameter controls the extension of the range beyond the worse parent. The value must be >= 0 and does not depend on Alpha.", new DoubleValue(0.25)));
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new RoundedBlendAlphaBetaCrossover(this, cloner);
83    }
84
85    /// <summary>
86    /// Performs the rounded blend alpha beta crossover (BLX-a-b) on two parent vectors.
87    /// </summary>
88    /// <exception cref="ArgumentException">
89    /// Thrown when either:<br/>
90    /// <list type="bullet">
91    /// <item><description>The length of <paramref name="betterParent"/> and <paramref name="worseParent"/> is not equal.</description></item>
92    /// <item><description>The parameter <paramref name="alpha"/> is smaller than 0.</description></item>
93    /// <item><description>The parameter <paramref name="beta"/> is smaller than 0.</description></item>
94    /// </list>
95    /// </exception>
96    /// <param name="random">The random number generator to use.</param>
97    /// <param name="betterParent">The better of the two parents with regard to their fitness.</param>
98    /// <param name="worseParent">The worse of the two parents with regard to their fitness.</param>
99    /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
100    /// <param name="alpha">The parameter alpha.</param>
101    /// <param name="beta">The parameter beta.</param>
102    /// <returns>The integer vector that results from the crossover.</returns>
103    public static IntegerVector Apply(IRandom random, IntegerVector betterParent, IntegerVector worseParent, IntMatrix bounds, DoubleValue alpha, DoubleValue beta) {
104      if (betterParent.Length != worseParent.Length) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: The parents' vectors are of different length.", "betterParent");
105      if (alpha.Value < 0) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Parameter alpha must be greater or equal to 0.", "alpha");
106      if (beta.Value < 0) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Parameter beta must be greater or equal to 0.", "beta");
107      if (bounds == null || bounds.Rows < 1 || bounds.Columns < 2) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Invalid bounds specified.", "bounds");
108
109      int length = betterParent.Length;
110      double min, max, d;
111      var result = new IntegerVector(length);
112      int minBound, maxBound, step = 1;
113      for (int i = 0; i < length; i++) {
114        minBound = bounds[i % bounds.Rows, 0];
115        maxBound = bounds[i % bounds.Rows, 1];
116        if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
117        maxBound = FloorFeasible(minBound, maxBound, step, maxBound - 1);
118
119        d = Math.Abs(betterParent[i] - worseParent[i]);
120        if (betterParent[i] <= worseParent[i]) {
121          min = FloorFeasible(minBound, maxBound, step, betterParent[i] - d * alpha.Value);
122          max = CeilingFeasible(minBound, maxBound, step, worseParent[i] + d * beta.Value);
123        } else {
124          min = FloorFeasible(minBound, maxBound, step, worseParent[i] - d * beta.Value);
125          max = CeilingFeasible(minBound, maxBound, step, betterParent[i] + d * alpha.Value);
126        }
127        result[i] = RoundFeasible(minBound, maxBound, step, min + random.NextDouble() * (max - min));
128      }
129      return result;
130    }
131
132    /// <summary>
133    /// Checks if the number of parents is equal to 2, if all parameters are available and forwards the call to <see cref="Apply(IRandom, IntegerVector, IntegerVector, IntMatrix, DoubleValue, DoubleValue)"/>.
134    /// </summary>
135    /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
136    /// <exception cref="InvalidOperationException">
137    /// Thrown when either:<br/>
138    /// <list type="bullet">
139    /// <item><description>Maximization parameter could not be found.</description></item>
140    /// <item><description>Quality parameter could not be found or the number of quality values is not equal to the number of parents.</description></item>
141    /// <item><description>Alpha parameter could not be found.</description></item>
142    /// <item><description>Beta parameter could not be found.</description></item>
143    /// </list>
144    /// </exception>
145    /// <param name="random">The random number generator to use.</param>
146    /// <param name="parents">The collection of parents (must be of size 2).</param>
147    /// <param name="bounds">The bounds and step size for each dimension (will be cycled in case there are less rows than elements in the parent vectors).</param>
148    /// <returns>The integer vector that results from the crossover.</returns>
149    protected override IntegerVector CrossBounded(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
150      if (parents.Length != 2) throw new ArgumentException("RoundedBlendAlphaBetaCrossover: Number of parents is not equal to 2.", "parents");
151      if (MaximizationParameter.ActualValue == null) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + MaximizationParameter.ActualName + " could not be found.");
152      if (QualityParameter.ActualValue == null || QualityParameter.ActualValue.Length != parents.Length) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + QualityParameter.ActualName + " could not be found, or not in the same quantity as there are parents.");
153      if (AlphaParameter.ActualValue == null || BetaParameter.ActualValue == null) throw new InvalidOperationException("RoundedBlendAlphaBetaCrossover: Parameter " + AlphaParameter.ActualName + " or paramter " + BetaParameter.ActualName + " could not be found.");
154
155      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
156      bool maximization = MaximizationParameter.ActualValue.Value;
157      if (maximization && qualities[0].Value >= qualities[1].Value || !maximization && qualities[0].Value <= qualities[1].Value)
158        return Apply(random, parents[0], parents[1], bounds, AlphaParameter.ActualValue, BetaParameter.ActualValue);
159      else {
160        return Apply(random, parents[1], parents[0], bounds, AlphaParameter.ActualValue, BetaParameter.ActualValue);
161      }
162    }
163  }
164}
Note: See TracBrowser for help on using the repository browser.