Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 11756 was 11171, checked in by ascheibe, 10 years ago

#2115 merged r11170 (copyright update) into trunk

  • Property svn:mime-type set to application/octet-stream
File size: 55.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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      // BackwardsCompatibility3.3
241      #region Backwards compatible code, remove with 3.4
242      if (!Parameters.ContainsKey("ReevaluateElites")) {
243        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 });
244      }
245      #endregion
246
247      Initialize();
248    }
249
250    private EvolutionStrategy(EvolutionStrategy original, Cloner cloner)
251      : base(original, cloner) {
252      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
253      Initialize();
254    }
255    public override IDeepCloneable Clone(Cloner cloner) {
256      return new EvolutionStrategy(this, cloner);
257    }
258
259    public override void Prepare() {
260      if (Problem != null) base.Prepare();
261    }
262
263    #region Events
264    protected override void OnProblemChanged() {
265      ParameterizeStochasticOperator(Problem.SolutionCreator);
266      ParameterizeStochasticOperator(Problem.Evaluator);
267      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
268      ParameterizeSolutionsCreator();
269      ParameterizeMainLoop();
270      ParameterizeAnalyzers();
271      ParameterizeIterationBasedOperators();
272      UpdateRecombinators();
273      UpdateMutators();
274      UpdateAnalyzers();
275      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
276      base.OnProblemChanged();
277    }
278    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
279      ParameterizeStochasticOperator(Problem.SolutionCreator);
280      ParameterizeSolutionsCreator();
281      base.Problem_SolutionCreatorChanged(sender, e);
282    }
283    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
284      ParameterizeStochasticOperator(Problem.Evaluator);
285      ParameterizeSolutionsCreator();
286      ParameterizeMainLoop();
287      ParameterizeAnalyzers();
288      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
289      base.Problem_EvaluatorChanged(sender, e);
290    }
291    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
292      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
293      ParameterizeIterationBasedOperators();
294      UpdateRecombinators();
295      UpdateMutators();
296      UpdateAnalyzers();
297      base.Problem_OperatorsChanged(sender, e);
298    }
299    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
300      ParameterizeMainLoop();
301      ParameterizeAnalyzers();
302    }
303    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
304      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
305      PopulationSize_ValueChanged(null, EventArgs.Empty);
306    }
307    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
308      if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
309      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
310        Children.Value = PopulationSize.Value;
311      if (PopulationSize.Value < ParentsPerChild.Value)
312        ParentsPerChild.Value = PopulationSize.Value;
313    }
314    private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
315      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
316      ParentsPerChild_ValueChanged(null, EventArgs.Empty);
317    }
318    private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
319      if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
320        ParentsPerChild.Value = 1;
321      if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
322      if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
323        PopulationSize.Value = ParentsPerChild.Value;
324    }
325    private void ChildrenParameter_ValueChanged(object sender, EventArgs e) {
326      Children.ValueChanged += new EventHandler(Children_ValueChanged);
327      Children_ValueChanged(null, EventArgs.Empty);
328    }
329    private void Children_ValueChanged(object sender, EventArgs e) {
330      if (Children.Value <= 0) Children.Value = 1;
331      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
332        PopulationSize.Value = Children.Value;
333    }
334    private void PlusSelectionParameter_ValueChanged(object sender, EventArgs e) {
335      PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
336      PlusSelection_ValueChanged(null, EventArgs.Empty);
337    }
338    private void PlusSelection_ValueChanged(object sender, EventArgs e) {
339      if (!PlusSelection.Value && Children.Value < PopulationSize.Value)
340        Children.Value = PopulationSize.Value;
341    }
342    private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
343      if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
344      else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
345      if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
346        if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
347          StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
348      }
349    }
350    private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
351      if (Mutator is ISelfAdaptiveManipulator) {
352        UpdateStrategyParameterOperators();
353      } else {
354        StrategyParameterCreatorParameter.ValidValues.Clear();
355        StrategyParameterCrossoverParameter.ValidValues.Clear();
356        StrategyParameterManipulatorParameter.ValidValues.Clear();
357        UpdateRecombinators();
358      }
359    }
360    private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
361      if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
362        StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
363    }
364    private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
365      if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
366        StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
367    }
368    #endregion
369
370    #region Helpers
371    private void Initialize() {
372      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
373      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
374      ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
375      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
376      ChildrenParameter.ValueChanged += new EventHandler(ChildrenParameter_ValueChanged);
377      Children.ValueChanged += new EventHandler(Children_ValueChanged);
378      PlusSelectionParameter.ValueChanged += new EventHandler(PlusSelectionParameter_ValueChanged);
379      PlusSelection.ValueChanged += new EventHandler(PlusSelection_ValueChanged);
380      RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
381      MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
382      StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
383      StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
384      if (Problem != null)
385        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
386    }
387    private void ParameterizeSolutionsCreator() {
388      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
389      SolutionsCreator.EvaluatorParameter.Hidden = true;
390      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
391      SolutionsCreator.SolutionCreatorParameter.Hidden = true;
392    }
393    private void ParameterizeMainLoop() {
394      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
395      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
396      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
397      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
398    }
399    private void ParameterizeStochasticOperator(IOperator op) {
400      if (op is IStochasticOperator) {
401        IStochasticOperator stOp = (IStochasticOperator)op;
402        stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
403        stOp.RandomParameter.Hidden = true;
404      }
405    }
406    private void ParameterizeAnalyzers() {
407      qualityAnalyzer.ResultsParameter.ActualName = "Results";
408      qualityAnalyzer.ResultsParameter.Hidden = true;
409      if (Problem != null) {
410        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
411        qualityAnalyzer.MaximizationParameter.Hidden = true;
412        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
413        qualityAnalyzer.QualityParameter.Depth = 1;
414        qualityAnalyzer.QualityParameter.Hidden = true;
415        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
416        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
417      } else {
418        qualityAnalyzer.MaximizationParameter.Hidden = false;
419        qualityAnalyzer.QualityParameter.Hidden = false;
420        qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
421      }
422    }
423    private void ParameterizeIterationBasedOperators() {
424      if (Problem != null) {
425        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
426          op.IterationsParameter.ActualName = "Generations";
427          op.IterationsParameter.Hidden = true;
428          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
429          op.MaximumIterationsParameter.Hidden = true;
430        }
431      }
432    }
433    private void UpdateStrategyParameterOperators() {
434      IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
435      IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
436      IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
437      ClearStrategyParameterOperators();
438      ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
439      if (manipulator != null) {
440        var operators = Problem.Operators.OfType<IOperator>().Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
441        foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
442          StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
443        foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
444          StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
445        foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
446          StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
447
448        if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
449          RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
450
451        if (oldStrategyCreator != null) {
452          IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
453          if (tmp1 != null) StrategyParameterCreator = tmp1;
454        } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
455        if (oldStrategyCrossover != null) {
456          IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
457          if (tmp2 != null) StrategyParameterCrossover = tmp2;
458        } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
459        if (oldStrategyManipulator != null) {
460          IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
461          if (tmp3 != null) StrategyParameterManipulator = tmp3;
462        } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
463      }
464    }
465    private void ClearStrategyParameterOperators() {
466      StrategyParameterCreatorParameter.ValidValues.Clear();
467      StrategyParameterCrossoverParameter.ValidValues.Clear();
468      StrategyParameterManipulatorParameter.ValidValues.Clear();
469    }
470    private void UpdateRecombinators() {
471      ICrossover oldRecombinator = Recombinator;
472      RecombinatorParameter.ValidValues.Clear();
473      foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
474        RecombinatorParameter.ValidValues.Add(recombinator);
475      }
476      if (oldRecombinator != null) {
477        ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
478        if (recombinator != null) RecombinatorParameter.Value = recombinator;
479      }
480    }
481    private void UpdateMutators() {
482      IManipulator oldMutator = MutatorParameter.Value;
483      MutatorParameter.ValidValues.Clear();
484      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
485        MutatorParameter.ValidValues.Add(mutator);
486      if (oldMutator != null) {
487        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
488        if (mutator != null) MutatorParameter.Value = mutator;
489      } else if (MutatorParameter.ValidValues.Count > 0 && Problem.Operators.OfType<ISelfAdaptiveManipulator>().Count() > 0) {
490        ISelfAdaptiveManipulator mutator = Problem.Operators.OfType<ISelfAdaptiveManipulator>().First();
491        if (mutator != null) MutatorParameter.Value = mutator;
492      }
493    }
494    private void UpdateAnalyzers() {
495      Analyzer.Operators.Clear();
496      if (Problem != null) {
497        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
498          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
499            param.Depth = 1;
500          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
501        }
502      }
503      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
504    }
505    private EvolutionStrategyMainLoop FindMainLoop(IOperator start) {
506      IOperator mainLoop = start;
507      while (mainLoop != null && !(mainLoop is EvolutionStrategyMainLoop))
508        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
509      if (mainLoop == null) return null;
510      else return (EvolutionStrategyMainLoop)mainLoop;
511    }
512    #endregion
513  }
514}
Note: See TracBrowser for help on using the repository browser.