Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.IntegerVectorEncoding/3.3/Manipulators/SelfAdaptiveRoundedNormalAllPositionsManipulator.cs @ 15584

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

#2640: Updated year of copyrights in license headers on stable

File size: 5.6 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.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 : BoundedIntegerVectorManipulator, 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    [StorableConstructor]
54    protected SelfAdaptiveRoundedNormalAllPositionsManipulator(bool deserializing) : base(deserializing) { }
55    protected SelfAdaptiveRoundedNormalAllPositionsManipulator(SelfAdaptiveRoundedNormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
56    /// <summary>
57    /// Initializes a new instance of <see cref="SelfAdaptiveRoundedNormalAllPositionsManipulator"/> with one.
58    /// </summary>
59    public SelfAdaptiveRoundedNormalAllPositionsManipulator()
60      : base() {
61      Parameters.Add(new LookupParameter<DoubleArray>("StrategyParameter", "The vector containing the endogenous strategy parameters."));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new SelfAdaptiveRoundedNormalAllPositionsManipulator(this, cloner);
66    }
67
68    /// <summary>
69    /// Performs an adaptive normally distributed all position manipulation on the given
70    /// <paramref name="vector"/> and rounding the results to the next feasible value.
71    /// </summary>
72    /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
73    /// as long as the vector to get manipulated.</exception>
74    /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param>
75    /// <param name="random">A random number generator.</param>
76    /// <param name="vector">The integer vector to manipulate.</param>
77    /// <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>
78    public static void Apply(IRandom random, IntegerVector vector, IntMatrix bounds, DoubleArray strategyParameters) {
79      if (strategyParameters == null || strategyParameters.Length == 0) throw new ArgumentException("SelfAdaptiveRoundedNormalAllPositionsManipulator: Vector containing the standard deviations is not defined.", "sigma");
80      if (bounds == null || bounds.Rows == 0 || bounds.Columns < 2) throw new ArgumentException("SelfAdaptiveRoundedNormalAllPositionsManipulator: Invalid bounds specified.", "bounds");
81      var N = new NormalDistributedRandom(random, 0.0, 1.0);
82      if (strategyParameters != null) {
83        for (int i = 0; i < vector.Length; i++) {
84          int min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1], step = 1;
85          if (bounds.Columns > 2) step = bounds[i % bounds.Rows, 2];
86
87          int value = (vector[i] + (int)Math.Round((N.NextDouble() * strategyParameters[i % strategyParameters.Length])) - min) / step;
88          max = FloorFeasible(min, max, step, max - 1);
89          vector[i] = RoundFeasible(min, max, step, value);
90        }
91      }
92    }
93
94    /// <summary>
95    /// Checks that the strategy vector is not null and forwards the call to the static Apply method.
96    /// </summary>
97    /// <param name="random">The random number generator.</param>
98    /// <param name="vector">The vector of integer values that is manipulated.</param>
99    /// <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>
100    protected override void ManipulateBounded(IRandom random, IntegerVector vector, IntMatrix bounds) {
101      Apply(random, vector, bounds, StrategyParameterParameter.ActualValue);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.