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