Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9584 was 9569, checked in by mkommend, 12 years ago

#2038: Added reevaluation of elites in ES, IslandGA, IslandOSGA, OSGA, SASEGASA, and RAPGA.

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