[8185] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17181] | 3 | * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[8185] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[17097] | 29 | using HEAL.Attic;
|
---|
[8185] | 30 | using HeuristicLab.Random;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.IntegerVectorEncoding {
|
---|
| 33 | /// <summary>
|
---|
| 34 | /// Mutates the endogenous strategy parameters.
|
---|
| 35 | /// </summary>
|
---|
| 36 | [Item("StdDevStrategyVectorManipulator", "Mutates the endogenous strategy parameters.")]
|
---|
[17097] | 37 | [StorableType("8DEED222-E816-4B80-B3F9-0A799EC40C4E")]
|
---|
[8185] | 38 | public class StdDevStrategyVectorManipulator : SingleSuccessorOperator, IStochasticOperator, IIntegerVectorStdDevStrategyParameterManipulator {
|
---|
| 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<DoubleArray> StrategyParameterParameter {
|
---|
| 46 | get { return (ILookupParameter<DoubleArray>)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]
|
---|
[17097] | 59 | protected StdDevStrategyVectorManipulator(StorableConstructorFlag _) : base(_) { }
|
---|
[8185] | 60 | protected StdDevStrategyVectorManipulator(StdDevStrategyVectorManipulator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 61 | public StdDevStrategyVectorManipulator()
|
---|
| 62 | : base() {
|
---|
| 63 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
| 64 | Parameters.Add(new LookupParameter<DoubleArray>("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 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 71 | return new StdDevStrategyVectorManipulator(this, cloner);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | /// <summary>
|
---|
| 75 | /// Mutates the endogenous strategy parameters.
|
---|
| 76 | /// </summary>
|
---|
| 77 | /// <param name="random">The random number generator to use.</param>
|
---|
| 78 | /// <param name="vector">The strategy vector to manipulate.</param>
|
---|
| 79 | /// <param name="generalLearningRate">The general learning rate dampens the mutation over all dimensions.</param>
|
---|
| 80 | /// <param name="learningRate">The learning rate dampens the mutation in each dimension.</param>
|
---|
| 81 | /// <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>
|
---|
| 82 | public static void Apply(IRandom random, DoubleArray vector, double generalLearningRate, double learningRate, DoubleMatrix bounds) {
|
---|
| 83 | NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
|
---|
| 84 | double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble());
|
---|
| 85 | for (int i = 0; i < vector.Length; i++) {
|
---|
| 86 | double change = vector[i] * generalMultiplier * Math.Exp(learningRate * N.NextDouble());
|
---|
| 87 | if (bounds != null) {
|
---|
| 88 | double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
|
---|
| 89 | if (min == max) vector[i] = min;
|
---|
| 90 | else {
|
---|
| 91 | if (change < min || change > max) change = Math.Max(min, Math.Min(max, change));
|
---|
| 92 | vector[i] = change;
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | /// <summary>
|
---|
| 98 | /// Mutates the endogenous strategy parameters.
|
---|
| 99 | /// </summary>
|
---|
| 100 | /// <remarks>Calls <see cref="OperatorBase.Apply"/> of base class <see cref="OperatorBase"/>.</remarks>
|
---|
| 101 | /// <inheritdoc select="returns"/>
|
---|
| 102 | public override IOperation Apply() {
|
---|
| 103 | var strategyParams = StrategyParameterParameter.ActualValue;
|
---|
| 104 | if (strategyParams != null) { // only apply if there is a strategy vector
|
---|
| 105 | IRandom random = RandomParameter.ActualValue;
|
---|
| 106 | double tau0 = GeneralLearningRateParameter.ActualValue.Value;
|
---|
| 107 | double tau = LearningRateParameter.ActualValue.Value;
|
---|
| 108 | Apply(random, strategyParams, tau0, tau, BoundsParameter.ActualValue);
|
---|
| 109 | }
|
---|
| 110 | return base.Apply();
|
---|
| 111 | }
|
---|
| 112 | }
|
---|
| 113 | }
|
---|