Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2434_crossvalidation/HeuristicLab.Algorithms.RAPGA/3.3/RAPGA.cs @ 16189

Last change on this file since 16189 was 12770, checked in by abeham, 9 years ago

#2432: Fixed AfterDeserialization hook

File size: 27.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.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Algorithms.RAPGA {
37  /// <summary>
38  /// A relevant alleles preserving genetic algorithm.
39  /// </summary>
40  [Item("RAPGA", "A relevant alleles preserving genetic algorithm (Affenzeller, M. et al. 2007. Self-adaptive population size adjustment for genetic algorithms. Proceedings of Computer Aided Systems Theory: EuroCAST 2007, Lecture Notes in Computer Science, pp 820–828. Springer).")]
41  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 140)]
42  [StorableClass]
43  public sealed class RAPGA : HeuristicOptimizationEngineAlgorithm, IStorableContent {
44    public string Filename { get; set; }
45
46    #region Problem Properties
47    public override Type ProblemType {
48      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
49    }
50    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
51      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
52      set { base.Problem = value; }
53    }
54    #endregion
55
56    #region Parameter Properties
57    private ValueParameter<IntValue> SeedParameter {
58      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
61      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
63    private ValueParameter<IntValue> PopulationSizeParameter {
64      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
65    }
66    private IValueParameter<IntValue> MinimumPopulationSizeParameter {
67      get { return (IValueParameter<IntValue>)Parameters["MinimumPopulationSize"]; }
68    }
69    private IValueParameter<IntValue> MaximumPopulationSizeParameter {
70      get { return (IValueParameter<IntValue>)Parameters["MaximumPopulationSize"]; }
71    }
72    private IValueParameter<DoubleValue> ComparisonFactorParameter {
73      get { return (IValueParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
74    }
75    private IValueParameter<IntValue> EffortParameter {
76      get { return (IValueParameter<IntValue>)Parameters["Effort"]; }
77    }
78    private IValueParameter<IntValue> BatchSizeParameter {
79      get { return (IValueParameter<IntValue>)Parameters["BatchSize"]; }
80    }
81    public IConstrainedValueParameter<ISelector> SelectorParameter {
82      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
83    }
84    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
85      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
86    }
87    private ValueParameter<PercentValue> MutationProbabilityParameter {
88      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
89    }
90    public IConstrainedValueParameter<IManipulator> MutatorParameter {
91      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
92    }
93    private ValueParameter<IntValue> ElitesParameter {
94      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
95    }
96    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
97      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
98    }
99    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
100      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
101    }
102    private ValueParameter<IntValue> MaximumGenerationsParameter {
103      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
104    }
105    public IConstrainedValueParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
106      get { return (IConstrainedValueParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
107    }
108    #endregion
109
110    #region Properties
111    public IntValue Seed {
112      get { return SeedParameter.Value; }
113      set { SeedParameter.Value = value; }
114    }
115    public BoolValue SetSeedRandomly {
116      get { return SetSeedRandomlyParameter.Value; }
117      set { SetSeedRandomlyParameter.Value = value; }
118    }
119    public IntValue PopulationSize {
120      get { return PopulationSizeParameter.Value; }
121      set { PopulationSizeParameter.Value = value; }
122    }
123    public IntValue MinimumPopulationSize {
124      get { return MinimumPopulationSizeParameter.Value; }
125      set { MinimumPopulationSizeParameter.Value = value; }
126    }
127    public IntValue MaximumPopulationSize {
128      get { return MaximumPopulationSizeParameter.Value; }
129      set { MaximumPopulationSizeParameter.Value = value; }
130    }
131    public DoubleValue ComparisonFactor {
132      get { return ComparisonFactorParameter.Value; }
133      set { ComparisonFactorParameter.Value = value; }
134    }
135    public IntValue Effort {
136      get { return EffortParameter.Value; }
137      set { EffortParameter.Value = value; }
138    }
139    public IntValue BatchSize {
140      get { return BatchSizeParameter.Value; }
141      set { BatchSizeParameter.Value = value; }
142    }
143    public ISelector Selector {
144      get { return SelectorParameter.Value; }
145      set { SelectorParameter.Value = value; }
146    }
147    public ICrossover Crossover {
148      get { return CrossoverParameter.Value; }
149      set { CrossoverParameter.Value = value; }
150    }
151    public PercentValue MutationProbability {
152      get { return MutationProbabilityParameter.Value; }
153      set { MutationProbabilityParameter.Value = value; }
154    }
155    public IManipulator Mutator {
156      get { return MutatorParameter.Value; }
157      set { MutatorParameter.Value = value; }
158    }
159    public IntValue Elites {
160      get { return ElitesParameter.Value; }
161      set { ElitesParameter.Value = value; }
162    }
163    public bool ReevaluteElites {
164      get { return ReevaluateElitesParameter.Value.Value; }
165      set { ReevaluateElitesParameter.Value.Value = value; }
166    }
167    public MultiAnalyzer Analyzer {
168      get { return AnalyzerParameter.Value; }
169      set { AnalyzerParameter.Value = value; }
170    }
171    public IntValue MaximumGenerations {
172      get { return MaximumGenerationsParameter.Value; }
173      set { MaximumGenerationsParameter.Value = value; }
174    }
175    public ISolutionSimilarityCalculator SimilarityCalculator {
176      get { return SimilarityCalculatorParameter.Value; }
177      set { SimilarityCalculatorParameter.Value = value; }
178    }
179    private RandomCreator RandomCreator {
180      get { return (RandomCreator)OperatorGraph.InitialOperator; }
181    }
182    private SolutionsCreator SolutionsCreator {
183      get { return (SolutionsCreator)RandomCreator.Successor; }
184    }
185    private RAPGAMainLoop RAPGAMainLoop {
186      get { return FindMainLoop(SolutionsCreator.Successor); }
187    }
188    [Storable]
189    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
190    [Storable]
191    private PopulationSizeAnalyzer populationSizeAnalyzer;
192    [Storable]
193    private OffspringSuccessAnalyzer offspringSuccessAnalyzer;
194    [Storable]
195    private SelectionPressureAnalyzer selectionPressureAnalyzer;
196    #endregion
197
198    [StorableConstructor]
199    private RAPGA(bool deserializing) : base(deserializing) { }
200    [StorableHook(HookType.AfterDeserialization)]
201    private void AfterDeserialization() {
202      // BackwardsCompatibility3.3
203      #region Backwards compatible code, remove with 3.4
204      if (!Parameters.ContainsKey("ReevaluateElites")) {
205        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 });
206      }
207      if (Parameters.ContainsKey("SimilarityCalculator") && Parameters["SimilarityCalculator"] is IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>) {
208#pragma warning disable 0618
209        var oldParameter = (IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)Parameters["SimilarityCalculator"];
210#pragma warning restore 0618
211        Parameters.Remove(oldParameter);
212        var newParameter = new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions.", new ItemSet<ISolutionSimilarityCalculator>(oldParameter.ValidValues));
213        var selectedSimilarityCalculator = newParameter.ValidValues.SingleOrDefault(x => x.GetType() == oldParameter.Value.GetType());
214        newParameter.Value = selectedSimilarityCalculator;
215        Parameters.Add(newParameter);
216      }
217      #endregion
218      Initialize();
219    }
220    private RAPGA(RAPGA original, Cloner cloner)
221      : base(original, cloner) {
222      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
223      populationSizeAnalyzer = cloner.Clone(original.populationSizeAnalyzer);
224      offspringSuccessAnalyzer = cloner.Clone(original.offspringSuccessAnalyzer);
225      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
226      Initialize();
227    }
228    public RAPGA()
229      : base() {
230      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
231      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
232      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
233      Parameters.Add(new ValueParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions.", new IntValue(2)));
234      Parameters.Add(new ValueParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions.", new IntValue(300)));
235      Parameters.Add(new ValueParameter<DoubleValue>("ComparisonFactor", "The comparison factor.", new DoubleValue(0.0)));
236      Parameters.Add(new ValueParameter<IntValue>("Effort", "The maximum number of offspring created in each generation.", new IntValue(1000)));
237      Parameters.Add(new ValueParameter<IntValue>("BatchSize", "The number of children that should be created during one iteration of the offspring creation process.", new IntValue(10)));
238      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
239      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
240      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
241      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
242      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
243      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 });
244      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
245      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
246      Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
247
248      RandomCreator randomCreator = new RandomCreator();
249      SolutionsCreator solutionsCreator = new SolutionsCreator();
250      SubScopesCounter subScopesCounter = new SubScopesCounter();
251      ResultsCollector resultsCollector = new ResultsCollector();
252      RAPGAMainLoop mainLoop = new RAPGAMainLoop();
253      OperatorGraph.InitialOperator = randomCreator;
254
255      randomCreator.RandomParameter.ActualName = "Random";
256      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
257      randomCreator.SeedParameter.Value = null;
258      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
259      randomCreator.SetSeedRandomlyParameter.Value = null;
260      randomCreator.Successor = solutionsCreator;
261
262      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
263      solutionsCreator.Successor = subScopesCounter;
264
265      subScopesCounter.Name = "Initialize EvaluatedSolutions";
266      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
267      subScopesCounter.Successor = resultsCollector;
268
269      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
270      resultsCollector.ResultsParameter.ActualName = "Results";
271      resultsCollector.Successor = mainLoop;
272
273      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
274      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
275      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
276      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
277      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
278      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
279      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
280      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
281      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
282      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
283      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
284      mainLoop.ResultsParameter.ActualName = "Results";
285
286      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
287        SelectorParameter.ValidValues.Add(selector);
288      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
289      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
290      ParameterizeSelectors();
291
292      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
293      populationSizeAnalyzer = new PopulationSizeAnalyzer();
294      offspringSuccessAnalyzer = new OffspringSuccessAnalyzer();
295      selectionPressureAnalyzer = new SelectionPressureAnalyzer();
296      ParameterizeAnalyzers();
297      UpdateAnalyzers();
298
299      Initialize();
300    }
301    public override IDeepCloneable Clone(Cloner cloner) {
302      return new RAPGA(this, cloner);
303    }
304
305    public override void Prepare() {
306      if (Problem != null && SimilarityCalculator != null) base.Prepare();
307    }
308
309    #region Events
310    protected override void OnProblemChanged() {
311      ParameterizeStochasticOperator(Problem.SolutionCreator);
312      ParameterizeStochasticOperator(Problem.Evaluator);
313      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
314      ParameterizeSolutionsCreator();
315      ParameterizeSelectors();
316      ParameterizeAnalyzers();
317      ParameterizeIterationBasedOperators();
318      UpdateCrossovers();
319      UpdateMutators();
320      UpdateAnalyzers();
321      UpdateSimilarityCalculators();
322      ParameterizeRAPGAMainLoop();
323      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
324      base.OnProblemChanged();
325    }
326
327    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
328      ParameterizeStochasticOperator(Problem.SolutionCreator);
329      ParameterizeSolutionsCreator();
330      base.Problem_SolutionCreatorChanged(sender, e);
331    }
332    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
333      ParameterizeStochasticOperator(Problem.Evaluator);
334      ParameterizeSolutionsCreator();
335      ParameterizeRAPGAMainLoop();
336      ParameterizeSelectors();
337      ParameterizeAnalyzers();
338      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
339      base.Problem_EvaluatorChanged(sender, e);
340    }
341    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
342      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
343      ParameterizeIterationBasedOperators();
344      UpdateCrossovers();
345      UpdateMutators();
346      UpdateAnalyzers();
347      UpdateSimilarityCalculators();
348      ParameterizeRAPGAMainLoop();
349      base.Problem_OperatorsChanged(sender, e);
350    }
351    private void SimilarityCalculatorParameter_ValueChanged(object sender, EventArgs e) {
352      ParameterizeRAPGAMainLoop();
353    }
354    private void BatchSizeParameter_ValueChanged(object sender, EventArgs e) {
355      BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
356      ParameterizeSelectors();
357    }
358    private void BatchSize_ValueChanged(object sender, EventArgs e) {
359      ParameterizeSelectors();
360    }
361    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
362      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
363      ParameterizeSelectors();
364    }
365    private void Elites_ValueChanged(object sender, EventArgs e) {
366      ParameterizeSelectors();
367    }
368
369    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
370      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
371      ParameterizeSelectors();
372    }
373    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
374      ParameterizeSelectors();
375    }
376    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
377      ParameterizeRAPGAMainLoop();
378      ParameterizeSelectors();
379      ParameterizeAnalyzers();
380      ParameterizeSimilarityCalculators();
381    }
382    #endregion
383
384    #region Helpers
385    private void Initialize() {
386      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
387      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
388      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
389      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
390      BatchSizeParameter.ValueChanged += new EventHandler(BatchSizeParameter_ValueChanged);
391      BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
392      SimilarityCalculatorParameter.ValueChanged += new EventHandler(SimilarityCalculatorParameter_ValueChanged);
393      if (Problem != null) {
394        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
395      }
396    }
397
398    private void ParameterizeSolutionsCreator() {
399      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
400      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
401    }
402    private void ParameterizeRAPGAMainLoop() {
403      RAPGAMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
404      RAPGAMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
405      RAPGAMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
406    }
407    private void ParameterizeStochasticOperator(IOperator op) {
408      IStochasticOperator stochasticOp = op as IStochasticOperator;
409      if (stochasticOp != null) {
410        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
411        stochasticOp.RandomParameter.Hidden = true;
412      }
413    }
414    private void ParameterizeSelectors() {
415      foreach (ISelector selector in SelectorParameter.ValidValues) {
416        selector.CopySelected = new BoolValue(true);
417        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * BatchSize.Value);
418        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
419        ParameterizeStochasticOperator(selector);
420      }
421      if (Problem != null) {
422        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
423          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
424          selector.MaximizationParameter.Hidden = true;
425          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
426          selector.QualityParameter.Hidden = true;
427        }
428      }
429    }
430    private void ParameterizeAnalyzers() {
431      qualityAnalyzer.ResultsParameter.ActualName = "Results";
432      qualityAnalyzer.ResultsParameter.Hidden = true;
433      populationSizeAnalyzer.ResultsParameter.ActualName = "Results";
434      populationSizeAnalyzer.ResultsParameter.Hidden = true;
435      offspringSuccessAnalyzer.ResultsParameter.ActualName = "Results";
436      offspringSuccessAnalyzer.ResultsParameter.Hidden = true;
437      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
438      selectionPressureAnalyzer.ResultsParameter.Hidden = true;
439      if (Problem != null) {
440        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
441        qualityAnalyzer.MaximizationParameter.Hidden = true;
442        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
443        qualityAnalyzer.QualityParameter.Depth = 1;
444        qualityAnalyzer.QualityParameter.Hidden = true;
445        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
446        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
447      }
448    }
449    private void ParameterizeIterationBasedOperators() {
450      if (Problem != null) {
451        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
452          op.IterationsParameter.ActualName = "Generations";
453          op.IterationsParameter.Hidden = true;
454          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
455          op.MaximumIterationsParameter.Hidden = true;
456        }
457      }
458    }
459    private void ParameterizeSimilarityCalculators() {
460      foreach (ISolutionSimilarityCalculator calc in SimilarityCalculatorParameter.ValidValues) {
461        calc.QualityVariableName = Problem.Evaluator.QualityParameter.ActualName;
462      }
463    }
464    private void UpdateCrossovers() {
465      ICrossover oldCrossover = CrossoverParameter.Value;
466      CrossoverParameter.ValidValues.Clear();
467      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
468
469      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
470        CrossoverParameter.ValidValues.Add(crossover);
471
472      if (oldCrossover != null) {
473        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
474        if (crossover != null) CrossoverParameter.Value = crossover;
475        else oldCrossover = null;
476      }
477      if (oldCrossover == null && defaultCrossover != null)
478        CrossoverParameter.Value = defaultCrossover;
479    }
480    private void UpdateMutators() {
481      IManipulator oldMutator = MutatorParameter.Value;
482      MutatorParameter.ValidValues.Clear();
483      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
484        MutatorParameter.ValidValues.Add(mutator);
485      if (oldMutator != null) {
486        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
487        if (mutator != null) MutatorParameter.Value = mutator;
488      }
489    }
490    private void UpdateAnalyzers() {
491      Analyzer.Operators.Clear();
492      if (Problem != null) {
493        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
494          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
495            param.Depth = 1;
496          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
497        }
498      }
499      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
500      Analyzer.Operators.Add(populationSizeAnalyzer, populationSizeAnalyzer.EnabledByDefault);
501      Analyzer.Operators.Add(offspringSuccessAnalyzer, offspringSuccessAnalyzer.EnabledByDefault);
502      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
503    }
504    private void UpdateSimilarityCalculators() {
505      ISolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
506      SimilarityCalculatorParameter.ValidValues.Clear();
507      ISolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISolutionSimilarityCalculator>().FirstOrDefault();
508
509      foreach (ISolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISolutionSimilarityCalculator>())
510        SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
511
512      if (!SimilarityCalculatorParameter.ValidValues.OfType<QualitySimilarityCalculator>().Any())
513        SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator {
514          QualityVariableName = Problem.Evaluator.QualityParameter.ActualName
515        });
516      if (!SimilarityCalculatorParameter.ValidValues.OfType<NoSimilarityCalculator>().Any())
517        SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator());
518
519      if (oldSimilarityCalculator != null) {
520        ISolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
521        if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
522        else oldSimilarityCalculator = null;
523      }
524      if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
525        SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
526    }
527    private RAPGAMainLoop FindMainLoop(IOperator start) {
528      IOperator mainLoop = start;
529      while (mainLoop != null && !(mainLoop is RAPGAMainLoop))
530        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
531      if (mainLoop == null) return null;
532      else return (RAPGAMainLoop)mainLoop;
533    }
534    #endregion
535  }
536}
Note: See TracBrowser for help on using the repository browser.