#region License Information
/* HeuristicLab
* Copyright (C) 2002-2014 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 HeuristicLab.Core;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Encodings.RealVectorEncoding;
using HeuristicLab.Data;
using HeuristicLab.Random;
using System;
namespace HeuristicLab.Algorithms.EvolutionStrategy {
///
/// Mutates the endogenous strategy parameters.
///
public class StrategyVectorManipulator : SingleSuccessorOperator, IStochasticOperator {
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public ILookupParameter StrategyVectorParameter {
get { return (ILookupParameter)Parameters["StrategyVector"]; }
}
public IValueLookupParameter GeneralLearningRateParameter {
get { return (IValueLookupParameter)Parameters["GeneralLearningRate"]; }
}
public IValueLookupParameter LearningRateParameter {
get { return (IValueLookupParameter)Parameters["LearningRate"]; }
}
///
/// Initializes a new instance of with four
/// parameters (Random, StrategyVector, GeneralLearningRate and
/// LearningRate).
///
public StrategyVectorManipulator()
: base() {
Parameters.Add(new LookupParameter("Random", "The random number generator to use."));
Parameters.Add(new LookupParameter("StrategyVector", "The strategy vector to manipulate."));
Parameters.Add(new ValueLookupParameter("GeneralLearningRate", "The general learning rate (tau0)."));
Parameters.Add(new ValueLookupParameter("LearningRate", "The learning rate (tau)."));
}
///
/// 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.
public static void Apply(IRandom random, RealVector vector, double generalLearningRate, double learningRate) {
NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0);
double generalMultiplier = Math.Exp(generalLearningRate * N.NextDouble());
for (int i = 0; i < vector.Length; i++) {
vector[i] *= generalMultiplier * Math.Exp(learningRate * N.NextDouble());
}
}
///
/// Mutates the endogenous strategy parameters.
///
/// Calls of base class .
///
public override IOperation Apply() {
RealVector strategyParams = StrategyVectorParameter.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);
}
return base.Apply();
}
}
}