Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HLScript/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithm.cs @ 12338

Last change on this file since 12338 was 9592, checked in by abeham, 12 years ago

#2038: Added tagging comment

File size: 27.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Linq;
24using HeuristicLab.Analysis;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Optimization.Operators;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.PluginInfrastructure;
34using HeuristicLab.Random;
35
36namespace HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm {
37  /// <summary>
38  /// An offspring selection genetic algorithm.
39  /// </summary>
40  [Item("Offspring Selection Genetic Algorithm", "An offspring selection genetic algorithm (Affenzeller, M. et al. 2009. Genetic Algorithms and Genetic Programming - Modern Concepts and Practical Applications. CRC Press).")]
41  [Creatable("Algorithms")]
42  [StorableClass]
43  public sealed class OffspringSelectionGeneticAlgorithm : HeuristicOptimizationEngineAlgorithm, IStorableContent {
44    public string Filename { get; set; }
45
46    #region Problem Properties
47    public override Type ProblemType {
48      get { return typeof(ISingleObjectiveHeuristicOptimizationProblem); }
49    }
50    public new ISingleObjectiveHeuristicOptimizationProblem Problem {
51      get { return (ISingleObjectiveHeuristicOptimizationProblem)base.Problem; }
52      set { base.Problem = value; }
53    }
54    #endregion
55
56    #region Parameter Properties
57    private ValueParameter<IntValue> SeedParameter {
58      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
61      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
63    private ValueParameter<IntValue> PopulationSizeParameter {
64      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
65    }
66    public IConstrainedValueParameter<ISelector> SelectorParameter {
67      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
68    }
69    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
70      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
71    }
72    private ValueParameter<PercentValue> MutationProbabilityParameter {
73      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
74    }
75    public IConstrainedValueParameter<IManipulator> MutatorParameter {
76      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
77    }
78    private ValueParameter<IntValue> ElitesParameter {
79      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
80    }
81    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
82      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
83    }
84    private ValueParameter<IntValue> MaximumGenerationsParameter {
85      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
86    }
87    private ValueLookupParameter<DoubleValue> SuccessRatioParameter {
88      get { return (ValueLookupParameter<DoubleValue>)Parameters["SuccessRatio"]; }
89    }
90    private ValueLookupParameter<DoubleValue> ComparisonFactorLowerBoundParameter {
91      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorLowerBound"]; }
92    }
93    private ValueLookupParameter<DoubleValue> ComparisonFactorUpperBoundParameter {
94      get { return (ValueLookupParameter<DoubleValue>)Parameters["ComparisonFactorUpperBound"]; }
95    }
96    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ComparisonFactorModifierParameter {
97      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ComparisonFactorModifier"]; }
98    }
99    private ValueLookupParameter<DoubleValue> MaximumSelectionPressureParameter {
100      get { return (ValueLookupParameter<DoubleValue>)Parameters["MaximumSelectionPressure"]; }
101    }
102    private ValueLookupParameter<BoolValue> OffspringSelectionBeforeMutationParameter {
103      get { return (ValueLookupParameter<BoolValue>)Parameters["OffspringSelectionBeforeMutation"]; }
104    }
105    private ValueLookupParameter<IntValue> SelectedParentsParameter {
106      get { return (ValueLookupParameter<IntValue>)Parameters["SelectedParents"]; }
107    }
108    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
109      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
110    }
111    private ValueParameter<IntValue> MaximumEvaluatedSolutionsParameter {
112      get { return (ValueParameter<IntValue>)Parameters["MaximumEvaluatedSolutions"]; }
113    }
114    #endregion
115
116    #region Properties
117    public IntValue Seed {
118      get { return SeedParameter.Value; }
119      set { SeedParameter.Value = value; }
120    }
121    public BoolValue SetSeedRandomly {
122      get { return SetSeedRandomlyParameter.Value; }
123      set { SetSeedRandomlyParameter.Value = value; }
124    }
125    public IntValue PopulationSize {
126      get { return PopulationSizeParameter.Value; }
127      set { PopulationSizeParameter.Value = value; }
128    }
129    public ISelector Selector {
130      get { return SelectorParameter.Value; }
131      set { SelectorParameter.Value = value; }
132    }
133    public ICrossover Crossover {
134      get { return CrossoverParameter.Value; }
135      set { CrossoverParameter.Value = value; }
136    }
137    public PercentValue MutationProbability {
138      get { return MutationProbabilityParameter.Value; }
139      set { MutationProbabilityParameter.Value = value; }
140    }
141    public IManipulator Mutator {
142      get { return MutatorParameter.Value; }
143      set { MutatorParameter.Value = value; }
144    }
145    public IntValue Elites {
146      get { return ElitesParameter.Value; }
147      set { ElitesParameter.Value = value; }
148    }
149    public bool ReevaluteElites {
150      get { return ReevaluateElitesParameter.Value.Value; }
151      set { ReevaluateElitesParameter.Value.Value = value; }
152    }
153    public IntValue MaximumGenerations {
154      get { return MaximumGenerationsParameter.Value; }
155      set { MaximumGenerationsParameter.Value = value; }
156    }
157    public DoubleValue SuccessRatio {
158      get { return SuccessRatioParameter.Value; }
159      set { SuccessRatioParameter.Value = value; }
160    }
161    public DoubleValue ComparisonFactorLowerBound {
162      get { return ComparisonFactorLowerBoundParameter.Value; }
163      set { ComparisonFactorLowerBoundParameter.Value = value; }
164    }
165    public DoubleValue ComparisonFactorUpperBound {
166      get { return ComparisonFactorUpperBoundParameter.Value; }
167      set { ComparisonFactorUpperBoundParameter.Value = value; }
168    }
169    public IDiscreteDoubleValueModifier ComparisonFactorModifier {
170      get { return ComparisonFactorModifierParameter.Value; }
171      set { ComparisonFactorModifierParameter.Value = value; }
172    }
173    public DoubleValue MaximumSelectionPressure {
174      get { return MaximumSelectionPressureParameter.Value; }
175      set { MaximumSelectionPressureParameter.Value = value; }
176    }
177    public BoolValue OffspringSelectionBeforeMutation {
178      get { return OffspringSelectionBeforeMutationParameter.Value; }
179      set { OffspringSelectionBeforeMutationParameter.Value = value; }
180    }
181    public IntValue SelectedParents {
182      get { return SelectedParentsParameter.Value; }
183      set { SelectedParentsParameter.Value = value; }
184    }
185    public MultiAnalyzer Analyzer {
186      get { return AnalyzerParameter.Value; }
187      set { AnalyzerParameter.Value = value; }
188    }
189    public IntValue MaximumEvaluatedSolutions {
190      get { return MaximumEvaluatedSolutionsParameter.Value; }
191      set { MaximumEvaluatedSolutionsParameter.Value = value; }
192    }
193    private RandomCreator RandomCreator {
194      get { return (RandomCreator)OperatorGraph.InitialOperator; }
195    }
196    private SolutionsCreator SolutionsCreator {
197      get { return (SolutionsCreator)RandomCreator.Successor; }
198    }
199    private OffspringSelectionGeneticAlgorithmMainLoop MainLoop {
200      get { return FindMainLoop(SolutionsCreator.Successor); }
201    }
202    [Storable]
203    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
204    [Storable]
205    private ValueAnalyzer selectionPressureAnalyzer;
206    [Storable]
207    private SuccessfulOffspringAnalyzer successfulOffspringAnalyzer;
208    #endregion
209
210    [StorableConstructor]
211    private OffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
212    [StorableHook(HookType.AfterDeserialization)]
213    private void AfterDeserialization() {
214      // BackwardsCompatibility3.3
215      #region Backwards compatible code, remove with 3.4
216      if (successfulOffspringAnalyzer == null)
217        successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
218      if (!Parameters.ContainsKey("ReevaluateElites")) {
219        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 });
220      }
221      #endregion
222
223      Initialize();
224    }
225    private OffspringSelectionGeneticAlgorithm(OffspringSelectionGeneticAlgorithm original, Cloner cloner)
226      : base(original, cloner) {
227      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
228      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
229      successfulOffspringAnalyzer = cloner.Clone(original.successfulOffspringAnalyzer);
230      Initialize();
231    }
232    public override IDeepCloneable Clone(Cloner cloner) {
233      return new OffspringSelectionGeneticAlgorithm(this, cloner);
234    }
235    public OffspringSelectionGeneticAlgorithm()
236      : base() {
237      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
238      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
239      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
240      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
241      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
242      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
243      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
244      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
245      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 });
246      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
247      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
248      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
249      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
250      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
251      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
252      Parameters.Add(new ValueLookupParameter<BoolValue>("OffspringSelectionBeforeMutation", "True if the offspring selection step should be applied before mutation, false if it should be applied after mutation.", new BoolValue(false)));
253      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(200)));
254      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
255      Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
256
257      RandomCreator randomCreator = new RandomCreator();
258      SolutionsCreator solutionsCreator = new SolutionsCreator();
259      SubScopesCounter subScopesCounter = new SubScopesCounter();
260      ResultsCollector resultsCollector = new ResultsCollector();
261      OffspringSelectionGeneticAlgorithmMainLoop mainLoop = new OffspringSelectionGeneticAlgorithmMainLoop();
262      OperatorGraph.InitialOperator = randomCreator;
263
264      randomCreator.RandomParameter.ActualName = "Random";
265      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
266      randomCreator.SeedParameter.Value = null;
267      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
268      randomCreator.SetSeedRandomlyParameter.Value = null;
269      randomCreator.Successor = solutionsCreator;
270
271      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
272      solutionsCreator.Successor = subScopesCounter;
273
274      subScopesCounter.Name = "Initialize EvaluatedSolutions";
275      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
276      subScopesCounter.Successor = resultsCollector;
277
278      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", "", "EvaluatedSolutions"));
279      resultsCollector.ResultsParameter.ActualName = "Results";
280      resultsCollector.Successor = mainLoop;
281
282      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
283      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
284      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
285      mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
286      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
287      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
288      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
289      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
290      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
291      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
292      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
293      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
294      mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
295      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
296      mainLoop.ResultsParameter.ActualName = "Results";
297      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
298      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
299
300      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
301        SelectorParameter.ValidValues.Add(selector);
302      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
303      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
304      ParameterizeSelectors();
305
306      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
307        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
308      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
309      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
310      ParameterizeComparisonFactorModifiers();
311
312      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
313      selectionPressureAnalyzer = new ValueAnalyzer();
314      successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
315      ParameterizeAnalyzers();
316      UpdateAnalyzers();
317
318      Initialize();
319    }
320
321
322
323    public override void Prepare() {
324      if (Problem != null) base.Prepare();
325    }
326
327    #region Events
328    protected override void OnProblemChanged() {
329      ParameterizeStochasticOperator(Problem.SolutionCreator);
330      ParameterizeStochasticOperator(Problem.Evaluator);
331      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
332      ParameterizeSolutionsCreator();
333      ParameterizMainLoop();
334      ParameterizeSelectors();
335      ParameterizeAnalyzers();
336      ParameterizeIterationBasedOperators();
337      UpdateCrossovers();
338      UpdateMutators();
339      UpdateAnalyzers();
340      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
341      base.OnProblemChanged();
342    }
343
344    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
345      ParameterizeStochasticOperator(Problem.SolutionCreator);
346      ParameterizeSolutionsCreator();
347      base.Problem_SolutionCreatorChanged(sender, e);
348    }
349    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
350      ParameterizeStochasticOperator(Problem.Evaluator);
351      ParameterizeSolutionsCreator();
352      ParameterizMainLoop();
353      ParameterizeSelectors();
354      ParameterizeAnalyzers();
355      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
356      base.Problem_EvaluatorChanged(sender, e);
357    }
358    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
359      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
360      ParameterizeIterationBasedOperators();
361      UpdateCrossovers();
362      UpdateMutators();
363      UpdateAnalyzers();
364      base.Problem_OperatorsChanged(sender, e);
365    }
366    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
367      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
368      ParameterizeSelectors();
369    }
370    private void Elites_ValueChanged(object sender, EventArgs e) {
371      ParameterizeSelectors();
372    }
373    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
374      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
375      ParameterizeSelectors();
376    }
377    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
378      ParameterizeSelectors();
379    }
380    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
381      ParameterizMainLoop();
382      ParameterizeSelectors();
383      ParameterizeAnalyzers();
384    }
385    #endregion
386
387    #region Helpers
388    private void Initialize() {
389      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
390      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
391      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
392      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
393      if (Problem != null) {
394        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
395      }
396    }
397    private void ParameterizeSolutionsCreator() {
398      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
399      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
400    }
401    private void ParameterizMainLoop() {
402      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
403      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
404      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
405    }
406    private void ParameterizeStochasticOperator(IOperator op) {
407      if (op is IStochasticOperator)
408        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
409    }
410    private void ParameterizeSelectors() {
411      foreach (ISelector selector in SelectorParameter.ValidValues) {
412        selector.CopySelected = new BoolValue(true);
413        selector.NumberOfSelectedSubScopesParameter.Value = null;
414        selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
415        ParameterizeStochasticOperator(selector);
416      }
417      if (Problem != null) {
418        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
419          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
420          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
421        }
422      }
423    }
424    private void ParameterizeAnalyzers() {
425      qualityAnalyzer.ResultsParameter.ActualName = "Results";
426      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
427      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
428      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
429      selectionPressureAnalyzer.ValueParameter.Depth = 0;
430      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
431      successfulOffspringAnalyzer.ResultsParameter.ActualName = "Results";
432      successfulOffspringAnalyzer.GenerationsParameter.ActualName = "Generations";
433      successfulOffspringAnalyzer.SuccessfulOffspringFlagParameter.Value.Value = "SuccessfulOffspring";
434      successfulOffspringAnalyzer.DepthParameter.Value = new IntValue(1);
435      if (Problem != null) {
436        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
437        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
438        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
439      }
440    }
441    private void ParameterizeComparisonFactorModifiers() {
442      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
443        modifier.IndexParameter.ActualName = "Generations";
444        modifier.EndIndexParameter.ActualName = MaximumGenerationsParameter.Name;
445        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
446        modifier.StartIndexParameter.Value = new IntValue(0);
447        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
448        modifier.ValueParameter.ActualName = "ComparisonFactor";
449      }
450    }
451    private void ParameterizeIterationBasedOperators() {
452      if (Problem != null) {
453        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
454          op.IterationsParameter.ActualName = "Generations";
455          op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
456        }
457      }
458    }
459    private void UpdateCrossovers() {
460      ICrossover oldCrossover = CrossoverParameter.Value;
461      CrossoverParameter.ValidValues.Clear();
462      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
463
464      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
465        CrossoverParameter.ValidValues.Add(crossover);
466
467      if (oldCrossover != null) {
468        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
469        if (crossover != null) CrossoverParameter.Value = crossover;
470        else oldCrossover = null;
471      }
472      if (oldCrossover == null && defaultCrossover != null)
473        CrossoverParameter.Value = defaultCrossover;
474    }
475    private void UpdateMutators() {
476      IManipulator oldMutator = MutatorParameter.Value;
477      MutatorParameter.ValidValues.Clear();
478      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
479        MutatorParameter.ValidValues.Add(mutator);
480      if (oldMutator != null) {
481        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
482        if (mutator != null) MutatorParameter.Value = mutator;
483      }
484    }
485    private void UpdateAnalyzers() {
486      Analyzer.Operators.Clear();
487      if (Problem != null) {
488        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
489          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
490            param.Depth = 1;
491          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
492        }
493      }
494      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
495      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
496      Analyzer.Operators.Add(successfulOffspringAnalyzer, successfulOffspringAnalyzer.EnabledByDefault);
497    }
498    private OffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
499      IOperator mainLoop = start;
500      while (mainLoop != null && !(mainLoop is OffspringSelectionGeneticAlgorithmMainLoop))
501        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
502      if (mainLoop == null) return null;
503      else return (OffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
504    }
505    #endregion
506  }
507}
Note: See TracBrowser for help on using the repository browser.