Free cookie consent management tool by TermsFeed Policy Generator

source: branches/RAPGA/HeuristicLab.Algorithms.RAPGA/3.3/RAPGA.cs @ 8377

Last change on this file since 8377 was 8377, checked in by jkarder, 12 years ago

#1247:

  • added support for batch offspring creation
  • added population size analyzer
  • improved operator graph
File size: 23.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.")]
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 ValueParameter<MultiAnalyzer> AnalyzerParameter {
97      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
98    }
99    private ValueParameter<IntValue> MaximumGenerationsParameter {
100      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
101    }
102    public IConstrainedValueParameter<ISolutionSimilarityCalculator> SimilarityCalculatorParameter {
103      get { return (IConstrainedValueParameter<ISolutionSimilarityCalculator>)Parameters["SimilarityCalculator"]; }
104    }
105    #endregion
106
107    #region Properties
108    public IntValue Seed {
109      get { return SeedParameter.Value; }
110      set { SeedParameter.Value = value; }
111    }
112    public BoolValue SetSeedRandomly {
113      get { return SetSeedRandomlyParameter.Value; }
114      set { SetSeedRandomlyParameter.Value = value; }
115    }
116    public IntValue PopulationSize {
117      get { return PopulationSizeParameter.Value; }
118      set { PopulationSizeParameter.Value = value; }
119    }
120    public IntValue MinimumPopulationSize {
121      get { return MinimumPopulationSizeParameter.Value; }
122      set { MinimumPopulationSizeParameter.Value = value; }
123    }
124    public IntValue MaximumPopulationSize {
125      get { return MaximumPopulationSizeParameter.Value; }
126      set { MaximumPopulationSizeParameter.Value = value; }
127    }
128    public DoubleValue ComparisonFactor {
129      get { return ComparisonFactorParameter.Value; }
130      set { ComparisonFactorParameter.Value = value; }
131    }
132    public IntValue Effort {
133      get { return EffortParameter.Value; }
134      set { EffortParameter.Value = value; }
135    }
136    public IntValue BatchSize {
137      get { return BatchSizeParameter.Value; }
138      set { BatchSizeParameter.Value = value; }
139    }
140    public ISelector Selector {
141      get { return SelectorParameter.Value; }
142      set { SelectorParameter.Value = value; }
143    }
144    public ICrossover Crossover {
145      get { return CrossoverParameter.Value; }
146      set { CrossoverParameter.Value = value; }
147    }
148    public PercentValue MutationProbability {
149      get { return MutationProbabilityParameter.Value; }
150      set { MutationProbabilityParameter.Value = value; }
151    }
152    public IManipulator Mutator {
153      get { return MutatorParameter.Value; }
154      set { MutatorParameter.Value = value; }
155    }
156    public IntValue Elites {
157      get { return ElitesParameter.Value; }
158      set { ElitesParameter.Value = value; }
159    }
160    public MultiAnalyzer Analyzer {
161      get { return AnalyzerParameter.Value; }
162      set { AnalyzerParameter.Value = value; }
163    }
164    public IntValue MaximumGenerations {
165      get { return MaximumGenerationsParameter.Value; }
166      set { MaximumGenerationsParameter.Value = value; }
167    }
168    public ISolutionSimilarityCalculator SimilarityCalculator {
169      get { return SimilarityCalculatorParameter.Value; }
170      set { SimilarityCalculatorParameter.Value = value; }
171    }
172    private RandomCreator RandomCreator {
173      get { return (RandomCreator)OperatorGraph.InitialOperator; }
174    }
175    private SolutionsCreator SolutionsCreator {
176      get { return (SolutionsCreator)RandomCreator.Successor; }
177    }
178    private RAPGAMainLoop RAPGAMainLoop {
179      get { return FindMainLoop(SolutionsCreator.Successor); }
180    }
181    [Storable]
182    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
183    [Storable]
184    private PopulationSizeAnalyzer populationSizeAnalyzer;
185    #endregion
186
187    [StorableConstructor]
188    private RAPGA(bool deserializing) : base(deserializing) { }
189    [StorableHook(HookType.AfterDeserialization)]
190    private void AfterDeserialization() { Initialize(); }
191    private RAPGA(RAPGA original, Cloner cloner)
192      : base(original, cloner) {
193      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
194      populationSizeAnalyzer = cloner.Clone(original.populationSizeAnalyzer);
195      Initialize();
196    }
197    public RAPGA()
198      : base() {
199      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
200      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
201      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
202      Parameters.Add(new ValueParameter<IntValue>("MinimumPopulationSize", "The minimum size of the population of solutions.", new IntValue(2)));
203      Parameters.Add(new ValueParameter<IntValue>("MaximumPopulationSize", "The maximum size of the population of solutions.", new IntValue(300)));
204      Parameters.Add(new ValueParameter<DoubleValue>("ComparisonFactor", "The comparison factor.", new DoubleValue(0.0)));
205      Parameters.Add(new ValueParameter<IntValue>("Effort", "The maximum number of offspring created in each generation.", new IntValue(1000)));
206      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)));
207      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
208      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
209      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
210      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
211      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
212      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
213      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
214      Parameters.Add(new ConstrainedValueParameter<ISolutionSimilarityCalculator>("SimilarityCalculator", "The operator used to calculate the similarity between two solutions."));
215
216      RandomCreator randomCreator = new RandomCreator();
217      SolutionsCreator solutionsCreator = new SolutionsCreator();
218      SubScopesCounter subScopesCounter = new SubScopesCounter();
219      ResultsCollector resultsCollector = new ResultsCollector();
220      RAPGAMainLoop mainLoop = new RAPGAMainLoop();
221      OperatorGraph.InitialOperator = randomCreator;
222
223      randomCreator.RandomParameter.ActualName = "Random";
224      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
225      randomCreator.SeedParameter.Value = null;
226      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
227      randomCreator.SetSeedRandomlyParameter.Value = null;
228      randomCreator.Successor = solutionsCreator;
229
230      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
231      solutionsCreator.Successor = subScopesCounter;
232
233      subScopesCounter.Name = "Initialize EvaluatedSolutions";
234      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
235      subScopesCounter.Successor = resultsCollector;
236
237      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
238      resultsCollector.ResultsParameter.ActualName = "Results";
239      resultsCollector.Successor = mainLoop;
240
241      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
242      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
243      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
244      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
245      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
246      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
247      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
248      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
249      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
250      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
251      mainLoop.ResultsParameter.ActualName = "Results";
252
253      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
254        SelectorParameter.ValidValues.Add(selector);
255      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
256      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
257      ParameterizeSelectors();
258
259      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
260      populationSizeAnalyzer = new PopulationSizeAnalyzer();
261      ParameterizeAnalyzers();
262      UpdateAnalyzers();
263
264      Initialize();
265    }
266    public override IDeepCloneable Clone(Cloner cloner) {
267      return new RAPGA(this, cloner);
268    }
269
270    public override void Prepare() {
271      if (Problem != null && SimilarityCalculator != null) base.Prepare();
272    }
273
274    #region Events
275    protected override void OnProblemChanged() {
276      ParameterizeStochasticOperator(Problem.SolutionCreator);
277      ParameterizeStochasticOperator(Problem.Evaluator);
278      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
279      ParameterizeSolutionsCreator();
280      ParameterizeSelectors();
281      ParameterizeAnalyzers();
282      ParameterizeIterationBasedOperators();
283      UpdateCrossovers();
284      UpdateMutators();
285      UpdateAnalyzers();
286      UpdateSimilarityCalculators();
287      ParameterizeRAPGAMainLoop();
288      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
289      base.OnProblemChanged();
290    }
291
292    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
293      ParameterizeStochasticOperator(Problem.SolutionCreator);
294      ParameterizeSolutionsCreator();
295      base.Problem_SolutionCreatorChanged(sender, e);
296    }
297    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
298      ParameterizeStochasticOperator(Problem.Evaluator);
299      ParameterizeSolutionsCreator();
300      ParameterizeRAPGAMainLoop();
301      ParameterizeSelectors();
302      ParameterizeAnalyzers();
303      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
304      base.Problem_EvaluatorChanged(sender, e);
305    }
306    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
307      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
308      ParameterizeIterationBasedOperators();
309      UpdateCrossovers();
310      UpdateMutators();
311      UpdateAnalyzers();
312      UpdateSimilarityCalculators();
313      ParameterizeRAPGAMainLoop();
314      base.Problem_OperatorsChanged(sender, e);
315    }
316    void BatchSizeParameter_ValueChanged(object sender, EventArgs e) {
317      BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
318      ParameterizeSelectors();
319    }
320    void BatchSize_ValueChanged(object sender, EventArgs e) {
321      ParameterizeSelectors();
322    }
323    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
324      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
325      ParameterizeSelectors();
326    }
327    private void Elites_ValueChanged(object sender, EventArgs e) {
328      ParameterizeSelectors();
329    }
330
331    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
332      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
333      ParameterizeSelectors();
334    }
335    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
336      ParameterizeSelectors();
337    }
338    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
339      ParameterizeRAPGAMainLoop();
340      ParameterizeSelectors();
341      ParameterizeAnalyzers();
342    }
343    #endregion
344
345    #region Helpers
346    private void Initialize() {
347      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
348      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
349      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
350      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
351      BatchSizeParameter.ValueChanged += new EventHandler(BatchSizeParameter_ValueChanged);
352      BatchSize.ValueChanged += new EventHandler(BatchSize_ValueChanged);
353      if (Problem != null) {
354        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
355      }
356    }
357
358    private void ParameterizeSolutionsCreator() {
359      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
360      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
361    }
362    private void ParameterizeRAPGAMainLoop() {
363      RAPGAMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
364      RAPGAMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
365      RAPGAMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
366      foreach (ISimilarityBasedOperator op in RAPGAMainLoop.OperatorGraph.Operators.OfType<ISimilarityBasedOperator>())
367        op.SimilarityCalculator = SimilarityCalculator;
368    }
369    private void ParameterizeStochasticOperator(IOperator op) {
370      IStochasticOperator stochasticOp = op as IStochasticOperator;
371      if (stochasticOp != null) {
372        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
373        stochasticOp.RandomParameter.Hidden = true;
374      }
375    }
376    private void ParameterizeSelectors() {
377      foreach (ISelector selector in SelectorParameter.ValidValues) {
378        selector.CopySelected = new BoolValue(true);
379        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * BatchSize.Value);
380        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
381        ParameterizeStochasticOperator(selector);
382      }
383      if (Problem != null) {
384        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
385          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
386          selector.MaximizationParameter.Hidden = true;
387          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
388          selector.QualityParameter.Hidden = true;
389        }
390      }
391    }
392    private void ParameterizeAnalyzers() {
393      qualityAnalyzer.ResultsParameter.ActualName = "Results";
394      qualityAnalyzer.ResultsParameter.Hidden = true;
395      populationSizeAnalyzer.ResultsParameter.ActualName = "Results";
396      populationSizeAnalyzer.ResultsParameter.Hidden = true;
397      if (Problem != null) {
398        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
399        qualityAnalyzer.MaximizationParameter.Hidden = true;
400        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
401        qualityAnalyzer.QualityParameter.Depth = 1;
402        qualityAnalyzer.QualityParameter.Hidden = true;
403        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
404        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
405      }
406    }
407    private void ParameterizeIterationBasedOperators() {
408      if (Problem != null) {
409        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
410          op.IterationsParameter.ActualName = "Generations";
411          op.IterationsParameter.Hidden = true;
412          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
413          op.MaximumIterationsParameter.Hidden = true;
414        }
415      }
416    }
417    private void UpdateCrossovers() {
418      ICrossover oldCrossover = CrossoverParameter.Value;
419      CrossoverParameter.ValidValues.Clear();
420      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
421
422      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
423        CrossoverParameter.ValidValues.Add(crossover);
424
425      if (oldCrossover != null) {
426        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
427        if (crossover != null) CrossoverParameter.Value = crossover;
428        else oldCrossover = null;
429      }
430      if (oldCrossover == null && defaultCrossover != null)
431        CrossoverParameter.Value = defaultCrossover;
432    }
433    private void UpdateMutators() {
434      IManipulator oldMutator = MutatorParameter.Value;
435      MutatorParameter.ValidValues.Clear();
436      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
437        MutatorParameter.ValidValues.Add(mutator);
438      if (oldMutator != null) {
439        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
440        if (mutator != null) MutatorParameter.Value = mutator;
441      }
442    }
443    private void UpdateAnalyzers() {
444      Analyzer.Operators.Clear();
445      if (Problem != null) {
446        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
447          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
448            param.Depth = 1;
449          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
450        }
451      }
452      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
453      Analyzer.Operators.Add(populationSizeAnalyzer, populationSizeAnalyzer.EnabledByDefault);
454    }
455    private void UpdateSimilarityCalculators() {
456      ISolutionSimilarityCalculator oldSimilarityCalculator = SimilarityCalculatorParameter.Value;
457      SimilarityCalculatorParameter.ValidValues.Clear();
458      ISolutionSimilarityCalculator defaultSimilarityCalculator = Problem.Operators.OfType<ISolutionSimilarityCalculator>().FirstOrDefault();
459
460      foreach (ISolutionSimilarityCalculator similarityCalculator in Problem.Operators.OfType<ISolutionSimilarityCalculator>())
461        SimilarityCalculatorParameter.ValidValues.Add(similarityCalculator);
462
463      if (oldSimilarityCalculator != null) {
464        ISolutionSimilarityCalculator similarityCalculator = SimilarityCalculatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldSimilarityCalculator.GetType());
465        if (similarityCalculator != null) SimilarityCalculatorParameter.Value = similarityCalculator;
466        else oldSimilarityCalculator = null;
467      }
468      if (oldSimilarityCalculator == null && defaultSimilarityCalculator != null)
469        SimilarityCalculatorParameter.Value = defaultSimilarityCalculator;
470    }
471    private RAPGAMainLoop FindMainLoop(IOperator start) {
472      IOperator mainLoop = start;
473      while (mainLoop != null && !(mainLoop is RAPGAMainLoop))
474        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
475      if (mainLoop == null) return null;
476      else return (RAPGAMainLoop)mainLoop;
477    }
478    #endregion
479  }
480}
Note: See TracBrowser for help on using the repository browser.