Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Encodings.RealVectorEncoding/3.3/Crossovers/BlendAlphaCrossover.cs @ 17246

Last change on this file since 17246 was 17246, checked in by gkronber, 5 years ago

#2925: merged r17037:17242 from trunk to branch

File size: 6.7 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.Parameters;
27using HEAL.Attic;
28
29namespace HeuristicLab.Encodings.RealVectorEncoding {
30  /// <summary>
31  /// Blend alpha crossover for real vectors (BLX-a). Creates a new offspring by selecting a random value
32  /// from the interval between the two alleles of the parent solutions. The interval is increased
33  /// in both directions by the factor alpha.
34  /// </summary>
35  /// <remarks>
36  /// It is implemented as described in Takahashi, M. and Kita, H. 2001. A crossover operator using independent component analysis for real-coded genetic algorithms Proceedings of the 2001 Congress on Evolutionary Computation, pp. 643-649.<br/>
37  /// The default value for alpha is 0.5.
38  /// </remarks>
39  [Item("BlendAlphaCrossover", "The blend alpha crossover (BLX-a) for real vectors creates new offspring by sampling a new value in the range [min_i - d * alpha, max_i + d * alpha) at each position i. Here min_i and max_i are the smaller and larger value of the two parents at position i and d is max_i - min_i. It is implemented as described in Takahashi, M. and Kita, H. 2001. A crossover operator using independent component analysis for real-coded genetic algorithms Proceedings of the 2001 Congress on Evolutionary Computation, pp. 643-649.")]
40  [StorableType("A56EA6B8-79A2-4762-B651-BEBAB95F8E8E")]
41  public class BlendAlphaCrossover : RealVectorCrossover {
42    /// <summary>
43    /// The alpha parameter specifies how much the interval between the parents should be extended to the left and right.
44    /// The value of this parameter also determines the name of the operator: BLX-0.0 for example means alpha is set to 0.
45    /// When Alpha is 0, then the offspring will only be chosen in between the parents, the bigger alpha is the more it will be possible to choose
46    /// values left and right of the min and max value for each gene.
47    /// </summary>
48    public ValueLookupParameter<DoubleValue> AlphaParameter {
49      get { return (ValueLookupParameter<DoubleValue>)Parameters["Alpha"]; }
50    }
51
52    [StorableConstructor]
53    protected BlendAlphaCrossover(StorableConstructorFlag _) : base(_) { }
54    protected BlendAlphaCrossover(BlendAlphaCrossover original, Cloner cloner) : base(original, cloner) { }
55    /// <summary>
56    /// Initializes a new instance of <see cref="BlendAlphaCrossover"/> with one parameter (<c>Alpha</c>).
57    /// </summary>
58    public BlendAlphaCrossover()
59      : base() {
60      Parameters.Add(new ValueLookupParameter<DoubleValue>("Alpha", "The Alpha parameter controls the extension of the range beyond the two parents. It must be >= 0. A value of 0.5 means that half the range is added to both sides of the intervals.", new DoubleValue(0.5)));
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new BlendAlphaCrossover(this, cloner);
65    }
66
67    /// <summary>
68    /// Performs the blend alpha crossover (BLX-a) of two real vectors.<br/>
69    /// It creates new offspring by sampling a new value in the range [min_i - d * alpha, max_i + d * alpha) at each position i.
70    /// Here min_i and max_i are the smaller and larger value of the two parents at position i and d is max_i - min_i.
71    /// </summary>
72    /// <exception cref="ArgumentException">
73    /// Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are of different length or<br/>
74    /// when <paramref name="alpha"/> is less than 0.
75    /// </exception>
76    /// <param name="random">The random number generator.</param>
77    /// <param name="parent1">The first parent for the crossover operation.</param>
78    /// <param name="parent2">The second parent for the crossover operation.</param>
79    /// <param name="alpha">The alpha value for the crossover.</param>
80    /// <returns>The newly created real vector resulting from the crossover operation.</returns>
81    public static RealVector Apply(IRandom random, RealVector parent1, RealVector parent2, DoubleValue alpha) {
82      if (parent1.Length != parent2.Length) throw new ArgumentException("BlendAlphaCrossover: The parents' vectors are of different length.", "parent1");
83      if (alpha.Value < 0) throw new ArgumentException("BlendAlphaCrossover: Paramter alpha must be greater or equal than 0.", "alpha");
84      int length = parent1.Length;
85      RealVector result = new RealVector(length);
86      double max = 0, min = 0, d = 0, resMin = 0, resMax = 0;
87
88      for (int i = 0; i < length; i++) {
89        max = Math.Max(parent1[i], parent2[i]);
90        min = Math.Min(parent1[i], parent2[i]);
91        d = Math.Abs(max - min);
92        resMin = min - d * alpha.Value;
93        resMax = max + d * alpha.Value;
94
95        result[i] = resMin + random.NextDouble() * Math.Abs(resMax - resMin);
96      }
97      return result;
98    }
99
100    /// <summary>
101    /// Checks that the number of parents is equal to 2 and forwards the call to <see cref="Apply(IRandom, RealVector, RealVector, DoubleValue)"/>.
102    /// </summary>
103    /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
104    /// <exception cref="InvalidOperationException">Thrown when the parameter alpha could not be found.</exception>
105    /// <param name="random">The random number generator to use.</param>
106    /// <param name="parents">The collection of parents (must be of size 2).</param>
107    /// <returns>The real vector resulting from the crossover operation.</returns>
108    protected override RealVector Cross(IRandom random, ItemArray<RealVector> parents) {
109      if (parents.Length != 2) throw new ArgumentException("BlendAlphaCrossover: The number of parents is not equal to 2", "parents");
110      if (AlphaParameter.ActualValue == null) throw new InvalidOperationException("BlendAlphaCrossover: Parameter " + AlphaParameter.ActualName + " could not be found.");
111      return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue);
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.