Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/StrategyParameters/StdDevStrategyVectorManipulator.cs @ 15651

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

#2640: Updated year of copyrights in license headers

File size: 5.9 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.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Random;
31
32namespace HeuristicLab.Encodings.RealVectorEncoding {
33  /// <summary>
34  /// Mutates the endogenous strategy parameters.
35  /// </summary>
36  [Item("StdDevStrategyVectorManipulator", "Mutates the endogenous strategy parameters.")]
37  [StorableClass]
38  public class StdDevStrategyVectorManipulator : SingleSuccessorOperator, IStochasticOperator, IRealVectorStdDevStrategyParameterManipulator {
39    public override bool CanChangeName {
40      get { return false; }
41    }
42    public ILookupParameter<IRandom> RandomParameter {
43      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
44    }
45    public ILookupParameter<RealVector> StrategyParameterParameter {
46      get { return (ILookupParameter<RealVector>)Parameters["StrategyParameter"]; }
47    }
48    public IValueLookupParameter<DoubleValue> GeneralLearningRateParameter {
49      get { return (IValueLookupParameter<DoubleValue>)Parameters["GeneralLearningRate"]; }
50    }
51    public IValueLookupParameter<DoubleValue> LearningRateParameter {
52      get { return (IValueLookupParameter<DoubleValue>)Parameters["LearningRate"]; }
53    }
54    public IValueLookupParameter<DoubleMatrix> BoundsParameter {
55      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["Bounds"]; }
56    }
57
58    [StorableConstructor]
59    protected StdDevStrategyVectorManipulator(bool deserializing) : base(deserializing) { }
60    protected StdDevStrategyVectorManipulator(StdDevStrategyVectorManipulator original, Cloner cloner) : base(original, cloner) { }
61    /// <summary>
62    /// Initializes a new instance of <see cref="StrategyVectorManipulator"/> with four
63    /// parameters (<c>Random</c>, <c>StrategyVector</c>, <c>GeneralLearningRate</c> and
64    /// <c>LearningRate</c>).
65    /// </summary>
66    public StdDevStrategyVectorManipulator()
67      : base() {
68      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
69      Parameters.Add(new LookupParameter<RealVector>("StrategyParameter", "The strategy parameter to manipulate."));
70      Parameters.Add(new ValueLookupParameter<DoubleValue>("GeneralLearningRate", "The general learning rate (tau0)."));
71      Parameters.Add(new ValueLookupParameter<DoubleValue>("LearningRate", "The learning rate (tau)."));
72      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("Bounds", "A 2 column matrix specifying the lower and upper bound for each dimension. If there are less rows than dimension the bounds vector is cycled.", new DoubleMatrix(new double[,] { { 0, 5 } })));
73    }
74
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new StdDevStrategyVectorManipulator(this, cloner);
77    }
78
79    /// <summary>
80    /// Mutates the endogenous strategy parameters.
81    /// </summary>
82    /// <param name="random">The random number generator to use.</param>
83    /// <param name="vector">The strategy vector to manipulate.</param>
84    /// <param name="generalLearningRate">The general learning rate dampens the mutation over all dimensions.</param>
85    /// <param name="learningRate">The learning rate dampens the mutation in each dimension.</param>
86    /// <param name="bounds">The minimal and maximal value for each component, bounds are cycled if the length of bounds is smaller than the length of vector</param>
87    public static void Apply(IRandom random, RealVector vector, double generalLearningRate, double learningRate, DoubleMatrix bounds) {
88      NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
89      double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble());
90      for (int i = 0; i < vector.Length; i++) {
91        double change = vector[i] * generalMultiplier * Math.Exp(learningRate * N.NextDouble());
92        if (bounds != null) {
93          double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
94          if (min == max) vector[i] = min;
95          else {
96            while (change < min || change > max)
97              change = vector[i] * generalMultiplier * Math.Exp(learningRate * N.NextDouble());
98            vector[i] = change;
99          }
100        }
101      }
102    }
103    /// <summary>
104    /// Mutates the endogenous strategy parameters.
105    /// </summary>
106    /// <remarks>Calls <see cref="OperatorBase.Apply"/> of base class <see cref="OperatorBase"/>.</remarks>
107    /// <inheritdoc select="returns"/>
108    public override IOperation Apply() {
109      RealVector strategyParams = StrategyParameterParameter.ActualValue;
110      if (strategyParams != null) { // only apply if there is a strategy vector
111        IRandom random = RandomParameter.ActualValue;
112        double tau0 = GeneralLearningRateParameter.ActualValue.Value;
113        double tau = LearningRateParameter.ActualValue.Value;
114        Apply(random, strategyParams, tau0, tau, BoundsParameter.ActualValue);
115      }
116      return base.Apply();
117    }
118  }
119}
Note: See TracBrowser for help on using the repository browser.