1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Analysis;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
29 | using HeuristicLab.Operators;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Optimization.Operators;
|
---|
32 | using HeuristicLab.Parameters;
|
---|
33 | using HEAL.Attic;
|
---|
34 | using HeuristicLab.PluginInfrastructure;
|
---|
35 | using HeuristicLab.Random;
|
---|
36 |
|
---|
37 | namespace HeuristicLab.Algorithms.CMAEvolutionStrategy {
|
---|
38 | [Item("CMA Evolution Strategy (CMAES)", "An evolution strategy based on covariance matrix adaptation.")]
|
---|
39 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 210)]
|
---|
40 | [StorableType("92CB2910-2D63-49A4-94DE-54DBA0CF2180")]
|
---|
41 | public sealed class CMAEvolutionStrategy : HeuristicOptimizationEngineAlgorithm, IStorableContent {
|
---|
42 | public string Filename { get; set; }
|
---|
43 | #region Strings
|
---|
44 | private const string SeedName = "Seed";
|
---|
45 | private const string SetSeedRandomlyName = "SetSeedRandomly";
|
---|
46 | private const string PopulationSizeName = "PopulationSize";
|
---|
47 | private const string InitialIterationsName = "InitialIterations";
|
---|
48 | private const string InitialSigmaName = "InitialSigma";
|
---|
49 | private const string MuName = "Mu";
|
---|
50 | private const string CMAInitializerName = "CMAInitializer";
|
---|
51 | private const string CMAMutatorName = "CMAMutator";
|
---|
52 | private const string CMARecombinatorName = "CMARecombinator";
|
---|
53 | private const string CMAUpdaterName = "CMAUpdater";
|
---|
54 | private const string AnalyzerName = "Analyzer";
|
---|
55 | private const string MaximumGenerationsName = "MaximumGenerations";
|
---|
56 | private const string MaximumEvaluatedSolutionsName = "MaximumEvaluatedSolutions";
|
---|
57 | private const string TargetQualityName = "TargetQuality";
|
---|
58 | private const string MinimumQualityChangeName = "MinimumQualityChange";
|
---|
59 | private const string MinimumQualityHistoryChangeName = "MinimumQualityHistoryChange";
|
---|
60 | private const string MinimumStandardDeviationName = "MinimumStandardDeviation";
|
---|
61 | private const string MaximumStandardDeviationChangeName = "MaximumStandardDeviationChange";
|
---|
62 | #endregion
|
---|
63 |
|
---|
64 | #region Problem Properties
|
---|
65 | public override Type ProblemType {
|
---|
66 | get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
|
---|
67 | }
|
---|
68 | public new ISingleObjectiveHeuristicOptimizationProblem Problem {
|
---|
69 | get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
|
---|
70 | set { base.Problem = value; }
|
---|
71 | }
|
---|
72 | #endregion
|
---|
73 |
|
---|
74 | #region Parameter Properties
|
---|
75 | public IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
76 | get { return (IValueParameter<MultiAnalyzer>)Parameters[AnalyzerName]; }
|
---|
77 | }
|
---|
78 | private IFixedValueParameter<IntValue> SeedParameter {
|
---|
79 | get { return (IFixedValueParameter<IntValue>)Parameters[SeedName]; }
|
---|
80 | }
|
---|
81 | private IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
82 | get { return (IFixedValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
|
---|
83 | }
|
---|
84 | private IFixedValueParameter<IntValue> PopulationSizeParameter {
|
---|
85 | get { return (IFixedValueParameter<IntValue>)Parameters[PopulationSizeName]; }
|
---|
86 | }
|
---|
87 | private IFixedValueParameter<IntValue> InitialIterationsParameter {
|
---|
88 | get { return (IFixedValueParameter<IntValue>)Parameters[InitialIterationsName]; }
|
---|
89 | }
|
---|
90 | public IValueParameter<DoubleArray> InitialSigmaParameter {
|
---|
91 | get { return (IValueParameter<DoubleArray>)Parameters[InitialSigmaName]; }
|
---|
92 | }
|
---|
93 | private OptionalValueParameter<IntValue> MuParameter {
|
---|
94 | get { return (OptionalValueParameter<IntValue>)Parameters[MuName]; }
|
---|
95 | }
|
---|
96 | public IConstrainedValueParameter<ICMAInitializer> CMAInitializerParameter {
|
---|
97 | get { return (IConstrainedValueParameter<ICMAInitializer>)Parameters[CMAInitializerName]; }
|
---|
98 | }
|
---|
99 | public IConstrainedValueParameter<ICMAManipulator> CMAMutatorParameter {
|
---|
100 | get { return (IConstrainedValueParameter<ICMAManipulator>)Parameters[CMAMutatorName]; }
|
---|
101 | }
|
---|
102 | public IConstrainedValueParameter<ICMARecombinator> CMARecombinatorParameter {
|
---|
103 | get { return (IConstrainedValueParameter<ICMARecombinator>)Parameters[CMARecombinatorName]; }
|
---|
104 | }
|
---|
105 | public IConstrainedValueParameter<ICMAUpdater> CMAUpdaterParameter {
|
---|
106 | get { return (IConstrainedValueParameter<ICMAUpdater>)Parameters[CMAUpdaterName]; }
|
---|
107 | }
|
---|
108 | private IFixedValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
109 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
|
---|
110 | }
|
---|
111 | private IFixedValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
|
---|
112 | get { return (IFixedValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsName]; }
|
---|
113 | }
|
---|
114 | private IFixedValueParameter<DoubleValue> TargetQualityParameter {
|
---|
115 | get { return (IFixedValueParameter<DoubleValue>)Parameters[TargetQualityName]; }
|
---|
116 | }
|
---|
117 | private IFixedValueParameter<DoubleValue> MinimumQualityChangeParameter {
|
---|
118 | get { return (IFixedValueParameter<DoubleValue>)Parameters[MinimumQualityChangeName]; }
|
---|
119 | }
|
---|
120 | private IFixedValueParameter<DoubleValue> MinimumQualityHistoryChangeParameter {
|
---|
121 | get { return (IFixedValueParameter<DoubleValue>)Parameters[MinimumQualityHistoryChangeName]; }
|
---|
122 | }
|
---|
123 | private IFixedValueParameter<DoubleValue> MinimumStandardDeviationParameter {
|
---|
124 | get { return (IFixedValueParameter<DoubleValue>)Parameters[MinimumStandardDeviationName]; }
|
---|
125 | }
|
---|
126 | private IFixedValueParameter<DoubleValue> MaximumStandardDeviationChangeParameter {
|
---|
127 | get { return (IFixedValueParameter<DoubleValue>)Parameters[MaximumStandardDeviationChangeName]; }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region Properties
|
---|
132 | public int Seed {
|
---|
133 | get { return SeedParameter.Value.Value; }
|
---|
134 | set { SeedParameter.Value.Value = value; }
|
---|
135 | }
|
---|
136 | public bool SetSeedRandomly {
|
---|
137 | get { return SetSeedRandomlyParameter.Value.Value; }
|
---|
138 | set { SetSeedRandomlyParameter.Value.Value = value; }
|
---|
139 | }
|
---|
140 | public int PopulationSize {
|
---|
141 | get { return PopulationSizeParameter.Value.Value; }
|
---|
142 | set { PopulationSizeParameter.Value.Value = value; }
|
---|
143 | }
|
---|
144 | public int InitialIterations {
|
---|
145 | get { return InitialIterationsParameter.Value.Value; }
|
---|
146 | set { InitialIterationsParameter.Value.Value = value; }
|
---|
147 | }
|
---|
148 | public int MaximumGenerations {
|
---|
149 | get { return MaximumGenerationsParameter.Value.Value; }
|
---|
150 | set { MaximumGenerationsParameter.Value.Value = value; }
|
---|
151 | }
|
---|
152 | public int MaximumEvaluatedSolutions {
|
---|
153 | get { return MaximumEvaluatedSolutionsParameter.Value.Value; }
|
---|
154 | set { MaximumEvaluatedSolutionsParameter.Value.Value = value; }
|
---|
155 | }
|
---|
156 | public double TargetQuality {
|
---|
157 | get { return TargetQualityParameter.Value.Value; }
|
---|
158 | set { TargetQualityParameter.Value.Value = value; }
|
---|
159 | }
|
---|
160 | public double MinimumQualityChange {
|
---|
161 | get { return MinimumQualityChangeParameter.Value.Value; }
|
---|
162 | set { MinimumQualityChangeParameter.Value.Value = value; }
|
---|
163 | }
|
---|
164 | public double MinimumQualityHistoryChange {
|
---|
165 | get { return MinimumQualityHistoryChangeParameter.Value.Value; }
|
---|
166 | set { MinimumQualityHistoryChangeParameter.Value.Value = value; }
|
---|
167 | }
|
---|
168 | public double MinimumStandardDeviation {
|
---|
169 | get { return MinimumStandardDeviationParameter.Value.Value; }
|
---|
170 | set { MinimumStandardDeviationParameter.Value.Value = value; }
|
---|
171 | }
|
---|
172 | public double MaximumStandardDeviationChange {
|
---|
173 | get { return MaximumStandardDeviationChangeParameter.Value.Value; }
|
---|
174 | set { MaximumStandardDeviationChangeParameter.Value.Value = value; }
|
---|
175 | }
|
---|
176 | public DoubleArray InitialSigma {
|
---|
177 | get { return InitialSigmaParameter.Value; }
|
---|
178 | set { InitialSigmaParameter.Value = value; }
|
---|
179 | }
|
---|
180 | public IntValue Mu {
|
---|
181 | get { return MuParameter.Value; }
|
---|
182 | set { MuParameter.Value = value; }
|
---|
183 | }
|
---|
184 | public ICMAInitializer CMAInitializer {
|
---|
185 | get { return CMAInitializerParameter.Value; }
|
---|
186 | set { CMAInitializerParameter.Value = value; }
|
---|
187 | }
|
---|
188 | public ICMAManipulator CMAMutator {
|
---|
189 | get { return CMAMutatorParameter.Value; }
|
---|
190 | set { CMAMutatorParameter.Value = value; }
|
---|
191 | }
|
---|
192 | public ICMARecombinator CMARecombinator {
|
---|
193 | get { return CMARecombinatorParameter.Value; }
|
---|
194 | set { CMARecombinatorParameter.Value = value; }
|
---|
195 | }
|
---|
196 | public MultiAnalyzer Analyzer {
|
---|
197 | get { return AnalyzerParameter.Value; }
|
---|
198 | set { AnalyzerParameter.Value = value; }
|
---|
199 | }
|
---|
200 | public ICMAUpdater CMAUpdater {
|
---|
201 | get { return CMAUpdaterParameter.Value; }
|
---|
202 | set { CMAUpdaterParameter.Value = value; }
|
---|
203 | }
|
---|
204 |
|
---|
205 | private RandomCreator RandomCreator {
|
---|
206 | get { return (RandomCreator)OperatorGraph.InitialOperator; }
|
---|
207 | }
|
---|
208 |
|
---|
209 | [Storable]
|
---|
210 | private BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
211 | [Storable]
|
---|
212 | private CMAAnalyzer cmaAnalyzer;
|
---|
213 | [Storable]
|
---|
214 | private Placeholder solutionCreator;
|
---|
215 | [Storable]
|
---|
216 | private Placeholder populationSolutionCreator;
|
---|
217 | [Storable]
|
---|
218 | private Placeholder evaluator;
|
---|
219 | [Storable]
|
---|
220 | private SubScopesSorter sorter;
|
---|
221 | [Storable]
|
---|
222 | private Terminator terminator;
|
---|
223 | #endregion
|
---|
224 |
|
---|
225 | [StorableConstructor]
|
---|
226 | private CMAEvolutionStrategy(StorableConstructorFlag _) : base(_) { }
|
---|
227 | private CMAEvolutionStrategy(CMAEvolutionStrategy original, Cloner cloner)
|
---|
228 | : base(original, cloner) {
|
---|
229 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
230 | cmaAnalyzer = cloner.Clone(original.cmaAnalyzer);
|
---|
231 | solutionCreator = cloner.Clone(original.solutionCreator);
|
---|
232 | populationSolutionCreator = cloner.Clone(original.populationSolutionCreator);
|
---|
233 | evaluator = cloner.Clone(original.evaluator);
|
---|
234 | sorter = cloner.Clone(original.sorter);
|
---|
235 | terminator = cloner.Clone(original.terminator);
|
---|
236 | RegisterEventHandlers();
|
---|
237 | }
|
---|
238 | public CMAEvolutionStrategy()
|
---|
239 | : base() {
|
---|
240 | Parameters.Add(new FixedValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
241 | Parameters.Add(new FixedValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
242 | Parameters.Add(new FixedValueParameter<IntValue>(PopulationSizeName, "λ (lambda) - the size of the offspring population.", new IntValue(20)));
|
---|
243 | Parameters.Add(new FixedValueParameter<IntValue>(InitialIterationsName, "The number of iterations that should be performed with only axis parallel mutation.", new IntValue(0)));
|
---|
244 | Parameters.Add(new FixedValueParameter<DoubleArray>(InitialSigmaName, "The initial sigma can be a single value or a value for each dimension. All values need to be > 0.", new DoubleArray(new[] { 0.5 })));
|
---|
245 | Parameters.Add(new OptionalValueParameter<IntValue>(MuName, "Optional, the mu best offspring that should be considered for update of the new mean and strategy parameters. If not given it will be automatically calculated."));
|
---|
246 | Parameters.Add(new ConstrainedValueParameter<ICMARecombinator>(CMARecombinatorName, "The operator used to calculate the new mean."));
|
---|
247 | Parameters.Add(new ConstrainedValueParameter<ICMAManipulator>(CMAMutatorName, "The operator used to manipulate a point."));
|
---|
248 | Parameters.Add(new ConstrainedValueParameter<ICMAInitializer>(CMAInitializerName, "The operator that initializes the covariance matrix and strategy parameters."));
|
---|
249 | Parameters.Add(new ConstrainedValueParameter<ICMAUpdater>(CMAUpdaterName, "The operator that updates the covariance matrix and strategy parameters."));
|
---|
250 | Parameters.Add(new ValueParameter<MultiAnalyzer>(AnalyzerName, "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
251 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
252 | Parameters.Add(new FixedValueParameter<IntValue>(MaximumEvaluatedSolutionsName, "The maximum number of evaluated solutions that should be computed.", new IntValue(int.MaxValue)));
|
---|
253 | Parameters.Add(new FixedValueParameter<DoubleValue>(TargetQualityName, "(stopFitness) Surpassing this quality value terminates the algorithm.", new DoubleValue(double.NaN)));
|
---|
254 | Parameters.Add(new FixedValueParameter<DoubleValue>(MinimumQualityChangeName, "(stopTolFun) If the range of fitness values is less than a certain value the algorithm terminates (set to 0 or positive value to enable).", new DoubleValue(double.NaN)));
|
---|
255 | Parameters.Add(new FixedValueParameter<DoubleValue>(MinimumQualityHistoryChangeName, "(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).", new DoubleValue(double.NaN)));
|
---|
256 | Parameters.Add(new FixedValueParameter<DoubleValue>(MinimumStandardDeviationName, "(stopTolXFactor) If the standard deviation falls below a certain value the algorithm terminates (set to 0 or positive to enable).", new DoubleValue(double.NaN)));
|
---|
257 | Parameters.Add(new FixedValueParameter<DoubleValue>(MaximumStandardDeviationChangeName, "(stopTolUpXFactor) If the standard deviation changes by a value larger than this parameter the algorithm stops (set to a value > 0 to enable).", new DoubleValue(double.NaN)));
|
---|
258 |
|
---|
259 | var randomCreator = new RandomCreator();
|
---|
260 | var variableCreator = new VariableCreator();
|
---|
261 | var resultsCollector = new ResultsCollector();
|
---|
262 | var cmaInitializer = new Placeholder();
|
---|
263 | solutionCreator = new Placeholder();
|
---|
264 | var subScopesCreator = new SubScopesCreator();
|
---|
265 | var ussp1 = new UniformSubScopesProcessor();
|
---|
266 | populationSolutionCreator = new Placeholder();
|
---|
267 | var cmaMutator = new Placeholder();
|
---|
268 | var ussp2 = new UniformSubScopesProcessor();
|
---|
269 | evaluator = new Placeholder();
|
---|
270 | var subScopesCounter = new SubScopesCounter();
|
---|
271 | sorter = new SubScopesSorter();
|
---|
272 | var analyzer = new Placeholder();
|
---|
273 | var cmaRecombinator = new Placeholder();
|
---|
274 | var generationsCounter = new IntCounter();
|
---|
275 | var cmaUpdater = new Placeholder();
|
---|
276 | terminator = new Terminator();
|
---|
277 |
|
---|
278 | OperatorGraph.InitialOperator = randomCreator;
|
---|
279 |
|
---|
280 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
281 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
282 | randomCreator.SeedParameter.Value = null;
|
---|
283 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
284 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
285 | randomCreator.Successor = variableCreator;
|
---|
286 |
|
---|
287 | variableCreator.Name = "Initialize Variables";
|
---|
288 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
289 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
290 | variableCreator.Successor = resultsCollector;
|
---|
291 |
|
---|
292 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
|
---|
293 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
294 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
295 | resultsCollector.Successor = cmaInitializer;
|
---|
296 |
|
---|
297 | cmaInitializer.Name = "Initialize Strategy Parameters";
|
---|
298 | cmaInitializer.OperatorParameter.ActualName = CMAInitializerParameter.Name;
|
---|
299 | cmaInitializer.Successor = subScopesCreator;
|
---|
300 |
|
---|
301 | subScopesCreator.NumberOfSubScopesParameter.ActualName = PopulationSizeParameter.Name;
|
---|
302 | subScopesCreator.Successor = ussp1;
|
---|
303 |
|
---|
304 | ussp1.Name = "Create population";
|
---|
305 | ussp1.Parallel = new BoolValue(false);
|
---|
306 | ussp1.Operator = populationSolutionCreator;
|
---|
307 | ussp1.Successor = solutionCreator;
|
---|
308 |
|
---|
309 | populationSolutionCreator.Name = "Initialize arx";
|
---|
310 | // populationSolutionCreator.OperatorParameter will be wired
|
---|
311 | populationSolutionCreator.Successor = null;
|
---|
312 |
|
---|
313 | solutionCreator.Name = "Initialize xmean";
|
---|
314 | // solutionCreator.OperatorParameter will be wired
|
---|
315 | solutionCreator.Successor = cmaMutator;
|
---|
316 |
|
---|
317 | cmaMutator.Name = "Sample population";
|
---|
318 | cmaMutator.OperatorParameter.ActualName = CMAMutatorParameter.Name;
|
---|
319 | cmaMutator.Successor = ussp2;
|
---|
320 |
|
---|
321 | ussp2.Name = "Evaluate offspring";
|
---|
322 | ussp2.Parallel = new BoolValue(true);
|
---|
323 | ussp2.Operator = evaluator;
|
---|
324 | ussp2.Successor = subScopesCounter;
|
---|
325 |
|
---|
326 | evaluator.Name = "Evaluator";
|
---|
327 | // evaluator.OperatorParameter will be wired
|
---|
328 | evaluator.Successor = null;
|
---|
329 |
|
---|
330 | subScopesCounter.Name = "Count EvaluatedSolutions";
|
---|
331 | subScopesCounter.AccumulateParameter.Value = new BoolValue(true);
|
---|
332 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
333 | subScopesCounter.Successor = sorter;
|
---|
334 |
|
---|
335 | // sorter.ValueParameter will be wired
|
---|
336 | // sorter.DescendingParameter will be wired
|
---|
337 | sorter.Successor = analyzer;
|
---|
338 |
|
---|
339 | analyzer.Name = "Analyzer";
|
---|
340 | analyzer.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
341 | analyzer.Successor = cmaRecombinator;
|
---|
342 |
|
---|
343 | cmaRecombinator.Name = "Create new xmean";
|
---|
344 | cmaRecombinator.OperatorParameter.ActualName = CMARecombinatorParameter.Name;
|
---|
345 | cmaRecombinator.Successor = generationsCounter;
|
---|
346 |
|
---|
347 | generationsCounter.Name = "Generations++";
|
---|
348 | generationsCounter.IncrementParameter.Value = new IntValue(1);
|
---|
349 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
350 | generationsCounter.Successor = cmaUpdater;
|
---|
351 |
|
---|
352 | cmaUpdater.Name = "Update distributions";
|
---|
353 | cmaUpdater.OperatorParameter.ActualName = CMAUpdaterParameter.Name;
|
---|
354 | cmaUpdater.Successor = terminator;
|
---|
355 |
|
---|
356 | terminator.Continue = cmaMutator;
|
---|
357 | terminator.Terminate = null;
|
---|
358 |
|
---|
359 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
360 | cmaAnalyzer = new CMAAnalyzer();
|
---|
361 |
|
---|
362 | InitializeOperators();
|
---|
363 | RegisterEventHandlers();
|
---|
364 | Parameterize();
|
---|
365 | }
|
---|
366 |
|
---|
367 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
368 | return new CMAEvolutionStrategy(this, cloner);
|
---|
369 | }
|
---|
370 |
|
---|
371 | [StorableHook(HookType.AfterDeserialization)]
|
---|
372 | private void AfterDeserialization() {
|
---|
373 | RegisterEventHandlers();
|
---|
374 | }
|
---|
375 |
|
---|
376 | public override void Prepare() {
|
---|
377 | if (Problem != null) base.Prepare();
|
---|
378 | }
|
---|
379 |
|
---|
380 | protected override void OnStarted() {
|
---|
381 | if (!(Problem.SolutionCreator is IRealVectorCreator))
|
---|
382 | throw new InvalidOperationException("Problems that do not use RealVectorEncoding cannot be solved by CMA-ES.");
|
---|
383 | base.OnStarted();
|
---|
384 | }
|
---|
385 |
|
---|
386 | #region Events
|
---|
387 | protected override void OnProblemChanged() {
|
---|
388 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
389 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
390 | if (creator != null) {
|
---|
391 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
392 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
393 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
394 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
395 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
396 | }
|
---|
397 | UpdateOperators();
|
---|
398 | UpdateAnalyzers();
|
---|
399 | Parameterize();
|
---|
400 | base.OnProblemChanged();
|
---|
401 | }
|
---|
402 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
403 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
404 | if (creator != null) {
|
---|
405 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
406 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
407 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
408 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
409 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
410 | }
|
---|
411 | Parameterize();
|
---|
412 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
413 | }
|
---|
414 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
415 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
416 | Parameterize();
|
---|
417 | base.Problem_EvaluatorChanged(sender, e);
|
---|
418 | }
|
---|
419 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
420 | UpdateOperators();
|
---|
421 | UpdateAnalyzers();
|
---|
422 | Parameterize();
|
---|
423 | base.Problem_OperatorsChanged(sender, e);
|
---|
424 | }
|
---|
425 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
426 | Parameterize();
|
---|
427 | }
|
---|
428 | private bool cmaesInitializerSync;
|
---|
429 | private void CMAESInitializerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
430 | if (cmaesInitializerSync) return;
|
---|
431 | UpdateOperators();
|
---|
432 | Parameterize();
|
---|
433 | }
|
---|
434 | private void RealVectorCreator_Changed(object sender, EventArgs e) {
|
---|
435 | Parameterize();
|
---|
436 | }
|
---|
437 | #endregion
|
---|
438 |
|
---|
439 | #region Helpers
|
---|
440 | private void RegisterEventHandlers() {
|
---|
441 | CMAInitializerParameter.ValueChanged += CMAESInitializerParameter_ValueChanged;
|
---|
442 | if (Problem != null) {
|
---|
443 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
444 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
445 | if (creator != null) {
|
---|
446 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
447 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
448 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
449 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
450 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
451 | }
|
---|
452 | }
|
---|
453 | }
|
---|
454 | private void InitializeOperators() {
|
---|
455 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAInitializer>())
|
---|
456 | CMAInitializerParameter.ValidValues.Add(op);
|
---|
457 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAManipulator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
458 | CMAMutatorParameter.ValidValues.Add(op);
|
---|
459 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMARecombinator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
460 | CMARecombinatorParameter.ValidValues.Add(op);
|
---|
461 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAUpdater>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
462 | CMAUpdaterParameter.ValidValues.Add(op);
|
---|
463 | }
|
---|
464 | private void UpdateOperators() {
|
---|
465 | cmaesInitializerSync = true;
|
---|
466 | try {
|
---|
467 | var oldMutator = CMAMutator;
|
---|
468 | var oldRecombinator = CMARecombinator;
|
---|
469 | var oldUpdater = CMAUpdater;
|
---|
470 |
|
---|
471 | if (CMAInitializer != null && (oldMutator == null || oldMutator.CMAType != CMAInitializer.CMAType)) {
|
---|
472 | CMAMutatorParameter.ValidValues.Clear();
|
---|
473 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAManipulator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
474 | CMAMutatorParameter.ValidValues.Add(op);
|
---|
475 | CMAMutator = CMAMutatorParameter.ValidValues.First();
|
---|
476 | }
|
---|
477 |
|
---|
478 | if (CMAInitializer != null && (oldRecombinator == null || oldRecombinator.CMAType != CMAInitializer.CMAType)) {
|
---|
479 | CMARecombinatorParameter.ValidValues.Clear();
|
---|
480 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMARecombinator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
481 | CMARecombinatorParameter.ValidValues.Add(op);
|
---|
482 | CMARecombinator = CMARecombinatorParameter.ValidValues.First();
|
---|
483 | }
|
---|
484 |
|
---|
485 | if (CMAInitializer != null && (oldUpdater == null || oldUpdater.CMAType != CMAInitializer.CMAType)) {
|
---|
486 | CMAUpdaterParameter.ValidValues.Clear();
|
---|
487 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAUpdater>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
488 | CMAUpdaterParameter.ValidValues.Add(op);
|
---|
489 | CMAUpdater = CMAUpdaterParameter.ValidValues.First();
|
---|
490 | }
|
---|
491 | }
|
---|
492 | finally { cmaesInitializerSync = false; }
|
---|
493 | }
|
---|
494 | private void UpdateAnalyzers() {
|
---|
495 | Analyzer.Operators.Clear();
|
---|
496 | if (Problem != null) {
|
---|
497 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
498 | foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
499 | param.Depth = 1;
|
---|
500 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
501 | }
|
---|
502 | }
|
---|
503 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
504 | Analyzer.Operators.Add(cmaAnalyzer, cmaAnalyzer.EnabledByDefault);
|
---|
505 | }
|
---|
506 | private void Parameterize() {
|
---|
507 |
|
---|
508 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
509 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
510 | op.PopulationSizeParameter.Hidden = true;
|
---|
511 | op.MuParameter.ActualName = MuParameter.Name;
|
---|
512 | op.MuParameter.Hidden = true;
|
---|
513 | op.InitialIterationsParameter.Value = null;
|
---|
514 | op.InitialIterationsParameter.ActualName = InitialIterationsParameter.Name;
|
---|
515 | op.InitialIterationsParameter.Hidden = true;
|
---|
516 | op.InitialSigmaParameter.Value = null;
|
---|
517 | op.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
518 | op.InitialSigmaParameter.Hidden = true;
|
---|
519 |
|
---|
520 | op.DimensionParameter.Hidden = false;
|
---|
521 |
|
---|
522 | ParameterizeStochasticOperator(op);
|
---|
523 | ParameterizeIterationBasedOperator(op);
|
---|
524 | }
|
---|
525 |
|
---|
526 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
527 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
528 | op.PopulationSizeParameter.Hidden = true;
|
---|
529 |
|
---|
530 | op.MeanParameter.Hidden = false;
|
---|
531 | op.BoundsParameter.Hidden = false;
|
---|
532 | op.RealVectorParameter.Hidden = false;
|
---|
533 |
|
---|
534 | ParameterizeStochasticOperator(op);
|
---|
535 | ParameterizeIterationBasedOperator(op);
|
---|
536 | }
|
---|
537 |
|
---|
538 | foreach (var op in CMARecombinatorParameter.ValidValues) {
|
---|
539 | op.OldMeanParameter.ActualName = "XOld";
|
---|
540 | op.OldMeanParameter.Hidden = true;
|
---|
541 |
|
---|
542 | op.OffspringParameter.Hidden = false;
|
---|
543 | op.MeanParameter.Hidden = false;
|
---|
544 |
|
---|
545 | ParameterizeStochasticOperator(op);
|
---|
546 | ParameterizeIterationBasedOperator(op);
|
---|
547 | }
|
---|
548 |
|
---|
549 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
550 | op.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
551 | op.MaximumEvaluatedSolutionsParameter.Hidden = true;
|
---|
552 | op.OldMeanParameter.ActualName = "XOld";
|
---|
553 | op.OldMeanParameter.Hidden = true;
|
---|
554 |
|
---|
555 | op.MeanParameter.Hidden = false;
|
---|
556 | op.OffspringParameter.Hidden = false;
|
---|
557 | op.QualityParameter.Hidden = false;
|
---|
558 |
|
---|
559 | ParameterizeStochasticOperator(op);
|
---|
560 | ParameterizeIterationBasedOperator(op);
|
---|
561 | }
|
---|
562 |
|
---|
563 | terminator.IterationsParameter.ActualName = "Generations";
|
---|
564 | terminator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
565 | terminator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
566 | terminator.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
567 | terminator.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
568 | terminator.MaximumStandardDeviationChangeParameter.ActualName = MaximumStandardDeviationChangeParameter.Name;
|
---|
569 | terminator.MinimumQualityChangeParameter.ActualName = MinimumQualityChangeParameter.Name;
|
---|
570 | terminator.MinimumQualityHistoryChangeParameter.ActualName = MinimumQualityHistoryChangeParameter.Name;
|
---|
571 | terminator.MinimumStandardDeviationParameter.ActualName = MinimumStandardDeviationParameter.Name;
|
---|
572 | terminator.TargetQualityParameter.ActualName = TargetQualityParameter.Name;
|
---|
573 | if (CMAUpdater != null)
|
---|
574 | terminator.DegenerateStateParameter.ActualName = CMAUpdater.DegenerateStateParameter.ActualName;
|
---|
575 |
|
---|
576 | var creator = Problem != null ? (Problem.SolutionCreator as IRealVectorCreator) : null;
|
---|
577 | if (Problem != null && creator != null) {
|
---|
578 |
|
---|
579 | solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
580 | #region Backwards compatible code, remove with 3.4
|
---|
581 | if (populationSolutionCreator != null) // BackwardsCompatibility3.3
|
---|
582 | // ONLY REMOVE THE CONDITION
|
---|
583 | populationSolutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
584 | #endregion
|
---|
585 | evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
586 | sorter.DescendingParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
587 | sorter.ValueParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
588 | terminator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
589 | terminator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
|
---|
590 |
|
---|
591 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
592 | if (creator.LengthParameter.Value == null) {
|
---|
593 | op.DimensionParameter.ActualName = creator.LengthParameter.ActualName;
|
---|
594 | op.DimensionParameter.Value = null;
|
---|
595 | } else op.DimensionParameter.Value = creator.LengthParameter.Value;
|
---|
596 | op.DimensionParameter.Hidden = true;
|
---|
597 | }
|
---|
598 |
|
---|
599 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
600 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
601 | op.MeanParameter.Hidden = true;
|
---|
602 | if (creator.BoundsParameter.Value == null) {
|
---|
603 | op.BoundsParameter.ActualName = creator.BoundsParameter.ActualName;
|
---|
604 | op.BoundsParameter.Value = null;
|
---|
605 | } else op.BoundsParameter.Value = creator.BoundsParameter.Value;
|
---|
606 | op.BoundsParameter.Hidden = true;
|
---|
607 | op.RealVectorParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
608 | op.RealVectorParameter.Depth = 1;
|
---|
609 | op.RealVectorParameter.Hidden = true;
|
---|
610 | }
|
---|
611 |
|
---|
612 | foreach (var op in CMARecombinatorParameter.ValidValues) {
|
---|
613 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
614 | op.MeanParameter.Hidden = true;
|
---|
615 | op.OffspringParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
616 | op.OffspringParameter.Depth = 1;
|
---|
617 | op.OffspringParameter.Hidden = true;
|
---|
618 | }
|
---|
619 |
|
---|
620 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
621 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
622 | op.MeanParameter.Hidden = true;
|
---|
623 | op.OffspringParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
624 | op.OffspringParameter.Depth = 1;
|
---|
625 | op.OffspringParameter.Hidden = true;
|
---|
626 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
627 | op.QualityParameter.Hidden = true;
|
---|
628 | }
|
---|
629 |
|
---|
630 | foreach (var op in Problem.Operators.OfType<IStochasticOperator>()) {
|
---|
631 | op.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
632 | op.RandomParameter.Hidden = true;
|
---|
633 | }
|
---|
634 |
|
---|
635 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
636 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
637 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
638 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
639 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
640 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
641 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
642 |
|
---|
643 | cmaAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
644 | cmaAnalyzer.QualityParameter.Depth = 1;
|
---|
645 | cmaAnalyzer.QualityParameter.Hidden = true;
|
---|
646 | cmaAnalyzer.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
647 | cmaAnalyzer.MeanParameter.Hidden = true;
|
---|
648 |
|
---|
649 | foreach (var op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
650 | op.IterationsParameter.ActualName = "Generations";
|
---|
651 | op.IterationsParameter.Hidden = true;
|
---|
652 | op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
653 | op.MaximumIterationsParameter.Hidden = true;
|
---|
654 | }
|
---|
655 | } else {
|
---|
656 | qualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
657 | qualityAnalyzer.QualityParameter.Hidden = false;
|
---|
658 | qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
659 |
|
---|
660 | cmaAnalyzer.MeanParameter.Hidden = false;
|
---|
661 | }
|
---|
662 |
|
---|
663 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
664 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
665 | cmaAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
666 | cmaAnalyzer.ResultsParameter.Hidden = true;
|
---|
667 | }
|
---|
668 |
|
---|
669 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
670 | var sOp = op as IStochasticOperator;
|
---|
671 | if (sOp != null) sOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
672 | }
|
---|
673 |
|
---|
674 | private void ParameterizeIterationBasedOperator(IOperator op) {
|
---|
675 | var iOp = op as IIterationBasedOperator;
|
---|
676 | if (iOp != null) {
|
---|
677 | iOp.IterationsParameter.ActualName = "Generations";
|
---|
678 | iOp.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
679 | }
|
---|
680 | }
|
---|
681 | #endregion
|
---|
682 | }
|
---|
683 | }
|
---|