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

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

#2267 made offspring selector configurable

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