Free cookie consent management tool by TermsFeed Policy Generator

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

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

Sorted usings and removed unused usings in entire solution (#1094)

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