source: branches/TerminationCriteria/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/OffspringSelectionGeneticAlgorithm.cs @ 12304

Last change on this file since 12304 was 12304, checked in by pfleck, 7 years ago

#2027 Used new termination-operator and -criteria in OSGA.

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