Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3057_DynamicALPS/TestProblems/oesr-alps-master/HeuristicLab.Algorithms.OESRALPS/GeneticAlgorithm.cs @ 17479

Last change on this file since 17479 was 17479, checked in by kyang, 4 years ago

#3057

  1. upload the latest version of ALPS with SMS-EMOA
  2. upload the related dynamic test problems (dynamic, single-objective symbolic regression), written by David Daninel.
File size: 26.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Collections.Generic;
24using System.Linq;
25using HEAL.Attic;
26using HeuristicLab.Algorithms.OESRALPS.Analyzers.Regression;
27using HeuristicLab.Analysis;
28using HeuristicLab.Common;
29using HeuristicLab.Core;
30using HeuristicLab.Data;
31using HeuristicLab.Operators;
32using HeuristicLab.Optimization;
33using HeuristicLab.Optimization.Operators;
34using HeuristicLab.Parameters;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Random;
37
38namespace HeuristicLab.Algorithms.SWGA
39{
40  /// <summary>
41  /// A genetic algorithm.
42  /// </summary>
43  [Item("Sliding window Genetic Algorithm (SWGA)", "A genetic algorithm with sliding window.")]
44  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 100)]
45  [StorableType("708F2368-F29C-47B8-8A80-E4CA5FDFEBFF")]
46
47    public sealed class GeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
48    public string Filename { get; set; }
49
50    private const string TerminateSlidingWindowParameterName = "TerminateSlidingWindow";
51       
52
53
54
55    #region Problem Properties
56    public override Type ProblemType {
57      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
58    }
59    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
60      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
61      set { base.Problem = value; }
62    }
63    #endregion
64
65    #region Parameter Properties
66    private ValueParameter<IntValue> SeedParameter {
67      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
68    }
69    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
70      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
71    }
72    private ValueParameter<IntValue> PopulationSizeParameter {
73      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
74    }
75    public IConstrainedValueParameter<ISelector> SelectorParameter {
76      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
77    }
78    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
79      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
80    }
81    private ValueParameter<PercentValue> MutationProbabilityParameter {
82      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
83    }
84    public IConstrainedValueParameter<IManipulator> MutatorParameter {
85      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
86    }
87    private ValueParameter<IntValue> ElitesParameter {
88      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
89    }
90    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
91      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
92    }
93    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
94      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
95    }
96    private ValueParameter<IntValue> MaximumGenerationsParameter {
97      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
98    }
99
100    private IValueParameter<MultiTerminator> TerminatorParameter {
101      get { return (IValueParameter<MultiTerminator>)Parameters["Terminator"]; }
102    }
103
104    public IFixedValueParameter<MultiAnalyzer> SlidingWindowParameter {
105      get { return (IFixedValueParameter<MultiAnalyzer>)Parameters["SlidingWindow"]; }
106    }
107    #endregion
108
109    #region Properties
110    public IntValue Seed {
111      get { return SeedParameter.Value; }
112      set { SeedParameter.Value = value; }
113    }
114    public BoolValue SetSeedRandomly {
115      get { return SetSeedRandomlyParameter.Value; }
116      set { SetSeedRandomlyParameter.Value = value; }
117    }
118    public IntValue PopulationSize {
119      get { return PopulationSizeParameter.Value; }
120      set { PopulationSizeParameter.Value = value; }
121    }
122    public ISelector Selector {
123      get { return SelectorParameter.Value; }
124      set { SelectorParameter.Value = value; }
125    }
126    public ICrossover Crossover {
127      get { return CrossoverParameter.Value; }
128      set { CrossoverParameter.Value = value; }
129    }
130    public PercentValue MutationProbability {
131      get { return MutationProbabilityParameter.Value; }
132      set { MutationProbabilityParameter.Value = value; }
133    }
134    public IManipulator Mutator {
135      get { return MutatorParameter.Value; }
136      set { MutatorParameter.Value = value; }
137    }
138    public IntValue Elites {
139      get { return ElitesParameter.Value; }
140      set { ElitesParameter.Value = value; }
141    }
142    public bool ReevaluteElites {
143      get { return ReevaluateElitesParameter.Value.Value; }
144      set { ReevaluateElitesParameter.Value.Value = value; }
145    }
146    public MultiAnalyzer Analyzer {
147      get { return AnalyzerParameter.Value; }
148      set { AnalyzerParameter.Value = value; }
149    }
150    public IntValue MaximumGenerations {
151      get { return MaximumGenerationsParameter.Value; }
152      set { MaximumGenerationsParameter.Value = value; }
153    }
154    private RandomCreator RandomCreator {
155      get { return (RandomCreator)OperatorGraph.InitialOperator; }
156    }
157    private SolutionsCreator SolutionsCreator {
158      get { return (SolutionsCreator)RandomCreator.Successor; }
159    }
160    private GeneticAlgorithmMainLoop GeneticAlgorithmMainLoop {
161      get { return FindMainLoop(SolutionsCreator.Successor); }
162    }
163    public MultiTerminator Terminators {
164      get { return TerminatorParameter.Value; }
165    }
166    public MultiAnalyzer SlidingWindow {
167        get { return SlidingWindowParameter.Value; }
168    }
169
170    [Storable]
171    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
172    #endregion
173
174    [Storable]
175    private SymbolicRegressionGenerationalSlidingWindowAnalyzer generationalSlidingWindowAnalyzer;
176    [Storable]
177    private SymbolicRegressionOffspringSelectionSlidingWindowAnalyzer offspringSelectionSlidingWindowAnalyze;
178    [Storable]
179    private SymbolicRegressionOverfittingSelectionSlidingWindowAnalyzer overfittingSelectionSlidingWindowAnalyzer;
180    [Storable]
181    private SymbolicRegressionAdaptiveSlidingWindowAnalyzer generationalAdaptiveSlidingWindowAnalyzer;
182
183    #region Preconfigured Terminators
184    [Storable]
185    private ComparisonTerminator<IntValue> generationsTerminator;
186    [Storable]
187    private ComparisonTerminator<IntValue> evaluationsTerminator;
188    [Storable]
189    private SingleObjectiveQualityTerminator qualityTerminator;
190    [Storable]
191    private ExecutionTimeTerminator executionTimeTerminator;
192    [Storable]
193    private ComparisonTerminator<BoolValue> slidingWindowTerminator;
194    #endregion
195
196    public GeneticAlgorithm()
197      : base() {
198      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
199      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
200      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
201      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
202      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
203      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
204      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
205      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
206      Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", new BoolValue(false)) { Hidden = true });
207      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
208      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
209
210      Parameters.Add(new ValueParameter<MultiTerminator>("Terminator", "The termination criteria that defines if the algorithm should continue or stop.", new MultiTerminator()));
211      Parameters.Add(new FixedValueParameter<MultiAnalyzer>("SlidingWindow", "Operator which moves a sliding window over the training data.", new MultiAnalyzer()));   
212      Parameters.Add(new ValueParameter<BoolValue>(TerminateSlidingWindowParameterName, "Indicates if the sliding window has reached the end of the training data set", new BoolValue(false)) { Hidden = true });
213
214      RandomCreator randomCreator = new RandomCreator();
215      SolutionsCreator solutionsCreator = new SolutionsCreator();
216      SubScopesCounter subScopesCounter = new SubScopesCounter();
217      ResultsCollector resultsCollector = new ResultsCollector();
218      GeneticAlgorithmMainLoop mainLoop = new GeneticAlgorithmMainLoop();
219      OperatorGraph.InitialOperator = randomCreator;
220
221      randomCreator.RandomParameter.ActualName = "Random";
222      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
223      randomCreator.SeedParameter.Value = null;
224      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
225      randomCreator.SetSeedRandomlyParameter.Value = null;
226      randomCreator.Successor = solutionsCreator;
227
228      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
229      solutionsCreator.Successor = subScopesCounter;
230
231      subScopesCounter.Name = "Initialize EvaluatedSolutions";
232      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
233      subScopesCounter.Successor = resultsCollector;
234
235      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
236      resultsCollector.ResultsParameter.ActualName = "Results";
237      resultsCollector.Successor = mainLoop;
238
239      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
240      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
241      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
242      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
243      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
244      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
245      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
246      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
247      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
248      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
249      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
250      mainLoop.ResultsParameter.ActualName = "Results";
251      mainLoop.TerminatorParameter.ActualName = TerminatorParameter.Name;
252      mainLoop.SlidingWindowParameter.ActualName = SlidingWindowParameter.Name;
253
254      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
255        SelectorParameter.ValidValues.Add(selector);
256      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
257      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
258      ParameterizeSelectors();
259
260      generationalSlidingWindowAnalyzer = new SymbolicRegressionGenerationalSlidingWindowAnalyzer();
261      offspringSelectionSlidingWindowAnalyze = new SymbolicRegressionOffspringSelectionSlidingWindowAnalyzer();
262      overfittingSelectionSlidingWindowAnalyzer = new SymbolicRegressionOverfittingSelectionSlidingWindowAnalyzer();
263      generationalAdaptiveSlidingWindowAnalyzer = new SymbolicRegressionAdaptiveSlidingWindowAnalyzer();
264
265      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
266
267      #region Create terminators
268      generationsTerminator = new ComparisonTerminator<IntValue>("Generations", ComparisonType.Less, new IntValue(1000)) { Name = "Generations" };
269      evaluationsTerminator = new ComparisonTerminator<IntValue>("EvaluatedSolutions", ComparisonType.Less, new IntValue(int.MaxValue)) { Name = "Evaluations" };
270      qualityTerminator = new SingleObjectiveQualityTerminator() { Name = "Quality" };
271      executionTimeTerminator = new ExecutionTimeTerminator(this, new TimeSpanValue(TimeSpan.FromMinutes(5)));
272      slidingWindowTerminator = new ComparisonTerminator<BoolValue>(TerminateSlidingWindowParameterName, ComparisonType.NotEqual, new BoolValue(true)) { Name = "Terminate sliding window" };
273      #endregion
274
275      ParameterizeAnalyzers();
276      UpdateAnalyzers();
277
278      UpdateTerminators();
279
280      Initialize();
281    }
282    [StorableConstructor]
283    private GeneticAlgorithm(StorableConstructorFlag _) : base(_) { }
284    [StorableHook(HookType.AfterDeserialization)]
285    private void AfterDeserialization() {
286      // BackwardsCompatibility3.3
287      #region Backwards compatible code, remove with 3.4
288      if (!Parameters.ContainsKey("ReevaluateElites")) {
289        Parameters.Add(new FixedValueParameter<BoolValue>("ReevaluateElites", "Flag to determine if elite individuals should be reevaluated (i.e., if stochastic fitness functions are used.)", (BoolValue)new BoolValue(false).AsReadOnly()) { Hidden = true });
290      }
291      var optionalMutatorParameter = MutatorParameter as OptionalConstrainedValueParameter<IManipulator>;
292      if (optionalMutatorParameter != null) {
293        Parameters.Remove(optionalMutatorParameter);
294        Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
295        foreach (var m in optionalMutatorParameter.ValidValues)
296          MutatorParameter.ValidValues.Add(m);
297        if (optionalMutatorParameter.Value == null) MutationProbability.Value = 0; // to guarantee that the old configuration results in the same behavior
298        else Mutator = optionalMutatorParameter.Value;
299        optionalMutatorParameter.ValidValues.Clear(); // to avoid dangling references to the old parameter its valid values are cleared
300      }
301      #endregion
302
303      Initialize();
304    }
305
306
307
308    private GeneticAlgorithm(GeneticAlgorithm original, Cloner cloner)
309      : base(original, cloner) {
310      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
311      generationalSlidingWindowAnalyzer = cloner.Clone(original.generationalSlidingWindowAnalyzer);
312      offspringSelectionSlidingWindowAnalyze = cloner.Clone(original.offspringSelectionSlidingWindowAnalyze);
313      overfittingSelectionSlidingWindowAnalyzer = cloner.Clone(original.overfittingSelectionSlidingWindowAnalyzer);
314      generationalAdaptiveSlidingWindowAnalyzer = cloner.Clone(original.generationalAdaptiveSlidingWindowAnalyzer);
315      Initialize();
316    }
317    public override IDeepCloneable Clone(Cloner cloner) {
318      return new GeneticAlgorithm(this, cloner);
319    }
320
321    public override void Prepare() {
322      if (Problem != null) base.Prepare();
323    }
324
325    #region Events
326    protected override void OnProblemChanged() {
327      ParameterizeStochasticOperator(Problem.SolutionCreator);
328      ParameterizeStochasticOperator(Problem.Evaluator);
329      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
330      ParameterizeSolutionsCreator();
331      ParameterizeGeneticAlgorithmMainLoop();
332      ParameterizeSelectors();
333      ParameterizeAnalyzers();
334      ParameterizeIterationBasedOperators();
335      UpdateCrossovers();
336      UpdateMutators();
337      UpdateAnalyzers();
338      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
339      base.OnProblemChanged();
340    }
341
342    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
343      ParameterizeStochasticOperator(Problem.SolutionCreator);
344      ParameterizeSolutionsCreator();
345      base.Problem_SolutionCreatorChanged(sender, e);
346    }
347    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
348      ParameterizeStochasticOperator(Problem.Evaluator);
349      ParameterizeSolutionsCreator();
350      ParameterizeGeneticAlgorithmMainLoop();
351      ParameterizeSelectors();
352      ParameterizeAnalyzers();
353      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
354      base.Problem_EvaluatorChanged(sender, e);
355    }
356    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
357      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
358      ParameterizeIterationBasedOperators();
359      UpdateCrossovers();
360      UpdateMutators();
361      UpdateAnalyzers();
362      base.Problem_OperatorsChanged(sender, e);
363    }
364    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
365      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
366      ParameterizeSelectors();
367    }
368    private void Elites_ValueChanged(object sender, EventArgs e) {
369      ParameterizeSelectors();
370    }
371
372    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
373      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
374      ParameterizeSelectors();
375    }
376    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
377      ParameterizeSelectors();
378    }
379    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
380      ParameterizeGeneticAlgorithmMainLoop();
381      ParameterizeSelectors();
382      ParameterizeAnalyzers();
383    }
384    #endregion
385
386    #region Helpers
387    private void Initialize() {
388      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
389      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
390      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
391      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
392      if (Problem != null) {
393        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
394      }
395    }
396
397    private void ParameterizeSolutionsCreator() {
398      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
399      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
400    }
401    private void ParameterizeGeneticAlgorithmMainLoop() {
402      GeneticAlgorithmMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
403      GeneticAlgorithmMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
404      GeneticAlgorithmMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
405    }
406    private void ParameterizeStochasticOperator(IOperator op) {
407      IStochasticOperator stochasticOp = op as IStochasticOperator;
408      if (stochasticOp != null) {
409        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
410        stochasticOp.RandomParameter.Hidden = true;
411      }
412    }
413    private void ParameterizeSelectors() {
414      foreach (ISelector selector in SelectorParameter.ValidValues) {
415        selector.CopySelected = new BoolValue(true);
416        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
417        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
418        ParameterizeStochasticOperator(selector);
419      }
420      if (Problem != null) {
421        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
422          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
423          selector.MaximizationParameter.Hidden = true;
424          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
425          selector.QualityParameter.Hidden = true;
426        }
427      }
428    }
429    private void ParameterizeAnalyzers() {
430      qualityAnalyzer.ResultsParameter.ActualName = "Results";
431      qualityAnalyzer.ResultsParameter.Hidden = true;
432      if (Problem != null) {
433        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
434        qualityAnalyzer.MaximizationParameter.Hidden = true;
435        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
436        qualityAnalyzer.QualityParameter.Depth = 1;
437        qualityAnalyzer.QualityParameter.Hidden = true;
438        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
439        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
440      }
441    }
442    private void ParameterizeIterationBasedOperators() {
443      if (Problem != null) {
444        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
445          op.IterationsParameter.ActualName = "Generations";
446          op.IterationsParameter.Hidden = true;
447          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
448          op.MaximumIterationsParameter.Hidden = true;
449        }
450      }
451    }
452    private void UpdateCrossovers() {
453      ICrossover oldCrossover = CrossoverParameter.Value;
454      CrossoverParameter.ValidValues.Clear();
455      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
456
457      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
458        CrossoverParameter.ValidValues.Add(crossover);
459
460      if (oldCrossover != null) {
461        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
462        if (crossover != null) CrossoverParameter.Value = crossover;
463        else oldCrossover = null;
464      }
465      if (oldCrossover == null && defaultCrossover != null)
466        CrossoverParameter.Value = defaultCrossover;
467    }
468    private void UpdateMutators() {
469      IManipulator oldMutator = MutatorParameter.Value;
470      MutatorParameter.ValidValues.Clear();
471      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
472
473      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
474        MutatorParameter.ValidValues.Add(mutator);
475
476      if (oldMutator != null) {
477        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
478        if (mutator != null) MutatorParameter.Value = mutator;
479        else oldMutator = null;
480      }
481
482      if (oldMutator == null && defaultMutator != null)
483        MutatorParameter.Value = defaultMutator;
484    }
485    private void UpdateAnalyzers() {
486      Analyzer.Operators.Clear();
487
488      SlidingWindow.Operators.Clear();
489
490      SlidingWindow.Operators.Add(generationalSlidingWindowAnalyzer);
491      SlidingWindow.Operators.Add(offspringSelectionSlidingWindowAnalyze, false);
492      SlidingWindow.Operators.Add(overfittingSelectionSlidingWindowAnalyzer, false);
493      SlidingWindow.Operators.Add(generationalAdaptiveSlidingWindowAnalyzer, false);
494      if (Problem != null) {
495        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
496          foreach (IScopeTreeLookupParameter 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    }
503    private GeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
504      IOperator mainLoop = start;
505      while (mainLoop != null && !(mainLoop is GeneticAlgorithmMainLoop))
506        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
507      if (mainLoop == null) return null;
508      else return (GeneticAlgorithmMainLoop)mainLoop;
509    }
510    #endregion
511
512    private void UpdateTerminators() {
513      var newTerminators = new Dictionary<ITerminator, bool> {
514        {generationsTerminator, Terminators.Operators.Contains(generationsTerminator) && Terminators.Operators.ItemChecked(generationsTerminator)},
515        {evaluationsTerminator, Terminators.Operators.Contains(evaluationsTerminator) && Terminators.Operators.ItemChecked(evaluationsTerminator)},
516        {qualityTerminator, Terminators.Operators.Contains(qualityTerminator) && Terminators.Operators.ItemChecked(qualityTerminator) },
517        {executionTimeTerminator, Terminators.Operators.Contains(executionTimeTerminator) && Terminators.Operators.ItemChecked(executionTimeTerminator)},
518        {slidingWindowTerminator, !Terminators.Operators.Contains(slidingWindowTerminator) || Terminators.Operators.ItemChecked(slidingWindowTerminator)}
519      };
520      if (Problem != null) {
521        foreach (var terminator in Problem.Operators.OfType<ITerminator>())
522          newTerminators.Add(terminator, !Terminators.Operators.Contains(terminator) || Terminators.Operators.ItemChecked(terminator));
523      }
524
525      Terminators.Operators.Clear();
526
527      foreach (var newTerminator in newTerminators)
528        Terminators.Operators.Add(newTerminator.Key, newTerminator.Value);
529    }
530  }
531}
Note: See TracBrowser for help on using the repository browser.