Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/Manipulators/FixedNormalAllPositionsManipulator.cs @ 13954

Last change on this file since 13954 was 12009, checked in by ascheibe, 10 years ago

#2212 updated copyright year

File size: 4.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Random;
28
29namespace HeuristicLab.Encodings.RealVectorEncoding {
30  /// <summary>
31  /// Manipulates each dimension in the real vector with the mutation strength given
32  /// in the strategy parameter vector.
33  /// </summary>
34  /// <remarks>
35  /// 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/>
36  /// 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.
37  /// </remarks>
38  [Item("FixedNormalAllPositionsManipulator", "This manipulation operator adds a value sigma_i * N_i(0,1) to the current value in each position i given the values for sigma_i in the parameter. If there are less elements in Sigma than positions, then Sigma 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.")]
39  [StorableClass]
40  public class FixedNormalAllPositionsManipulator : RealVectorManipulator {
41
42    public IValueParameter<RealVector> SigmaParameter {
43      get { return (IValueParameter<RealVector>)Parameters["Sigma"]; }
44    }
45
46    [StorableConstructor]
47    protected FixedNormalAllPositionsManipulator(bool deserializing) : base(deserializing) { }
48    protected FixedNormalAllPositionsManipulator(FixedNormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { }
49    /// <summary>
50    /// Initializes a new instance of <see cref="NormalAllPositionsManipulator"/> with one
51    /// parameter (<c>StrategyVector</c>).
52    /// </summary>
53    public FixedNormalAllPositionsManipulator()
54      : base() {
55      Parameters.Add(new ValueParameter<RealVector>("Sigma", "The vector containing the standard deviations used for manipulating each dimension. If it is only of length one the same sigma will be used for every dimension.", new RealVector(new double[] { 1 })));
56    }
57
58    public override IDeepCloneable Clone(Cloner cloner) {
59      return new FixedNormalAllPositionsManipulator(this, cloner);
60    }
61
62    /// <summary>
63    /// Performs an adaptive normally distributed all position manipulation on the given
64    /// <paramref name="vector"/>.
65    /// </summary>
66    /// <exception cref="InvalidOperationException">Thrown when the strategy vector is not
67    /// as long as the vector to get manipulated.</exception>
68    /// <param name="sigma">The strategy vector determining the strength of the mutation.</param>
69    /// <param name="random">A random number generator.</param>
70    /// <param name="vector">The real vector to manipulate.</param>
71    /// <returns>The manipulated real vector.</returns>
72    public static void Apply(IRandom random, RealVector vector, RealVector sigma) {
73      if (sigma == null || sigma.Length == 0) throw new ArgumentException("ERROR: Vector containing the standard deviations is not defined.", "sigma");
74      NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
75      for (int i = 0; i < vector.Length; i++) {
76        vector[i] = vector[i] + (N.NextDouble() * sigma[i % sigma.Length]);
77      }
78    }
79
80    /// <summary>
81    /// Checks that the strategy vector is not null and forwards the call to <see cref="Apply(IRandom, RealVector, RealVector)"/>.
82    /// </summary>
83    /// <param name="random">The random number generator.</param>
84    /// <param name="realVector">The vector of real values that is manipulated.</param>
85    protected override void Manipulate(IRandom random, RealVector realVector) {
86      Apply(random, realVector, SigmaParameter.Value);
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.