Free cookie consent management tool by TermsFeed Policy Generator

source: tags/3.3.0/HeuristicLab.Encodings.RealVectorEncoding/3.3/StrategyParameters/StdDevStrategyVectorManipulator.cs @ 17824

Last change on this file since 17824 was 3520, checked in by abeham, 14 years ago

forbid changing some operator names #889, #890, #913, #914, #934, #924

File size: 5.2 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 HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Operators;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Data;
28using HeuristicLab.Random;
29using System;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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    /// <summary>
58    /// Initializes a new instance of <see cref="StrategyVectorManipulator"/> with four
59    /// parameters (<c>Random</c>, <c>StrategyVector</c>, <c>GeneralLearningRate</c> and
60    /// <c>LearningRate</c>).
61    /// </summary>
62    public StdDevStrategyVectorManipulator()
63      : base() {
64      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
65      Parameters.Add(new LookupParameter<RealVector>("StrategyParameter", "The strategy parameter to manipulate."));
66      Parameters.Add(new ValueLookupParameter<DoubleValue>("GeneralLearningRate", "The general learning rate (tau0)."));
67      Parameters.Add(new ValueLookupParameter<DoubleValue>("LearningRate", "The learning rate (tau)."));
68      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 } })));
69    }
70
71    /// <summary>
72    /// Mutates the endogenous strategy parameters.
73    /// </summary>
74    /// <param name="random">The random number generator to use.</param>
75    /// <param name="vector">The strategy vector to manipulate.</param>
76    /// <param name="generalLearningRate">The general learning rate dampens the mutation over all dimensions.</param>
77    /// <param name="learningRate">The learning rate dampens the mutation in each dimension.</param>
78    public static void Apply(IRandom random, RealVector vector, double generalLearningRate, double learningRate, DoubleMatrix bounds) {
79      NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
80      double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble());
81      for (int i = 0; i < vector.Length; i++) {
82        vector[i] *= generalMultiplier * Math.Exp(learningRate * N.NextDouble());
83        if (bounds != null) {
84          double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
85          if (vector[i] < min) vector[i] = min;
86          if (vector[i] > max) vector[i] = max;
87        }
88      }
89    }
90    /// <summary>
91    /// Mutates the endogenous strategy parameters.
92    /// </summary>
93    /// <remarks>Calls <see cref="OperatorBase.Apply"/> of base class <see cref="OperatorBase"/>.</remarks>
94    /// <inheritdoc select="returns"/>
95    public override IOperation Apply() {
96      RealVector strategyParams = StrategyParameterParameter.ActualValue;
97      if (strategyParams != null) { // only apply if there is a strategy vector
98        IRandom random = RandomParameter.ActualValue;
99        double tau0 = GeneralLearningRateParameter.ActualValue.Value;
100        double tau = LearningRateParameter.ActualValue.Value;
101        Apply(random, strategyParams, tau0, tau, BoundsParameter.ActualValue);
102      }
103      return base.Apply();
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.