Free cookie consent management tool by TermsFeed Policy Generator

source: branches/CMAES/HeuristicLab.Encodings.RealVectorEncoding/3.3/CMAESOperators/CMAInitializer.cs @ 9118

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

#1961: Updated CMA-ES (working version), but no wiring

File size: 8.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.Data;
25using HeuristicLab.Operators;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using System;
30using System.Linq;
31
32namespace HeuristicLab.Encodings.RealVectorEncoding {
33  [Item("CMAInitializer", "Initializes the covariance matrix and step size variables.")]
34  [StorableClass]
35  public class CMAInitializer : SingleSuccessorOperator, IRealVectorOperator, ICMAESInitializer, IIterationBasedOperator {
36
37    public Type CMAType {
38      get { return typeof(CMAParameters); }
39    }
40
41    #region Parameter Properties
42    public IValueLookupParameter<IntValue> DimensionParameter {
43      get { return (IValueLookupParameter<IntValue>)Parameters["Dimension"]; }
44    }
45
46    public IValueLookupParameter<DoubleArray> InitialSigmaParameter {
47      get { return (IValueLookupParameter<DoubleArray>)Parameters["InitialSigma"]; }
48    }
49
50    public IValueLookupParameter<DoubleArray> MaxSigmaParameter {
51      get { return (IValueLookupParameter<DoubleArray>)Parameters["MaxSigma"]; }
52    }
53
54    public IValueLookupParameter<DoubleArray> MinSigmaParameter {
55      get { return (IValueLookupParameter<DoubleArray>)Parameters["MinSigma"]; }
56    }
57
58    public ILookupParameter<IntValue> IterationsParameter {
59      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
60    }
61
62    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
63      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
64    }
65
66    public IValueLookupParameter<IntValue> InitialIterationsParameter {
67      get { return (IValueLookupParameter<IntValue>)Parameters["InitialIterations"]; }
68    }
69
70    public ILookupParameter<IntValue> PopulationSizeParameter {
71      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
72    }
73
74    public ILookupParameter<IntValue> MuParameter {
75      get { return (ILookupParameter<IntValue>)Parameters["Mu"]; }
76    }
77
78    public ILookupParameter<CMAParameters> StrategyParametersParameter {
79      get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
80    }
81    #endregion
82
83    [StorableConstructor]
84    protected CMAInitializer(bool deserializing) : base(deserializing) { }
85    protected CMAInitializer(CMAInitializer original, Cloner cloner) : base(original, cloner) { }
86    public CMAInitializer()
87      : base() {
88      Parameters.Add(new ValueLookupParameter<IntValue>("Dimension", "The problem dimension (N)."));
89      Parameters.Add(new ValueLookupParameter<DoubleArray>("InitialSigma", "The initial value for Sigma (need to be > 0), can be single dimensioned or an array that should be equal to the size of the vector.", new DoubleArray(new[] { 0.5 })));
90      Parameters.Add(new ValueLookupParameter<DoubleArray>("MaxSigma", "The maximum sigma value can be omitted, given as one value for all dimensions or a value for each dimension."));
91      Parameters.Add(new ValueLookupParameter<DoubleArray>("MinSigma", "The minimum sigma value can be omitted, given as one value for all dimensions or a value for each dimension."));
92      Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration that is being processed."));
93      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations to be processed."));
94      Parameters.Add(new ValueLookupParameter<IntValue>("InitialIterations", "The number of iterations that should be performed using the diagonal covariance matrix only."));
95      Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The population size (lambda)."));
96      Parameters.Add(new LookupParameter<IntValue>("Mu", "The number of offspring considered for updating of the strategy parameters."));
97      Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The strategy parameters for real-encoded CMA-ES."));
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new CMAInitializer(this, cloner);
102    }
103
104    public override IOperation Apply() {
105      var N = DimensionParameter.ActualValue.Value;
106      var lambda = PopulationSizeParameter.ActualValue.Value;
107
108      var sp = new CMAParameters(N, lambda);
109
110      var s = InitialSigmaParameter.ActualValue;
111      if (s == null || s.Length == 0) throw new InvalidOperationException("Initial standard deviation (sigma) must be given.");
112      var sigma = s.Max();
113      if (sigma <= 0) throw new InvalidOperationException("Initial standard deviation (sigma) must be > 0.");
114
115      var pc = new double[N]; // evolution paths for C
116      var ps = new double[N]; // evolution paths for sigma
117      var B = new double[N, N]; // B defines the coordinate system
118      var D = new double[N]; // diagonal D defines the scaling
119      var C = new double[N, N]; // covariance matrix C
120      var BDz = new double[N];
121      double minSqrtdiagC = int.MaxValue, maxSqrtdiagC = int.MinValue;
122      for (int i = 0; i < N; i++) {
123        B[i, i] = 1;
124        if (s.Length == 1) D[i] = 1;
125        else if (s.Length == N) D[i] = s[i] / sigma;
126        else throw new InvalidOperationException("Initial standard deviation (sigma) must either contain only one value for all dimension or for every dimension.");
127        if (D[i] <= 0) throw new InvalidOperationException("Initial standard deviation (sigma) values must all be > 0.");
128        C[i, i] = D[i] * D[i];
129        if (Math.Sqrt(C[i, i]) < minSqrtdiagC) minSqrtdiagC = Math.Sqrt(C[i, i]);
130        if (Math.Sqrt(C[i, i]) > maxSqrtdiagC) maxSqrtdiagC = Math.Sqrt(C[i, i]);
131      }
132
133      // ensure maximal and minimal standard deviations
134      var minSigma = MinSigmaParameter.ActualValue;
135      if (minSigma != null && minSigma.Length > 0) {
136        for (int i = 0; i < N; i++) {
137          var d = minSigma[(int)Math.Min(i, minSigma.Length - 1)];
138          if (d > sigma * minSqrtdiagC) sigma = d / minSqrtdiagC;
139        }
140      }
141      var maxSigma = MaxSigmaParameter.ActualValue;
142      if (maxSigma != null && maxSigma.Length > 0) {
143        for (int i = 0; i < N; i++) {
144          var d = maxSigma[(int)Math.Min(i, maxSigma.Length - 1)];
145          if (d > sigma * maxSqrtdiagC) sigma = d / maxSqrtdiagC;
146        }
147      }
148      // end ensure ...
149
150      // testAndCorrectNumerics
151      double fac = 1;
152      if (D.Max() < 1e-6)
153        fac = 1.0 / D.Max();
154      else if (D.Min() > 1e4)
155        fac = 1.0 / D.Min();
156
157      if (fac != 1.0) {
158        sigma /= fac;
159        for (int i = 0; i < N; i++) {
160          pc[i] *= fac;
161          D[i] *= fac;
162          for (int j = 0; j < N; j++)
163            C[i, j] *= fac * fac;
164        }
165      }
166      // end testAndCorrectNumerics
167
168      var initialIterations = InitialIterationsParameter.ActualValue;
169      if (initialIterations == null) {
170        initialIterations = new IntValue(0);
171      }
172
173      double maxD = D.Max(), minD = D.Min();
174      if (minD == 0) sp.AxisRatio = new DoubleValue(double.PositiveInfinity);
175      else sp.AxisRatio = new DoubleValue(maxD / minD);
176      sp.PC = new DoubleArray(pc);
177      sp.PS = new DoubleArray(ps);
178      sp.B = new DoubleMatrix(B);
179      sp.D = new DoubleArray(D);
180      sp.C = new DoubleMatrix(C);
181      sp.BDz = new DoubleArray(BDz);
182      sp.Sigma = new DoubleValue(sigma);
183      if (maxSigma != null) sp.MaxSigma = (DoubleArray)maxSigma.Clone();
184      if (minSigma != null) sp.MinSigma = (DoubleArray)minSigma.Clone();
185      sp.InitialIterations = new IntValue(initialIterations.Value);
186
187      MuParameter.ActualValue = new IntValue(sp.Mu.Value);
188      StrategyParametersParameter.ActualValue = sp;
189      return base.Apply();
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.