Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SASEGASA.cs @ 10483

Last change on this file since 10483 was 8206, checked in by gkronber, 12 years ago

#1847: merged r8084:8205 from trunk into GP move operators branch

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