Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Crossovers/RoundedUniformArithmeticCrossover.cs @ 16451

Last change on this file since 16451 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.IntegerVectorEncoding {
30  /// <summary>
31  /// The rounded uniform arithmetic crossover (continuous recombination) constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for a position x in the vector with a given probability (otherwise p1 is taken at this position).
32  /// </summary>
33  [Item("RoundedUniformSomePositionsArithmeticCrossover", "The uniform some positions arithmetic crossover (continuous recombination) constructs an offspring by calculating x = alpha * p1 + (1-alpha) * p2 for a position x in the vector with a given probability (otherwise p1 is taken at this position).")]
34  [StorableClass]
35  public class RoundedUniformArithmeticCrossover : BoundedIntegerVectorCrossover, IBoundedIntegerVectorOperator {
36
37    /// <summary>
38    /// 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).
39    /// </summary>
40    public ValueLookupParameter<DoubleValue> AlphaParameter {
41      get { return (ValueLookupParameter<DoubleValue>)Parameters["Alpha"]; }
42    }
43    /// <summary>
44    /// The probability in the range (0;1] for each position in the vector to be crossed.
45    /// </summary>
46    public ValueLookupParameter<DoubleValue> ProbabilityParameter {
47      get { return (ValueLookupParameter<DoubleValue>)Parameters["Probability"]; }
48    }
49
50    [StorableConstructor]
51    protected RoundedUniformArithmeticCrossover(bool deserializing) : base(deserializing) { }
52    protected RoundedUniformArithmeticCrossover(RoundedUniformArithmeticCrossover original, Cloner cloner) : base(original, cloner) { }
53    /// <summary>
54    /// Initializes a new instance with two parameters (<c>Alpha</c> and <c>Probability</c>).
55    /// </summary>
56    public RoundedUniformArithmeticCrossover()
57      : base() {
58      Parameters.Add(new ValueLookupParameter<DoubleValue>("Alpha", "The alpha value in the range (0;1) that defines whether the point should be close to parent1 (->1) or parent2 (->0)", new DoubleValue(0.5)));
59      Parameters.Add(new ValueLookupParameter<DoubleValue>("Probability", "The probability for crossing a position in the range (0;1]", new DoubleValue(1)));
60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new RoundedUniformArithmeticCrossover(this, cloner);
64    }
65
66    /// <summary>
67    /// Performs the arithmetic crossover on some positions by taking either x = alpha * p1 + (1 - alpha) * p2 or x = p1 depending on the probability for a gene to be crossed.
68    /// </summary>
69    /// <param name="random">The random number generator.</param>
70    /// <param name="parent1">The first parent vector.</param>
71    /// <param name="parent2">The second parent vector.</param>
72    /// <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>
73    /// <param name="alpha">The alpha parameter (<see cref="AlphaParameter"/>).</param>
74    /// <param name="probability">The probability parameter (<see cref="ProbabilityParameter"/>).</param>
75    /// <returns>The vector resulting from the crossover.</returns>
76    public static IntegerVector Apply(IRandom random, IntegerVector parent1, IntegerVector parent2, IntMatrix bounds, DoubleValue alpha, DoubleValue probability) {
77      int length = parent1.Length;
78      if (length != parent2.Length) throw new ArgumentException("RoundedUniformArithmeticCrossover: The parent vectors are of different length.", "parent1");
79      if (alpha.Value < 0 || alpha.Value > 1) throw new ArgumentException("RoundedUniformArithmeticCrossover: Parameter alpha must be in the range [0;1]", "alpha");
80      if (probability.Value < 0 || probability.Value > 1) throw new ArgumentException("RoundedUniformArithmeticCrossover: Parameter probability must be in the range [0;1]", "probability");
81
82      var result = new IntegerVector(length);
83      for (int i = 0; i < length; i++) {
84        if (random.NextDouble() < probability.Value) {
85          int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
86          if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
87          max = FloorFeasible(min, max, step, max - 1);
88          double value = alpha.Value * parent1[i] + (1 - alpha.Value) * parent2[i];
89          result[i] = RoundFeasible(min, max, step, value);
90        } else result[i] = parent1[i];
91      }
92      return result;
93    }
94
95    /// <summary>
96    /// Checks that there are exactly 2 parents, that the alpha and the probability parameter are not null and fowards the call to the static Apply method.
97    /// </summary>
98    /// <exception cref="ArgumentException">Thrown when there are not exactly two parents.</exception>
99    /// <exception cref="InvalidOperationException">Thrown when either the alpha parmeter or the probability parameter could not be found.</exception>
100    /// <param name="random">The random number generator.</param>
101    /// <param name="parents">The collection of parents (must be of size 2).</param>
102    /// /// <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>
103    /// <returns>The vector resulting from the crossover.</returns>
104    protected override IntegerVector CrossBounded(IRandom random, ItemArray<IntegerVector> parents, IntMatrix bounds) {
105      if (parents.Length != 2) throw new ArgumentException("RoundedUniformArithmeticCrossover: There must be exactly two parents.", "parents");
106      if (AlphaParameter.ActualValue == null) throw new InvalidOperationException("RoundedUniformArithmeticCrossover: Parameter " + AlphaParameter.ActualName + " could not be found.");
107      if (ProbabilityParameter.ActualValue == null) throw new InvalidOperationException("RoundedUniformArithmeticCrossover: Parameter " + ProbabilityParameter.ActualName + " could not be found.");
108      return Apply(random, parents[0], parents[1], bounds, AlphaParameter.ActualValue, ProbabilityParameter.ActualValue);
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.