source: branches/VOSGA/HeuristicLab.Algorithms.VOffspringSelectionGeneticAlgorithm/VOffspringSelectionGeneticAlgorithm.cs @ 11513

Last change on this file since 11513 was 11513, checked in by ascheibe, 8 years ago

#2267 made subscopes offspring comparer configurable

File size: 31.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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.VOffspringSelectionGeneticAlgorithm {
37  /// <summary>
38  /// An offspring selection genetic algorithm.
39  /// </summary>
40  [Item("VOSGA", "An OSGA with selectable OS criteria and automtic crossover/mutation selection.")]
41  [Creatable("Algorithms")]
42  [StorableClass]
43  public sealed class VOffspringSelectionGeneticAlgorithm : 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    private IFixedValueParameter<BoolValue> FillPopulationWithParentsParameter {
115      get { return (IFixedValueParameter<BoolValue>)Parameters["FillPopulationWithParents"]; }
116    }
117    public IConstrainedValueParameter<IOffspringSelector> OffspringSelectorParameter {
118      get { return (IConstrainedValueParameter<IOffspringSelector>)Parameters["OffspringSelector"]; }
119    }
120    public ConstrainedValueParameter<ISubScopesQualityComparatorOperator> SubScopesQualityComparatorParameter {
121      get { return (ConstrainedValueParameter<ISubScopesQualityComparatorOperator>)Parameters["SubScopesQualityComparator"]; }
122    }
123    #endregion
124
125    #region Properties
126    public IntValue Seed {
127      get { return SeedParameter.Value; }
128      set { SeedParameter.Value = value; }
129    }
130    public BoolValue SetSeedRandomly {
131      get { return SetSeedRandomlyParameter.Value; }
132      set { SetSeedRandomlyParameter.Value = value; }
133    }
134    public IntValue PopulationSize {
135      get { return PopulationSizeParameter.Value; }
136      set { PopulationSizeParameter.Value = value; }
137    }
138    public ISelector Selector {
139      get { return SelectorParameter.Value; }
140      set { SelectorParameter.Value = value; }
141    }
142    public IOffspringSelector OffspringSelector {
143      get { return OffspringSelectorParameter.Value; }
144      set { OffspringSelectorParameter.Value = value; }
145    }
146    public ICrossover Crossover {
147      get { return CrossoverParameter.Value; }
148      set { CrossoverParameter.Value = value; }
149    }
150    public PercentValue MutationProbability {
151      get { return MutationProbabilityParameter.Value; }
152      set { MutationProbabilityParameter.Value = value; }
153    }
154    public IManipulator Mutator {
155      get { return MutatorParameter.Value; }
156      set { MutatorParameter.Value = value; }
157    }
158    public IntValue Elites {
159      get { return ElitesParameter.Value; }
160      set { ElitesParameter.Value = value; }
161    }
162    public bool ReevaluteElites {
163      get { return ReevaluateElitesParameter.Value.Value; }
164      set { ReevaluateElitesParameter.Value.Value = value; }
165    }
166    public IntValue MaximumGenerations {
167      get { return MaximumGenerationsParameter.Value; }
168      set { MaximumGenerationsParameter.Value = value; }
169    }
170    public DoubleValue SuccessRatio {
171      get { return SuccessRatioParameter.Value; }
172      set { SuccessRatioParameter.Value = value; }
173    }
174    public DoubleValue ComparisonFactorLowerBound {
175      get { return ComparisonFactorLowerBoundParameter.Value; }
176      set { ComparisonFactorLowerBoundParameter.Value = value; }
177    }
178    public DoubleValue ComparisonFactorUpperBound {
179      get { return ComparisonFactorUpperBoundParameter.Value; }
180      set { ComparisonFactorUpperBoundParameter.Value = value; }
181    }
182    public IDiscreteDoubleValueModifier ComparisonFactorModifier {
183      get { return ComparisonFactorModifierParameter.Value; }
184      set { ComparisonFactorModifierParameter.Value = value; }
185    }
186    public DoubleValue MaximumSelectionPressure {
187      get { return MaximumSelectionPressureParameter.Value; }
188      set { MaximumSelectionPressureParameter.Value = value; }
189    }
190    public BoolValue OffspringSelectionBeforeMutation {
191      get { return OffspringSelectionBeforeMutationParameter.Value; }
192      set { OffspringSelectionBeforeMutationParameter.Value = value; }
193    }
194    public IntValue SelectedParents {
195      get { return SelectedParentsParameter.Value; }
196      set { SelectedParentsParameter.Value = value; }
197    }
198    public MultiAnalyzer Analyzer {
199      get { return AnalyzerParameter.Value; }
200      set { AnalyzerParameter.Value = value; }
201    }
202    public IntValue MaximumEvaluatedSolutions {
203      get { return MaximumEvaluatedSolutionsParameter.Value; }
204      set { MaximumEvaluatedSolutionsParameter.Value = value; }
205    }
206    public bool FillPopulationWithParents {
207      get { return FillPopulationWithParentsParameter.Value.Value; }
208      set { FillPopulationWithParentsParameter.Value.Value = value; }
209    }
210    public ISubScopesQualityComparatorOperator SubScopesQualityComparator {
211      get { return SubScopesQualityComparatorParameter.Value; }
212      set { SubScopesQualityComparatorParameter.Value = value; }
213    }
214
215    private RandomCreator RandomCreator {
216      get { return (RandomCreator)OperatorGraph.InitialOperator; }
217    }
218    private SolutionsCreator SolutionsCreator {
219      get { return (SolutionsCreator)RandomCreator.Successor; }
220    }
221    private VOffspringSelectionGeneticAlgorithmMainLoop MainLoop {
222      get { return FindMainLoop(SolutionsCreator.Successor); }
223    }
224    [Storable]
225    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
226    [Storable]
227    private ValueAnalyzer selectionPressureAnalyzer;
228    [Storable]
229    private SuccessfulOffspringAnalyzer successfulOffspringAnalyzer;
230    #endregion
231
232    [StorableConstructor]
233    private VOffspringSelectionGeneticAlgorithm(bool deserializing) : base(deserializing) { }
234    [StorableHook(HookType.AfterDeserialization)]
235    private void AfterDeserialization() {
236      // BackwardsCompatibility3.3
237      #region Backwards compatible code, remove with 3.4
238      if (successfulOffspringAnalyzer == null)
239        successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
240      if (!Parameters.ContainsKey("ReevaluateElites")) {
241        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 });
242      }
243      if (!Parameters.ContainsKey("FillPopulationWithParents"))
244        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 });
245      #endregion
246
247      Initialize();
248    }
249    private VOffspringSelectionGeneticAlgorithm(VOffspringSelectionGeneticAlgorithm original, Cloner cloner)
250      : base(original, cloner) {
251      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
252      selectionPressureAnalyzer = cloner.Clone(original.selectionPressureAnalyzer);
253      successfulOffspringAnalyzer = cloner.Clone(original.successfulOffspringAnalyzer);
254      Initialize();
255    }
256    public override IDeepCloneable Clone(Cloner cloner) {
257      return new VOffspringSelectionGeneticAlgorithm(this, cloner);
258    }
259    public VOffspringSelectionGeneticAlgorithm()
260      : base() {
261      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
262      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
263      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
264      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
265      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
266      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
267      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
268      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
269      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 });
270      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
271      Parameters.Add(new ValueLookupParameter<DoubleValue>("SuccessRatio", "The ratio of successful to total children that should be achieved.", new DoubleValue(1)));
272      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorLowerBound", "The lower bound of the comparison factor (start).", new DoubleValue(0)));
273      Parameters.Add(new ValueLookupParameter<DoubleValue>("ComparisonFactorUpperBound", "The upper bound of the comparison factor (end).", new DoubleValue(1)));
274      Parameters.Add(new OptionalConstrainedValueParameter<IDiscreteDoubleValueModifier>("ComparisonFactorModifier", "The operator used to modify the comparison factor.", new ItemSet<IDiscreteDoubleValueModifier>(new IDiscreteDoubleValueModifier[] { new LinearDiscreteDoubleValueModifier() }), new LinearDiscreteDoubleValueModifier()));
275      Parameters.Add(new ValueLookupParameter<DoubleValue>("MaximumSelectionPressure", "The maximum selection pressure that terminates the algorithm.", new DoubleValue(100)));
276      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)));
277      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)));
278      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
279      Parameters.Add(new ValueParameter<IntValue>("MaximumEvaluatedSolutions", "The maximum number of evaluated solutions (approximately).", new IntValue(int.MaxValue)));
280      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 });
281      Parameters.Add(new ConstrainedValueParameter<IOffspringSelector>("OffspringSelector", "The operator used as selection criterea for deciding which individuals are successful and which should be disgarded."));
282      Parameters.Add(new ConstrainedValueParameter<ISubScopesQualityComparatorOperator>("SubScopesQualityComparator", "The operator used to compare solution candidates with."));
283
284      RandomCreator randomCreator = new RandomCreator();
285      SolutionsCreator solutionsCreator = new SolutionsCreator();
286      SubScopesCounter subScopesCounter = new SubScopesCounter();
287      ResultsCollector resultsCollector = new ResultsCollector();
288      VOffspringSelectionGeneticAlgorithmMainLoop mainLoop = new VOffspringSelectionGeneticAlgorithmMainLoop();
289      OperatorGraph.InitialOperator = randomCreator;
290
291      randomCreator.RandomParameter.ActualName = "Random";
292      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
293      randomCreator.SeedParameter.Value = null;
294      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
295      randomCreator.SetSeedRandomlyParameter.Value = null;
296      randomCreator.Successor = solutionsCreator;
297
298      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
299      solutionsCreator.Successor = subScopesCounter;
300
301      subScopesCounter.Name = "Initialize EvaluatedSolutions";
302      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
303      subScopesCounter.Successor = resultsCollector;
304
305      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", "", "EvaluatedSolutions"));
306      resultsCollector.ResultsParameter.ActualName = "Results";
307      resultsCollector.Successor = mainLoop;
308
309      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
310      mainLoop.ComparisonFactorModifierParameter.ActualName = ComparisonFactorModifierParameter.Name;
311      mainLoop.ComparisonFactorParameter.ActualName = "ComparisonFactor";
312      mainLoop.ComparisonFactorStartParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
313      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
314      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
315      mainLoop.ReevaluateElitesParameter.ActualName = ReevaluateElitesParameter.Name;
316      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
317      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
318      mainLoop.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
319      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
320      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
321      mainLoop.OffspringSelectionBeforeMutationParameter.ActualName = OffspringSelectionBeforeMutationParameter.Name;
322      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
323      mainLoop.ResultsParameter.ActualName = "Results";
324      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
325      mainLoop.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
326      mainLoop.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
327
328      foreach (ISelector selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
329        SelectorParameter.ValidValues.Add(selector);
330      ISelector proportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("ProportionalSelector"));
331      if (proportionalSelector != null) SelectorParameter.Value = proportionalSelector;
332      ParameterizeSelectors();
333
334      foreach (IDiscreteDoubleValueModifier modifier in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name))
335        ComparisonFactorModifierParameter.ValidValues.Add(modifier);
336      IDiscreteDoubleValueModifier linearModifier = ComparisonFactorModifierParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("LinearDiscreteDoubleValueModifier"));
337      if (linearModifier != null) ComparisonFactorModifierParameter.Value = linearModifier;
338      ParameterizeComparisonFactorModifiers();
339
340      foreach (IOffspringSelector selector in ApplicationManager.Manager.GetInstances<IOffspringSelector>().OrderBy(x => x.Name))
341        OffspringSelectorParameter.ValidValues.Add(selector);
342      IOffspringSelector standardOffspringSelector = OffspringSelectorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("StandardOffspringSelector"));
343      if (standardOffspringSelector != null) OffspringSelectorParameter.Value = standardOffspringSelector;
344      ParameterizeOffspringSelectors();
345
346      foreach (ISubScopesQualityComparatorOperator comparator in ApplicationManager.Manager.GetInstances<ISubScopesQualityComparatorOperator>().OrderBy(x => x.Name))
347        SubScopesQualityComparatorParameter.ValidValues.Add(comparator);
348      ISubScopesQualityComparatorOperator standardComparator = SubScopesQualityComparatorParameter.ValidValues.FirstOrDefault(x => x.GetType().Name.Equals("WeightedParentsQualityComparator"));
349      if (standardComparator != null) SubScopesQualityComparatorParameter.Value = standardComparator;
350      ParameterizeSubScopesQualityComparators();
351
352      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
353      selectionPressureAnalyzer = new ValueAnalyzer();
354      successfulOffspringAnalyzer = new SuccessfulOffspringAnalyzer();
355      ParameterizeAnalyzers();
356      UpdateAnalyzers();
357
358      Initialize();
359    }
360
361    public override void Prepare() {
362      if (Problem != null) base.Prepare();
363    }
364
365    #region Events
366    protected override void OnProblemChanged() {
367      ParameterizeStochasticOperator(Problem.SolutionCreator);
368      ParameterizeStochasticOperator(Problem.Evaluator);
369      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
370      ParameterizeSolutionsCreator();
371      ParameterizMainLoop();
372      ParameterizeSelectors();
373      ParameterizeAnalyzers();
374      ParameterizeIterationBasedOperators();
375      UpdateCrossovers();
376      UpdateMutators();
377      UpdateAnalyzers();
378      ParameterizeSubScopesQualityComparators();
379      ParameterizeOffspringSelectors();
380      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
381      base.OnProblemChanged();
382    }
383
384    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
385      ParameterizeStochasticOperator(Problem.SolutionCreator);
386      ParameterizeSolutionsCreator();
387      base.Problem_SolutionCreatorChanged(sender, e);
388    }
389    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
390      ParameterizeStochasticOperator(Problem.Evaluator);
391      ParameterizeSolutionsCreator();
392      ParameterizMainLoop();
393      ParameterizeSelectors();
394      ParameterizeAnalyzers();
395      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
396      base.Problem_EvaluatorChanged(sender, e);
397    }
398    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
399      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
400      ParameterizeIterationBasedOperators();
401      UpdateCrossovers();
402      UpdateMutators();
403      UpdateAnalyzers();
404      base.Problem_OperatorsChanged(sender, e);
405    }
406    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
407      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
408      ParameterizeSelectors();
409    }
410    private void Elites_ValueChanged(object sender, EventArgs e) {
411      ParameterizeSelectors();
412    }
413    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
414      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
415      ParameterizeSelectors();
416    }
417    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
418      ParameterizeSelectors();
419    }
420    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
421      ParameterizMainLoop();
422      ParameterizeSelectors();
423      ParameterizeAnalyzers();
424    }
425    #endregion
426
427    #region Helpers
428    private void Initialize() {
429      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
430      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
431      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
432      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
433      if (Problem != null) {
434        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
435      }
436    }
437    private void ParameterizeSolutionsCreator() {
438      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
439      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
440    }
441    private void ParameterizMainLoop() {
442      MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
443      MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
444      MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
445    }
446    private void ParameterizeStochasticOperator(IOperator op) {
447      if (op is IStochasticOperator)
448        ((IStochasticOperator)op).RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
449    }
450    private void ParameterizeSelectors() {
451      foreach (ISelector selector in SelectorParameter.ValidValues) {
452        selector.CopySelected = new BoolValue(true);
453        selector.NumberOfSelectedSubScopesParameter.Value = null;
454        selector.NumberOfSelectedSubScopesParameter.ActualName = SelectedParentsParameter.Name;
455        ParameterizeStochasticOperator(selector);
456      }
457      if (Problem != null) {
458        foreach (ISingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
459          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
460          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
461        }
462      }
463    }
464    private void ParameterizeOffspringSelectors() {
465      foreach (IOffspringSelector selector in OffspringSelectorParameter.ValidValues) {
466        //TODO: do we need to set these parameters? if yes, where?
467        // selector.CurrentSuccessRatioParameter.ActualName = CurrentSuccessRatioParameter.Name;
468        //selector.SelectionPressureParameter.ActualName = SelectionPressureParameter.Name;
469        selector.MaximumSelectionPressureParameter.ActualName = MaximumSelectionPressureParameter.Name;
470        selector.SuccessRatioParameter.ActualName = SuccessRatioParameter.Name;
471        selector.OffspringPopulationParameter.ActualName = "OffspringPopulation";
472        selector.OffspringPopulationWinnersParameter.ActualName = "OffspringPopulationWinners";
473        selector.SuccessfulOffspringParameter.ActualName = "SuccessfulOffspring";
474        selector.FillPopulationWithParentsParameter.ActualName = FillPopulationWithParentsParameter.Name;
475      }
476    }
477    private void ParameterizeSubScopesQualityComparators() {
478      foreach (var comp in SubScopesQualityComparatorParameter.ValidValues) {
479        comp.ComparisonFactorParameter.ActualName = "ComparisonFactor";
480        if (Problem != null) {
481          comp.LeftSideParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
482          comp.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
483          comp.RightSideParameter.ActualName = Problem.Evaluator.QualityParameter.Name;
484        }
485        comp.ResultParameter.ActualName = "SuccessfulOffspring";
486      }
487    }
488    private void ParameterizeAnalyzers() {
489      qualityAnalyzer.ResultsParameter.ActualName = "Results";
490      selectionPressureAnalyzer.Name = "SelectionPressure Analyzer";
491      selectionPressureAnalyzer.ResultsParameter.ActualName = "Results";
492      selectionPressureAnalyzer.ValueParameter.ActualName = "SelectionPressure";
493      selectionPressureAnalyzer.ValueParameter.Depth = 0;
494      selectionPressureAnalyzer.ValuesParameter.ActualName = "Selection Pressure History";
495      successfulOffspringAnalyzer.ResultsParameter.ActualName = "Results";
496      successfulOffspringAnalyzer.GenerationsParameter.ActualName = "Generations";
497      successfulOffspringAnalyzer.SuccessfulOffspringFlagParameter.Value.Value = "SuccessfulOffspring";
498      successfulOffspringAnalyzer.DepthParameter.Value = new IntValue(1);
499      if (Problem != null) {
500        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
501        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
502        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
503      }
504    }
505    private void ParameterizeComparisonFactorModifiers() {
506      foreach (IDiscreteDoubleValueModifier modifier in ComparisonFactorModifierParameter.ValidValues) {
507        modifier.IndexParameter.ActualName = "Generations";
508        modifier.EndIndexParameter.ActualName = MaximumGenerationsParameter.Name;
509        modifier.EndValueParameter.ActualName = ComparisonFactorUpperBoundParameter.Name;
510        modifier.StartIndexParameter.Value = new IntValue(0);
511        modifier.StartValueParameter.ActualName = ComparisonFactorLowerBoundParameter.Name;
512        modifier.ValueParameter.ActualName = "ComparisonFactor";
513      }
514    }
515    private void ParameterizeIterationBasedOperators() {
516      if (Problem != null) {
517        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
518          op.IterationsParameter.ActualName = "Generations";
519          op.MaximumIterationsParameter.ActualName = MaximumGenerationsParameter.Name;
520        }
521      }
522    }
523    private void UpdateCrossovers() {
524      ICrossover oldCrossover = CrossoverParameter.Value;
525      CrossoverParameter.ValidValues.Clear();
526      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
527
528      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
529        CrossoverParameter.ValidValues.Add(crossover);
530
531      if (oldCrossover != null) {
532        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
533        if (crossover != null) CrossoverParameter.Value = crossover;
534        else oldCrossover = null;
535      }
536      if (oldCrossover == null && defaultCrossover != null)
537        CrossoverParameter.Value = defaultCrossover;
538    }
539    private void UpdateMutators() {
540      IManipulator oldMutator = MutatorParameter.Value;
541      MutatorParameter.ValidValues.Clear();
542      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
543        MutatorParameter.ValidValues.Add(mutator);
544      if (oldMutator != null) {
545        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
546        if (mutator != null) MutatorParameter.Value = mutator;
547      }
548    }
549    private void UpdateAnalyzers() {
550      Analyzer.Operators.Clear();
551      if (Problem != null) {
552        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
553          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
554            param.Depth = 1;
555          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
556        }
557      }
558      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
559      Analyzer.Operators.Add(selectionPressureAnalyzer, selectionPressureAnalyzer.EnabledByDefault);
560      Analyzer.Operators.Add(successfulOffspringAnalyzer, successfulOffspringAnalyzer.EnabledByDefault);
561    }
562    private VOffspringSelectionGeneticAlgorithmMainLoop FindMainLoop(IOperator start) {
563      IOperator mainLoop = start;
564      while (mainLoop != null && !(mainLoop is VOffspringSelectionGeneticAlgorithmMainLoop))
565        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
566      if (mainLoop == null) return null;
567      else return (VOffspringSelectionGeneticAlgorithmMainLoop)mainLoop;
568    }
569    #endregion
570  }
571}
Note: See TracBrowser for help on using the repository browser.