Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SASEGASA.cs @ 10187

Last change on this file since 10187 was 9592, checked in by abeham, 11 years ago

#2038: Added tagging comment

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