Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ParameterBinding/HeuristicLab.Encodings.RealVectorEncoding/3.3/StrategyParameters/StdDevStrategyVectorManipulator.cs @ 4857

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

Merged cloning refactoring branch back into trunk (#922)

File size: 5.8 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.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        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 (vector[i] < min) vector[i] = min;
95          if (vector[i] > max) vector[i] = max;
96        }
97      }
98    }
99    /// <summary>
100    /// Mutates the endogenous strategy parameters.
101    /// </summary>
102    /// <remarks>Calls <see cref="OperatorBase.Apply"/> of base class <see cref="OperatorBase"/>.</remarks>
103    /// <inheritdoc select="returns"/>
104    public override IOperation Apply() {
105      RealVector strategyParams = StrategyParameterParameter.ActualValue;
106      if (strategyParams != null) { // only apply if there is a strategy vector
107        IRandom random = RandomParameter.ActualValue;
108        double tau0 = GeneralLearningRateParameter.ActualValue.Value;
109        double tau = LearningRateParameter.ActualValue.Value;
110        Apply(random, strategyParams, tau0, tau, BoundsParameter.ActualValue);
111      }
112      return base.Apply();
113    }
114  }
115}
Note: See TracBrowser for help on using the repository browser.