Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVector/3.3/Crossovers/UniformAllPositionsArithmeticCrossover.cs @ 2969

Last change on this file since 2969 was 2964, checked in by abeham, 15 years ago

Updated real vector operators #890:
Added AverageCrossover, UniformAllPositionsArithmeticCrossover, and UniformSomePositionsArithmeticCrossover
Removed ContinuousCrossover, CompleteContinuousCrossover, and UniformAllPositionsManipulator
Updated the description and documentation in HeuristicCrossover, LocalCrossover, and RandomConvexCrossover

File size: 5.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  /// The uniform all positions arithmetic crossover constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for every position x in the vector.
31  /// </summary>
32  /// <remarks>
33  /// By setting alpha = 0.5 it is the same as the <see cref="AverageCrossover"/>, but only on two parents.
34  /// It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.
35  /// </remarks>
36  [Item("UniformAllPositionsArithmeticCrossover", "The uniform all positions arithmetic crossover constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for every position x in the vector. Note that for alpha = 0.5 it is the same as the AverageCrossover. It is implemented as described in Michalewicz, Z. 1999. Genetic Algorithms + Data Structures = Evolution Programs. Third, Revised and Extended Edition, Spring-Verlag Berlin Heidelberg.")]
37  [EmptyStorableClass]
38  public class UniformAllPositionsArithmeticCrossover : RealVectorCrossover {
39    /// <summary>
40    /// The alpha parameter needs to be in the interval [0;1] and specifies how close the resulting offspring should be either to parent1 (alpha -> 0) or parent2 (alpha -> 1).
41    /// </summary>
42    public ValueLookupParameter<DoubleData> AlphaParameter {
43      get { return (ValueLookupParameter<DoubleData>)Parameters["Alpha"]; }
44    }
45
46    /// <summary>
47    /// Initializes a new instance with one parameter (<c>Alpha</c>).
48    /// </summary>
49    public UniformAllPositionsArithmeticCrossover()
50      : base() {
51      Parameters.Add(new ValueLookupParameter<DoubleData>("Alpha", "The alpha value in the range [0;1]", new DoubleData(0.33)));
52    }
53
54    /// <summary>
55    /// Performs the arithmetic crossover on all positions by calculating x = alpha * p1 + (1 - alpha) * p2.
56    /// </summary>
57    /// <exception cref="ArgumentException">Thrown when the parent vectors are of different length or alpha is outside the range [0;1].</exception>
58    /// <param name="random">The random number generator.</param>
59    /// <param name="parent1">The first parent vector.</param>
60    /// <param name="parent2">The second parent vector.</param>
61    /// <param name="alpha">The alpha parameter (<see cref="AlphaParameter"/>).</param>
62    /// <returns>The vector resulting from the crossover.</returns>
63    public static DoubleArrayData Apply(IRandom random, DoubleArrayData parent1, DoubleArrayData parent2, DoubleData alpha) {
64      int length = parent1.Length;
65      if (length != parent2.Length) throw new ArgumentException("UniformAllPositionsArithmeticCrossover: The parent vectors are of different length.", "parent1");
66      if (alpha.Value < 0 || alpha.Value > 1) throw new ArgumentException("UniformAllPositionsArithmeticCrossover: Parameter alpha must be in the range [0;1]", "alpha");
67      DoubleArrayData result = new DoubleArrayData(length);
68      for (int i = 0; i < length; i++) {
69        result[i] = alpha.Value * parent1[i] + (1 - alpha.Value) * parent2[i];
70      }
71      return result;
72    }
73
74    /// <summary>
75    /// Checks that there are exactly 2 parents, that the alpha parameter is not null and fowards the call to <see cref="Apply(IRandom, DoubleArrayData, DoubleArrrayData, DoubleData)"/>.
76    /// </summary>
77    /// <exception cref="ArgumentException">Thrown when there are not exactly two parents.</exception>
78    /// <exception cref="InvalidOperationException">Thrown when the alpha parmeter could not be found.</exception>
79    /// <param name="random">The random number generator.</param>
80    /// <param name="parents">The collection of parents (must be of size 2).</param>
81    /// <returns>The vector resulting from the crossover.</returns>
82    protected override DoubleArrayData Cross(IRandom random, ItemArray<DoubleArrayData> parents) {
83      if (parents.Length != 2) throw new ArgumentException("UniformAllPositionsArithmeticCrossover: There must be exactly two parents.", "parents");
84      if (AlphaParameter.ActualValue == null) throw new InvalidOperationException("UniformAllPositionsArithmeticCrossover: Parameter " + AlphaParameter.ActualName + " could not be found.");
85      return Apply(random, parents[0], parents[1], AlphaParameter.ActualValue);
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.