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

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

#2027 Made a genetic ComparisonTerminationCriterion instead of the MaximumIterationsTerminationCriterion.

File size: 29.3 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      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
329      selectionPressureAnalyzer = new ValueAnalyzer();
330      successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
331      ParameterizeAnalyzers();
332      UpdateAnalyzers();
333      AddTerminationCriteria();
334
335      Initialize();
336    }
337
338
339
340    public override void Prepare() {
341      if (Problem != null) base.Prepare();
342    }
343
344    #region Events
345    protected override void OnProblemChanged() {
346      ParameterizeStochasticOperator(Problem.SolutionCreator);
347      ParameterizeStochasticOperator(Problem.Evaluator);
348      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
349      ParameterizeSolutionsCreator();
350      ParameterizMainLoop();
351      ParameterizeSelectors();
352      ParameterizeAnalyzers();
353      ParameterizeIterationBasedOperators();
354      UpdateCrossovers();
355      UpdateMutators();
356      UpdateAnalyzers();
357      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
358      base.OnProblemChanged();
359    }
360
361    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
362      ParameterizeStochasticOperator(Problem.SolutionCreator);
363      ParameterizeSolutionsCreator();
364      base.Problem_SolutionCreatorChanged(sender, e);
365    }
366    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
367      ParameterizeStochasticOperator(Problem.Evaluator);
368      ParameterizeSolutionsCreator();
369      ParameterizMainLoop();
370      ParameterizeSelectors();
371      ParameterizeAnalyzers();
372      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
373      base.Problem_EvaluatorChanged(sender, e);
374    }
375    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
376      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
377      ParameterizeIterationBasedOperators();
378      UpdateCrossovers();
379      UpdateMutators();
380      UpdateAnalyzers();
381      base.Problem_OperatorsChanged(sender, e);
382    }
383    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
384      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
385      ParameterizeSelectors();
386    }
387    private void Elites_ValueChanged(object sender, EventArgs e) {
388      ParameterizeSelectors();
389    }
390    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
391      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
392      ParameterizeSelectors();
393    }
394    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
395      ParameterizeSelectors();
396    }
397    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
398      ParameterizMainLoop();
399      ParameterizeSelectors();
400      ParameterizeAnalyzers();
401    }
402    #endregion
403
404    #region Helpers
405    private void Initialize() {
406      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
407      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
408      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
409      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
410      if (Problem != null) {
411        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
412      }
413    }
414    private void ParameterizeSolutionsCreator() {
415      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
416      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
417    }
418    private void ParameterizMainLoop() {
419      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
420      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
421      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
422    }
423    private void ParameterizeStochasticOperator(IOperator op) {
424      if (op is IStochasticOperator)
425        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
426    }
427    private void ParameterizeSelectors() {
428      foreach (ISelector selector in SelectorParameter.ValidValues) {
429        selector.CopySelected = new BoolValue(true);
430        selector.NumberOfSelectedSubScopesParameter.Value = null;
431        selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
432        ParameterizeStochasticOperator(selector);
433      }
434      if (Problem != null) {
435        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
436          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
437          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
438        }
439      }
440    }
441    private void ParameterizeAnalyzers() {
442      qualityAnalyzer.ResultsParameter.ActualName = "Results";
443      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
444      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
445      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
446      selectionPressureAnalyzer.ValueParameter.Depth = 0;
447      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
448      successfulOffspringAnalyzer.ResultsParameter.ActualName = "Results";
449      successfulOffspringAnalyzer.GenerationsParameter.ActualName = "Generations";
450      successfulOffspringAnalyzer.SuccessfulOffspringFlagParameter.Value.Value = "SuccessfulOffspring";
451      successfulOffspringAnalyzer.DepthParameter.Value = new IntValue(1);
452      if (Problem != null) {
453        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
454        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
455        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
456      }
457    }
458    private void ParameterizeComparisonFactorModifiers() {
459      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
460        modifier.IndexParameter.ActualName = "Generations";
461        modifier.EndIndexParameter.ActualName = MaximumGenerationsParameter.Name;
462        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
463        modifier.StartIndexParameter.Value = new IntValue(0);
464        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
465        modifier.ValueParameter.ActualName = "ComparisonFactor";
466      }
467    }
468    private void ParameterizeIterationBasedOperators() {
469      if (Problem != null) {
470        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
471          op.IterationsParameter.ActualName = "Generations";
472          op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
473        }
474      }
475    }
476    private void UpdateCrossovers() {
477      ICrossover oldCrossover = CrossoverParameter.Value;
478      CrossoverParameter.ValidValues.Clear();
479      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
480
481      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
482        CrossoverParameter.ValidValues.Add(crossover);
483
484      if (oldCrossover != null) {
485        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
486        if (crossover != null) CrossoverParameter.Value = crossover;
487        else oldCrossover = null;
488      }
489      if (oldCrossover == null && defaultCrossover != null)
490        CrossoverParameter.Value = defaultCrossover;
491    }
492    private void UpdateMutators() {
493      IManipulator oldMutator = MutatorParameter.Value;
494      MutatorParameter.ValidValues.Clear();
495      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
496        MutatorParameter.ValidValues.Add(mutator);
497      if (oldMutator != null) {
498        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
499        if (mutator != null) MutatorParameter.Value = mutator;
500      }
501    }
502    private void UpdateAnalyzers() {
503      Analyzer.Operators.Clear();
504      if (Problem != null) {
505        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
506          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
507            param.Depth = 1;
508          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
509        }
510      }
511      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
512      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
513      Analyzer.Operators.Add(successfulOffspringAnalyzer, successfulOffspringAnalyzer.EnabledByDefault);
514    }
515    private void AddTerminationCriteria() {
516      TerminationCriteriaParameter.Value.Operators.Add(
517        new ComparisonTerminationCriterion<IntValue>("Generations", "MaximumGenerations", ComparisonType.GreaterOrEqual) { Name = "Maximum Generations" });
518      TerminationCriteriaParameter.Value.Operators.Add(
519        new ComparisonTerminationCriterion<IntValue>("EvaluatedSolutions", "EvaluatedSolutionsGenerations", ComparisonType.GreaterOrEqual) { Name = "Maximum Evaluations" });
520      TerminationCriteriaParameter.Value.Operators.Add(
521        new ComparisonTerminationCriterion<DoubleValue>("SelectionPressure", "MaximumSelectionPressure", ComparisonType.GreaterOrEqual) { Name = "Maximum Selection Pressure" });
522    }
523    private OffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
524      IOperator mainLoop = start;
525      while (mainLoop != null && !(mainLoop is OffspringSelectionGeneticAlgorithmMainLoop))
526        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
527      if (mainLoop == null) return null;
528      else return (OffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
529    }
530    #endregion
531  }
532}
Note: See TracBrowser for help on using the repository browser.