Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAEvolutionStrategy.cs @ 14044

Last change on this file since 14044 was 12629, checked in by abeham, 9 years ago

#2367:

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