Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/NormalAllPositionsManipulator.cs @ 4722

Last change on this file since 4722 was 4722, checked in by swagner, 14 years ago

Merged cloning refactoring branch back into trunk (#922)

File size: 5.1 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.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("NormalAllPositionsManipulator", "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 taken from the strategy vector, 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  public class NormalAllPositionsManipulator : RealVectorManipulator, ISelfAdaptiveManipulator {
42    public Type StrategyParameterType {
43      get { return typeof(IRealVectorStdDevStrategyParameterOperator); }
44    }
45    /// <summary>
46    /// Parameter for the strategy vector.
47    /// </summary>
48    public IValueLookupParameter<RealVector> StrategyParameterParameter {
49      get { return (IValueLookupParameter<RealVector>)Parameters["StrategyParameter"]; }
50    }
51
52    IParameter ISelfAdaptiveManipulator.StrategyParameterParameter {
53      get { return StrategyParameterParameter; }
54    }
55
56    [StorableConstructor]
57    protected NormalAllPositionsManipulator(bool deserializing) : base(deserializing) { }
58    protected NormalAllPositionsManipulator(NormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
59    /// <summary>
60    /// Initializes a new instance of <see cref="NormalAllPositionsManipulator"/> with one
61    /// parameter (<c>StrategyVector</c>).
62    /// </summary>
63    public NormalAllPositionsManipulator()
64      : base() {
65      Parameters.Add(new ValueLookupParameter<RealVector>("StrategyParameter", "The vector containing the endogenous strategy parameters."));
66    }
67
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new NormalAllPositionsManipulator(this, cloner);
70    }
71
72    /// <summary>
73    /// Performs an adaptive normally distributed all position manipulation on the given
74    /// <paramref name="vector"/>.
75    /// </summary>
76    /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
77    /// as long as the vector to get manipulated.</exception>
78    /// <param name="strategyParameters">The strategy vector determining the strength of the mutation.</param>
79    /// <param name="random">A random number generator.</param>
80    /// <param name="vector">The real vector to manipulate.</param>
81    /// <returns>The manipulated real vector.</returns>
82    public static void Apply(IRandom random, RealVector vector, RealVector strategyParameters) {
83      NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
84      if (strategyParameters != null && strategyParameters.Length > 0) {
85        for (int i = 0; i < vector.Length; i++) {
86          vector[i] = vector[i] + (N.NextDouble() * strategyParameters[i % strategyParameters.Length]);
87        }
88      } else {
89        for (int i = 0; i < vector.Length; i++) {
90          vector[i] = vector[i] + N.NextDouble();
91        }
92      }
93    }
94
95    /// <summary>
96    /// Checks that the strategy vector is not null and forwards the call to <see cref="Apply(IRandom, RealVector, RealVector)"/>.
97    /// </summary>
98    /// <param name="random">The random number generator.</param>
99    /// <param name="realVector">The vector of real values that is manipulated.</param>
100    protected override void Manipulate(IRandom random, RealVector realVector) {
101      Apply(random, realVector, StrategyParameterParameter.ActualValue);
102    }
103  }
104}
Note: See TracBrowser for help on using the repository browser.