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 HeuristicLab.Analysis;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.RealVectorEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Optimization.Operators;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 | using HeuristicLab.PluginInfrastructure;
|
---|
33 | using HeuristicLab.Random;
|
---|
34 | using System;
|
---|
35 | using System.Linq;
|
---|
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 readonly BestAverageWorstQualityAnalyzer qualityAnalyzer;
|
---|
211 | [Storable]
|
---|
212 | private readonly CMAAnalyzer cmaAnalyzer;
|
---|
213 | [Storable]
|
---|
214 | private readonly Placeholder solutionCreator;
|
---|
215 | [Storable]
|
---|
216 | private readonly Placeholder evaluator;
|
---|
217 | [Storable]
|
---|
218 | private readonly SubScopesSorter sorter;
|
---|
219 | [Storable]
|
---|
220 | private readonly 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.OperatorParameter.ActualName = CMAInitializerParameter.Name;
|
---|
293 | cmaInitializer.Successor = solutionCreator;
|
---|
294 |
|
---|
295 | solutionCreator.Name = "Initialize xmean";
|
---|
296 | // solutionCreator.OperatorParameter will be wired
|
---|
297 | solutionCreator.Successor = subScopesCreator;
|
---|
298 |
|
---|
299 | subScopesCreator.NumberOfSubScopesParameter.ActualName = PopulationSizeParameter.Name;
|
---|
300 | subScopesCreator.Successor = cmaMutator;
|
---|
301 |
|
---|
302 | cmaMutator.Name = "Sample population";
|
---|
303 | cmaMutator.OperatorParameter.ActualName = CMAMutatorParameter.Name;
|
---|
304 | cmaMutator.Successor = ussp;
|
---|
305 |
|
---|
306 | ussp.Name = "Evaluate offspring";
|
---|
307 | ussp.Parallel = new BoolValue(true);
|
---|
308 | ussp.Operator = evaluator;
|
---|
309 | ussp.Successor = subScopesCounter;
|
---|
310 |
|
---|
311 | evaluator.Name = "Evaluator";
|
---|
312 | // evaluator.OperatorParameter will be wired
|
---|
313 | evaluator.Successor = null;
|
---|
314 |
|
---|
315 | subScopesCounter.Name = "Count EvaluatedSolutions";
|
---|
316 | subScopesCounter.AccumulateParameter.Value = new BoolValue(true);
|
---|
317 | subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
|
---|
318 | subScopesCounter.Successor = sorter;
|
---|
319 |
|
---|
320 | // sorter.ValueParameter will be wired
|
---|
321 | // sorter.DescendingParameter will be wired
|
---|
322 | sorter.Successor = analyzer;
|
---|
323 |
|
---|
324 | analyzer.Name = "Analyzer";
|
---|
325 | analyzer.OperatorParameter.ActualName = AnalyzerParameter.Name;
|
---|
326 | analyzer.Successor = cmaRecombinator;
|
---|
327 |
|
---|
328 | cmaRecombinator.Name = "Create new xmean";
|
---|
329 | cmaRecombinator.OperatorParameter.ActualName = CMARecombinatorParameter.Name;
|
---|
330 | cmaRecombinator.Successor = generationsCounter;
|
---|
331 |
|
---|
332 | generationsCounter.Name = "Generations++";
|
---|
333 | generationsCounter.IncrementParameter.Value = new IntValue(1);
|
---|
334 | generationsCounter.ValueParameter.ActualName = "Generations";
|
---|
335 | generationsCounter.Successor = cmaUpdater;
|
---|
336 |
|
---|
337 | cmaUpdater.Name = "Update distributions";
|
---|
338 | cmaUpdater.OperatorParameter.ActualName = CMAUpdaterParameter.Name;
|
---|
339 | cmaUpdater.Successor = terminator;
|
---|
340 |
|
---|
341 | terminator.Continue = cmaMutator;
|
---|
342 | terminator.Terminate = null;
|
---|
343 |
|
---|
344 | qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
|
---|
345 | cmaAnalyzer = new CMAAnalyzer();
|
---|
346 |
|
---|
347 | InitializeOperators();
|
---|
348 | RegisterEventHandlers();
|
---|
349 | Parameterize();
|
---|
350 | }
|
---|
351 |
|
---|
352 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
353 | return new CMAEvolutionStrategy(this, cloner);
|
---|
354 | }
|
---|
355 |
|
---|
356 | [StorableHook(HookType.AfterDeserialization)]
|
---|
357 | private void AfterDeserialization() {
|
---|
358 | RegisterEventHandlers();
|
---|
359 | }
|
---|
360 |
|
---|
361 | public override void Prepare() {
|
---|
362 | if (Problem != null) base.Prepare();
|
---|
363 | }
|
---|
364 |
|
---|
365 | #region Events
|
---|
366 | protected override void OnProblemChanged() {
|
---|
367 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
368 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
369 | if (creator != null) {
|
---|
370 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
371 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
372 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
373 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
374 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
375 | }
|
---|
376 | UpdateOperators();
|
---|
377 | UpdateAnalyzers();
|
---|
378 | Parameterize();
|
---|
379 | base.OnProblemChanged();
|
---|
380 | }
|
---|
381 | protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
|
---|
382 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
383 | if (creator != null) {
|
---|
384 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
385 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
386 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
387 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
388 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
389 | }
|
---|
390 | Parameterize();
|
---|
391 | base.Problem_SolutionCreatorChanged(sender, e);
|
---|
392 | }
|
---|
393 | protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
|
---|
394 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
395 | Parameterize();
|
---|
396 | base.Problem_EvaluatorChanged(sender, e);
|
---|
397 | }
|
---|
398 | protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
|
---|
399 | UpdateOperators();
|
---|
400 | UpdateAnalyzers();
|
---|
401 | Parameterize();
|
---|
402 | base.Problem_OperatorsChanged(sender, e);
|
---|
403 | }
|
---|
404 | private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
|
---|
405 | Parameterize();
|
---|
406 | }
|
---|
407 | private bool cmaesInitializerSync;
|
---|
408 | private void CMAESInitializerParameter_ValueChanged(object sender, EventArgs e) {
|
---|
409 | if (cmaesInitializerSync) return;
|
---|
410 | UpdateOperators();
|
---|
411 | Parameterize();
|
---|
412 | }
|
---|
413 | private void RealVectorCreator_Changed(object sender, EventArgs e) {
|
---|
414 | Parameterize();
|
---|
415 | }
|
---|
416 | #endregion
|
---|
417 |
|
---|
418 | #region Helpers
|
---|
419 | private void RegisterEventHandlers() {
|
---|
420 | CMAInitializerParameter.ValueChanged += CMAESInitializerParameter_ValueChanged;
|
---|
421 | if (Problem != null) {
|
---|
422 | Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
|
---|
423 | var creator = Problem.SolutionCreator as IRealVectorCreator;
|
---|
424 | if (creator != null) {
|
---|
425 | creator.RealVectorParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
426 | creator.LengthParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
427 | creator.LengthParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
428 | creator.BoundsParameter.ActualNameChanged += RealVectorCreator_Changed;
|
---|
429 | creator.BoundsParameter.ValueChanged += RealVectorCreator_Changed;
|
---|
430 | }
|
---|
431 | }
|
---|
432 | }
|
---|
433 | private void InitializeOperators() {
|
---|
434 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAInitializer>())
|
---|
435 | CMAInitializerParameter.ValidValues.Add(op);
|
---|
436 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAManipulator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
437 | CMAMutatorParameter.ValidValues.Add(op);
|
---|
438 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMARecombinator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
439 | CMARecombinatorParameter.ValidValues.Add(op);
|
---|
440 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAUpdater>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
441 | CMAUpdaterParameter.ValidValues.Add(op);
|
---|
442 | }
|
---|
443 | private void UpdateOperators() {
|
---|
444 | cmaesInitializerSync = true;
|
---|
445 | try {
|
---|
446 | var oldMutator = CMAMutator;
|
---|
447 | var oldRecombinator = CMARecombinator;
|
---|
448 | var oldUpdater = CMAUpdater;
|
---|
449 |
|
---|
450 | if (CMAInitializer != null && (oldMutator == null || oldMutator.CMAType != CMAInitializer.CMAType)) {
|
---|
451 | CMAMutatorParameter.ValidValues.Clear();
|
---|
452 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAManipulator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
453 | CMAMutatorParameter.ValidValues.Add(op);
|
---|
454 | CMAMutator = CMAMutatorParameter.ValidValues.First();
|
---|
455 | }
|
---|
456 |
|
---|
457 | if (CMAInitializer != null && (oldRecombinator == null || oldRecombinator.CMAType != CMAInitializer.CMAType)) {
|
---|
458 | CMARecombinatorParameter.ValidValues.Clear();
|
---|
459 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMARecombinator>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
460 | CMARecombinatorParameter.ValidValues.Add(op);
|
---|
461 | CMARecombinator = CMARecombinatorParameter.ValidValues.First();
|
---|
462 | }
|
---|
463 |
|
---|
464 | if (CMAInitializer != null && (oldUpdater == null || oldUpdater.CMAType != CMAInitializer.CMAType)) {
|
---|
465 | CMAUpdaterParameter.ValidValues.Clear();
|
---|
466 | foreach (var op in ApplicationManager.Manager.GetInstances<ICMAUpdater>().Where(x => x.CMAType == CMAInitializer.CMAType))
|
---|
467 | CMAUpdaterParameter.ValidValues.Add(op);
|
---|
468 | CMAUpdater = CMAUpdaterParameter.ValidValues.First();
|
---|
469 | }
|
---|
470 | } finally { cmaesInitializerSync = false; }
|
---|
471 | }
|
---|
472 | private void UpdateAnalyzers() {
|
---|
473 | Analyzer.Operators.Clear();
|
---|
474 | if (Problem != null) {
|
---|
475 | foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
|
---|
476 | foreach (var param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
|
---|
477 | param.Depth = 1;
|
---|
478 | Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
|
---|
479 | }
|
---|
480 | }
|
---|
481 | Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
|
---|
482 | Analyzer.Operators.Add(cmaAnalyzer, cmaAnalyzer.EnabledByDefault);
|
---|
483 | }
|
---|
484 | private void Parameterize() {
|
---|
485 |
|
---|
486 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
487 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
488 | op.PopulationSizeParameter.Hidden = true;
|
---|
489 | op.MuParameter.ActualName = MuParameter.Name;
|
---|
490 | op.MuParameter.Hidden = true;
|
---|
491 | op.InitialIterationsParameter.Value = null;
|
---|
492 | op.InitialIterationsParameter.ActualName = InitialIterationsParameter.Name;
|
---|
493 | op.InitialIterationsParameter.Hidden = true;
|
---|
494 | op.InitialSigmaParameter.Value = null;
|
---|
495 | op.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
496 | op.InitialSigmaParameter.Hidden = true;
|
---|
497 |
|
---|
498 | op.DimensionParameter.Hidden = false;
|
---|
499 |
|
---|
500 | if (op is IStochasticOperator)
|
---|
501 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
502 | if (op is IIterationBasedOperator) {
|
---|
503 | ((IIterationBasedOperator)op).IterationsParameter.ActualName = "Generations";
|
---|
504 | ((IIterationBasedOperator)op).MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
509 | op.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
|
---|
510 | op.PopulationSizeParameter.Hidden = true;
|
---|
511 |
|
---|
512 | op.MeanParameter.Hidden = false;
|
---|
513 | op.BoundsParameter.Hidden = false;
|
---|
514 | op.RealVectorParameter.Hidden = false;
|
---|
515 |
|
---|
516 | if (op is IStochasticOperator)
|
---|
517 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
518 | if (op is IIterationBasedOperator) {
|
---|
519 | ((IIterationBasedOperator)op).IterationsParameter.ActualName = "Generations";
|
---|
520 | ((IIterationBasedOperator)op).MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
521 | }
|
---|
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 | if (op is IStochasticOperator)
|
---|
532 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
533 | if (op is IIterationBasedOperator) {
|
---|
534 | ((IIterationBasedOperator)op).IterationsParameter.ActualName = "Generations";
|
---|
535 | ((IIterationBasedOperator)op).MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
536 | }
|
---|
537 | }
|
---|
538 |
|
---|
539 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
540 | op.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
541 | op.MaximumEvaluatedSolutionsParameter.Hidden = true;
|
---|
542 | op.OldMeanParameter.ActualName = "XOld";
|
---|
543 | op.OldMeanParameter.Hidden = true;
|
---|
544 |
|
---|
545 | op.MeanParameter.Hidden = false;
|
---|
546 | op.OffspringParameter.Hidden = false;
|
---|
547 | op.QualityParameter.Hidden = false;
|
---|
548 |
|
---|
549 | if (op is IStochasticOperator)
|
---|
550 | ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
551 | if (op is IIterationBasedOperator) {
|
---|
552 | ((IIterationBasedOperator)op).IterationsParameter.ActualName = "Generations";
|
---|
553 | ((IIterationBasedOperator)op).MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
554 | }
|
---|
555 | }
|
---|
556 |
|
---|
557 | terminator.IterationsParameter.ActualName = "Generations";
|
---|
558 | terminator.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
559 | terminator.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
|
---|
560 | terminator.InitialSigmaParameter.ActualName = InitialSigmaParameter.Name;
|
---|
561 | terminator.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
|
---|
562 | terminator.MaximumStandardDeviationChangeParameter.ActualName = MaximumStandardDeviationChangeParameter.Name;
|
---|
563 | terminator.MinimumQualityChangeParameter.ActualName = MinimumQualityChangeParameter.Name;
|
---|
564 | terminator.MinimumQualityHistoryChangeParameter.ActualName = MinimumQualityHistoryChangeParameter.Name;
|
---|
565 | terminator.MinimumStandardDeviationParameter.ActualName = MinimumStandardDeviationParameter.Name;
|
---|
566 | terminator.TargetQualityParameter.ActualName = TargetQualityParameter.Name;
|
---|
567 | if (CMAUpdater != null)
|
---|
568 | terminator.DegenerateStateParameter.ActualName = CMAUpdater.DegenerateStateParameter.ActualName;
|
---|
569 |
|
---|
570 | var creator = Problem != null ? (Problem.SolutionCreator as IRealVectorCreator) : null;
|
---|
571 | if (Problem != null && creator != null) {
|
---|
572 |
|
---|
573 | solutionCreator.OperatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
|
---|
574 | evaluator.OperatorParameter.ActualName = Problem.EvaluatorParameter.Name;
|
---|
575 | sorter.DescendingParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
576 | sorter.ValueParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
577 | terminator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
578 | terminator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
|
---|
579 |
|
---|
580 | foreach (var op in CMAInitializerParameter.ValidValues) {
|
---|
581 | if (creator.LengthParameter.Value == null) {
|
---|
582 | op.DimensionParameter.ActualName = creator.LengthParameter.ActualName;
|
---|
583 | op.DimensionParameter.Value = null;
|
---|
584 | } else op.DimensionParameter.Value = creator.LengthParameter.Value;
|
---|
585 | op.DimensionParameter.Hidden = true;
|
---|
586 | }
|
---|
587 |
|
---|
588 | foreach (var op in CMAMutatorParameter.ValidValues) {
|
---|
589 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
590 | op.MeanParameter.Hidden = true;
|
---|
591 | if (creator.BoundsParameter.Value == null) {
|
---|
592 | op.BoundsParameter.ActualName = creator.BoundsParameter.ActualName;
|
---|
593 | op.BoundsParameter.Value = null;
|
---|
594 | } else op.BoundsParameter.Value = creator.BoundsParameter.Value;
|
---|
595 | op.BoundsParameter.Hidden = true;
|
---|
596 | op.RealVectorParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
597 | op.RealVectorParameter.Depth = 1;
|
---|
598 | op.RealVectorParameter.Hidden = true;
|
---|
599 | }
|
---|
600 |
|
---|
601 | foreach (var op in CMARecombinatorParameter.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 | }
|
---|
608 |
|
---|
609 | foreach (var op in CMAUpdaterParameter.ValidValues) {
|
---|
610 | op.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
611 | op.MeanParameter.Hidden = true;
|
---|
612 | op.OffspringParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
613 | op.OffspringParameter.Depth = 1;
|
---|
614 | op.OffspringParameter.Hidden = true;
|
---|
615 | op.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
616 | op.QualityParameter.Hidden = true;
|
---|
617 | }
|
---|
618 |
|
---|
619 | foreach (var op in Problem.Operators.OfType<IStochasticOperator>()) {
|
---|
620 | op.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
|
---|
621 | op.RandomParameter.Hidden = true;
|
---|
622 | }
|
---|
623 |
|
---|
624 | qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
|
---|
625 | qualityAnalyzer.MaximizationParameter.Hidden = true;
|
---|
626 | qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
|
---|
627 | qualityAnalyzer.QualityParameter.Depth = 1;
|
---|
628 | qualityAnalyzer.QualityParameter.Hidden = true;
|
---|
629 | qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
|
---|
630 | qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
|
---|
631 |
|
---|
632 | cmaAnalyzer.MeanParameter.ActualName = creator.RealVectorParameter.ActualName;
|
---|
633 | cmaAnalyzer.MeanParameter.Hidden = true;
|
---|
634 |
|
---|
635 | foreach (var op in Problem.Operators.OfType<IIterationBasedOperator>()) {
|
---|
636 | op.IterationsParameter.ActualName = "Generations";
|
---|
637 | op.IterationsParameter.Hidden = true;
|
---|
638 | op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
|
---|
639 | op.MaximumIterationsParameter.Hidden = true;
|
---|
640 | }
|
---|
641 | } else {
|
---|
642 | qualityAnalyzer.MaximizationParameter.Hidden = false;
|
---|
643 | qualityAnalyzer.QualityParameter.Hidden = false;
|
---|
644 | qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
|
---|
645 |
|
---|
646 | cmaAnalyzer.MeanParameter.Hidden = false;
|
---|
647 | }
|
---|
648 |
|
---|
649 | qualityAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
650 | qualityAnalyzer.ResultsParameter.Hidden = true;
|
---|
651 | cmaAnalyzer.ResultsParameter.ActualName = "Results";
|
---|
652 | cmaAnalyzer.ResultsParameter.Hidden = true;
|
---|
653 | }
|
---|
654 | #endregion
|
---|
655 | }
|
---|
656 | }
|
---|