Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 13164 was 13164, checked in by gkronber, 7 years ago

#2478: copied selected parts from OSES branch to trunk

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