Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CMAES/HeuristicLab.Encodings.RealVectorEncoding/3.3/CMAESOperators/CovarianceMatrixMutator.cs @ 9115

Last change on this file since 9115 was 9115, checked in by abeham, 11 years ago

#1961: Added CMA-ES branch

File size: 3.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Operators;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Random;
29using System;
30
31namespace HeuristicLab.Encodings.RealVectorEncoding {
32  [Item("CovarianceMatrixMutator", "Mutates the solution vector according to the CMA-ES scheme.")]
33  [StorableClass]
34  public class CovarianceMatrixMutator : SingleSuccessorOperator, IStochasticOperator, IRealVectorOperator, ICMAESManipulator {
35
36    public Type CMAType {
37      get { return typeof(CovarianceMatrixStrategyParameters); }
38    }
39
40    #region Parameter Properties
41    public ILookupParameter<IRandom> RandomParameter {
42      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
43    }
44
45    public ILookupParameter<RealVector> RealVectorParameter {
46      get { return (ILookupParameter<RealVector>)Parameters["RealVector"]; }
47    }
48
49    public ILookupParameter<CovarianceMatrixStrategyParameters> StrategyParametersParameter {
50      get { return (ILookupParameter<CovarianceMatrixStrategyParameters>)Parameters["StrategyParameters"]; }
51    }
52    #endregion
53
54    [StorableConstructor]
55    protected CovarianceMatrixMutator(bool deserializing) : base(deserializing) { }
56    protected CovarianceMatrixMutator(CovarianceMatrixMutator original, Cloner cloner) : base(original, cloner) { }
57    public CovarianceMatrixMutator()
58      : base() {
59      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
60      Parameters.Add(new LookupParameter<RealVector>("RealVector", "The solution vector of real values."));
61      Parameters.Add(new LookupParameter<CovarianceMatrixStrategyParameters>("StrategyParameters", "The CMA-ES strategy parameters used for mutation."));
62    }
63
64    public override IDeepCloneable Clone(Cloner cloner) {
65      return new CovarianceMatrixMutator(this, cloner);
66    }
67
68    public override IOperation Apply() {
69      var random = RandomParameter.ActualValue;
70      var arx = RealVectorParameter.ActualValue;
71      var sp = StrategyParametersParameter.ActualValue;
72
73      var nd = new NormalDistributedRandom(random, 0, 1);
74
75      var artmp = new double[arx.Length];
76      for (int i = 0; i < arx.Length; ++i)
77        artmp[i] = sp.D[i] * nd.NextDouble();
78
79      for (int i = 0; i < arx.Length; i++) {
80        var sum = 0.0;
81        for (int j = 0; j < arx.Length; j++)
82          sum += sp.B[i, j] * artmp[j];
83        arx[i] += sp.Sigma.Value * artmp[i]; // m + sig * Normal(0,C)
84      }
85      return base.Apply();
86    }
87  }
88}
Note: See TracBrowser for help on using the repository browser.