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 | [Item("CMA Evolution Strategy", "An evolution strategy based on covariance matrix adaptation.")]
|
---|
39 | [Creatable("Algorithms")]
|
---|
40 | [StorableClass]
|
---|
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(bool deserializing) : base(deserializing) { }
|
---|
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 | } finally { cmaesInitializerSync = false; }
|
---|
492 | }
|
---|
493 | private void UpdateAnalyzers() {
|
---|
494 | Analyzer.Operators.Clear();
|
---|
495 | if (Problem != null) {
|
---|
496 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
497 | foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
498 | param.Depth = 1;
|
---|
499 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
500 | }
|
---|
501 | }
|
---|
502 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
503 | Analyzer.Operators.Add(cmaAnalyzer, cmaAnalyzer.EnabledByDefault);
|
---|
504 | }
|
---|
505 | private void Parameterize() {
|
---|
506 |
|
---|
507 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
508 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
509 | op.PopulationSizeParameter.Hidden = true;
|
---|
510 | op.MuParameter.ActualName = MuParameter.Name;
|
---|
511 | op.MuParameter.Hidden = true;
|
---|
512 | op.InitialIterationsParameter.Value = null;
|
---|
513 | op.InitialIterationsParameter.ActualName = InitialIterationsParameter.Name;
|
---|
514 | op.InitialIterationsParameter.Hidden = true;
|
---|
515 | op.InitialSigmaParameter.Value = null;
|
---|
516 | op.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
517 | op.InitialSigmaParameter.Hidden = true;
|
---|
518 |
|
---|
519 | op.DimensionParameter.Hidden = false;
|
---|
520 |
|
---|
521 | ParameterizeStochasticOperator(op);
|
---|
522 | ParameterizeIterationBasedOperator(op);
|
---|
523 | }
|
---|
524 |
|
---|
525 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
526 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
527 | op.PopulationSizeParameter.Hidden = true;
|
---|
528 |
|
---|
529 | op.MeanParameter.Hidden = false;
|
---|
530 | op.BoundsParameter.Hidden = false;
|
---|
531 | op.RealVectorParameter.Hidden = false;
|
---|
532 |
|
---|
533 | ParameterizeStochasticOperator(op);
|
---|
534 | ParameterizeIterationBasedOperator(op);
|
---|
535 | }
|
---|
536 |
|
---|
537 | foreach (var op in CMARecombinatorParameter.ValidValues) {
|
---|
538 | op.OldMeanParameter.ActualName = "XOld";
|
---|
539 | op.OldMeanParameter.Hidden = true;
|
---|
540 |
|
---|
541 | op.OffspringParameter.Hidden = false;
|
---|
542 | op.MeanParameter.Hidden = false;
|
---|
543 |
|
---|
544 | ParameterizeStochasticOperator(op);
|
---|
545 | ParameterizeIterationBasedOperator(op);
|
---|
546 | }
|
---|
547 |
|
---|
548 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
549 | op.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
550 | op.MaximumEvaluatedSolutionsParameter.Hidden = true;
|
---|
551 | op.OldMeanParameter.ActualName = "XOld";
|
---|
552 | op.OldMeanParameter.Hidden = true;
|
---|
553 |
|
---|
554 | op.MeanParameter.Hidden = false;
|
---|
555 | op.OffspringParameter.Hidden = false;
|
---|
556 | op.QualityParameter.Hidden = false;
|
---|
557 |
|
---|
558 | ParameterizeStochasticOperator(op);
|
---|
559 | ParameterizeIterationBasedOperator(op);
|
---|
560 | }
|
---|
561 |
|
---|
562 | terminator.IterationsParameter.ActualName = "Generations";
|
---|
563 | terminator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
564 | terminator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
565 | terminator.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
566 | terminator.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
567 | terminator.MaximumStandardDeviationChangeParameter.ActualName = MaximumStandardDeviationChangeParameter.Name;
|
---|
568 | terminator.MinimumQualityChangeParameter.ActualName = MinimumQualityChangeParameter.Name;
|
---|
569 | terminator.MinimumQualityHistoryChangeParameter.ActualName = MinimumQualityHistoryChangeParameter.Name;
|
---|
570 | terminator.MinimumStandardDeviationParameter.ActualName = MinimumStandardDeviationParameter.Name;
|
---|
571 | terminator.TargetQualityParameter.ActualName = TargetQualityParameter.Name;
|
---|
572 | if (CMAUpdater != null)
|
---|
573 | terminator.DegenerateStateParameter.ActualName = CMAUpdater.DegenerateStateParameter.ActualName;
|
---|
574 |
|
---|
575 | var creator = Problem != null ? (Problem.SolutionCreator as IRealVectorCreator) : null;
|
---|
576 | if (Problem != null && creator != null) {
|
---|
577 |
|
---|
578 | solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
579 | #region Backwards compatible code, remove with 3.4
|
---|
580 | if (populationSolutionCreator != null) // BackwardsCompatibility3.3
|
---|
581 | // ONLY REMOVE THE CONDITION
|
---|
582 | populationSolutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
583 | #endregion
|
---|
584 | evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
585 | sorter.DescendingParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
586 | sorter.ValueParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
587 | terminator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
588 | terminator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
|
---|
589 |
|
---|
590 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
591 | if (creator.LengthParameter.Value == null) {
|
---|
592 | op.DimensionParameter.ActualName = creator.LengthParameter.ActualName;
|
---|
593 | op.DimensionParameter.Value = null;
|
---|
594 | } else op.DimensionParameter.Value = creator.LengthParameter.Value;
|
---|
595 | op.DimensionParameter.Hidden = true;
|
---|
596 | }
|
---|
597 |
|
---|
598 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
599 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
600 | op.MeanParameter.Hidden = true;
|
---|
601 | if (creator.BoundsParameter.Value == null) {
|
---|
602 | op.BoundsParameter.ActualName = creator.BoundsParameter.ActualName;
|
---|
603 | op.BoundsParameter.Value = null;
|
---|
604 | } else op.BoundsParameter.Value = creator.BoundsParameter.Value;
|
---|
605 | op.BoundsParameter.Hidden = true;
|
---|
606 | op.RealVectorParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
607 | op.RealVectorParameter.Depth = 1;
|
---|
608 | op.RealVectorParameter.Hidden = true;
|
---|
609 | }
|
---|
610 |
|
---|
611 | foreach (var op in CMARecombinatorParameter.ValidValues) {
|
---|
612 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
613 | op.MeanParameter.Hidden = true;
|
---|
614 | op.OffspringParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
615 | op.OffspringParameter.Depth = 1;
|
---|
616 | op.OffspringParameter.Hidden = true;
|
---|
617 | }
|
---|
618 |
|
---|
619 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
620 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
621 | op.MeanParameter.Hidden = true;
|
---|
622 | op.OffspringParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
623 | op.OffspringParameter.Depth = 1;
|
---|
624 | op.OffspringParameter.Hidden = true;
|
---|
625 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
626 | op.QualityParameter.Hidden = true;
|
---|
627 | }
|
---|
628 |
|
---|
629 | foreach (var op in Problem.Operators.OfType<IStochasticOperator>()) {
|
---|
630 | op.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
631 | op.RandomParameter.Hidden = true;
|
---|
632 | }
|
---|
633 |
|
---|
634 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
635 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
636 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
637 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
638 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
639 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
640 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
641 |
|
---|
642 | cmaAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
643 | cmaAnalyzer.QualityParameter.Depth = 1;
|
---|
644 | cmaAnalyzer.QualityParameter.Hidden = true;
|
---|
645 | cmaAnalyzer.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
646 | cmaAnalyzer.MeanParameter.Hidden = true;
|
---|
647 |
|
---|
648 | foreach (var op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
649 | op.IterationsParameter.ActualName = "Generations";
|
---|
650 | op.IterationsParameter.Hidden = true;
|
---|
651 | op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
652 | op.MaximumIterationsParameter.Hidden = true;
|
---|
653 | }
|
---|
654 | } else {
|
---|
655 | qualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
656 | qualityAnalyzer.QualityParameter.Hidden = false;
|
---|
657 | qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
658 |
|
---|
659 | cmaAnalyzer.MeanParameter.Hidden = false;
|
---|
660 | }
|
---|
661 |
|
---|
662 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
663 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
664 | cmaAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
665 | cmaAnalyzer.ResultsParameter.Hidden = true;
|
---|
666 | }
|
---|
667 |
|
---|
668 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
669 | var sOp = op as IStochasticOperator;
|
---|
670 | if (sOp != null) sOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
671 | }
|
---|
672 |
|
---|
673 | private void ParameterizeIterationBasedOperator(IOperator op) {
|
---|
674 | var iOp = op as IIterationBasedOperator;
|
---|
675 | if (iOp != null) {
|
---|
676 | iOp.IterationsParameter.ActualName = "Generations";
|
---|
677 | iOp.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
678 | }
|
---|
679 | }
|
---|
680 | #endregion
|
---|
681 | }
|
---|
682 | }
|
---|