1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 | private IValueParameter<MultiAnalyzer> AnalyzerParameter {
|
---|
76 | get { return (IValueParameter<MultiAnalyzer>)Parameters[AnalyzerName]; }
|
---|
77 | }
|
---|
78 | private IValueParameter<IntValue> SeedParameter {
|
---|
79 | get { return (IValueParameter<IntValue>)Parameters[SeedName]; }
|
---|
80 | }
|
---|
81 | private IValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
82 | get { return (IValueParameter<BoolValue>)Parameters[SetSeedRandomlyName]; }
|
---|
83 | }
|
---|
84 | private IValueParameter<IntValue> PopulationSizeParameter {
|
---|
85 | get { return (IValueParameter<IntValue>)Parameters[PopulationSizeName]; }
|
---|
86 | }
|
---|
87 | private IValueParameter<IntValue> InitialIterationsParameter {
|
---|
88 | get { return (IValueParameter<IntValue>)Parameters[InitialIterationsName]; }
|
---|
89 | }
|
---|
90 | private 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 | private IConstrainedValueParameter<ICMAInitializer> CMAInitializerParameter {
|
---|
97 | get { return (IConstrainedValueParameter<ICMAInitializer>)Parameters[CMAInitializerName]; }
|
---|
98 | }
|
---|
99 | private IConstrainedValueParameter<ICMAManipulator> CMAMutatorParameter {
|
---|
100 | get { return (IConstrainedValueParameter<ICMAManipulator>)Parameters[CMAMutatorName]; }
|
---|
101 | }
|
---|
102 | private IConstrainedValueParameter<ICMARecombinator> CMARecombinatorParameter {
|
---|
103 | get { return (IConstrainedValueParameter<ICMARecombinator>)Parameters[CMARecombinatorName]; }
|
---|
104 | }
|
---|
105 | private IConstrainedValueParameter<ICMAUpdater> CMAUpdaterParameter {
|
---|
106 | get { return (IConstrainedValueParameter<ICMAUpdater>)Parameters[CMAUpdaterName]; }
|
---|
107 | }
|
---|
108 | private IValueParameter<IntValue> MaximumGenerationsParameter {
|
---|
109 | get { return (IValueParameter<IntValue>)Parameters[MaximumGenerationsName]; }
|
---|
110 | }
|
---|
111 | private IValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
|
---|
112 | get { return (IValueParameter<IntValue>)Parameters[MaximumEvaluatedSolutionsName]; }
|
---|
113 | }
|
---|
114 | private IValueParameter<DoubleValue> TargetQualityParameter {
|
---|
115 | get { return (IValueParameter<DoubleValue>)Parameters[TargetQualityName]; }
|
---|
116 | }
|
---|
117 | private IValueParameter<DoubleValue> MinimumQualityChangeParameter {
|
---|
118 | get { return (IValueParameter<DoubleValue>)Parameters[MinimumQualityChangeName]; }
|
---|
119 | }
|
---|
120 | private IValueParameter<DoubleValue> MinimumQualityHistoryChangeParameter {
|
---|
121 | get { return (IValueParameter<DoubleValue>)Parameters[MinimumQualityHistoryChangeName]; }
|
---|
122 | }
|
---|
123 | private IValueParameter<DoubleValue> MinimumStandardDeviationParameter {
|
---|
124 | get { return (IValueParameter<DoubleValue>)Parameters[MinimumStandardDeviationName]; }
|
---|
125 | }
|
---|
126 | private IValueParameter<DoubleValue> MaximumStandardDeviationChangeParameter {
|
---|
127 | get { return (IValueParameter<DoubleValue>)Parameters[MaximumStandardDeviationChangeName]; }
|
---|
128 | }
|
---|
129 | #endregion
|
---|
130 |
|
---|
131 | #region Properties
|
---|
132 | public IntValue Seed {
|
---|
133 | get { return SeedParameter.Value; }
|
---|
134 | set { SeedParameter.Value = value; }
|
---|
135 | }
|
---|
136 | public BoolValue SetSeedRandomly {
|
---|
137 | get { return SetSeedRandomlyParameter.Value; }
|
---|
138 | set { SetSeedRandomlyParameter.Value = value; }
|
---|
139 | }
|
---|
140 | public IntValue PopulationSize {
|
---|
141 | get { return PopulationSizeParameter.Value; }
|
---|
142 | set { PopulationSizeParameter.Value = value; }
|
---|
143 | }
|
---|
144 | public IntValue InitialIterations {
|
---|
145 | get { return InitialIterationsParameter.Value; }
|
---|
146 | set { InitialIterationsParameter.Value = value; }
|
---|
147 | }
|
---|
148 | public IntValue MaximumGenerations {
|
---|
149 | get { return MaximumGenerationsParameter.Value; }
|
---|
150 | set { MaximumGenerationsParameter.Value = value; }
|
---|
151 | }
|
---|
152 | public IntValue MaximumEvaluatedSolutions {
|
---|
153 | get { return MaximumEvaluatedSolutionsParameter.Value; }
|
---|
154 | set { MaximumEvaluatedSolutionsParameter.Value = value; }
|
---|
155 | }
|
---|
156 | public DoubleValue TargetQuality {
|
---|
157 | get { return TargetQualityParameter.Value; }
|
---|
158 | set { TargetQualityParameter.Value = value; }
|
---|
159 | }
|
---|
160 | public DoubleValue MinimumQualityChange {
|
---|
161 | get { return MinimumQualityChangeParameter.Value; }
|
---|
162 | set { MinimumQualityChangeParameter.Value = value; }
|
---|
163 | }
|
---|
164 | public DoubleValue MinimumQualityHistoryChange {
|
---|
165 | get { return MinimumQualityHistoryChangeParameter.Value; }
|
---|
166 | set { MinimumQualityHistoryChangeParameter.Value = value; }
|
---|
167 | }
|
---|
168 | public DoubleValue MinimumStandardDeviation {
|
---|
169 | get { return MinimumStandardDeviationParameter.Value; }
|
---|
170 | set { MinimumStandardDeviationParameter.Value = value; }
|
---|
171 | }
|
---|
172 | public DoubleValue MaximumStandardDeviationChange {
|
---|
173 | get { return MaximumStandardDeviationChangeParameter.Value; }
|
---|
174 | set { MaximumStandardDeviationChangeParameter.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 evaluator;
|
---|
217 | [Storable]
|
---|
218 | private SubScopesSorter sorter;
|
---|
219 | [Storable]
|
---|
220 | private Terminator terminator;
|
---|
221 | #endregion
|
---|
222 |
|
---|
223 | [StorableConstructor]
|
---|
224 | private CMAEvolutionStrategy(bool deserializing) : base(deserializing) { }
|
---|
225 | private CMAEvolutionStrategy(CMAEvolutionStrategy original, Cloner cloner)
|
---|
226 | : base(original, cloner) {
|
---|
227 | qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
|
---|
228 | cmaAnalyzer = cloner.Clone(original.cmaAnalyzer);
|
---|
229 | solutionCreator = cloner.Clone(original.solutionCreator);
|
---|
230 | evaluator = cloner.Clone(original.evaluator);
|
---|
231 | sorter = cloner.Clone(original.sorter);
|
---|
232 | terminator = cloner.Clone(original.terminator);
|
---|
233 | RegisterEventHandlers();
|
---|
234 | }
|
---|
235 | public CMAEvolutionStrategy()
|
---|
236 | : base() {
|
---|
237 | Parameters.Add(new ValueParameter<IntValue>(SeedName, "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
|
---|
238 | Parameters.Add(new ValueParameter<BoolValue>(SetSeedRandomlyName, "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
|
---|
239 | Parameters.Add(new ValueParameter<IntValue>(PopulationSizeName, "λ (lambda) - the size of the offspring population.", new IntValue(20)));
|
---|
240 | Parameters.Add(new ValueParameter<IntValue>(InitialIterationsName, "The number of iterations that should be performed with only axis parallel mutation.", new IntValue(0)));
|
---|
241 | Parameters.Add(new ValueParameter<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 })));
|
---|
242 | 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."));
|
---|
243 | Parameters.Add(new ConstrainedValueParameter<ICMARecombinator>(CMARecombinatorName, "The operator used to calculate the new mean."));
|
---|
244 | Parameters.Add(new ConstrainedValueParameter<ICMAManipulator>(CMAMutatorName, "The operator used to manipulate a point."));
|
---|
245 | Parameters.Add(new ConstrainedValueParameter<ICMAInitializer>(CMAInitializerName, "The operator that initializes the covariance matrix and strategy parameters."));
|
---|
246 | Parameters.Add(new ConstrainedValueParameter<ICMAUpdater>(CMAUpdaterName, "The operator that updates the covariance matrix and strategy parameters."));
|
---|
247 | Parameters.Add(new ValueParameter<MultiAnalyzer>(AnalyzerName, "The operator used to analyze each generation.", new MultiAnalyzer()));
|
---|
248 | Parameters.Add(new ValueParameter<IntValue>(MaximumGenerationsName, "The maximum number of generations which should be processed.", new IntValue(1000)));
|
---|
249 | Parameters.Add(new ValueParameter<IntValue>(MaximumEvaluatedSolutionsName, "The maximum number of evaluated solutions that should be computed.", new IntValue(int.MaxValue)));
|
---|
250 | Parameters.Add(new ValueParameter<DoubleValue>(TargetQualityName, "(stopFitness) Surpassing this quality value terminates the algorithm.", new DoubleValue(double.NaN)));
|
---|
251 | Parameters.Add(new ValueParameter<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)));
|
---|
252 | Parameters.Add(new ValueParameter<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)));
|
---|
253 | Parameters.Add(new ValueParameter<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)));
|
---|
254 | Parameters.Add(new ValueParameter<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)));
|
---|
255 |
|
---|
256 | var randomCreator = new RandomCreator();
|
---|
257 | var variableCreator = new VariableCreator();
|
---|
258 | var resultsCollector = new ResultsCollector();
|
---|
259 | var cmaInitializer = new Placeholder();
|
---|
260 | solutionCreator = new Placeholder();
|
---|
261 | var subScopesCreator = new SubScopesCreator();
|
---|
262 | var cmaMutator = new Placeholder();
|
---|
263 | var ussp = new UniformSubScopesProcessor();
|
---|
264 | evaluator = new Placeholder();
|
---|
265 | var subScopesCounter = new SubScopesCounter();
|
---|
266 | sorter = new SubScopesSorter();
|
---|
267 | var analyzer = new Placeholder();
|
---|
268 | var cmaRecombinator = new Placeholder();
|
---|
269 | var generationsCounter = new IntCounter();
|
---|
270 | var cmaUpdater = new Placeholder();
|
---|
271 | terminator = new Terminator();
|
---|
272 |
|
---|
273 | OperatorGraph.InitialOperator = randomCreator;
|
---|
274 |
|
---|
275 | randomCreator.RandomParameter.ActualName = "Random";
|
---|
276 | randomCreator.SeedParameter.ActualName = SeedParameter.Name;
|
---|
277 | randomCreator.SeedParameter.Value = null;
|
---|
278 | randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
|
---|
279 | randomCreator.SetSeedRandomlyParameter.Value = null;
|
---|
280 | randomCreator.Successor = variableCreator;
|
---|
281 |
|
---|
282 | variableCreator.Name = "Initialize Variables";
|
---|
283 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("EvaluatedSolutions", new IntValue(0)));
|
---|
284 | variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0)));
|
---|
285 | variableCreator.Successor = resultsCollector;
|
---|
286 |
|
---|
287 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("EvaluatedSolutions"));
|
---|
288 | resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Generations"));
|
---|
289 | resultsCollector.ResultsParameter.ActualName = "Results";
|
---|
290 | resultsCollector.Successor = cmaInitializer;
|
---|
291 |
|
---|
292 | cmaInitializer.Name = "Initialize Strategy Parameters";
|
---|
293 | cmaInitializer.OperatorParameter.ActualName = CMAInitializerParameter.Name;
|
---|
294 | cmaInitializer.Successor = solutionCreator;
|
---|
295 |
|
---|
296 | solutionCreator.Name = "Initialize xmean";
|
---|
297 | // solutionCreator.OperatorParameter will be wired
|
---|
298 | solutionCreator.Successor = subScopesCreator;
|
---|
299 |
|
---|
300 | subScopesCreator.NumberOfSubScopesParameter.ActualName = PopulationSizeParameter.Name;
|
---|
301 | subScopesCreator.Successor = cmaMutator;
|
---|
302 |
|
---|
303 | cmaMutator.Name = "Sample population";
|
---|
304 | cmaMutator.OperatorParameter.ActualName = CMAMutatorParameter.Name;
|
---|
305 | cmaMutator.Successor = ussp;
|
---|
306 |
|
---|
307 | ussp.Name = "Evaluate offspring";
|
---|
308 | ussp.Parallel = new BoolValue(true);
|
---|
309 | ussp.Operator = evaluator;
|
---|
310 | ussp.Successor = subScopesCounter;
|
---|
311 |
|
---|
312 | evaluator.Name = "Evaluator";
|
---|
313 | // evaluator.OperatorParameter will be wired
|
---|
314 | evaluator.Successor = null;
|
---|
315 |
|
---|
316 | subScopesCounter.Name = "Count EvaluatedSolutions";
|
---|
317 | subScopesCounter.AccumulateParameter.Value = new BoolValue(true);
|
---|
318 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
319 | subScopesCounter.Successor = sorter;
|
---|
320 |
|
---|
321 | // sorter.ValueParameter will be wired
|
---|
322 | // sorter.DescendingParameter will be wired
|
---|
323 | sorter.Successor = analyzer;
|
---|
324 |
|
---|
325 | analyzer.Name = "Analyzer";
|
---|
326 | analyzer.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
327 | analyzer.Successor = cmaRecombinator;
|
---|
328 |
|
---|
329 | cmaRecombinator.Name = "Create new xmean";
|
---|
330 | cmaRecombinator.OperatorParameter.ActualName = CMARecombinatorParameter.Name;
|
---|
331 | cmaRecombinator.Successor = generationsCounter;
|
---|
332 |
|
---|
333 | generationsCounter.Name = "Generations++";
|
---|
334 | generationsCounter.IncrementParameter.Value = new IntValue(1);
|
---|
335 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
336 | generationsCounter.Successor = cmaUpdater;
|
---|
337 |
|
---|
338 | cmaUpdater.Name = "Update distributions";
|
---|
339 | cmaUpdater.OperatorParameter.ActualName = CMAUpdaterParameter.Name;
|
---|
340 | cmaUpdater.Successor = terminator;
|
---|
341 |
|
---|
342 | terminator.Continue = cmaMutator;
|
---|
343 | terminator.Terminate = null;
|
---|
344 |
|
---|
345 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
346 | cmaAnalyzer = new CMAAnalyzer();
|
---|
347 |
|
---|
348 | InitializeOperators();
|
---|
349 | RegisterEventHandlers();
|
---|
350 | Parameterize();
|
---|
351 | }
|
---|
352 |
|
---|
353 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
354 | return new CMAEvolutionStrategy(this, cloner);
|
---|
355 | }
|
---|
356 |
|
---|
357 | [StorableHook(HookType.AfterDeserialization)]
|
---|
358 | private void AfterDeserialization() {
|
---|
359 | RegisterEventHandlers();
|
---|
360 | }
|
---|
361 |
|
---|
362 | public override void Prepare() {
|
---|
363 | if (Problem != null) base.Prepare();
|
---|
364 | }
|
---|
365 |
|
---|
366 | protected override void OnStarted() {
|
---|
367 | if (!(Problem.SolutionCreator is IRealVectorCreator))
|
---|
368 | throw new InvalidOperationException("Problems that do not use RealVectorEncoding cannot be solved by CMA-ES.");
|
---|
369 | base.OnStarted();
|
---|
370 | }
|
---|
371 |
|
---|
372 | #region Events
|
---|
373 | protected override void OnProblemChanged() {
|
---|
374 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
375 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
376 | if (creator != null) {
|
---|
377 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
378 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
379 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
380 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
381 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
382 | }
|
---|
383 | UpdateOperators();
|
---|
384 | UpdateAnalyzers();
|
---|
385 | Parameterize();
|
---|
386 | base.OnProblemChanged();
|
---|
387 | }
|
---|
388 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
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 | Parameterize();
|
---|
398 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
399 | }
|
---|
400 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
401 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
402 | Parameterize();
|
---|
403 | base.Problem_EvaluatorChanged(sender, e);
|
---|
404 | }
|
---|
405 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
406 | UpdateOperators();
|
---|
407 | UpdateAnalyzers();
|
---|
408 | Parameterize();
|
---|
409 | base.Problem_OperatorsChanged(sender, e);
|
---|
410 | }
|
---|
411 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
412 | Parameterize();
|
---|
413 | }
|
---|
414 | private bool cmaesInitializerSync;
|
---|
415 | private void CMAESInitializerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
416 | if (cmaesInitializerSync) return;
|
---|
417 | UpdateOperators();
|
---|
418 | Parameterize();
|
---|
419 | }
|
---|
420 | private void RealVectorCreator_Changed(object sender, EventArgs e) {
|
---|
421 | Parameterize();
|
---|
422 | }
|
---|
423 | #endregion
|
---|
424 |
|
---|
425 | #region Helpers
|
---|
426 | private void RegisterEventHandlers() {
|
---|
427 | CMAInitializerParameter.ValueChanged += CMAESInitializerParameter_ValueChanged;
|
---|
428 | if (Problem != null) {
|
---|
429 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
430 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
431 | if (creator != null) {
|
---|
432 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
433 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
434 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
435 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
436 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
437 | }
|
---|
438 | }
|
---|
439 | }
|
---|
440 | private void InitializeOperators() {
|
---|
441 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAInitializer>())
|
---|
442 | CMAInitializerParameter.ValidValues.Add(op);
|
---|
443 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAManipulator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
444 | CMAMutatorParameter.ValidValues.Add(op);
|
---|
445 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMARecombinator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
446 | CMARecombinatorParameter.ValidValues.Add(op);
|
---|
447 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAUpdater>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
448 | CMAUpdaterParameter.ValidValues.Add(op);
|
---|
449 | }
|
---|
450 | private void UpdateOperators() {
|
---|
451 | cmaesInitializerSync = true;
|
---|
452 | try {
|
---|
453 | var oldMutator = CMAMutator;
|
---|
454 | var oldRecombinator = CMARecombinator;
|
---|
455 | var oldUpdater = CMAUpdater;
|
---|
456 |
|
---|
457 | if (CMAInitializer != null && (oldMutator == null || oldMutator.CMAType != CMAInitializer.CMAType)) {
|
---|
458 | CMAMutatorParameter.ValidValues.Clear();
|
---|
459 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAManipulator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
460 | CMAMutatorParameter.ValidValues.Add(op);
|
---|
461 | CMAMutator = CMAMutatorParameter.ValidValues.First();
|
---|
462 | }
|
---|
463 |
|
---|
464 | if (CMAInitializer != null && (oldRecombinator == null || oldRecombinator.CMAType != CMAInitializer.CMAType)) {
|
---|
465 | CMARecombinatorParameter.ValidValues.Clear();
|
---|
466 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMARecombinator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
467 | CMARecombinatorParameter.ValidValues.Add(op);
|
---|
468 | CMARecombinator = CMARecombinatorParameter.ValidValues.First();
|
---|
469 | }
|
---|
470 |
|
---|
471 | if (CMAInitializer != null && (oldUpdater == null || oldUpdater.CMAType != CMAInitializer.CMAType)) {
|
---|
472 | CMAUpdaterParameter.ValidValues.Clear();
|
---|
473 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAUpdater>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
474 | CMAUpdaterParameter.ValidValues.Add(op);
|
---|
475 | CMAUpdater = CMAUpdaterParameter.ValidValues.First();
|
---|
476 | }
|
---|
477 | }
|
---|
478 | finally { cmaesInitializerSync = false; }
|
---|
479 | }
|
---|
480 | private void UpdateAnalyzers() {
|
---|
481 | Analyzer.Operators.Clear();
|
---|
482 | if (Problem != null) {
|
---|
483 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
484 | foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
485 | param.Depth = 1;
|
---|
486 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
487 | }
|
---|
488 | }
|
---|
489 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
490 | Analyzer.Operators.Add(cmaAnalyzer, cmaAnalyzer.EnabledByDefault);
|
---|
491 | }
|
---|
492 | private void Parameterize() {
|
---|
493 |
|
---|
494 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
495 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
496 | op.PopulationSizeParameter.Hidden = true;
|
---|
497 | op.MuParameter.ActualName = MuParameter.Name;
|
---|
498 | op.MuParameter.Hidden = true;
|
---|
499 | op.InitialIterationsParameter.Value = null;
|
---|
500 | op.InitialIterationsParameter.ActualName = InitialIterationsParameter.Name;
|
---|
501 | op.InitialIterationsParameter.Hidden = true;
|
---|
502 | op.InitialSigmaParameter.Value = null;
|
---|
503 | op.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
504 | op.InitialSigmaParameter.Hidden = true;
|
---|
505 |
|
---|
506 | op.DimensionParameter.Hidden = false;
|
---|
507 |
|
---|
508 | ParameterizeStochasticOperator(op);
|
---|
509 | ParameterizeIterationBasedOperator(op);
|
---|
510 | }
|
---|
511 |
|
---|
512 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
513 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
514 | op.PopulationSizeParameter.Hidden = true;
|
---|
515 |
|
---|
516 | op.MeanParameter.Hidden = false;
|
---|
517 | op.BoundsParameter.Hidden = false;
|
---|
518 | op.RealVectorParameter.Hidden = false;
|
---|
519 |
|
---|
520 | ParameterizeStochasticOperator(op);
|
---|
521 | ParameterizeIterationBasedOperator(op);
|
---|
522 | }
|
---|
523 |
|
---|
524 | foreach (var op in CMARecombinatorParameter.ValidValues) {
|
---|
525 | op.OldMeanParameter.ActualName = "XOld";
|
---|
526 | op.OldMeanParameter.Hidden = true;
|
---|
527 |
|
---|
528 | op.OffspringParameter.Hidden = false;
|
---|
529 | op.MeanParameter.Hidden = false;
|
---|
530 |
|
---|
531 | ParameterizeStochasticOperator(op);
|
---|
532 | ParameterizeIterationBasedOperator(op);
|
---|
533 | }
|
---|
534 |
|
---|
535 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
536 | op.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
537 | op.MaximumEvaluatedSolutionsParameter.Hidden = true;
|
---|
538 | op.OldMeanParameter.ActualName = "XOld";
|
---|
539 | op.OldMeanParameter.Hidden = true;
|
---|
540 |
|
---|
541 | op.MeanParameter.Hidden = false;
|
---|
542 | op.OffspringParameter.Hidden = false;
|
---|
543 | op.QualityParameter.Hidden = false;
|
---|
544 |
|
---|
545 | ParameterizeStochasticOperator(op);
|
---|
546 | ParameterizeIterationBasedOperator(op);
|
---|
547 | }
|
---|
548 |
|
---|
549 | terminator.IterationsParameter.ActualName = "Generations";
|
---|
550 | terminator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
551 | terminator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
552 | terminator.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
553 | terminator.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
554 | terminator.MaximumStandardDeviationChangeParameter.ActualName = MaximumStandardDeviationChangeParameter.Name;
|
---|
555 | terminator.MinimumQualityChangeParameter.ActualName = MinimumQualityChangeParameter.Name;
|
---|
556 | terminator.MinimumQualityHistoryChangeParameter.ActualName = MinimumQualityHistoryChangeParameter.Name;
|
---|
557 | terminator.MinimumStandardDeviationParameter.ActualName = MinimumStandardDeviationParameter.Name;
|
---|
558 | terminator.TargetQualityParameter.ActualName = TargetQualityParameter.Name;
|
---|
559 | if (CMAUpdater != null)
|
---|
560 | terminator.DegenerateStateParameter.ActualName = CMAUpdater.DegenerateStateParameter.ActualName;
|
---|
561 |
|
---|
562 | var creator = Problem != null ? (Problem.SolutionCreator as IRealVectorCreator) : null;
|
---|
563 | if (Problem != null && creator != null) {
|
---|
564 |
|
---|
565 | solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
566 | evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
567 | sorter.DescendingParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
568 | sorter.ValueParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
569 | terminator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
570 | terminator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
|
---|
571 |
|
---|
572 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
573 | if (creator.LengthParameter.Value == null) {
|
---|
574 | op.DimensionParameter.ActualName = creator.LengthParameter.ActualName;
|
---|
575 | op.DimensionParameter.Value = null;
|
---|
576 | } else op.DimensionParameter.Value = creator.LengthParameter.Value;
|
---|
577 | op.DimensionParameter.Hidden = true;
|
---|
578 | }
|
---|
579 |
|
---|
580 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
581 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
582 | op.MeanParameter.Hidden = true;
|
---|
583 | if (creator.BoundsParameter.Value == null) {
|
---|
584 | op.BoundsParameter.ActualName = creator.BoundsParameter.ActualName;
|
---|
585 | op.BoundsParameter.Value = null;
|
---|
586 | } else op.BoundsParameter.Value = creator.BoundsParameter.Value;
|
---|
587 | op.BoundsParameter.Hidden = true;
|
---|
588 | op.RealVectorParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
589 | op.RealVectorParameter.Depth = 1;
|
---|
590 | op.RealVectorParameter.Hidden = true;
|
---|
591 | }
|
---|
592 |
|
---|
593 | foreach (var op in CMARecombinatorParameter.ValidValues) {
|
---|
594 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
595 | op.MeanParameter.Hidden = true;
|
---|
596 | op.OffspringParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
597 | op.OffspringParameter.Depth = 1;
|
---|
598 | op.OffspringParameter.Hidden = true;
|
---|
599 | }
|
---|
600 |
|
---|
601 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
602 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
603 | op.MeanParameter.Hidden = true;
|
---|
604 | op.OffspringParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
605 | op.OffspringParameter.Depth = 1;
|
---|
606 | op.OffspringParameter.Hidden = true;
|
---|
607 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
608 | op.QualityParameter.Hidden = true;
|
---|
609 | }
|
---|
610 |
|
---|
611 | foreach (var op in Problem.Operators.OfType<IStochasticOperator>()) {
|
---|
612 | op.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
613 | op.RandomParameter.Hidden = true;
|
---|
614 | }
|
---|
615 |
|
---|
616 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
617 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
618 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
619 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
620 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
621 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
622 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
623 |
|
---|
624 | cmaAnalyzer.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
625 | cmaAnalyzer.MeanParameter.Hidden = true;
|
---|
626 |
|
---|
627 | foreach (var op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
628 | op.IterationsParameter.ActualName = "Generations";
|
---|
629 | op.IterationsParameter.Hidden = true;
|
---|
630 | op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
631 | op.MaximumIterationsParameter.Hidden = true;
|
---|
632 | }
|
---|
633 | } else {
|
---|
634 | qualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
635 | qualityAnalyzer.QualityParameter.Hidden = false;
|
---|
636 | qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
637 |
|
---|
638 | cmaAnalyzer.MeanParameter.Hidden = false;
|
---|
639 | }
|
---|
640 |
|
---|
641 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
642 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
643 | cmaAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
644 | cmaAnalyzer.ResultsParameter.Hidden = true;
|
---|
645 | }
|
---|
646 |
|
---|
647 | private void ParameterizeStochasticOperator(IOperator op) {
|
---|
648 | var sOp = op as IStochasticOperator;
|
---|
649 | if (sOp != null) sOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
650 | }
|
---|
651 |
|
---|
652 | private void ParameterizeIterationBasedOperator(IOperator op) {
|
---|
653 | var iOp = op as IIterationBasedOperator;
|
---|
654 | if (iOp != null) {
|
---|
655 | iOp.IterationsParameter.ActualName = "Generations";
|
---|
656 | iOp.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
657 | }
|
---|
658 | }
|
---|
659 | #endregion
|
---|
660 | }
|
---|
661 | }
|
---|