Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.CMAEvolutionStrategy/3.3/CMAEvolutionStrategy.cs @ 10188

Last change on this file since 10188 was 9709, checked in by abeham, 11 years ago

#1961: implemented reviewer comments

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