Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Algorithms.RAPGA/3.3/RAPGA.cs @ 12467

Last change on this file since 12467 was 9592, checked in by abeham, 12 years ago

#2038: Added tagging comment

File size: 26.6 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.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("Algorithms")]
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<ISingleObjectiveSolutionSimilarityCalculator> SimilarityCalculatorParameter {
106      get { return (IConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>)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 ISingleObjectiveSolutionSimilarityCalculator 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      #endregion
208      Initialize();
209    }
210    private RAPGA(RAPGA original, Cloner cloner)
211      : base(original, cloner) {
212      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
213      populationSizeAnalyzer = cloner.Clone(original.populationSizeAnalyzer);
214      offspringSuccessAnalyzer = cloner.Clone(original.offspringSuccessAnalyzer);
215      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
216      Initialize();
217    }
218    public RAPGA()
219      : base() {
220      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
221      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
222      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
223      Parameters.Add(new ValueParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions.", new IntValue(2)));
224      Parameters.Add(new ValueParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions.", new IntValue(300)));
225      Parameters.Add(new ValueParameter<DoubleValue>("ComparisonFactor", "The comparison factor.", new DoubleValue(0.0)));
226      Parameters.Add(new ValueParameter<IntValue>("Effort", "The maximum number of offspring created in each generation.", new IntValue(1000)));
227      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)));
228      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
229      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
230      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
231      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
232      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
233      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 });
234      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
235      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
236      Parameters.Add(new ConstrainedValueParameter<ISingleObjectiveSolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
237
238      RandomCreator randomCreator = new RandomCreator();
239      SolutionsCreator solutionsCreator = new SolutionsCreator();
240      SubScopesCounter subScopesCounter = new SubScopesCounter();
241      ResultsCollector resultsCollector = new ResultsCollector();
242      RAPGAMainLoop mainLoop = new RAPGAMainLoop();
243      OperatorGraph.InitialOperator = randomCreator;
244
245      randomCreator.RandomParameter.ActualName = "Random";
246      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
247      randomCreator.SeedParameter.Value = null;
248      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
249      randomCreator.SetSeedRandomlyParameter.Value = null;
250      randomCreator.Successor = solutionsCreator;
251
252      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
253      solutionsCreator.Successor = subScopesCounter;
254
255      subScopesCounter.Name = "Initialize EvaluatedSolutions";
256      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
257      subScopesCounter.Successor = resultsCollector;
258
259      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
260      resultsCollector.ResultsParameter.ActualName = "Results";
261      resultsCollector.Successor = mainLoop;
262
263      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
264      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
265      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
266      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
267      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
268      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
269      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
270      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
271      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
272      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
273      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
274      mainLoop.ResultsParameter.ActualName = "Results";
275
276      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
277        SelectorParameter.ValidValues.Add(selector);
278      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
279      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
280      ParameterizeSelectors();
281
282      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
283      populationSizeAnalyzer = new PopulationSizeAnalyzer();
284      offspringSuccessAnalyzer = new OffspringSuccessAnalyzer();
285      selectionPressureAnalyzer = new SelectionPressureAnalyzer();
286      ParameterizeAnalyzers();
287      UpdateAnalyzers();
288
289      Initialize();
290    }
291    public override IDeepCloneable Clone(Cloner cloner) {
292      return new RAPGA(this, cloner);
293    }
294
295    public override void Prepare() {
296      if (Problem != null && SimilarityCalculator != null) base.Prepare();
297    }
298
299    #region Events
300    protected override void OnProblemChanged() {
301      ParameterizeStochasticOperator(Problem.SolutionCreator);
302      ParameterizeStochasticOperator(Problem.Evaluator);
303      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
304      ParameterizeSolutionsCreator();
305      ParameterizeSelectors();
306      ParameterizeAnalyzers();
307      ParameterizeIterationBasedOperators();
308      UpdateCrossovers();
309      UpdateMutators();
310      UpdateAnalyzers();
311      UpdateSimilarityCalculators();
312      ParameterizeRAPGAMainLoop();
313      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
314      base.OnProblemChanged();
315    }
316
317    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
318      ParameterizeStochasticOperator(Problem.SolutionCreator);
319      ParameterizeSolutionsCreator();
320      base.Problem_SolutionCreatorChanged(sender, e);
321    }
322    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
323      ParameterizeStochasticOperator(Problem.Evaluator);
324      ParameterizeSolutionsCreator();
325      ParameterizeRAPGAMainLoop();
326      ParameterizeSelectors();
327      ParameterizeAnalyzers();
328      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
329      base.Problem_EvaluatorChanged(sender, e);
330    }
331    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
332      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
333      ParameterizeIterationBasedOperators();
334      UpdateCrossovers();
335      UpdateMutators();
336      UpdateAnalyzers();
337      UpdateSimilarityCalculators();
338      ParameterizeRAPGAMainLoop();
339      base.Problem_OperatorsChanged(sender, e);
340    }
341    private void SimilarityCalculatorParameter_ValueChanged(object sender, EventArgs e) {
342      ParameterizeRAPGAMainLoop();
343    }
344    private void BatchSizeParameter_ValueChanged(object sender, EventArgs e) {
345      BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
346      ParameterizeSelectors();
347    }
348    private void BatchSize_ValueChanged(object sender, EventArgs e) {
349      ParameterizeSelectors();
350    }
351    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
352      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
353      ParameterizeSelectors();
354    }
355    private void Elites_ValueChanged(object sender, EventArgs e) {
356      ParameterizeSelectors();
357    }
358
359    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
360      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
361      ParameterizeSelectors();
362    }
363    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
364      ParameterizeSelectors();
365    }
366    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
367      ParameterizeRAPGAMainLoop();
368      ParameterizeSelectors();
369      ParameterizeAnalyzers();
370      ParameterizeSimilarityCalculators();
371    }
372    #endregion
373
374    #region Helpers
375    private void Initialize() {
376      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
377      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
378      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
379      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
380      BatchSizeParameter.ValueChanged += new EventHandler(BatchSizeParameter_ValueChanged);
381      BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
382      SimilarityCalculatorParameter.ValueChanged += new EventHandler(SimilarityCalculatorParameter_ValueChanged);
383      if (Problem != null) {
384        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
385      }
386    }
387
388    private void ParameterizeSolutionsCreator() {
389      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
390      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
391    }
392    private void ParameterizeRAPGAMainLoop() {
393      RAPGAMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
394      RAPGAMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
395      RAPGAMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
396      foreach (ISimilarityBasedOperator op in RAPGAMainLoop.OperatorGraph.Operators.OfType<ISimilarityBasedOperator>())
397        op.SimilarityCalculator = SimilarityCalculator;
398    }
399    private void ParameterizeStochasticOperator(IOperator op) {
400      IStochasticOperator stochasticOp = op as IStochasticOperator;
401      if (stochasticOp != null) {
402        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
403        stochasticOp.RandomParameter.Hidden = true;
404      }
405    }
406    private void ParameterizeSelectors() {
407      foreach (ISelector selector in SelectorParameter.ValidValues) {
408        selector.CopySelected = new BoolValue(true);
409        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * BatchSize.Value);
410        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
411        ParameterizeStochasticOperator(selector);
412      }
413      if (Problem != null) {
414        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
415          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
416          selector.MaximizationParameter.Hidden = true;
417          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
418          selector.QualityParameter.Hidden = true;
419        }
420      }
421    }
422    private void ParameterizeAnalyzers() {
423      qualityAnalyzer.ResultsParameter.ActualName = "Results";
424      qualityAnalyzer.ResultsParameter.Hidden = true;
425      populationSizeAnalyzer.ResultsParameter.ActualName = "Results";
426      populationSizeAnalyzer.ResultsParameter.Hidden = true;
427      offspringSuccessAnalyzer.ResultsParameter.ActualName = "Results";
428      offspringSuccessAnalyzer.ResultsParameter.Hidden = true;
429      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
430      selectionPressureAnalyzer.ResultsParameter.Hidden = true;
431      if (Problem != null) {
432        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
433        qualityAnalyzer.MaximizationParameter.Hidden = true;
434        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
435        qualityAnalyzer.QualityParameter.Depth = 1;
436        qualityAnalyzer.QualityParameter.Hidden = true;
437        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
438        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
439      }
440    }
441    private void ParameterizeIterationBasedOperators() {
442      if (Problem != null) {
443        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
444          op.IterationsParameter.ActualName = "Generations";
445          op.IterationsParameter.Hidden = true;
446          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
447          op.MaximumIterationsParameter.Hidden = true;
448        }
449      }
450    }
451    private void ParameterizeSimilarityCalculators() {
452      foreach (ISingleObjectiveSolutionSimilarityCalculator calc in SimilarityCalculatorParameter.ValidValues) {
453        calc.QualityVariableName = Problem.Evaluator.QualityParameter.ActualName;
454      }
455    }
456    private void UpdateCrossovers() {
457      ICrossover oldCrossover = CrossoverParameter.Value;
458      CrossoverParameter.ValidValues.Clear();
459      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
460
461      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
462        CrossoverParameter.ValidValues.Add(crossover);
463
464      if (oldCrossover != null) {
465        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
466        if (crossover != null) CrossoverParameter.Value = crossover;
467        else oldCrossover = null;
468      }
469      if (oldCrossover == null && defaultCrossover != null)
470        CrossoverParameter.Value = defaultCrossover;
471    }
472    private void UpdateMutators() {
473      IManipulator oldMutator = MutatorParameter.Value;
474      MutatorParameter.ValidValues.Clear();
475      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
476        MutatorParameter.ValidValues.Add(mutator);
477      if (oldMutator != null) {
478        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
479        if (mutator != null) MutatorParameter.Value = mutator;
480      }
481    }
482    private void UpdateAnalyzers() {
483      Analyzer.Operators.Clear();
484      if (Problem != null) {
485        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
486          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
487            param.Depth = 1;
488          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
489        }
490      }
491      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
492      Analyzer.Operators.Add(populationSizeAnalyzer, populationSizeAnalyzer.EnabledByDefault);
493      Analyzer.Operators.Add(offspringSuccessAnalyzer, offspringSuccessAnalyzer.EnabledByDefault);
494      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
495    }
496    private void UpdateSimilarityCalculators() {
497      ISingleObjectiveSolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
498      SimilarityCalculatorParameter.ValidValues.Clear();
499      ISingleObjectiveSolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>().FirstOrDefault();
500
501      SimilarityCalculatorParameter.ValidValues.Add(new QualitySimilarityCalculator { QualityVariableName = Problem.Evaluator.QualityParameter.ActualName });
502      SimilarityCalculatorParameter.ValidValues.Add(new NoSimilarityCalculator());
503
504      foreach (ISingleObjectiveSolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISingleObjectiveSolutionSimilarityCalculator>())
505        SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
506
507      if (oldSimilarityCalculator != null) {
508        ISingleObjectiveSolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
509        if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
510        else oldSimilarityCalculator = null;
511      }
512      if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
513        SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
514    }
515    private RAPGAMainLoop FindMainLoop(IOperator start) {
516      IOperator mainLoop = start;
517      while (mainLoop != null && !(mainLoop is RAPGAMainLoop))
518        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
519      if (mainLoop == null) return null;
520      else return (RAPGAMainLoop)mainLoop;
521    }
522    #endregion
523  }
524}
Note: See TracBrowser for help on using the repository browser.