Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/BlendAlphaCrossover.cs @ 2928

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

updated documentation of some of the operators #890

File size: 6.3 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  /// Blend alpha crossover for real vectors (BLX-a). Creates a new offspring by selecting a random value
31  /// from the interval between the two alleles of the parent solutions. The interval is increased
32  /// in both directions by the factor alpha.
33  /// </summary>
34  /// <remarks>
35  /// 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/>
36  /// The default value for alpha is 0.5.
37  /// </remarks>
38  [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.")]
39  [EmptyStorableClass]
40  public class BlendAlphaCrossover : RealVectorCrossover {
41    /// <summary>
42    /// The alpha parameter specifies how much the interval between the parents should be extended to the left and right.
43    /// The value of this parameter also determines the name of the operator: BLX-0.0 for example means alpha is set to 0.
44    /// 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
45    /// values left and right of the min and max value for each gene.
46    /// </summary>
47    public ValueLookupParameter<DoubleData> AlphaParameter {
48      get { return (ValueLookupParameter<DoubleData>)Parameters["Alpha"]; }
49    }
50    /// <summary>
51    /// Initializes a new instance of <see cref="BlendAlphaCrossover"/> with one parameter (<c>Alpha</c>).
52    /// </summary>
53    public BlendAlphaCrossover()
54      : base() {
55      Parameters.Add(new ValueLookupParameter<DoubleData>("Alpha", "Value for alpha", new DoubleData(0.5)));
56    }
57
58    /// <summary>
59    /// Performs the blend alpha crossover (BLX-a) of two real vectors.<br/>
60    /// It creates new offspring by sampling a new value in the range [min_i - d * alpha, max_i + d * alpha) at each position i.
61    /// 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.
62    /// </summary>
63    /// <exception cref="ArgumentException">
64    /// Thrown when <paramref name="parent1"/> and <paramref name="parent2"/> are of different length or<br/>
65    /// when <paramref name="alpha"/> is less than 0.
66    /// </exception>
67    /// <param name="random">The random number generator.</param>
68    /// <param name="parent1">The first parent for the crossover operation.</param>
69    /// <param name="parent2">The second parent for the crossover operation.</param>
70    /// <param name="alpha">The alpha value for the crossover.</param>
71    /// <returns>The newly created real vector resulting from the crossover operation.</returns>
72    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2, DoubleData alpha) {
73      if (parent1.Length != parent2.Length) throw new ArgumentException("BlendAlphaCrossover: The parents' vectors are of different length.", "parent1");
74      if (alpha.Value < 0) throw new ArgumentException("BlendAlphaCrossover: Paramter alpha must be greater or equal than 0.", "alpha");
75      int length = parent1.Length;
76      DoubleArrayData result = new DoubleArrayData(length);
77      double max = 0, min = 0, d = 0, resMin = 0, resMax = 0;
78
79      for (int i = 0; i < length; i++) {
80        max = Math.Max(parent1[i], parent2[i]);
81        min = Math.Min(parent1[i], parent2[i]);
82        d = Math.Abs(max - min);
83        resMin = min - d * alpha.Value;
84        resMax = max + d * alpha.Value;
85
86        result[i] = resMin + random.NextDouble() * Math.Abs(resMax - resMin);
87      }
88      return result;
89    }
90
91    /// <summary>
92    /// Checks that the number of parents is equal to 2 and forwards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrayData, DoubleData)"/>.
93    /// </summary>
94    /// <exception cref="ArgumentException">Thrown when the number of parents is not equal to 2.</exception>
95    /// <exception cref="InvalidOperationException">Thrown when the parameter alpha could not be found.</exception>
96    /// <param name="random">The random number generator to use.</param>
97    /// <param name="parents">The collection of parents (must be of size 2).</param>
98    /// <returns>The real vector resulting from the crossover operation.</returns>
99    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
100      if (parents.Length != 2) throw new ArgumentException("BlendAlphaCrossover: The number of parents is not equal to 2", "parents");
101      if (AlphaParameter.ActualValue == null) throw new InvalidOperationException("BlendAlphaCrossover: Parameter " + AlphaParameter.ActualName + " could not be found.");
102      return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue);
103    }
104  }
105}
Note: See TracBrowser for help on using the repository browser.