Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1888_OaaS/HeuristicLab.Algorithms.EvolutionStrategy/3.3/EvolutionStrategy.cs @ 16189

Last change on this file since 16189 was 9363, checked in by spimming, 12 years ago

#1888:

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