Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.OffspringSelectionGeneticAlgorithm/3.3/SASEGASA.cs @ 5594

Last change on this file since 5594 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

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