Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/Terminator.cs @ 11290

Last change on this file since 11290 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: 9.4 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.Algorithms.CMAEvolutionStrategy {
33  [Item("Terminator", "Decides if the algorithm should terminate or not.")]
34  [StorableClass]
35  public class Terminator : Operator, IIterationBasedOperator {
36
37    protected OperatorParameter ContinueParameter {
38      get { return (OperatorParameter)Parameters["Continue"]; }
39    }
40    protected OperatorParameter TerminateParameter {
41      get { return (OperatorParameter)Parameters["Terminate"]; }
42    }
43
44    public IOperator Continue {
45      get { return ContinueParameter.Value; }
46      set { ContinueParameter.Value = value; }
47    }
48
49    public IOperator Terminate {
50      get { return TerminateParameter.Value; }
51      set { TerminateParameter.Value = value; }
52    }
53
54    public IValueLookupParameter<BoolValue> MaximizationParameter {
55      get { return (IValueLookupParameter<BoolValue>)Parameters["Maximization"]; }
56    }
57
58    public ILookupParameter<CMAParameters> StrategyParametersParameter {
59      get { return (ILookupParameter<CMAParameters>)Parameters["StrategyParameters"]; }
60    }
61
62    public ILookupParameter<IntValue> IterationsParameter {
63      get { return (ILookupParameter<IntValue>)Parameters["Iterations"]; }
64    }
65
66    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
67      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
68    }
69
70    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
71      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
72    }
73
74    public IValueLookupParameter<IntValue> MaximumEvaluatedSolutionsParameter {
75      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
76    }
77
78    public IScopeTreeLookupParameter<DoubleValue> QualityParameter {
79      get { return (IScopeTreeLookupParameter<DoubleValue>)Parameters["Quality"]; }
80    }
81
82    public IValueLookupParameter<DoubleValue> TargetQualityParameter {
83      get { return (IValueLookupParameter<DoubleValue>)Parameters["TargetQuality"]; }
84    }
85
86    public IValueLookupParameter<DoubleValue> MinimumQualityChangeParameter {
87      get { return (IValueLookupParameter<DoubleValue>)Parameters["MinimumQualityChange"]; }
88    }
89
90    public IValueLookupParameter<DoubleValue> MinimumQualityHistoryChangeParameter {
91      get { return (IValueLookupParameter<DoubleValue>)Parameters["MinimumQualityHistoryChange"]; }
92    }
93
94    public IValueLookupParameter<DoubleValue> MinimumStandardDeviationParameter {
95      get { return (IValueLookupParameter<DoubleValue>)Parameters["MinimumStandardDeviation"]; }
96    }
97
98    public ILookupParameter<DoubleArray> InitialSigmaParameter {
99      get { return (ILookupParameter<DoubleArray>)Parameters["InitialSigma"]; }
100    }
101
102    public IValueLookupParameter<DoubleValue> MaximumStandardDeviationChangeParameter {
103      get { return (IValueLookupParameter<DoubleValue>)Parameters["MaximumStandardDeviationChange"]; }
104    }
105
106    public ILookupParameter<BoolValue> DegenerateStateParameter {
107      get { return (ILookupParameter<BoolValue>)Parameters["DegenerateState"]; }
108    }
109
110    [StorableConstructor]
111    protected Terminator(bool deserializing) : base(deserializing) { }
112    protected Terminator(Terminator original, Cloner cloner)
113      : base(original, cloner) { }
114    public Terminator() {
115      Parameters.Add(new OperatorParameter("Continue", "The operator that is executed if the stop conditions have not been met!"));
116      Parameters.Add(new OperatorParameter("Terminate", "The operator that is executed if the stop conditions have been met!"));
117      Parameters.Add(new ValueLookupParameter<BoolValue>("Maximization", "True if the problem is to be maximized and false otherwise."));
118      Parameters.Add(new LookupParameter<CMAParameters>("StrategyParameters", "The CMA-ES strategy parameters."));
119      Parameters.Add(new LookupParameter<IntValue>("Iterations", "The number of iterations passed."));
120      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations."));
121      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solutions."));
122      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions."));
123      Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>("Quality", "The quality of the offspring."));
124      Parameters.Add(new ValueLookupParameter<DoubleValue>("TargetQuality", "(stopFitness) Surpassing this quality value terminates the algorithm."));
125      Parameters.Add(new ValueLookupParameter<DoubleValue>("MinimumQualityChange", "(stopTolFun) If the range of fitness values is less than a certain value the algorithm terminates (set to 0 or positive value to enable)."));
126      Parameters.Add(new ValueLookupParameter<DoubleValue>("MinimumQualityHistoryChange", "(stopTolFunHist) If the range of fitness values is less than a certain value for a certain time the algorithm terminates (set to 0 or positive to enable)."));
127      Parameters.Add(new ValueLookupParameter<DoubleValue>("MinimumStandardDeviation", "(stopTolXFactor) If the standard deviation falls below a certain value the algorithm terminates (set to 0 or positive to enable)."));
128      Parameters.Add(new LookupParameter<DoubleArray>("InitialSigma", "The initial value for Sigma."));
129      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumStandardDeviationChange", "(stopTolUpXFactor) If the standard deviation changes by a value larger than this parameter the algorithm stops (set to a value > 0 to enable)."));
130      Parameters.Add(new LookupParameter<BoolValue>("DegenerateState", "Whether the algorithm state has degenerated and should be terminated."));
131    }
132
133    public override IDeepCloneable Clone(Cloner cloner) {
134      return new Terminator(this, cloner);
135    }
136
137    public override IOperation Apply() {
138      var terminateOp = Terminate != null ? ExecutionContext.CreateOperation(Terminate) : null;
139
140      var degenerated = DegenerateStateParameter.ActualValue.Value;
141      if (degenerated) return terminateOp;
142
143      var iterations = IterationsParameter.ActualValue.Value;
144      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
145      if (iterations >= maxIterations) return terminateOp;
146
147      var evals = EvaluatedSolutionsParameter.ActualValue.Value;
148      var maxEvals = MaximumEvaluatedSolutionsParameter.ActualValue.Value;
149      if (evals >= maxEvals) return terminateOp;
150
151      var maximization = MaximizationParameter.ActualValue.Value;
152      var bestQuality = QualityParameter.ActualValue.First().Value;
153      var targetQuality = TargetQualityParameter.ActualValue.Value;
154      if (iterations > 1 && (maximization && bestQuality >= targetQuality
155        || !maximization && bestQuality <= targetQuality)) return terminateOp;
156
157      var sp = StrategyParametersParameter.ActualValue;
158      var worstQuality = QualityParameter.ActualValue.Last().Value;
159      var minHist = sp.QualityHistory.Min();
160      var maxHist = sp.QualityHistory.Max();
161      var change = Math.Max(maxHist, Math.Max(bestQuality, worstQuality))
162                   - Math.Min(minHist, Math.Min(bestQuality, worstQuality));
163      var stopTolFun = MinimumQualityChangeParameter.ActualValue.Value;
164      if (change <= stopTolFun) return terminateOp;
165
166      if (iterations > sp.QualityHistorySize &&
167          maxHist - minHist <= MinimumQualityHistoryChangeParameter.ActualValue.Value)
168        return terminateOp;
169
170      double minSqrtdiagC = int.MaxValue, maxSqrtdiagC = int.MinValue;
171      for (int i = 0; i < sp.C.GetLength(0); i++) {
172        if (Math.Sqrt(sp.C[i, i]) < minSqrtdiagC) minSqrtdiagC = Math.Sqrt(sp.C[i, i]);
173        if (Math.Sqrt(sp.C[i, i]) > maxSqrtdiagC) maxSqrtdiagC = Math.Sqrt(sp.C[i, i]);
174      }
175
176      var tolx = MinimumStandardDeviationParameter.ActualValue.Value;
177      if (sp.Sigma * maxSqrtdiagC < tolx
178          && sp.Sigma * sp.PC.Select(x => Math.Abs(x)).Max() < tolx) return terminateOp;
179
180      var stopTolUpXFactor = MaximumStandardDeviationChangeParameter.ActualValue.Value;
181      if (sp.Sigma * maxSqrtdiagC > stopTolUpXFactor * InitialSigmaParameter.ActualValue.Max())
182        return terminateOp;
183
184      return Continue != null ? ExecutionContext.CreateOperation(Continue) : null;
185    }
186  }
187}
Note: See TracBrowser for help on using the repository browser.