Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveHiveEngine/HeuristicLab.Algorithms.EvolutionStrategy/3.3/EvolutionStrategy.cs @ 9372

Last change on this file since 9372 was 7259, checked in by swagner, 13 years ago

Updated year of copyrights to 2012 (#1716)

  • Property svn:mime-type set to application/octet-stream
File size: 53.3 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.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 : HeuristicOptimizationEngineAlgorithm, IStorableContent {
43    public string Filename { get; set; }
44
45    #region Problem Properties
46    public override Type ProblemType {
47      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
48    }
49    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
50      get { return (ISingleObjectiveHeuristicOptimizationProblem)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    public ConstrainedValueParameter<IManipulator> MutatorParameter {
78      get { return (ConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
79    }
80    public 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    public OptionalConstrainedValueParameter<IStrategyParameterCreator> StrategyParameterCreatorParameter {
87      get { return (OptionalConstrainedValueParameter<IStrategyParameterCreator>)Parameters["StrategyParameterCreator"]; }
88    }
89    public OptionalConstrainedValueParameter<IStrategyParameterCrossover> StrategyParameterCrossoverParameter {
90      get { return (OptionalConstrainedValueParameter<IStrategyParameterCrossover>)Parameters["StrategyParameterCrossover"]; }
91    }
92    public 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    public 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 { return FindMainLoop(SolutionsCreator.Successor); }
159    }
160    [Storable]
161    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
162    #endregion
163
164    public EvolutionStrategy()
165      : base() {
166      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
167      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
168      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population.", new IntValue(20)));
169      Parameters.Add(new ValueParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined.", new IntValue(1)));
170      Parameters.Add(new ValueParameter<IntValue>("Children", "λ (lambda) - the size of the offspring population.", new IntValue(100)));
171      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
172      Parameters.Add(new ValueParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population).", new BoolValue(true)));
173      Parameters.Add(new OptionalConstrainedValueParameter<ICrossover>("Recombinator", "The operator used to cross solutions."));
174      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
175      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCreator>("StrategyParameterCreator", "The operator that creates the strategy parameters."));
176      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCrossover>("StrategyParameterCrossover", "The operator that recombines the strategy parameters."));
177      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterManipulator>("StrategyParameterManipulator", "The operator that manipulates the strategy parameters."));
178      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
179
180      RandomCreator randomCreator = new RandomCreator();
181      SolutionsCreator solutionsCreator = new SolutionsCreator();
182      SubScopesCounter subScopesCounter = new SubScopesCounter();
183      UniformSubScopesProcessor strategyVectorProcessor = new UniformSubScopesProcessor();
184      Placeholder strategyVectorCreator = new Placeholder();
185      ResultsCollector resultsCollector = new ResultsCollector();
186      EvolutionStrategyMainLoop mainLoop = new EvolutionStrategyMainLoop();
187      OperatorGraph.InitialOperator = randomCreator;
188
189      randomCreator.RandomParameter.ActualName = "Random";
190      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
191      randomCreator.SeedParameter.Value = null;
192      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
193      randomCreator.SetSeedRandomlyParameter.Value = null;
194      randomCreator.Successor = solutionsCreator;
195
196      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
197      solutionsCreator.Successor = subScopesCounter;
198
199      subScopesCounter.Name = "Initialize EvaluatedSolutions";
200      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
201      subScopesCounter.Successor = strategyVectorProcessor;
202
203      strategyVectorProcessor.Operator = strategyVectorCreator;
204      strategyVectorProcessor.Successor = resultsCollector;
205
206      strategyVectorCreator.OperatorParameter.ActualName = "StrategyParameterCreator";
207
208      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
209      resultsCollector.ResultsParameter.ActualName = "Results";
210      resultsCollector.Successor = mainLoop;
211
212      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
213      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
214      mainLoop.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
215      mainLoop.ChildrenParameter.ActualName = ChildrenParameter.Name;
216      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
217      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
218      mainLoop.RecombinatorParameter.ActualName = RecombinatorParameter.Name;
219      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
220      mainLoop.ResultsParameter.ActualName = "Results";
221      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
222
223      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
224      ParameterizeAnalyzers();
225      UpdateAnalyzers();
226
227      Initialize();
228    }
229    [StorableConstructor]
230    private EvolutionStrategy(bool deserializing) : base(deserializing) { }
231    [StorableHook(HookType.AfterDeserialization)]
232    private void AfterDeserialization() {
233      Initialize();
234    }
235
236    private EvolutionStrategy(EvolutionStrategy original, Cloner cloner)
237      : base(original, cloner) {
238      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
239      Initialize();
240    }
241    public override IDeepCloneable Clone(Cloner cloner) {
242      return new EvolutionStrategy(this, cloner);
243    }
244
245    public override void Prepare() {
246      if (Problem != null) base.Prepare();
247    }
248
249    #region Events
250    protected override void OnProblemChanged() {
251      ParameterizeStochasticOperator(Problem.SolutionCreator);
252      ParameterizeStochasticOperator(Problem.Evaluator);
253      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
254      ParameterizeSolutionsCreator();
255      ParameterizeMainLoop();
256      ParameterizeAnalyzers();
257      ParameterizeIterationBasedOperators();
258      UpdateRecombinators();
259      UpdateMutators();
260      UpdateAnalyzers();
261      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
262      base.OnProblemChanged();
263    }
264    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
265      ParameterizeStochasticOperator(Problem.SolutionCreator);
266      ParameterizeSolutionsCreator();
267      base.Problem_SolutionCreatorChanged(sender, e);
268    }
269    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
270      ParameterizeStochasticOperator(Problem.Evaluator);
271      ParameterizeSolutionsCreator();
272      ParameterizeMainLoop();
273      ParameterizeAnalyzers();
274      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
275      base.Problem_EvaluatorChanged(sender, e);
276    }
277    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
278      foreach (IOperator op in Problem.Operators) ParameterizeStochasticOperator(op);
279      ParameterizeIterationBasedOperators();
280      UpdateRecombinators();
281      UpdateMutators();
282      UpdateAnalyzers();
283      base.Problem_OperatorsChanged(sender, e);
284    }
285    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
286      ParameterizeMainLoop();
287      ParameterizeAnalyzers();
288    }
289    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
290      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
291      PopulationSize_ValueChanged(null, EventArgs.Empty);
292    }
293    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
294      if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
295      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
296        Children.Value = PopulationSize.Value;
297      if (PopulationSize.Value < ParentsPerChild.Value)
298        ParentsPerChild.Value = PopulationSize.Value;
299    }
300    private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
301      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
302      ParentsPerChild_ValueChanged(null, EventArgs.Empty);
303    }
304    private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
305      if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
306        ParentsPerChild.Value = 1;
307      if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
308      if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
309        PopulationSize.Value = ParentsPerChild.Value;
310    }
311    private void ChildrenParameter_ValueChanged(object sender, EventArgs e) {
312      Children.ValueChanged += new EventHandler(Children_ValueChanged);
313      Children_ValueChanged(null, EventArgs.Empty);
314    }
315    private void Children_ValueChanged(object sender, EventArgs e) {
316      if (Children.Value <= 0) Children.Value = 1;
317      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
318        PopulationSize.Value = Children.Value;
319    }
320    private void PlusSelectionParameter_ValueChanged(object sender, EventArgs e) {
321      PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
322      PlusSelection_ValueChanged(null, EventArgs.Empty);
323    }
324    private void PlusSelection_ValueChanged(object sender, EventArgs e) {
325      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
326        Children.Value = PopulationSize.Value;
327    }
328    private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
329      if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
330      else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
331      if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
332        if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
333          StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
334      }
335    }
336    private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
337      if (Mutator is ISelfAdaptiveManipulator) {
338        UpdateStrategyParameterOperators();
339      } else {
340        StrategyParameterCreatorParameter.ValidValues.Clear();
341        StrategyParameterCrossoverParameter.ValidValues.Clear();
342        StrategyParameterManipulatorParameter.ValidValues.Clear();
343        UpdateRecombinators();
344      }
345    }
346    private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
347      if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
348        StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
349    }
350    private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
351      if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
352        StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
353    }
354    #endregion
355
356    #region Helpers
357    private void Initialize() {
358      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
359      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
360      ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
361      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
362      ChildrenParameter.ValueChanged += new EventHandler(ChildrenParameter_ValueChanged);
363      Children.ValueChanged += new EventHandler(Children_ValueChanged);
364      PlusSelectionParameter.ValueChanged += new EventHandler(PlusSelectionParameter_ValueChanged);
365      PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
366      RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
367      MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
368      StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
369      StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
370      if (Problem != null)
371        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
372    }
373    private void ParameterizeSolutionsCreator() {
374      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
375      SolutionsCreator.EvaluatorParameter.Hidden = true;
376      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
377      SolutionsCreator.SolutionCreatorParameter.Hidden = true;
378    }
379    private void ParameterizeMainLoop() {
380      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
381      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
382      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
383      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
384    }
385    private void ParameterizeStochasticOperator(IOperator op) {
386      if (op is IStochasticOperator) {
387        IStochasticOperator stOp = (IStochasticOperator)op;
388        stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
389        stOp.RandomParameter.Hidden = true;
390      }
391    }
392    private void ParameterizeAnalyzers() {
393      qualityAnalyzer.ResultsParameter.ActualName = "Results";
394      qualityAnalyzer.ResultsParameter.Hidden = true;
395      if (Problem != null) {
396        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
397        qualityAnalyzer.MaximizationParameter.Hidden = true;
398        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
399        qualityAnalyzer.QualityParameter.Depth = 1;
400        qualityAnalyzer.QualityParameter.Hidden = true;
401        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
402        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
403      } else {
404        qualityAnalyzer.MaximizationParameter.Hidden = false;
405        qualityAnalyzer.QualityParameter.Hidden = false;
406        qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
407      }
408    }
409    private void ParameterizeIterationBasedOperators() {
410      if (Problem != null) {
411        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
412          op.IterationsParameter.ActualName = "Generations";
413          op.IterationsParameter.Hidden = true;
414          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
415          op.MaximumIterationsParameter.Hidden = true;
416        }
417      }
418    }
419    private void UpdateStrategyParameterOperators() {
420      IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
421      IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
422      IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
423      ClearStrategyParameterOperators();
424      ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
425      if (manipulator != null) {
426        var operators = Problem.Operators.Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
427        foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
428          StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
429        foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
430          StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
431        foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
432          StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
433
434        if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
435          RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
436
437        if (oldStrategyCreator != null) {
438          IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
439          if (tmp1 != null) StrategyParameterCreator = tmp1;
440        } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
441        if (oldStrategyCrossover != null) {
442          IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
443          if (tmp2 != null) StrategyParameterCrossover = tmp2;
444        } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
445        if (oldStrategyManipulator != null) {
446          IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
447          if (tmp3 != null) StrategyParameterManipulator = tmp3;
448        } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
449      }
450    }
451    private void ClearStrategyParameterOperators() {
452      StrategyParameterCreatorParameter.ValidValues.Clear();
453      StrategyParameterCrossoverParameter.ValidValues.Clear();
454      StrategyParameterManipulatorParameter.ValidValues.Clear();
455    }
456    private void UpdateRecombinators() {
457      ICrossover oldRecombinator = Recombinator;
458      RecombinatorParameter.ValidValues.Clear();
459      foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
460        RecombinatorParameter.ValidValues.Add(recombinator);
461      }
462      if (oldRecombinator != null) {
463        ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
464        if (recombinator != null) RecombinatorParameter.Value = recombinator;
465      }
466    }
467    private void UpdateMutators() {
468      IManipulator oldMutator = MutatorParameter.Value;
469      MutatorParameter.ValidValues.Clear();
470      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
471        MutatorParameter.ValidValues.Add(mutator);
472      if (oldMutator != null) {
473        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
474        if (mutator != null) MutatorParameter.Value = mutator;
475      } else if (MutatorParameter.ValidValues.Count > 0 && Problem.Operators.OfType<ISelfAdaptiveManipulator>().Count() > 0) {
476        ISelfAdaptiveManipulator mutator = Problem.Operators.OfType<ISelfAdaptiveManipulator>().First();
477        if (mutator != null) MutatorParameter.Value = mutator;
478      }
479    }
480    private void UpdateAnalyzers() {
481      Analyzer.Operators.Clear();
482      if (Problem != null) {
483        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
484          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
485            param.Depth = 1;
486          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
487        }
488      }
489      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
490    }
491    private EvolutionStrategyMainLoop FindMainLoop(IOperator start) {
492      IOperator mainLoop = start;
493      while (mainLoop != null && !(mainLoop is EvolutionStrategyMainLoop))
494        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
495      if (mainLoop == null) return null;
496      else return (EvolutionStrategyMainLoop)mainLoop;
497    }
498    #endregion
499  }
500}
Note: See TracBrowser for help on using the repository browser.