source: trunk/sources/HeuristicLab.Algorithms.OffspringSelectionEvolutionStrategy/3.3/OffspringSelectionEvolutionStrategy.cs @ 13261

Last change on this file since 13261 was 13261, checked in by gkronber, 3 years ago

#2478:

  • refactoring of ChildrenCopyCreator
  • fixed the bug that EvolutionStrategyOffspringSelector ignored if the problem was a maximization or minimization problem
  • removed unnecessary backwards-compatibility code
  • Property svn:mime-type set to application/octet-stream
File size: 59.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.OffspringSelectionEvolutionStrategy {
36  [Item("Offspring Selection Evolution Strategy (OSES)", "An evolution strategy with offspring selection.")]
37  [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms, Priority = 205)]
38  [StorableClass]
39  public sealed class OffspringSelectionEvolutionStrategy : 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> MaximumGenerationsParameter {
66      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
67    }
68    private ValueParameter<BoolValue> PlusSelectionParameter {
69      get { return (ValueParameter<BoolValue>)Parameters["PlusSelection"]; }
70    }
71    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
72      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
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
93    private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
94      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
95    }
96    private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
97      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
98    }
99    private ValueLookupParameter<IntValue> SelectedParentsParameter {
100      get { return (ValueLookupParameter<IntValue>)Parameters["SelectedParents"]; }
101    }
102    private ValueLookupParameter<DoubleValue> ComparisonFactorParameter {
103      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactor"]; }
104    }
105    private ValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
106      get { return (ValueParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
107    }
108    #endregion
109
110    #region Properties
111    public IntValue Seed {
112      get { return SeedParameter.Value; }
113      set { SeedParameter.Value = value; }
114    }
115    public BoolValue SetSeedRandomly {
116      get { return SetSeedRandomlyParameter.Value; }
117      set { SetSeedRandomlyParameter.Value = value; }
118    }
119    public IntValue PopulationSize {
120      get { return PopulationSizeParameter.Value; }
121      set { PopulationSizeParameter.Value = value; }
122    }
123    public IntValue ParentsPerChild {
124      get { return ParentsPerChildParameter.Value; }
125      set { ParentsPerChildParameter.Value = value; }
126    }
127    public IntValue MaximumGenerations {
128      get { return MaximumGenerationsParameter.Value; }
129      set { MaximumGenerationsParameter.Value = value; }
130    }
131    public BoolValue PlusSelection {
132      get { return PlusSelectionParameter.Value; }
133      set { PlusSelectionParameter.Value = value; }
134    }
135    public bool ReevaluteElites {
136      get { return ReevaluateElitesParameter.Value.Value; }
137      set { ReevaluateElitesParameter.Value.Value = value; }
138    }
139    public IManipulator Mutator {
140      get { return MutatorParameter.Value; }
141      set { MutatorParameter.Value = value; }
142    }
143    public ICrossover Recombinator {
144      get { return RecombinatorParameter.Value; }
145      set { RecombinatorParameter.Value = value; }
146    }
147    public MultiAnalyzer Analyzer {
148      get { return AnalyzerParameter.Value; }
149      set { AnalyzerParameter.Value = value; }
150    }
151    public IStrategyParameterCreator StrategyParameterCreator {
152      get { return StrategyParameterCreatorParameter.Value; }
153      set { StrategyParameterCreatorParameter.Value = value; }
154    }
155    public IStrategyParameterCrossover StrategyParameterCrossover {
156      get { return StrategyParameterCrossoverParameter.Value; }
157      set { StrategyParameterCrossoverParameter.Value = value; }
158    }
159    public IStrategyParameterManipulator StrategyParameterManipulator {
160      get { return StrategyParameterManipulatorParameter.Value; }
161      set { StrategyParameterManipulatorParameter.Value = value; }
162    }
163
164    public DoubleValue SuccessRatio {
165      get { return SuccessRatioParameter.Value; }
166      set { SuccessRatioParameter.Value = value; }
167    }
168    public DoubleValue MaximumSelectionPressure {
169      get { return MaximumSelectionPressureParameter.Value; }
170      set { MaximumSelectionPressureParameter.Value = value; }
171    }
172    public IntValue SelectedParents {
173      get { return SelectedParentsParameter.Value; }
174      set { SelectedParentsParameter.Value = value; }
175    }
176    public DoubleValue ComparisonFactor {
177      get { return ComparisonFactorParameter.Value; }
178      set { ComparisonFactorParameter.Value = value; }
179    }
180    public IntValue MaximumEvaluatedSolutions {
181      get { return MaximumEvaluatedSolutionsParameter.Value; }
182      set { MaximumEvaluatedSolutionsParameter.Value = value; }
183    }
184
185    private RandomCreator RandomCreator {
186      get { return (RandomCreator)OperatorGraph.InitialOperator; }
187    }
188    private SolutionsCreator SolutionsCreator {
189      get { return (SolutionsCreator)RandomCreator.Successor; }
190    }
191    private OffspringSelectionEvolutionStrategyMainLoop MainLoop {
192      get { return FindMainLoop(SolutionsCreator.Successor); }
193    }
194    [Storable]
195    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
196    [Storable]
197    private ValueAnalyzer selectionPressureAnalyzer;
198    #endregion
199
200    public OffspringSelectionEvolutionStrategy()
201      : base() {
202      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
203      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
204      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "µ (mu) - the size of the population.", new IntValue(20)));
205      Parameters.Add(new ValueParameter<IntValue>("ParentsPerChild", "ρ (rho) - how many parents should be recombined.", new IntValue(1)));
206      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
207      Parameters.Add(new ValueParameter<BoolValue>("PlusSelection", "True for plus selection (elitist population), false for comma selection (non-elitist population).", new BoolValue(true)));
208      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 });
209      Parameters.Add(new OptionalConstrainedValueParameter<ICrossover>("Recombinator", "The operator used to cross solutions."));
210      Parameters.Add(new ConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
211      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCreator>("StrategyParameterCreator", "The operator that creates the strategy parameters."));
212      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterCrossover>("StrategyParameterCrossover", "The operator that recombines the strategy parameters."));
213      Parameters.Add(new OptionalConstrainedValueParameter<IStrategyParameterManipulator>("StrategyParameterManipulator", "The operator that manipulates the strategy parameters."));
214      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
215
216      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
217      Parameters.Add(new ValueLookupParameter<IntValue>("SelectedParents", "How much parents should be selected each time the offspring selection step is performed until the population is filled. This parameter should be about the same or twice the size of PopulationSize for smaller problems, and less for large problems.", new IntValue(40)));
218      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
219      Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions.", new IntValue(int.MaxValue)));
220      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactor", "The comparison factor is used to determine whether the offspring should be compared to the better parent, the worse parent or a quality value linearly interpolated between them. It is in the range [0;1].", new DoubleValue(0.5)));
221
222
223
224      RandomCreator randomCreator = new RandomCreator();
225      SolutionsCreator solutionsCreator = new SolutionsCreator();
226      SubScopesCounter subScopesCounter = new SubScopesCounter();
227      UniformSubScopesProcessor strategyVectorProcessor = new UniformSubScopesProcessor();
228      Placeholder strategyVectorCreator = new Placeholder();
229      ResultsCollector resultsCollector = new ResultsCollector();
230      OffspringSelectionEvolutionStrategyMainLoop mainLoop = new OffspringSelectionEvolutionStrategyMainLoop();
231      OperatorGraph.InitialOperator = randomCreator;
232
233      randomCreator.RandomParameter.ActualName = "Random";
234      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
235      randomCreator.SeedParameter.Value = null;
236      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
237      randomCreator.SetSeedRandomlyParameter.Value = null;
238      randomCreator.Successor = solutionsCreator;
239
240      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
241      solutionsCreator.Successor = subScopesCounter;
242
243      subScopesCounter.Name = "Initialize EvaluatedSolutions";
244      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
245      subScopesCounter.Successor = strategyVectorProcessor;
246
247      strategyVectorProcessor.Operator = strategyVectorCreator;
248      strategyVectorProcessor.Successor = resultsCollector;
249
250      strategyVectorCreator.OperatorParameter.ActualName = "StrategyParameterCreator";
251
252      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
253      resultsCollector.ResultsParameter.ActualName = "Results";
254      resultsCollector.Successor = mainLoop;
255
256      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
257      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
258      mainLoop.ParentsPerChildParameter.ActualName = ParentsPerChildParameter.Name;
259      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
260      mainLoop.PlusSelectionParameter.ActualName = PlusSelectionParameter.Name;
261      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
262      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
263      mainLoop.RecombinatorParameter.ActualName = RecombinatorParameter.Name;
264      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
265      mainLoop.ResultsParameter.ActualName = "Results";
266      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
267
268      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
269      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
270      mainLoop.MaximumEvaluatedSolutionsParameter.ActualName = MaximumEvaluatedSolutionsParameter.Name;
271      mainLoop.SelectedParentsParameter.ActualName = SelectedParentsParameter.Name;
272      mainLoop.ComparisonFactorParameter.ActualName = ComparisonFactorParameter.Name;
273      mainLoop.CurrentSuccessRatioParameter.ActualName = "CurrentSuccessRatio";
274      mainLoop.SelectionPressureParameter.ActualName = "SelectionPressure";
275
276      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
277      selectionPressureAnalyzer = new ValueAnalyzer();
278      ParameterizeAnalyzers();
279      UpdateAnalyzers();
280
281      Initialize();
282    }
283    [StorableConstructor]
284    private OffspringSelectionEvolutionStrategy(bool deserializing) : base(deserializing) { }
285    [StorableHook(HookType.AfterDeserialization)]
286    private void AfterDeserialization() {
287      Initialize();
288    }
289
290    private OffspringSelectionEvolutionStrategy(OffspringSelectionEvolutionStrategy original, Cloner cloner)
291      : base(original, cloner) {
292      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
293      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
294      Initialize();
295    }
296    public override IDeepCloneable Clone(Cloner cloner) {
297      return new OffspringSelectionEvolutionStrategy(this, cloner);
298    }
299
300    public override void Prepare() {
301      if (Problem != null) base.Prepare();
302    }
303
304    #region Events
305    protected override void OnProblemChanged() {
306      ParameterizeStochasticOperator(Problem.SolutionCreator);
307      ParameterizeStochasticOperator(Problem.Evaluator);
308      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
309      ParameterizeSolutionsCreator();
310      ParameterizeMainLoop();
311      ParameterizeAnalyzers();
312      ParameterizeIterationBasedOperators();
313      UpdateRecombinators();
314      UpdateMutators();
315      UpdateAnalyzers();
316      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
317      base.OnProblemChanged();
318    }
319    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
320      ParameterizeStochasticOperator(Problem.SolutionCreator);
321      ParameterizeSolutionsCreator();
322      base.Problem_SolutionCreatorChanged(sender, e);
323    }
324    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
325      ParameterizeStochasticOperator(Problem.Evaluator);
326      ParameterizeSolutionsCreator();
327      ParameterizeMainLoop();
328      ParameterizeAnalyzers();
329      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
330      base.Problem_EvaluatorChanged(sender, e);
331    }
332    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
333      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
334      ParameterizeIterationBasedOperators();
335      UpdateRecombinators();
336      UpdateMutators();
337      UpdateAnalyzers();
338      base.Problem_OperatorsChanged(sender, e);
339    }
340    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
341      ParameterizeMainLoop();
342      ParameterizeAnalyzers();
343    }
344    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
345      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
346      PopulationSize_ValueChanged(null, EventArgs.Empty);
347    }
348    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
349      if (PopulationSize.Value <= 0) PopulationSize.Value = 1;
350      if (PopulationSize.Value < ParentsPerChild.Value)
351        ParentsPerChild.Value = PopulationSize.Value;
352    }
353    private void ParentsPerChildParameter_ValueChanged(object sender, EventArgs e) {
354      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
355      ParentsPerChild_ValueChanged(null, EventArgs.Empty);
356    }
357    private void ParentsPerChild_ValueChanged(object sender, EventArgs e) {
358      if (ParentsPerChild.Value < 1 || ParentsPerChild.Value > 1 && RecombinatorParameter.ValidValues.Count == 0)
359        ParentsPerChild.Value = 1;
360      if (ParentsPerChild.Value > 1 && Recombinator == null) Recombinator = RecombinatorParameter.ValidValues.First();
361      if (ParentsPerChild.Value > 1 && ParentsPerChild.Value > PopulationSize.Value)
362        PopulationSize.Value = ParentsPerChild.Value;
363    }
364    private void RecombinatorParameter_ValueChanged(object sender, EventArgs e) {
365      if (Recombinator == null && ParentsPerChild.Value > 1) ParentsPerChild.Value = 1;
366      else if (Recombinator != null && ParentsPerChild.Value == 1) ParentsPerChild.Value = 2;
367      if (Recombinator != null && Mutator is ISelfAdaptiveManipulator && StrategyParameterCrossover == null) {
368        if (StrategyParameterCrossoverParameter.ValidValues.Count > 0)
369          StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
370      }
371    }
372    private void MutatorParameter_ValueChanged(object sender, EventArgs e) {
373      if (Mutator is ISelfAdaptiveManipulator) {
374        UpdateStrategyParameterOperators();
375      } else {
376        StrategyParameterCreatorParameter.ValidValues.Clear();
377        StrategyParameterCrossoverParameter.ValidValues.Clear();
378        StrategyParameterManipulatorParameter.ValidValues.Clear();
379        UpdateRecombinators();
380      }
381    }
382    private void StrategyParameterCreatorParameter_ValueChanged(object sender, EventArgs e) {
383      if (Mutator is ISelfAdaptiveManipulator && StrategyParameterCreator == null && StrategyParameterCreatorParameter.ValidValues.Count > 0)
384        StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
385    }
386    private void StrategyParameterCrossoverParameter_ValueChanged(object sender, EventArgs e) {
387      if (Mutator is ISelfAdaptiveManipulator && Recombinator != null && StrategyParameterCrossover == null && StrategyParameterCrossoverParameter.ValidValues.Count > 0)
388        StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
389    }
390    #endregion
391
392    #region Helpers
393    private void Initialize() {
394      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
395      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
396      ParentsPerChildParameter.ValueChanged += new EventHandler(ParentsPerChildParameter_ValueChanged);
397      ParentsPerChild.ValueChanged += new EventHandler(ParentsPerChild_ValueChanged);
398      RecombinatorParameter.ValueChanged += new EventHandler(RecombinatorParameter_ValueChanged);
399      MutatorParameter.ValueChanged += new EventHandler(MutatorParameter_ValueChanged);
400      StrategyParameterCrossoverParameter.ValueChanged += new EventHandler(StrategyParameterCrossoverParameter_ValueChanged);
401      StrategyParameterCreatorParameter.ValueChanged += new EventHandler(StrategyParameterCreatorParameter_ValueChanged);
402      if (Problem != null)
403        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
404    }
405    private void ParameterizeSolutionsCreator() {
406      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
407      SolutionsCreator.EvaluatorParameter.Hidden = true;
408      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
409      SolutionsCreator.SolutionCreatorParameter.Hidden = true;
410    }
411    private void ParameterizeMainLoop() {
412      MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
413      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
414      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
415      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
416    }
417    private void ParameterizeStochasticOperator(IOperator op) {
418      if (op is IStochasticOperator) {
419        IStochasticOperator stOp = (IStochasticOperator)op;
420        stOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
421        stOp.RandomParameter.Hidden = true;
422      }
423    }
424    private void ParameterizeAnalyzers() {
425      qualityAnalyzer.ResultsParameter.ActualName = "Results";
426      qualityAnalyzer.ResultsParameter.Hidden = true;
427      if (Problem != null) {
428        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
429        qualityAnalyzer.MaximizationParameter.Hidden = true;
430        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
431        qualityAnalyzer.QualityParameter.Depth = 1;
432        qualityAnalyzer.QualityParameter.Hidden = true;
433        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
434        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
435      } else {
436        qualityAnalyzer.MaximizationParameter.Hidden = false;
437        qualityAnalyzer.QualityParameter.Hidden = false;
438        qualityAnalyzer.BestKnownQualityParameter.Hidden = false;
439      }
440
441      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
442      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
443      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
444      selectionPressureAnalyzer.ValueParameter.Depth = 0;
445      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
446    }
447    private void ParameterizeIterationBasedOperators() {
448      if (Problem != null) {
449        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
450          op.IterationsParameter.ActualName = "Generations";
451          op.IterationsParameter.Hidden = true;
452          op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
453          op.MaximumIterationsParameter.Hidden = true;
454        }
455      }
456    }
457    private void UpdateStrategyParameterOperators() {
458      IStrategyParameterCreator oldStrategyCreator = StrategyParameterCreator;
459      IStrategyParameterCrossover oldStrategyCrossover = StrategyParameterCrossover;
460      IStrategyParameterManipulator oldStrategyManipulator = StrategyParameterManipulator;
461      ClearStrategyParameterOperators();
462      ISelfAdaptiveManipulator manipulator = (Mutator as ISelfAdaptiveManipulator);
463      if (manipulator != null) {
464        var operators = Problem.Operators.OfType<IOperator>().Where(x => manipulator.StrategyParameterType.IsAssignableFrom(x.GetType())).OrderBy(x => x.Name);
465        foreach (IStrategyParameterCreator strategyCreator in operators.OfType<IStrategyParameterCreator>())
466          StrategyParameterCreatorParameter.ValidValues.Add(strategyCreator);
467        foreach (IStrategyParameterCrossover strategyRecombinator in operators.OfType<IStrategyParameterCrossover>())
468          StrategyParameterCrossoverParameter.ValidValues.Add(strategyRecombinator);
469        foreach (IStrategyParameterManipulator strategyManipulator in operators.OfType<IStrategyParameterManipulator>())
470          StrategyParameterManipulatorParameter.ValidValues.Add(strategyManipulator);
471
472        if (StrategyParameterCrossoverParameter.ValidValues.Count == 0)
473          RecombinatorParameter.ValidValues.Clear(); // if there is no strategy parameter crossover, there can be no crossover when the mutation operator needs strategy parameters
474
475        if (oldStrategyCreator != null) {
476          IStrategyParameterCreator tmp1 = StrategyParameterCreatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCreator.GetType());
477          if (tmp1 != null) StrategyParameterCreator = tmp1;
478        } else if (StrategyParameterCreatorParameter.ValidValues.Count > 0) StrategyParameterCreator = StrategyParameterCreatorParameter.ValidValues.First();
479        if (oldStrategyCrossover != null) {
480          IStrategyParameterCrossover tmp2 = StrategyParameterCrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyCrossover.GetType());
481          if (tmp2 != null) StrategyParameterCrossover = tmp2;
482        } else if (StrategyParameterCrossoverParameter.ValidValues.Count > 0) StrategyParameterCrossover = StrategyParameterCrossoverParameter.ValidValues.First();
483        if (oldStrategyManipulator != null) {
484          IStrategyParameterManipulator tmp3 = StrategyParameterManipulatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldStrategyManipulator.GetType());
485          if (tmp3 != null) StrategyParameterManipulator = tmp3;
486        } else if (StrategyParameterManipulatorParameter.ValidValues.Count > 0) StrategyParameterManipulator = StrategyParameterManipulatorParameter.ValidValues.First();
487      }
488    }
489    private void ClearStrategyParameterOperators() {
490      StrategyParameterCreatorParameter.ValidValues.Clear();
491      StrategyParameterCrossoverParameter.ValidValues.Clear();
492      StrategyParameterManipulatorParameter.ValidValues.Clear();
493    }
494    private void UpdateRecombinators() {
495      ICrossover oldRecombinator = Recombinator;
496      RecombinatorParameter.ValidValues.Clear();
497      foreach (ICrossover recombinator in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name)) {
498        RecombinatorParameter.ValidValues.Add(recombinator);
499      }
500      if (oldRecombinator != null) {
501        ICrossover recombinator = RecombinatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldRecombinator.GetType());
502        if (recombinator != null) RecombinatorParameter.Value = recombinator;
503      }
504    }
505    private void UpdateMutators() {
506      IManipulator oldMutator = MutatorParameter.Value;
507      MutatorParameter.ValidValues.Clear();
508      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
509        MutatorParameter.ValidValues.Add(mutator);
510      if (oldMutator != null) {
511        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
512        if (mutator != null) MutatorParameter.Value = mutator;
513      } else if (MutatorParameter.ValidValues.Count > 0 && Problem.Operators.OfType<ISelfAdaptiveManipulator>().Any()) {
514        ISelfAdaptiveManipulator mutator = Problem.Operators.OfType<ISelfAdaptiveManipulator>().First();
515        if (mutator != null) MutatorParameter.Value = mutator;
516      }
517    }
518    private void UpdateAnalyzers() {
519      Analyzer.Operators.Clear();
520      if (Problem != null) {
521        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
522          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
523            param.Depth = 1;
524          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
525        }
526      }
527      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
528      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
529    }
530    private OffspringSelectionEvolutionStrategyMainLoop FindMainLoop(IOperator start) {
531      IOperator mainLoop = start;
532      while (mainLoop != null && !(mainLoop is OffspringSelectionEvolutionStrategyMainLoop))
533        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
534      return (OffspringSelectionEvolutionStrategyMainLoop) mainLoop;
535    }
536    #endregion
537  }
538}
Note: See TracBrowser for help on using the repository browser.