Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.4/CMAEvolutionStrategy.cs @ 15160

Last change on this file since 15160 was 13361, checked in by mkommend, 9 years ago

#2521: Adapted real vector encoding, test function problems, P3, CMA-ES and optimization.

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