source: branches/2520_PersistenceReintegration/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/SelfAdaptiveNormalAllPositionsManipulator.cs @ 16453

Last change on this file since 16453 was 16453, checked in by jkarder, 11 months ago

#2520: updated year of copyrights

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Random;
29
30namespace HeuristicLab.Encodings.RealVectorEncoding {
31  /// <summary>
32  /// Manipulates each dimension in the real vector with the mutation strength given
33  /// in the strategy parameter vector.
34  /// </summary>
35  /// <remarks>
36  /// It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.<br/>
37  /// The strategy vector can be of smaller length than the solution vector, in which case values are taken from the beginning again once the end of the strategy vector is reached.
38  /// </remarks>
39  [Item("SelfAdaptiveNormalAllPositionsManipulator", "This manipulation operator adds a value sigma_i * N(0,1) to the current value in each position i. 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. It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.")]
40  [StorableClass]
41  // BackwardsCompatibility3.3
42  // Rename class to match file- and itemname when upgrading to 3.4
43  public class NormalAllPositionsManipulator : RealVectorManipulator, ISelfAdaptiveManipulator {
44    public Type StrategyParameterType {
45      get { return typeof(IRealVectorStdDevStrategyParameterOperator); }
46    }
47    /// <summary>
48    /// Parameter for the strategy vector.
49    /// </summary>
50    public ILookupParameter<RealVector> StrategyParameterParameter {
51      get { return (ILookupParameter<RealVector>)Parameters["StrategyParameter"]; }
52    }
53
54    IParameter ISelfAdaptiveManipulator.StrategyParameterParameter {
55      get { return StrategyParameterParameter; }
56    }
57
58    [StorableConstructor]
59    protected NormalAllPositionsManipulator(bool deserializing) : base(deserializing) { }
60    protected NormalAllPositionsManipulator(NormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
61    /// <summary>
62    /// Initializes a new instance of <see cref="NormalAllPositionsManipulator"/> with one
63    /// parameter (<c>StrategyVector</c>).
64    /// </summary>
65    public NormalAllPositionsManipulator()
66      : base() {
67      Parameters.Add(new LookupParameter<RealVector>("StrategyParameter", "The vector containing the endogenous strategy parameters."));
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new NormalAllPositionsManipulator(this, cloner);
72    }
73
74    /// <summary>
75    /// Performs an adaptive normally distributed all position manipulation on the given
76    /// <paramref name="vector"/>.
77    /// </summary>
78    /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
79    /// as long as the vector to get manipulated.</exception>
80    /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param>
81    /// <param name="random">A random number generator.</param>
82    /// <param name="vector">The real vector to manipulate.</param>
83    /// <returns>The manipulated real vector.</returns>
84    public static void Apply(IRandom random, RealVector vector, RealVector strategyParameters) {
85      NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
86      if (strategyParameters != null) {
87        for (int i = 0; i < vector.Length; i++) {
88          vector[i] = vector[i] + (N.NextDouble() * strategyParameters[i % strategyParameters.Length]);
89        }
90      } else {
91        // BackwardsCompatibility3.3
92        #region Backwards compatible region (remove with 3.4)
93        // when upgrading to 3.4 remove the whole condition and just put the if-branch in place (you should then also throw an ArgumentException when strategyParameters is null or has length 0)
94        for (int i = 0; i < vector.Length; i++) {
95          vector[i] = vector[i] + N.NextDouble();
96        }
97        #endregion
98      }
99    }
100
101    /// <summary>
102    /// Checks that the strategy vector is not null and forwards the call to <see cref="Apply(IRandom, RealVector, RealVector)"/>.
103    /// </summary>
104    /// <param name="random">The random number generator.</param>
105    /// <param name="realVector">The vector of real values that is manipulated.</param>
106    protected override void Manipulate(IRandom random, RealVector realVector) {
107      Apply(random, realVector, StrategyParameterParameter.ActualValue);
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.