Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SASEGASA.cs @ 12566

Last change on this file since 12566 was 11594, checked in by jkarder, 10 years ago

#2116: merged r10041-r11593 from trunk into branch

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