Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Encodings.RealVectorEncoding/3.3/StrategyParameters/StrategyVectorManipulator.cs @ 3336

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

updated Evolution Stratgy #932
StrategyParameters moved completely out of the ES into the encoding and problem, because they are inherently problem specific
The ProblemDimension and a few other parameters of ES were removed
Fixed a few bugs also and added some documentation

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