Free cookie consent management tool by TermsFeed Policy Generator

source: branches/IntegerVectorEncoding/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/SelfAdaptiveRoundedNormalAllPositionsManipulator.cs @ 7686

Last change on this file since 7686 was 7686, checked in by abeham, 12 years ago

#1775: Updated integer vector encoding

  • Added advanced bounds to all operators
  • Added new operators
  • Changed DiscreteCrossover to work with >= 2 parents
File size: 5.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Encodings.IntegerVectorEncoding {
32  /// <summary>
33  /// Manipulates each dimension in the integer vector with the mutation strength given
34  /// in the strategy parameter vector.
35  /// </summary>
36  [Item("SelfAdaptiveRoundedNormalAllPositionsManipulator", "This manipulation operator adds a value sigma_i * N(0,1) to the current value in each position i. The resulting value is rounded to the next feasible value. The values for sigma_i are looked up dynamically. If there are less elements in the strategy vector than positions, then the strategy vector is cycled.")]
37  [StorableClass]
38  public class SelfAdaptiveRoundedNormalAllPositionsManipulator : IntegerVectorManipulator, ISelfAdaptiveManipulator {
39    public Type StrategyParameterType {
40      get { return typeof(IIntegerVectorStdDevStrategyParameterOperator); }
41    }
42    /// <summary>
43    /// Parameter for the strategy vector.
44    /// </summary>
45    public ILookupParameter<DoubleArray> StrategyParameterParameter {
46      get { return (ILookupParameter<DoubleArray>)Parameters["StrategyParameter"]; }
47    }
48
49    IParameter ISelfAdaptiveManipulator.StrategyParameterParameter {
50      get { return StrategyParameterParameter; }
51    }
52
53    public IValueLookupParameter<IntMatrix> BoundsParameter {
54      get { return (IValueLookupParameter<IntMatrix>)Parameters["Bounds"]; }
55    }
56
57    [StorableConstructor]
58    protected SelfAdaptiveRoundedNormalAllPositionsManipulator(bool deserializing) : base(deserializing) { }
59    protected SelfAdaptiveRoundedNormalAllPositionsManipulator(SelfAdaptiveRoundedNormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
60    /// <summary>
61    /// Initializes a new instance of <see cref="SelfAdaptiveRoundedNormalAllPositionsManipulator"/> with one.
62    /// </summary>
63    public SelfAdaptiveRoundedNormalAllPositionsManipulator()
64      : base() {
65      Parameters.Add(new LookupParameter<DoubleArray>("StrategyParameter", "The vector containing the endogenous strategy parameters."));
66      Parameters.Add(new ValueLookupParameter<IntMatrix>("Bounds", "The bounds matrix can contain one row for each dimension with three columns specifying minimum (inclusive), maximum (exclusive), and step size. If less rows are given the matrix is cycled."));
67    }
68
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new SelfAdaptiveRoundedNormalAllPositionsManipulator(this, cloner);
71    }
72
73    /// <summary>
74    /// Performs an adaptive normally distributed all position manipulation on the given
75    /// <paramref name="vector"/> and rounding the results to the next feasible value.
76    /// </summary>
77    /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
78    /// as long as the vector to get manipulated.</exception>
79    /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param>
80    /// <param name="random">A random number generator.</param>
81    /// <param name="vector">The integer vector to manipulate.</param>
82    public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, DoubleArray strategyParameters) {
83      if (strategyParameters == null || strategyParameters.Length == 0) throw new ArgumentException("ERROR: Vector containing the standard deviations is not defined.", "sigma");
84      if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("RoundedNormalAllPositionsManipulator: Invalid bounds specified.", "bounds");
85      var N = new NormalDistributedRandom(random, 0.0, 1.0);
86      if (strategyParameters != null) {
87        for (int i = 0; i < vector.Length; i++) {
88          int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
89          if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
90
91          int value = (vector[i] + (int)Math.Round((N.NextDouble() * strategyParameters[i % strategyParameters.Length])) - min) / step;
92          vector[i] = value * step + min;
93        }
94      }
95    }
96
97    /// <summary>
98    /// Checks that the strategy vector is not null and forwards the call to the static Apply method.
99    /// </summary>
100    /// <param name="random">The random number generator.</param>
101    /// <param name="vector">The vector of integer values that is manipulated.</param>
102    protected override void Manipulate(IRandom random, IntegerVector vector) {
103      Apply(random, vector, BoundsParameter.ActualValue, StrategyParameterParameter.ActualValue);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.