Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Encodings.RealVectorEncoding/3.3/StrategyParameters/StdDevStrategyVectorManipulator.cs @ 17991

Last change on this file since 17991 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 5.9 KB
RevLine 
[3336]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[3336]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
[4068]22using System;
[4722]23using HeuristicLab.Common;
[3336]24using HeuristicLab.Core;
[4068]25using HeuristicLab.Data;
[3336]26using HeuristicLab.Operators;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
[17097]29using HEAL.Attic;
[3336]30using HeuristicLab.Random;
31
32namespace HeuristicLab.Encodings.RealVectorEncoding {
33  /// <summary>
34  /// Mutates the endogenous strategy parameters.
35  /// </summary>
[3450]36  [Item("StdDevStrategyVectorManipulator", "Mutates the endogenous strategy parameters.")]
[17097]37  [StorableType("599385AA-FBA0-4EE2-8B6E-B937C73E5901")]
[3450]38  public class StdDevStrategyVectorManipulator : SingleSuccessorOperator, IStochasticOperator, IRealVectorStdDevStrategyParameterManipulator {
[3520]39    public override bool CanChangeName {
40      get { return false; }
41    }
[3336]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    }
[4722]57
58    [StorableConstructor]
[17097]59    protected StdDevStrategyVectorManipulator(StorableConstructorFlag _) : base(_) { }
[4722]60    protected StdDevStrategyVectorManipulator(StdDevStrategyVectorManipulator original, Cloner cloner) : base(original, cloner) { }
[3336]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>
[3450]66    public StdDevStrategyVectorManipulator()
[3336]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
[4722]75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new StdDevStrategyVectorManipulator(this, cloner);
77    }
78
[3336]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>
[4545]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>
[3336]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++) {
[5381]91        double change = vector[i] * generalMultiplier * Math.Exp(learningRate * N.NextDouble());
[3336]92        if (bounds != null) {
93          double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
[5381]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          }
[3336]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.