#region License Information /* HeuristicLab * Copyright (C) 2002-2019 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.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Random; namespace HeuristicLab.Encodings.RealVectorEncoding { /// /// Manipulates each dimension in the real vector with the mutation strength given /// in the strategy parameter vector. /// /// /// It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.
/// The strategy vector can be of smaller length than the solution vector, in which case values are taken from the beginning again once the end of the strategy vector is reached. ///
[Item("SelfAdaptiveNormalAllPositionsManipulator", "This manipulation operator adds a value sigma_i * N(0,1) to the current value in each position i. The values for sigma_i are looked up dynamically. If there are less elements in the strategy vector than positions, then the strategy vector is cycled. It is implemented as described in Beyer, H.-G. and Schwefel, H.-P. 2002. Evolution Strategies - A Comprehensive Introduction Natural Computing, 1, pp. 3-52.")] [StorableClass] // BackwardsCompatibility3.3 // Rename class to match file- and itemname when upgrading to 3.4 public class NormalAllPositionsManipulator : RealVectorManipulator, ISelfAdaptiveManipulator { public Type StrategyParameterType { get { return typeof(IRealVectorStdDevStrategyParameterOperator); } } /// /// Parameter for the strategy vector. /// public ILookupParameter StrategyParameterParameter { get { return (ILookupParameter)Parameters["StrategyParameter"]; } } IParameter ISelfAdaptiveManipulator.StrategyParameterParameter { get { return StrategyParameterParameter; } } [StorableConstructor] protected NormalAllPositionsManipulator(bool deserializing) : base(deserializing) { } protected NormalAllPositionsManipulator(NormalAllPositionsManipulator original, Cloner cloner) : base(original, cloner) { } /// /// Initializes a new instance of with one /// parameter (StrategyVector). /// public NormalAllPositionsManipulator() : base() { Parameters.Add(new LookupParameter("StrategyParameter", "The vector containing the endogenous strategy parameters.")); } public override IDeepCloneable Clone(Cloner cloner) { return new NormalAllPositionsManipulator(this, cloner); } /// /// Performs an adaptive normally distributed all position manipulation on the given /// . /// /// Thrown when the strategy vector is not /// as long as the vector to get manipulated. /// The strategy vector determining the strength of the mutation. /// A random number generator. /// The real vector to manipulate. /// The manipulated real vector. public static void Apply(IRandom random, RealVector vector, RealVector strategyParameters) { NormalDistributedRandom N = new NormalDistributedRandom(random, 0.0, 1.0); if (strategyParameters != null) { for (int i = 0; i < vector.Length; i++) { vector[i] = vector[i] + (N.NextDouble() * strategyParameters[i % strategyParameters.Length]); } } else { // BackwardsCompatibility3.3 #region Backwards compatible region (remove with 3.4) // when upgrading to 3.4 remove the whole condition and just put the if-branch in place (you should then also throw an ArgumentException when strategyParameters is null or has length 0) for (int i = 0; i < vector.Length; i++) { vector[i] = vector[i] + N.NextDouble(); } #endregion } } /// /// Checks that the strategy vector is not null and forwards the call to . /// /// The random number generator. /// The vector of real values that is manipulated. protected override void Manipulate(IRandom random, RealVector realVector) { Apply(random, realVector, StrategyParameterParameter.ActualValue); } } }