#region License Information /* HeuristicLab * Copyright (C) 2002-2013 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.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; using System; namespace HeuristicLab.Encodings.RealVectorEncoding { [Item("CovarianceMatrixMutator", "Mutates the solution vector according to the CMA-ES scheme.")] [StorableClass] public sealed class CMAMutator : SingleSuccessorOperator, IStochasticOperator, IRealVectorOperator, ICMAESManipulator, IIterationBasedOperator { private const int MaxTries = 1000; public Type CMAType { get { return typeof(CMAParameters); } } #region Parameter Properties public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public ILookupParameter IterationsParameter { get { return (ILookupParameter)Parameters["Iterations"]; } } public IValueLookupParameter MaximumIterationsParameter { get { return (IValueLookupParameter)Parameters["MaximumIterations"]; } } public ILookupParameter RealVectorParameter { get { return (ILookupParameter)Parameters["RealVector"]; } } public IValueLookupParameter BoundsParameter { get { return (IValueLookupParameter)Parameters["Bounds"]; } } public ILookupParameter StrategyParametersParameter { get { return (ILookupParameter)Parameters["StrategyParameters"]; } } #endregion [StorableConstructor] private CMAMutator(bool deserializing) : base(deserializing) { } private CMAMutator(CMAMutator original, Cloner cloner) : base(original, cloner) { } public CMAMutator() : base() { Parameters.Add(new LookupParameter("Random", "The random number generator to use.")); Parameters.Add(new LookupParameter("Iterations", "The current iteration that is being processed.")); Parameters.Add(new ValueLookupParameter("MaximumIterations", "The maximum number of iterations to be processed.")); Parameters.Add(new LookupParameter("RealVector", "The solution vector of real values.")); Parameters.Add(new ValueLookupParameter("Bounds", "The bounds for the dimensions.")); Parameters.Add(new LookupParameter("StrategyParameters", "The CMA-ES strategy parameters used for mutation.")); } public override IDeepCloneable Clone(Cloner cloner) { return new CMAMutator(this, cloner); } public override IOperation Apply() { var random = RandomParameter.ActualValue; var arx = RealVectorParameter.ActualValue; var sp = StrategyParametersParameter.ActualValue; var iterations = IterationsParameter.ActualValue.Value; var initialIterations = sp.InitialIterations.Value; var bounds = BoundsParameter.ActualValue; var nd = new NormalDistributedRandom(random, 0, 1); var copy = (RealVector)arx.Clone(); int tries; if (initialIterations > iterations) { for (int i = 0; i < arx.Length; i++) { tries = 0; do { tries++; arx[i] = copy[i] + sp.Sigma.Value * sp.D[i] * nd.NextDouble(); } while ((bounds[i % bounds.Rows, 0] > arx[i] || arx[i] > bounds[i % bounds.Rows, 1]) && tries < MaxTries); } } else { bool inRange; var B = sp.B; tries = 0; do { tries++; inRange = true; var artmp = new double[arx.Length]; for (int i = 0; i < arx.Length; ++i) artmp[i] = sp.D[i] * nd.NextDouble(); for (int i = 0; i < arx.Length; i++) { var sum = 0.0; for (int j = 0; j < arx.Length; j++) sum += B[i, j] * artmp[j]; arx[i] = copy[i] + sp.Sigma.Value * sum; // m + sig * Normal(0,C) if (bounds[i % bounds.Rows, 0] > arx[i] || arx[i] > bounds[i % bounds.Rows, 1]) inRange = false; } } while (!inRange && tries < MaxTries); } return base.Apply(); } } }