[9129] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17097] | 3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9129] | 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 |
|
---|
| 22 | using HeuristicLab.Common;
|
---|
| 23 | using HeuristicLab.Core;
|
---|
| 24 | using HeuristicLab.Data;
|
---|
| 25 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
| 26 | using HeuristicLab.Operators;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Parameters;
|
---|
[17097] | 29 | using HEAL.Attic;
|
---|
[9129] | 30 | using System;
|
---|
| 31 | using System.Linq;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
|
---|
| 34 | [Item("CMAUpdater", "Updates the covariance matrix and strategy parameters of CMA-ES.")]
|
---|
[17097] | 35 | [StorableType("872D120A-1CBE-4C01-8250-E34BF8BBD9DA")]
|
---|
[11970] | 36 | public class CMAUpdater : SingleSuccessorOperator, ICMAUpdater, IIterationBasedOperator, ISingleObjectiveOperator {
|
---|
[9129] | 37 |
|
---|
| 38 | public Type CMAType {
|
---|
| 39 | get { return typeof(CMAParameters); }
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | #region Parameter Properties
|
---|
| 43 | public ILookupParameter<CMAParameters> StrategyParametersParameter {
|
---|
| 44 | get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public ILookupParameter<RealVector> MeanParameter {
|
---|
| 48 | get { return (ILookupParameter<RealVector>)Parameters["Mean"]; }
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | public ILookupParameter<RealVector> OldMeanParameter {
|
---|
| 52 | get { return (ILookupParameter<RealVector>)Parameters["OldMean"]; }
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public IScopeTreeLookupParameter<RealVector> OffspringParameter {
|
---|
| 56 | get { return (IScopeTreeLookupParameter<RealVector>)Parameters["Offspring"]; }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
|
---|
| 60 | get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | public ILookupParameter<IntValue> IterationsParameter {
|
---|
| 64 | get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 68 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public IValueLookupParameter<IntValue> MaximumEvaluatedSolutionsParameter {
|
---|
| 72 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public ILookupParameter<BoolValue> DegenerateStateParameter {
|
---|
| 76 | get { return (ILookupParameter<BoolValue>)Parameters["DegenerateState"]; }
|
---|
| 77 | }
|
---|
| 78 | #endregion
|
---|
| 79 |
|
---|
| 80 | [StorableConstructor]
|
---|
[17097] | 81 | protected CMAUpdater(StorableConstructorFlag _) : base(_) { }
|
---|
[9129] | 82 | protected CMAUpdater(CMAUpdater original, Cloner cloner) : base(original, cloner) { }
|
---|
| 83 | public CMAUpdater()
|
---|
| 84 | : base() {
|
---|
| 85 | Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The strategy parameters of CMA-ES."));
|
---|
| 86 | Parameters.Add(new LookupParameter<RealVector>("Mean", "The new mean."));
|
---|
| 87 | Parameters.Add(new LookupParameter<RealVector>("OldMean", "The old mean."));
|
---|
| 88 | Parameters.Add(new ScopeTreeLookupParameter<RealVector>("Offspring", "The created offspring solutions."));
|
---|
| 89 | Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the offspring."));
|
---|
| 90 | Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations passed."));
|
---|
| 91 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations."));
|
---|
| 92 | Parameters.Add(new ValueLookupParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions."));
|
---|
| 93 | Parameters.Add(new LookupParameter<BoolValue>("DegenerateState", "Whether the algorithm state has degenerated and should be terminated."));
|
---|
| 94 | MeanParameter.ActualName = "XMean";
|
---|
| 95 | OldMeanParameter.ActualName = "XOld";
|
---|
| 96 | OffspringParameter.ActualName = "RealVector";
|
---|
| 97 | }
|
---|
| 98 |
|
---|
| 99 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 100 | return new CMAUpdater(this, cloner);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | public override IOperation Apply() {
|
---|
| 104 | var iterations = IterationsParameter.ActualValue.Value;
|
---|
| 105 |
|
---|
| 106 | var xold = OldMeanParameter.ActualValue;
|
---|
| 107 | var xmean = MeanParameter.ActualValue;
|
---|
| 108 | var offspring = OffspringParameter.ActualValue;
|
---|
| 109 | var quality = QualityParameter.ActualValue;
|
---|
| 110 | var lambda = offspring.Length;
|
---|
| 111 |
|
---|
| 112 | var N = xmean.Length;
|
---|
| 113 | var sp = StrategyParametersParameter.ActualValue;
|
---|
| 114 |
|
---|
| 115 | #region Initialize default values for strategy parameter adjustment
|
---|
[9297] | 116 | if (sp.ChiN == 0) sp.ChiN = Math.Sqrt(N) * (1.0 - 1.0 / (4.0 * N) + 1.0 / (21.0 * N * N));
|
---|
| 117 | if (sp.MuEff == 0) sp.MuEff = sp.Weights.Sum() * sp.Weights.Sum() / sp.Weights.Sum(x => x * x);
|
---|
| 118 | if (sp.CS == 0) sp.CS = (sp.MuEff + 2) / (N + sp.MuEff + 3);
|
---|
| 119 | if (sp.Damps == 0) {
|
---|
[9129] | 120 | var maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
| 121 | var maxEvals = MaximumEvaluatedSolutionsParameter.ActualValue.Value;
|
---|
[9297] | 122 | sp.Damps = 2 * Math.Max(0, Math.Sqrt((sp.MuEff - 1) / (N + 1)) - 1)
|
---|
| 123 | * Math.Max(0.3, 1 - N / (1e-6 + Math.Min(maxIterations, maxEvals / lambda))) + sp.CS + 1;
|
---|
[9129] | 124 | }
|
---|
[9297] | 125 | if (sp.CC == 0) sp.CC = 4.0 / (N + 4);
|
---|
| 126 | if (sp.MuCov == 0) sp.MuCov = sp.MuEff;
|
---|
[9298] | 127 | if (sp.CCov == 0) sp.CCov = 2.0 / ((N + 1.41) * (N + 1.41) * sp.MuCov)
|
---|
| 128 | + (1 - (1.0 / sp.MuCov)) * Math.Min(1, (2 * sp.MuEff - 1) / (sp.MuEff + (N + 2) * (N + 2)));
|
---|
[9297] | 129 | if (sp.CCovSep == 0) sp.CCovSep = Math.Min(1, sp.CCov * (N + 1.5) / 3);
|
---|
[9129] | 130 | #endregion
|
---|
| 131 |
|
---|
| 132 | sp.QualityHistory.Enqueue(quality[0].Value);
|
---|
| 133 | while (sp.QualityHistory.Count > sp.QualityHistorySize && sp.QualityHistorySize >= 0)
|
---|
| 134 | sp.QualityHistory.Dequeue();
|
---|
| 135 |
|
---|
| 136 | for (int i = 0; i < N; i++) {
|
---|
[9297] | 137 | sp.BDz[i] = Math.Sqrt(sp.MuEff) * (xmean[i] - xold[i]) / sp.Sigma;
|
---|
[9129] | 138 | }
|
---|
| 139 |
|
---|
[9297] | 140 | if (sp.InitialIterations >= iterations) {
|
---|
[9129] | 141 | for (int i = 0; i < N; i++) {
|
---|
[9297] | 142 | sp.PS[i] = (1 - sp.CS) * sp.PS[i]
|
---|
| 143 | + Math.Sqrt(sp.CS * (2 - sp.CS)) * sp.BDz[i] / sp.D[i];
|
---|
[9129] | 144 | }
|
---|
| 145 | } else {
|
---|
| 146 | var artmp = new double[N];
|
---|
| 147 | for (int i = 0; i < N; i++) {
|
---|
| 148 | var sum = 0.0;
|
---|
| 149 | for (int j = 0; j < N; j++) {
|
---|
| 150 | sum += sp.B[j, i] * sp.BDz[j];
|
---|
| 151 | }
|
---|
| 152 | artmp[i] = sum / sp.D[i];
|
---|
| 153 | }
|
---|
| 154 | for (int i = 0; i < N; i++) {
|
---|
| 155 | var sum = 0.0;
|
---|
| 156 | for (int j = 0; j < N; j++) {
|
---|
| 157 | sum += sp.B[i, j] * artmp[j];
|
---|
| 158 | }
|
---|
[9297] | 159 | sp.PS[i] = (1 - sp.CS) * sp.PS[i] + Math.Sqrt(sp.CS * (2 - sp.CS)) * sum;
|
---|
[9129] | 160 | }
|
---|
| 161 | }
|
---|
| 162 | var normPS = Math.Sqrt(sp.PS.Select(x => x * x).Sum());
|
---|
[9297] | 163 | var hsig = normPS / Math.Sqrt(1 - Math.Pow(1 - sp.CS, 2 * iterations)) / sp.ChiN < 1.4 + 2.0 / (N + 1) ? 1.0 : 0.0;
|
---|
[9129] | 164 | for (int i = 0; i < sp.PC.Length; i++) {
|
---|
[9297] | 165 | sp.PC[i] = (1 - sp.CC) * sp.PC[i]
|
---|
| 166 | + hsig * Math.Sqrt(sp.CC * (2 - sp.CC)) * sp.BDz[i];
|
---|
[9129] | 167 | }
|
---|
| 168 |
|
---|
[9297] | 169 | if (sp.CCov > 0) {
|
---|
| 170 | if (sp.InitialIterations >= iterations) {
|
---|
[9129] | 171 | for (int i = 0; i < N; i++) {
|
---|
[9297] | 172 | sp.C[i, i] = (1 - sp.CCovSep) * sp.C[i, i]
|
---|
| 173 | + sp.CCov * (1 / sp.MuCov)
|
---|
| 174 | * (sp.PC[i] * sp.PC[i] + (1 - hsig) * sp.CC * (2 - sp.CC) * sp.C[i, i]);
|
---|
| 175 | for (int k = 0; k < sp.Mu; k++) {
|
---|
| 176 | sp.C[i, i] += sp.CCov * (1 - 1 / sp.MuCov) * sp.Weights[k] * (offspring[k][i] - xold[i]) *
|
---|
| 177 | (offspring[k][i] - xold[i]) / (sp.Sigma * sp.Sigma);
|
---|
[9129] | 178 | }
|
---|
| 179 | }
|
---|
| 180 | } else {
|
---|
| 181 | for (int i = 0; i < N; i++) {
|
---|
| 182 | for (int j = 0; j < N; j++) {
|
---|
[9297] | 183 | sp.C[i, j] = (1 - sp.CCov) * sp.C[i, j]
|
---|
| 184 | + sp.CCov * (1 / sp.MuCov)
|
---|
| 185 | * (sp.PC[i] * sp.PC[j] + (1 - hsig) * sp.CC * (2 - sp.CC) * sp.C[i, j]);
|
---|
| 186 | for (int k = 0; k < sp.Mu; k++) {
|
---|
| 187 | sp.C[i, j] += sp.CCov * (1 - 1 / sp.MuCov) * sp.Weights[k] * (offspring[k][i] - xold[i]) *
|
---|
| 188 | (offspring[k][j] - xold[j]) / (sp.Sigma * sp.Sigma);
|
---|
[9129] | 189 | }
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | }
|
---|
| 193 | }
|
---|
[9297] | 194 | sp.Sigma *= Math.Exp((sp.CS / sp.Damps) * (normPS / sp.ChiN - 1));
|
---|
[9129] | 195 |
|
---|
| 196 | double minSqrtdiagC = int.MaxValue, maxSqrtdiagC = int.MinValue;
|
---|
| 197 | for (int i = 0; i < N; i++) {
|
---|
| 198 | if (Math.Sqrt(sp.C[i, i]) < minSqrtdiagC) minSqrtdiagC = Math.Sqrt(sp.C[i, i]);
|
---|
| 199 | if (Math.Sqrt(sp.C[i, i]) > maxSqrtdiagC) maxSqrtdiagC = Math.Sqrt(sp.C[i, i]);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | // ensure maximal and minimal standard deviations
|
---|
[9297] | 203 | if (sp.SigmaBounds != null && sp.SigmaBounds.GetLength(0) > 0) {
|
---|
[9129] | 204 | for (int i = 0; i < N; i++) {
|
---|
[9297] | 205 | var d = sp.SigmaBounds[Math.Min(i, sp.SigmaBounds.GetLength(0) - 1), 0];
|
---|
| 206 | if (d > sp.Sigma * minSqrtdiagC) sp.Sigma = d / minSqrtdiagC;
|
---|
[9129] | 207 | }
|
---|
| 208 | for (int i = 0; i < N; i++) {
|
---|
[9297] | 209 | var d = sp.SigmaBounds[Math.Min(i, sp.SigmaBounds.GetLength(0) - 1), 1];
|
---|
| 210 | if (d > sp.Sigma * maxSqrtdiagC) sp.Sigma = d / maxSqrtdiagC;
|
---|
[9129] | 211 | }
|
---|
| 212 | }
|
---|
| 213 | // end ensure ...
|
---|
| 214 |
|
---|
| 215 | // testAndCorrectNumerics
|
---|
| 216 | double fac = 1;
|
---|
| 217 | if (sp.D.Max() < 1e-6)
|
---|
| 218 | fac = 1.0 / sp.D.Max();
|
---|
| 219 | else if (sp.D.Min() > 1e4)
|
---|
| 220 | fac = 1.0 / sp.D.Min();
|
---|
| 221 |
|
---|
| 222 | if (fac != 1.0) {
|
---|
[9297] | 223 | sp.Sigma /= fac;
|
---|
[9129] | 224 | for (int i = 0; i < N; i++) {
|
---|
| 225 | sp.PC[i] *= fac;
|
---|
| 226 | sp.D[i] *= fac;
|
---|
| 227 | for (int j = 0; j < N; j++)
|
---|
| 228 | sp.C[i, j] *= fac * fac;
|
---|
| 229 | }
|
---|
| 230 | }
|
---|
| 231 | // end testAndCorrectNumerics
|
---|
| 232 |
|
---|
[9148] | 233 |
|
---|
[9297] | 234 | if (sp.InitialIterations >= iterations) {
|
---|
[9129] | 235 | for (int i = 0; i < N; i++)
|
---|
| 236 | sp.D[i] = Math.Sqrt(sp.C[i, i]);
|
---|
[9148] | 237 | DegenerateStateParameter.ActualValue = new BoolValue(false);
|
---|
[9129] | 238 | } else {
|
---|
[9148] | 239 |
|
---|
[12629] | 240 | double[] d;
|
---|
| 241 | double[,] b;
|
---|
| 242 | var success = alglib.smatrixevd(sp.C, N, 1, true, out d, out b);
|
---|
[12630] | 243 | sp.D = d;
|
---|
| 244 | sp.B = b;
|
---|
[9291] | 245 |
|
---|
[9129] | 246 | DegenerateStateParameter.ActualValue = new BoolValue(!success);
|
---|
| 247 |
|
---|
[9297] | 248 | // assign D to eigenvalue square roots
|
---|
| 249 | for (int i = 0; i < N; i++) {
|
---|
[13175] | 250 | if (sp.D[i] <= 0) { // numerical problem?
|
---|
[9148] | 251 | DegenerateStateParameter.ActualValue.Value = true;
|
---|
| 252 | sp.D[i] = 0;
|
---|
[9297] | 253 | } else sp.D[i] = Math.Sqrt(sp.D[i]);
|
---|
| 254 | }
|
---|
[9129] | 255 |
|
---|
[9297] | 256 | if (sp.D.Min() == 0.0) sp.AxisRatio = double.PositiveInfinity;
|
---|
| 257 | else sp.AxisRatio = sp.D.Max() / sp.D.Min();
|
---|
[9129] | 258 | }
|
---|
| 259 | return base.Apply();
|
---|
| 260 | }
|
---|
| 261 | }
|
---|
| 262 | } |
---|