Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.EvolutionStrategy/3.3/EvolutionStrategy.cs @ 5356

Last change on this file since 5356 was 5356, checked in by abeham, 14 years ago

#1344

  • Adapted all algorithms to count evaluated solutions / moves and outside of the parallel region
  • Used the same pattern in every algorithm: Initialize and collect the variable in the Algorithm and increment it in the main loops and main operators
  • Property svn:mime-type set to application/octet-stream
File size: 50.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Random;
34
35namespace HeuristicLab.Algorithms.EvolutionStrategy {
36  /// <summary>
37  /// A standard genetic algorithm.
38  /// </summary>
39  [Item("Evolution Strategy", "An evolution strategy.")]
40  [Creatable("Algorithms")]
41  [StorableClass]
42  public sealed class EvolutionStrategy : EngineAlgorithm, IStorableContent {
43    public string Filename { get; set; }
44
45    #region Problem Properties
46    public override Type ProblemType {
47      get { return typeof(ISingleObjectiveProblem); }
48    }
49    public new ISingleObjectiveProblem Problem {
50      get { return (ISingleObjectiveProblem)base.Problem; }
51      set { base.Problem = value; }
52    }
53    #endregion
54
55    #region Parameter Properties
56    private ValueParameter<IntValue> SeedParameter {
57      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
58    }
59    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
60      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
61    }
62    private ValueParameter<IntValue> PopulationSizeParameter {
63      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
64    }
65    private ValueParameter<IntValue> ParentsPerChildParameter {
66      get { return (ValueParameter<IntValue>)Parameters["ParentsPerChild"]; }
67    }
68    private ValueParameter<IntValue> ChildrenParameter {
69      get { return (ValueParameter<IntValue>)Parameters["Children"]; }
70    }
71    private ValueParameter<IntValue> MaximumGenerationsParameter {
72      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
73    }
74    private ValueParameter<BoolValue> PlusSelectionParameter {
75      get { return (ValueParameter<BoolValue>)Parameters["PlusSelection"]; }
76    }
77    private ConstrainedValueParameter<IManipulator> MutatorParameter {
78      get { return (ConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
79    }
80    private OptionalConstrainedValueParameter<ICrossover> RecombinatorParameter {
81      get { return (OptionalConstrainedValueParameter<ICrossover>)Parameters["Recombinator"]; }
82    }
83    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
84      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
85    }
86    private OptionalConstrainedValueParameter<IStrategyParameterCreator> StrategyParameterCreatorParameter {
87      get { return (OptionalConstrainedValueParameter<IStrategyParameterCreator>)Parameters["StrategyParameterCreator"]; }
88    }
89    private OptionalConstrainedValueParameter<IStrategyParameterCrossover> StrategyParameterCrossoverParameter {
90      get { return (OptionalConstrainedValueParameter<IStrategyParameterCrossover>)Parameters["StrategyParameterCrossover"]; }
91    }
92    private OptionalConstrainedValueParameter<IStrategyParameterManipulator> StrategyParameterManipulatorParameter {
93      get { return (OptionalConstrainedValueParameter<IStrategyParameterManipulator>)Parameters["StrategyParameterManipulator"]; }
94    }
95    #endregion
96
97    #region Properties
98    public IntValue Seed {
99      get { return SeedParameter.Value; }
100      set { SeedParameter.Value = value; }
101    }
102    public BoolValue SetSeedRandomly {
103      get { return SetSeedRandomlyParameter.Value; }
104      set { SetSeedRandomlyParameter.Value = value; }
105    }
106    public IntValue PopulationSize {
107      get { return PopulationSizeParameter.Value; }
108      set { PopulationSizeParameter.Value = value; }
109    }
110    public IntValue ParentsPerChild {
111      get { return ParentsPerChildParameter.Value; }
112      set { ParentsPerChildParameter.Value = value; }
113    }
114    public IntValue Children {
115      get { return ChildrenParameter.Value; }
116      set { ChildrenParameter.Value = value; }
117    }
118    public IntValue MaximumGenerations {
119      get { return MaximumGenerationsParameter.Value; }
120      set { MaximumGenerationsParameter.Value = value; }
121    }
122    private BoolValue PlusSelection {
123      get { return PlusSelectionParameter.Value; }
124      set { PlusSelectionParameter.Value = value; }
125    }
126    public IManipulator Mutator {
127      get { return MutatorParameter.Value; }
128      set { MutatorParameter.Value = value; }
129    }
130    public ICrossover Recombinator {
131      get { return RecombinatorParameter.Value; }
132      set { RecombinatorParameter.Value = value; }
133    }
134    public MultiAnalyzer Analyzer {
135      get { return AnalyzerParameter.Value; }
136      set { AnalyzerParameter.Value = value; }
137    }
138    public IStrategyParameterCreator StrategyParameterCreator {
139      get { return StrategyParameterCreatorParameter.Value; }
140      set { StrategyParameterCreatorParameter.Value = value; }
141    }
142    public IStrategyParameterCrossover StrategyParameterCrossover {
143      get { return StrategyParameterCrossoverParameter.Value; }
144      set { StrategyParameterCrossoverParameter.Value = value; }
145    }
146    public IStrategyParameterManipulator StrategyParameterManipulator {
147      get { return StrategyParameterManipulatorParameter.Value; }
148      set { StrategyParameterManipulatorParameter.Value = value; }
149    }
150
151    private RandomCreator RandomCreator {
152      get { return (RandomCreator)OperatorGraph.InitialOperator; }
153    }
154    private SolutionsCreator SolutionsCreator {
155      get { return (SolutionsCreator)RandomCreator.Successor; }
156    }
157    private EvolutionStrategyMainLoop MainLoop {
158      get {
159        return (EvolutionStrategyMainLoop)(
160          (ResultsCollector)(
161            (UniformSubScopesProcessor)(
162              (SubScopesCounter)SolutionsCreator.Successor
163            ).Successor
164          ).Successor
165        ).Successor;
166      }
167    }
168    [Storable]
169    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
170    #endregion
171
172    public EvolutionStrategy()
173      : base() {
174      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
175      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
176      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population.", new IntValue(20)));
177      Parameters.Add(new ValueParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined.", new IntValue(1)));
178      Parameters.Add(new ValueParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population.", new IntValue(100)));
179      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
180      Parameters.Add(new ValueParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population).", new BoolValue(true)));
181      Parameters.Add(new OptionalConstrainedValueParameter<ICrossover>("Recombinator", "The operator used to cross solutions."));
182      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
183      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCreator>("StrategyParameterCreator", "The operator that creates the strategy parameters."));
184      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCrossover>("StrategyParameterCrossover", "The operator that recombines the strategy parameters."));
185      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterManipulator>("StrategyParameterManipulator", "The operator that manipulates the strategy parameters."));
186      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
187
188      RandomCreator randomCreator = new RandomCreator();
189      SolutionsCreator solutionsCreator = new SolutionsCreator();
190      SubScopesCounter subScopesCounter = new SubScopesCounter();
191      UniformSubScopesProcessor strategyVectorProcessor = new UniformSubScopesProcessor();
192      Placeholder strategyVectorCreator = new Placeholder();
193      ResultsCollector resultsCollector = new ResultsCollector();
194      EvolutionStrategyMainLoop mainLoop = new EvolutionStrategyMainLoop();
195      OperatorGraph.InitialOperator = randomCreator;
196
197      randomCreator.RandomParameter.ActualName = "Random";
198      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
199      randomCreator.SeedParameter.Value = null;
200      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
201      randomCreator.SetSeedRandomlyParameter.Value = null;
202      randomCreator.Successor = solutionsCreator;
203
204      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
205      solutionsCreator.Successor = subScopesCounter;
206
207      subScopesCounter.Name = "Initialize EvaluatedSolutions";
208      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
209      subScopesCounter.Successor = strategyVectorProcessor;
210
211      strategyVectorProcessor.Operator = strategyVectorCreator;
212      strategyVectorProcessor.Successor = resultsCollector;
213
214      strategyVectorCreator.OperatorParameter.ActualName = "StrategyParameterCreator";
215
216      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
217      resultsCollector.ResultsParameter.ActualName = "Results";
218      resultsCollector.Successor = mainLoop;
219
220      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
221      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
222      mainLoop.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
223      mainLoop.ChildrenParameter.ActualName = ChildrenParameter.Name;
224      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
225      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
226      mainLoop.RecombinatorParameter.ActualName = RecombinatorParameter.Name;
227      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
228      mainLoop.ResultsParameter.ActualName = "Results";
229      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
230
231      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
232      ParameterizeAnalyzers();
233      UpdateAnalyzers();
234
235      Initialize();
236    }
237    [StorableConstructor]
238    private EvolutionStrategy(bool deserializing) : base(deserializing) { }
239    [StorableHook(HookType.AfterDeserialization)]
240    private void AfterDeserialization() {
241      Initialize();
242    }
243
244    private EvolutionStrategy(EvolutionStrategy original, Cloner cloner)
245      : base(original, cloner) {
246      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
247      Initialize();
248    }
249    public override IDeepCloneable Clone(Cloner cloner) {
250      return new EvolutionStrategy(this, cloner);
251    }
252
253    public override void Prepare() {
254      if (Problem != null) base.Prepare();
255    }
256
257    #region Events
258    protected override void OnProblemChanged() {
259      ParameterizeStochasticOperator(Problem.SolutionCreator);
260      ParameterizeStochasticOperator(Problem.Evaluator);
261      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
262      ParameterizeSolutionsCreator();
263      ParameterizeMainLoop();
264      ParameterizeAnalyzers();
265      ParameterizeIterationBasedOperators();
266      UpdateRecombinators();
267      UpdateMutators();
268      UpdateAnalyzers();
269      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
270      base.OnProblemChanged();
271    }
272    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
273      ParameterizeStochasticOperator(Problem.SolutionCreator);
274      ParameterizeSolutionsCreator();
275      base.Problem_SolutionCreatorChanged(sender, e);
276    }
277    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
278      ParameterizeStochasticOperator(Problem.Evaluator);
279      ParameterizeSolutionsCreator();
280      ParameterizeMainLoop();
281      ParameterizeAnalyzers();
282      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
283      base.Problem_EvaluatorChanged(sender, e);
284    }
285    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
286      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
287      ParameterizeIterationBasedOperators();
288      UpdateRecombinators();
289      UpdateMutators();
290      UpdateAnalyzers();
291      base.Problem_OperatorsChanged(sender, e);
292    }
293    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
294      ParameterizeMainLoop();
295      ParameterizeAnalyzers();
296    }
297    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
298      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
299      PopulationSize_ValueChanged(null, EventArgs.Empty);
300    }
301    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
302      if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
303      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
304        Children.Value = PopulationSize.Value;
305      if (PopulationSize.Value < ParentsPerChild.Value)
306        ParentsPerChild.Value = PopulationSize.Value;
307    }
308    private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
309      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
310      ParentsPerChild_ValueChanged(null, EventArgs.Empty);
311    }
312    private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
313      if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
314        ParentsPerChild.Value = 1;
315      if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
316      if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
317        PopulationSize.Value = ParentsPerChild.Value;
318    }
319    private void ChildrenParameter_ValueChanged(object sender, EventArgs e) {
320      Children.ValueChanged += new EventHandler(Children_ValueChanged);
321      Children_ValueChanged(null, EventArgs.Empty);
322    }
323    private void Children_ValueChanged(object sender, EventArgs e) {
324      if (Children.Value <= 0) Children.Value = 1;
325      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
326        PopulationSize.Value = Children.Value;
327    }
328    private void PlusSelectionParameter_ValueChanged(object sender, EventArgs e) {
329      PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
330      PlusSelection_ValueChanged(null, EventArgs.Empty);
331    }
332    private void PlusSelection_ValueChanged(object sender, EventArgs e) {
333      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
334        Children.Value = PopulationSize.Value;
335    }
336    private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
337      if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
338      else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
339      if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
340        if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
341          StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
342      }
343    }
344    private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
345      if (Mutator is ISelfAdaptiveManipulator) {
346        UpdateStrategyParameterOperators();
347      } else {
348        StrategyParameterCreatorParameter.ValidValues.Clear();
349        StrategyParameterCrossoverParameter.ValidValues.Clear();
350        StrategyParameterManipulatorParameter.ValidValues.Clear();
351        UpdateRecombinators();
352      }
353    }
354    private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
355      if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
356        StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
357    }
358    private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
359      if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
360        StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
361    }
362    #endregion
363
364    #region Helpers
365    private void Initialize() {
366      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
367      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
368      ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
369      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
370      ChildrenParameter.ValueChanged += new EventHandler(ChildrenParameter_ValueChanged);
371      Children.ValueChanged += new EventHandler(Children_ValueChanged);
372      PlusSelectionParameter.ValueChanged += new EventHandler(PlusSelectionParameter_ValueChanged);
373      PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
374      RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
375      MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
376      StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
377      StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
378      if (Problem != null)
379        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
380    }
381    private void ParameterizeSolutionsCreator() {
382      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
383      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
384    }
385    private void ParameterizeMainLoop() {
386      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
387      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
388      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
389      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
390    }
391    private void ParameterizeStochasticOperator(IOperator op) {
392      if (op is IStochasticOperator)
393        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
394    }
395    private void ParameterizeAnalyzers() {
396      qualityAnalyzer.ResultsParameter.ActualName = "Results";
397      if (Problem != null) {
398        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
399        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
400        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
401      }
402    }
403    private void ParameterizeIterationBasedOperators() {
404      if (Problem != null) {
405        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
406          op.IterationsParameter.ActualName = "Generations";
407          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
408        }
409      }
410    }
411    private void UpdateStrategyParameterOperators() {
412      IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
413      IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
414      IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
415      ClearStrategyParameterOperators();
416      ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
417      if (manipulator != null) {
418        var operators = Problem.Operators.Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
419        foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
420          StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
421        foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
422          StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
423        foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
424          StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
425
426        if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
427          RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
428
429        if (oldStrategyCreator != null) {
430          IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
431          if (tmp1 != null) StrategyParameterCreator = tmp1;
432        } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
433        if (oldStrategyCrossover != null) {
434          IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
435          if (tmp2 != null) StrategyParameterCrossover = tmp2;
436        } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
437        if (oldStrategyManipulator != null) {
438          IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
439          if (tmp3 != null) StrategyParameterManipulator = tmp3;
440        } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
441      }
442    }
443    private void ClearStrategyParameterOperators() {
444      StrategyParameterCreatorParameter.ValidValues.Clear();
445      StrategyParameterCrossoverParameter.ValidValues.Clear();
446      StrategyParameterManipulatorParameter.ValidValues.Clear();
447    }
448    private void UpdateRecombinators() {
449      ICrossover oldRecombinator = Recombinator;
450      RecombinatorParameter.ValidValues.Clear();
451      foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
452        RecombinatorParameter.ValidValues.Add(recombinator);
453      }
454      if (oldRecombinator != null) {
455        ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
456        if (recombinator != null) RecombinatorParameter.Value = recombinator;
457      }
458    }
459    private void UpdateMutators() {
460      IManipulator oldMutator = MutatorParameter.Value;
461      MutatorParameter.ValidValues.Clear();
462      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
463        MutatorParameter.ValidValues.Add(mutator);
464      if (oldMutator != null) {
465        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
466        if (mutator != null) MutatorParameter.Value = mutator;
467      } else if (MutatorParameter.ValidValues.Count > 0 && Problem.Operators.OfType<ISelfAdaptiveManipulator>().Count() > 0) {
468        ISelfAdaptiveManipulator mutator = Problem.Operators.OfType<ISelfAdaptiveManipulator>().First();
469        if (mutator != null) MutatorParameter.Value = mutator;
470      }
471    }
472    private void UpdateAnalyzers() {
473      Analyzer.Operators.Clear();
474      if (Problem != null) {
475        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>())
476          Analyzer.Operators.Add(analyzer);
477      }
478      Analyzer.Operators.Add(qualityAnalyzer);
479    }
480    #endregion
481  }
482}
Note: See TracBrowser for help on using the repository browser.