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