Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAOperators/CMAInitializer.cs @ 10879

Last change on this file since 10879 was 9297, checked in by abeham, 12 years ago

#1961:

  • Improved performance of CMA-ES without ALGLIB
    • All CMA-ES parameter are standard .NET types instead of HL types
File size: 8.3 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.Collections.Generic;
31using System.Linq;
32
33namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
34  [Item("CMAInitializer", "Initializes the covariance matrix and step size variables.")]
35  [StorableClass]
36  public class CMAInitializer : SingleSuccessorOperator, ICMAInitializer, IIterationBasedOperator {
37
38    public Type CMAType {
39      get { return typeof(CMAParameters); }
40    }
41
42    #region Parameter Properties
43    public IValueLookupParameter<IntValue> DimensionParameter {
44      get { return (IValueLookupParameter<IntValue>)Parameters["Dimension"]; }
45    }
46
47    public IValueLookupParameter<DoubleArray> InitialSigmaParameter {
48      get { return (IValueLookupParameter<DoubleArray>)Parameters["InitialSigma"]; }
49    }
50
51    public IValueLookupParameter<DoubleMatrix> SigmaBoundsParameter {
52      get { return (IValueLookupParameter<DoubleMatrix>)Parameters["SigmaBounds"]; }
53    }
54
55    public ILookupParameter<IntValue> IterationsParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
57    }
58
59    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
60      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
61    }
62
63    public IValueLookupParameter<IntValue> InitialIterationsParameter {
64      get { return (IValueLookupParameter<IntValue>)Parameters["InitialIterations"]; }
65    }
66
67    public ILookupParameter<IntValue> PopulationSizeParameter {
68      get { return (ILookupParameter<IntValue>)Parameters["PopulationSize"]; }
69    }
70
71    public ILookupParameter<IntValue> MuParameter {
72      get { return (ILookupParameter<IntValue>)Parameters["Mu"]; }
73    }
74
75    public ILookupParameter<CMAParameters> StrategyParametersParameter {
76      get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
77    }
78    #endregion
79
80    [StorableConstructor]
81    protected CMAInitializer(bool deserializing) : base(deserializing) { }
82    protected CMAInitializer(CMAInitializer original, Cloner cloner) : base(original, cloner) { }
83    public CMAInitializer()
84      : base() {
85      Parameters.Add(new ValueLookupParameter<IntValue>("Dimension", "The problem dimension (N)."));
86      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."));
87      Parameters.Add(new ValueLookupParameter<DoubleMatrix>("SigmaBounds", "The bounds for sigma value can be omitted, given as one value for all dimensions or a value for each dimension. First column specifies minimum, second column maximum value."));
88      Parameters.Add(new LookupParameter<IntValue>("Iterations", "The current iteration that is being processed."));
89      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations to be processed."));
90      Parameters.Add(new ValueLookupParameter<IntValue>("InitialIterations", "The number of iterations that should be performed using the diagonal covariance matrix only.", new IntValue(0)));
91      Parameters.Add(new LookupParameter<IntValue>("PopulationSize", "The population size (lambda)."));
92      Parameters.Add(new LookupParameter<IntValue>("Mu", "Optional, the number of offspring considered for updating of the strategy parameters."));
93      Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The strategy parameters for real-encoded CMA-ES."));
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new CMAInitializer(this, cloner);
98    }
99
100    public override IOperation Apply() {
101      var N = DimensionParameter.ActualValue.Value;
102      var lambda = PopulationSizeParameter.ActualValue.Value;
103      var mu = MuParameter.ActualValue;
104
105      var sp = new CMAParameters();
106      sp.Mu = mu == null ? (int)Math.Floor(lambda / 2.0) : mu.Value;
107      sp.QualityHistorySize = 10 + 30 * N / lambda;
108      sp.QualityHistory = new Queue<double>(sp.QualityHistorySize + 1);
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 sigmaBounds = SigmaBoundsParameter.ActualValue;
135      if (sigmaBounds != null && sigmaBounds.Rows > 0) {
136        for (int i = 0; i < N; i++) {
137          var d = sigmaBounds[Math.Min(i, sigmaBounds.Rows - 1), 0];
138          if (d > sigma * minSqrtdiagC) sigma = d / minSqrtdiagC;
139        }
140        for (int i = 0; i < N; i++) {
141          var d = sigmaBounds[Math.Min(i, sigmaBounds.Rows - 1), 1];
142          if (d > sigma * maxSqrtdiagC) sigma = d / maxSqrtdiagC;
143        }
144      }
145      // end ensure ...
146
147      // testAndCorrectNumerics
148      double fac = 1;
149      if (D.Max() < 1e-6)
150        fac = 1.0 / D.Max();
151      else if (D.Min() > 1e4)
152        fac = 1.0 / D.Min();
153
154      if (fac != 1.0) {
155        sigma /= fac;
156        for (int i = 0; i < N; i++) {
157          pc[i] *= fac;
158          D[i] *= fac;
159          for (int j = 0; j < N; j++)
160            C[i, j] *= fac * fac;
161        }
162      }
163      // end testAndCorrectNumerics
164
165      var initialIterations = InitialIterationsParameter.ActualValue;
166      if (initialIterations == null) {
167        initialIterations = new IntValue(0);
168      }
169
170      double maxD = D.Max(), minD = D.Min();
171      if (minD == 0) sp.AxisRatio = double.PositiveInfinity;
172      else sp.AxisRatio = maxD / minD;
173      sp.PC = pc;
174      sp.PS = ps;
175      sp.B = B;
176      sp.D = D;
177      sp.C = C;
178      sp.BDz = BDz;
179      sp.Sigma = sigma;
180      if (sigmaBounds != null) {
181        sp.SigmaBounds = new double[sigmaBounds.Rows, sigmaBounds.Columns];
182        for (int i = 0; i < sigmaBounds.Rows; i++)
183          for (int j = 0; j < sigmaBounds.Columns; j++)
184            sp.SigmaBounds[i, j] = sigmaBounds[i, j];
185      }
186      sp.InitialIterations = initialIterations.Value;
187
188      StrategyParametersParameter.ActualValue = sp;
189      return base.Apply();
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.