#region License Information
/* HeuristicLab
* Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Random;
namespace HeuristicLab.Encodings.RealVectorEncoding {
///
/// Mutates the endogenous strategy parameters.
///
[Item("StdDevStrategyVectorManipulator", "Mutates the endogenous strategy parameters.")]
[StorableClass]
public class StdDevStrategyVectorManipulator : SingleSuccessorOperator, IStochasticOperator, IRealVectorStdDevStrategyParameterManipulator {
public override bool CanChangeName {
get { return false; }
}
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public ILookupParameter StrategyParameterParameter {
get { return (ILookupParameter)Parameters["StrategyParameter"]; }
}
public IValueLookupParameter GeneralLearningRateParameter {
get { return (IValueLookupParameter)Parameters["GeneralLearningRate"]; }
}
public IValueLookupParameter LearningRateParameter {
get { return (IValueLookupParameter)Parameters["LearningRate"]; }
}
public IValueLookupParameter BoundsParameter {
get { return (IValueLookupParameter)Parameters["Bounds"]; }
}
[StorableConstructor]
protected StdDevStrategyVectorManipulator(bool deserializing) : base(deserializing) { }
protected StdDevStrategyVectorManipulator(StdDevStrategyVectorManipulator original, Cloner cloner) : base(original, cloner) { }
///
/// Initializes a new instance of with four
/// parameters (Random, StrategyVector, GeneralLearningRate and
/// LearningRate).
///
public StdDevStrategyVectorManipulator()
: base() {
Parameters.Add(new LookupParameter("Random", "The random number generator to use."));
Parameters.Add(new LookupParameter("StrategyParameter", "The strategy parameter to manipulate."));
Parameters.Add(new ValueLookupParameter("GeneralLearningRate", "The general learning rate (tau0)."));
Parameters.Add(new ValueLookupParameter("LearningRate", "The learning rate (tau)."));
Parameters.Add(new ValueLookupParameter("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 } })));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new StdDevStrategyVectorManipulator(this, cloner);
}
///
/// Mutates the endogenous strategy parameters.
///
/// The random number generator to use.
/// The strategy vector to manipulate.
/// The general learning rate dampens the mutation over all dimensions.
/// The learning rate dampens the mutation in each dimension.
/// The minimal and maximal value for each component, bounds are cycled if the length of bounds is smaller than the length of vector
public static void Apply(IRandom random, RealVector vector, double generalLearningRate, double learningRate, DoubleMatrix bounds) {
NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble());
for (int i = 0; i < vector.Length; i++) {
double change = vector[i] * generalMultiplier * Math.Exp(learningRate * N.NextDouble());
if (bounds != null) {
double min = bounds[i % bounds.Rows, 0], max = bounds[i % bounds.Rows, 1];
if (min == max) vector[i] = min;
else {
while (change < min || change > max)
change = vector[i] * generalMultiplier * Math.Exp(learningRate * N.NextDouble());
vector[i] = change;
}
}
}
}
///
/// Mutates the endogenous strategy parameters.
///
/// Calls of base class .
///
public override IOperation Apply() {
RealVector strategyParams = StrategyParameterParameter.ActualValue;
if (strategyParams != null) { // only apply if there is a strategy vector
IRandom random = RandomParameter.ActualValue;
double tau0 = GeneralLearningRateParameter.ActualValue.Value;
double tau = LearningRateParameter.ActualValue.Value;
Apply(random, strategyParams, tau0, tau, BoundsParameter.ActualValue);
}
return base.Apply();
}
}
}