Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ALPS/HeuristicLab.Algorithms.ALPS/3.3/AlpsGeneticAlgorithm.cs @ 11655

Last change on this file since 11655 was 11620, checked in by pfleck, 10 years ago

#2269

  • Implemented automatic calculation of the age limits based on ALPS' parameter.
  • Renamed AgeDiversityAnalyzer to AgeDistributionAnalyzer.
File size: 27.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections;
24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Analysis;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Operators;
31using HeuristicLab.Optimization;
32using HeuristicLab.Optimization.Operators;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Random;
37using HeuristicLab.Selection;
38
39namespace HeuristicLab.Algorithms.ALPS {
40  [Item("ALPS Genetic Algorithm", "A genetic algorithm with an age-layered population structure.")]
41  [Creatable("Algorithms")]
42  [StorableClass]
43  public sealed class AlpsGeneticAlgorithm : 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 IValueParameter<IntValue> SeedParameter {
58      get { return (IValueParameter<IntValue>)Parameters["Seed"]; }
59    }
60    private IValueParameter<BoolValue> SetSeedRandomlyParameter {
61      get { return (IValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
62    }
63    private IValueParameter<MultiAnalyzer> AnalyzerParameter {
64      get { return (IValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
65    }
66    private IValueParameter<MultiAnalyzer> LayerAnalyzerParameter {
67      get { return (IValueParameter<MultiAnalyzer>)Parameters["LayerAnalyzer"]; }
68    }
69    private IValueParameter<IntValue> NumberOfLayersParameter {
70      get { return (IValueParameter<IntValue>)Parameters["NumberOfLayers"]; }
71    }
72    private IValueParameter<IntValue> PopulationSizeParameter {
73      get { return (IValueParameter<IntValue>)Parameters["PopulationSize"]; }
74    }
75    private IValueParameter<IntValue> MaximumGenerationsParameter {
76      get { return (IValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
77    }
78    private IValueParameter<AgingScheme> AgingSchemeParameter {
79      get { return (IValueParameter<AgingScheme>)Parameters["AgingScheme"]; }
80    }
81    private IValueParameter<IntValue> AgeGapParameter {
82      get { return (IValueParameter<IntValue>)Parameters["AgeGap"]; }
83    }
84    private IValueParameter<IntArray> AgeLimitsParameter {
85      get { return (IValueParameter<IntArray>)Parameters["AgeLimits"]; }
86    }
87    private IValueParameter<ReductionOperation> AgeInheritanceParameter {
88      get { return (IValueParameter<ReductionOperation>)Parameters["AgeInheritance"]; }
89    }
90    public IConstrainedValueParameter<ISelector> SelectorParameter {
91      get { return (IConstrainedValueParameter<ISelector>)Parameters["Selector"]; }
92    }
93    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
94      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
95    }
96    private IValueParameter<PercentValue> MutationProbabilityParameter {
97      get { return (IValueParameter<PercentValue>)Parameters["MutationProbability"]; }
98    }
99    public IConstrainedValueParameter<IManipulator> MutatorParameter {
100      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
101    }
102    private IValueParameter<IntValue> ElitesParameter {
103      get { return (IValueParameter<IntValue>)Parameters["Elites"]; }
104    }
105    private IFixedValueParameter<BoolValue> ReevaluateElitesParameter {
106      get { return (IFixedValueParameter<BoolValue>)Parameters["ReevaluateElites"]; }
107    }
108    #endregion
109
110    #region Properties
111    public IntValue Seed {
112      get { return SeedParameter.Value; }
113      set { SeedParameter.Value = value; }
114    }
115    public BoolValue SetSeedRandomly {
116      get { return SetSeedRandomlyParameter.Value; }
117      set { SetSeedRandomlyParameter.Value = value; }
118    }
119    public MultiAnalyzer Analyzer {
120      get { return AnalyzerParameter.Value; }
121      set { AnalyzerParameter.Value = value; }
122    }
123    public MultiAnalyzer LayerAnalyzer {
124      get { return LayerAnalyzerParameter.Value; }
125      set { LayerAnalyzerParameter.Value = value; }
126    }
127    public IntValue NumberOfLayers {
128      get { return NumberOfLayersParameter.Value; }
129      set { NumberOfLayersParameter.Value = value; }
130    }
131
132    public IntValue PopulationSize {
133      get { return PopulationSizeParameter.Value; }
134      set { PopulationSizeParameter.Value = value; }
135    }
136    public IntValue MaximumGenerations {
137      get { return MaximumGenerationsParameter.Value; }
138      set { MaximumGenerationsParameter.Value = value; }
139    }
140    public AgingScheme AgingScheme {
141      get { return AgingSchemeParameter.Value; }
142      set { AgingSchemeParameter.Value = value; }
143    }
144    public IntValue AgeGap {
145      get { return AgeGapParameter.Value; }
146      set { AgeGapParameter.Value = value; }
147    }
148    public IntArray AgeLimits {
149      get { return AgeLimitsParameter.Value; }
150      set { AgeLimitsParameter.Value = value; }
151    }
152    public ReductionOperation AgeInheritance {
153      get { return AgeInheritanceParameter.Value; }
154      set { AgeInheritanceParameter.Value = value; }
155    }
156    public ISelector Selector {
157      get { return SelectorParameter.Value; }
158      set { SelectorParameter.Value = value; }
159    }
160    public ICrossover Crossover {
161      get { return CrossoverParameter.Value; }
162      set { CrossoverParameter.Value = value; }
163    }
164    public PercentValue MutationProbability {
165      get { return MutationProbabilityParameter.Value; }
166      set { MutationProbabilityParameter.Value = value; }
167    }
168    public IManipulator Mutator {
169      get { return MutatorParameter.Value; }
170      set { MutatorParameter.Value = value; }
171    }
172    public IntValue Elites {
173      get { return ElitesParameter.Value; }
174      set { ElitesParameter.Value = value; }
175    }
176    public bool ReevaluteElites {
177      get { return ReevaluateElitesParameter.Value.Value; }
178      set { ReevaluateElitesParameter.Value.Value = value; }
179    }
180
181    private RandomCreator GlobalRandomCreator {
182      get { return (RandomCreator)OperatorGraph.InitialOperator; }
183    }
184    private SolutionsCreator SolutionsCreator {
185      get { return OperatorGraph.Iterate().OfType<SolutionsCreator>().First(); }
186    }
187    private AlpsGeneticAlgorithmMainLoop MainLoop {
188      get { return OperatorGraph.Iterate().OfType<AlpsGeneticAlgorithmMainLoop>().First(); }
189    }
190
191    [Storable]
192    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
193    [Storable]
194    private BestAverageWorstQualityAnalyzer layerQualityAnalyzer;
195    #endregion
196
197    [StorableConstructor]
198    private AlpsGeneticAlgorithm(bool deserializing)
199      : base(deserializing) { }
200    private AlpsGeneticAlgorithm(AlpsGeneticAlgorithm original, Cloner cloner)
201      : base(original, cloner) {
202      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
203      layerQualityAnalyzer = cloner.Clone(original.layerQualityAnalyzer);
204      Initialize();
205    }
206    public override IDeepCloneable Clone(Cloner cloner) {
207      return new AlpsGeneticAlgorithm(this, cloner);
208    }
209    public AlpsGeneticAlgorithm()
210      : base() {
211      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
212      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
213      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze the islands.", new MultiAnalyzer()));
214      Parameters.Add(new ValueParameter<MultiAnalyzer>("LayerAnalyzer", "The operator used to analyze each layer.", new MultiAnalyzer()));
215      Parameters.Add(new ValueParameter<IntValue>("NumberOfLayers", "The number of layers.", new IntValue(5)));
216      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions each layer.", new IntValue(20)));
217      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations that should be processed.", new IntValue(1000)));
218      Parameters.Add(new ValueParameter<AgingScheme>("AgingScheme", "The aging scheme for setting the age-limits for the layers.", new AgingScheme(AgingSchemes.Polynomial)));
219      Parameters.Add(new ValueParameter<IntValue>("AgeGap", "The frequency of reseeding the lowest layer and scaling factor for the age-limits for the layers", new IntValue(5)));
220      Parameters.Add(new ValueParameter<IntArray>("AgeLimits", new IntArray(new[] { 5, 20, 45, 80, 125 })) { Hidden = true });
221      Parameters.Add(new ValueParameter<ReductionOperation>("AgeInheritance", "The operator for determining the age of an offspring based the parents' age.", new ReductionOperation(ReductionOperations.Max)) { Hidden = true });
222      Parameters.Add(new ConstrainedValueParameter<ISelector>("Selector", "The operator used to select solutions for reproduction."));
223      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
224      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
225      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
226      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
227      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 });
228
229      var globalRandomCreator = new RandomCreator();
230      var layer0Creator = new SubScopesCreator() { Name = "Create Layer Zero" };
231      var layer0Processor = new SubScopesProcessor();
232      var localRandomCreator = new LocalRandomCreator();
233      var layerVariableCreator = new VariableCreator();
234      var layerSolutionsCreator = new SolutionsCreator();
235      var initializeAgeProcessor = new UniformSubScopesProcessor();
236      var initializeAge = new VariableCreator() { Name = "Initialize Age" };
237      var initializeLocalEvaluatedSolutions = new SubScopesCounter();
238      var initializeGlobalEvaluatedSolutions = new DataReducer() { Name = "Initialize EvaluatedSolutions" };
239      var resultsCollector = new ResultsCollector();
240      var mainLoop = new AlpsGeneticAlgorithmMainLoop();
241
242      OperatorGraph.InitialOperator = globalRandomCreator;
243
244      globalRandomCreator.RandomParameter.ActualName = "GlobalRandom";
245      globalRandomCreator.SeedParameter.Value = null;
246      globalRandomCreator.SetSeedRandomlyParameter.Value = null;
247      globalRandomCreator.Successor = layer0Creator;
248
249      layer0Creator.NumberOfSubScopesParameter.Value = new IntValue(1);
250      layer0Creator.Successor = layer0Processor;
251
252      layer0Processor.Operators.Add(localRandomCreator);
253      layer0Processor.Successor = initializeGlobalEvaluatedSolutions;
254
255      localRandomCreator.Successor = layerVariableCreator;
256
257      layerVariableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Layer", new IntValue(0)));
258      //layerVariableCreator.CollectedValues.Add(new ValueParameter<ResultCollection>("Results", new ResultCollection()));
259      layerVariableCreator.Successor = layerSolutionsCreator;
260
261      layerSolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
262      layerSolutionsCreator.Successor = initializeAgeProcessor;
263
264      initializeAgeProcessor.Operator = initializeAge;
265      initializeAgeProcessor.Successor = initializeLocalEvaluatedSolutions;
266
267      initializeAge.CollectedValues.Add(new ValueParameter<IntValue>("Age", new IntValue(0)));
268      initializeAge.Successor = null;
269
270      initializeLocalEvaluatedSolutions.ValueParameter.ActualName = "LayerEvaluatedSolutions";
271      initializeLocalEvaluatedSolutions.Successor = null;
272
273      initializeGlobalEvaluatedSolutions.ReductionOperation.Value.Value = ReductionOperations.Sum;
274      initializeGlobalEvaluatedSolutions.TargetOperation.Value.Value = ReductionOperations.Assign;
275      initializeGlobalEvaluatedSolutions.ParameterToReduce.ActualName = "LayerEvaluatedSolutions";
276      initializeGlobalEvaluatedSolutions.TargetParameter.ActualName = "EvaluatedSolutions";
277      initializeGlobalEvaluatedSolutions.Successor = resultsCollector;
278
279      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
280      resultsCollector.Successor = mainLoop;
281
282      foreach (var selector in ApplicationManager.Manager.GetInstances<ISelector>().Where(s => !(s is IMultiObjectiveSelector)).OrderBy(s => Name))
283        SelectorParameter.ValidValues.Add(selector);
284      var porportionalSelector = SelectorParameter.ValidValues.FirstOrDefault(s => s is ProportionalSelector);
285      if (porportionalSelector != null) SelectorParameter.Value = porportionalSelector;
286
287      ParameterizeSelectors();
288
289      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
290      layerQualityAnalyzer = new BestAverageWorstQualityAnalyzer();
291      ParameterizeAnalyzers();
292      UpdateAnalyzers();
293
294      Initialize();
295    }
296
297    public override void Prepare() {
298      if (Problem != null)
299        base.Prepare();
300    }
301
302    #region Events
303    protected override void OnProblemChanged() {
304      ParameterizeStochasticOperator(Problem.SolutionCreator);
305      ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
306      foreach (var @operator in Problem.Operators.OfType<IOperator>())
307        ParameterizeStochasticOperator(@operator);
308      ParameterizeSolutionsCreator();
309      ParameterizeMainLoop();
310      ParameterizeSelectors();
311      ParameterizeAnalyzers();
312      ParameterizeIterationBasedOperators();
313      UpdateCrossovers();
314      UpdateMutators();
315      UpdateAnalyzers();
316      Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
317      base.OnProblemChanged();
318    }
319
320    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
321      ParameterizeStochasticOperator(Problem.SolutionCreator);
322      ParameterizeSolutionsCreator();
323      base.Problem_SolutionCreatorChanged(sender, e);
324    }
325    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
326      ParameterizeStochasticOperatorForLayer(Problem.Evaluator);
327      ParameterizeSolutionsCreator();
328      ParameterizeMainLoop();
329      ParameterizeSelectors();
330      ParameterizeAnalyzers();
331      Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
332      base.Problem_EvaluatorChanged(sender, e);
333    }
334    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
335      foreach (var @operator in Problem.Operators.OfType<IOperator>())
336        ParameterizeStochasticOperator(@operator);
337      ParameterizeIterationBasedOperators();
338      UpdateCrossovers();
339      UpdateMutators();
340      UpdateAnalyzers();
341      base.Problem_OperatorsChanged(sender, e);
342    }
343    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
344      ParameterizeMainLoop();
345      ParameterizeSelectors();
346      ParameterizeAnalyzers();
347    }
348
349    void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
350      PopulationSizeParameter.ValueChanged += PopulationSize_ValueChanged;
351      ParameterizeSelectors();
352    }
353    void PopulationSize_ValueChanged(object sender, EventArgs e) {
354      ParameterizeSelectors();
355    }
356    void ElitesParameter_ValueChanged(object sender, EventArgs e) {
357      Elites.ValueChanged += ElitesParameter_ValueChanged;
358      ParameterizeSelectors();
359    }
360    void Elites_ValueChanged(object sender, EventArgs e) {
361      ParameterizeSelectors();
362    }
363    private void AgeGapParameter_ValueChanged(object sender, EventArgs e) {
364      AgeGap.ValueChanged += AgeGap_ValueChanged;
365      RecalculateAgeLimits();
366    }
367    private void AgeGap_ValueChanged(object sender, EventArgs e) {
368      RecalculateAgeLimits();
369    }
370    private void AgingSchemeParameter_ValueChanged(object sender, EventArgs e) {
371      AgingScheme.ValueChanged += AgingScheme_ValueChanged;
372      RecalculateAgeLimits();
373    }
374    private void AgingScheme_ValueChanged(object sender, EventArgs e) {
375      RecalculateAgeLimits();
376    }
377    private void NumberOfLayersParameter_ValueChanged(object sender, EventArgs e) {
378      NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
379      RecalculateAgeLimits();
380    }
381    private void NumberOfLayers_ValueChanged(object sender, EventArgs e) {
382      RecalculateAgeLimits();
383    }
384    #endregion
385
386    #region Parameterization
387    private void Initialize() {
388      PopulationSizeParameter.ValueChanged += PopulationSizeParameter_ValueChanged;
389      PopulationSize.ValueChanged += PopulationSize_ValueChanged;
390      ElitesParameter.ValueChanged += ElitesParameter_ValueChanged;
391      Elites.ValueChanged += Elites_ValueChanged;
392      if (Problem != null)
393        Problem.Evaluator.QualityParameter.ActualNameChanged += Evaluator_QualityParameter_ActualNameChanged;
394      AgeGapParameter.ValueChanged += AgeGapParameter_ValueChanged;
395      AgeGap.ValueChanged += AgeGap_ValueChanged;
396      AgingSchemeParameter.ValueChanged += AgingSchemeParameter_ValueChanged;
397      AgingScheme.ValueChanged += AgingScheme_ValueChanged;
398      NumberOfLayersParameter.ValueChanged += NumberOfLayersParameter_ValueChanged;
399      NumberOfLayers.ValueChanged += NumberOfLayers_ValueChanged;
400    }
401    private void ParameterizeSolutionsCreator() {
402      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
403      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
404      MainLoop.LayerUpdator.SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
405      MainLoop.LayerUpdator.SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
406    }
407    private void ParameterizeMainLoop() {
408      //MainLoop.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
409      //MainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
410      //MainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
411      //MainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
412      MainLoop.MainOperator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
413      MainLoop.MainOperator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
414      MainLoop.MainOperator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
415      MainLoop.EldersEmigrator.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
416      MainLoop.EldersEmigrator.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
417      MainLoop.LayerUpdator.SolutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
418    }
419    private void ParameterizeSelectors() {
420      foreach (var selector in SelectorParameter.ValidValues) {
421        selector.CopySelected = new BoolValue(true);
422        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSize.Value - Elites.Value));
423        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
424        ParameterizeStochasticOperatorForLayer(selector);
425      }
426      if (Problem != null) {
427        foreach (var selector in SelectorParameter.ValidValues.OfType<ISingleObjectiveSelector>()) {
428          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
429          selector.MaximizationParameter.Hidden = true;
430          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
431          selector.QualityParameter.Hidden = true;
432        }
433      }
434    }
435    private void ParameterizeAnalyzers() {
436      qualityAnalyzer.ResultsParameter.ActualName = "Results";
437      qualityAnalyzer.ResultsParameter.Hidden = true;
438      qualityAnalyzer.QualityParameter.Depth = 2;
439      layerQualityAnalyzer.ResultsParameter.ActualName = "Results";
440      layerQualityAnalyzer.ResultsParameter.Hidden = true;
441      layerQualityAnalyzer.QualityParameter.Depth = 1;
442      if (Problem != null) {
443        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
444        qualityAnalyzer.MaximizationParameter.Hidden = true;
445        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
446        qualityAnalyzer.QualityParameter.Hidden = true;
447        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
448        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
449        layerQualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
450        layerQualityAnalyzer.MaximizationParameter.Hidden = true;
451        layerQualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
452        layerQualityAnalyzer.QualityParameter.Hidden = true;
453        layerQualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
454        layerQualityAnalyzer.BestKnownQualityParameter.Hidden = true;
455      }
456    }
457    private void ParameterizeIterationBasedOperators() {
458      if (Problem != null) {
459        foreach (var @operator in Problem.Operators.OfType<IIterationBasedOperator>()) {
460          @operator.IterationsParameter.ActualName = "Generations";
461          @operator.IterationsParameter.Hidden = true;
462          @operator.MaximumIterationsParameter.ActualName = "MaximumGenerations";
463          @operator.MaximumIterationsParameter.Hidden = true;
464        }
465      }
466    }
467    private void ParameterizeStochasticOperator(IOperator @operator) {
468      var stochasticOperator = @operator as IStochasticOperator;
469      if (stochasticOperator != null) {
470        stochasticOperator.RandomParameter.ActualName = GlobalRandomCreator.RandomParameter.ActualName;
471        stochasticOperator.RandomParameter.Hidden = true;
472      }
473    }
474    private void ParameterizeStochasticOperatorForLayer(IOperator @operator) {
475      var stochasticOperator = @operator as IStochasticOperator;
476      if (stochasticOperator != null) {
477        stochasticOperator.RandomParameter.ActualName = "LocalRandom";
478        stochasticOperator.RandomParameter.Hidden = true;
479      }
480    }
481    #endregion
482
483    #region Updates
484    private void UpdateCrossovers() {
485      var oldCrossover = CrossoverParameter.Value;
486      var defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
487      CrossoverParameter.ValidValues.Clear();
488      foreach (var crossover in Problem.Operators.OfType<ICrossover>().OrderBy(c => c.Name)) {
489        ParameterizeStochasticOperatorForLayer(crossover);
490        CrossoverParameter.ValidValues.Add(crossover);
491      }
492      if (oldCrossover != null) {
493        var crossover = CrossoverParameter.ValidValues.FirstOrDefault(c => c.GetType() == oldCrossover.GetType());
494        CrossoverParameter.Value = crossover;
495      }
496      if (oldCrossover == null && defaultCrossover != null)
497        CrossoverParameter.Value = defaultCrossover;
498    }
499    private void UpdateMutators() {
500      var oldMutator = MutatorParameter.Value;
501      MutatorParameter.ValidValues.Clear();
502      foreach (var mutator in Problem.Operators.OfType<IManipulator>().OrderBy(m => m.Name)) {
503        ParameterizeStochasticOperatorForLayer(mutator);
504        MutatorParameter.ValidValues.Add(mutator);
505      }
506      if (oldMutator != null) {
507        var mutator = MutatorParameter.ValidValues.FirstOrDefault(m => m.GetType() == oldMutator.GetType());
508        MutatorParameter.Value = mutator;
509      }
510    }
511    private void UpdateAnalyzers() {
512      Analyzer.Operators.Clear();
513      LayerAnalyzer.Operators.Clear();
514
515      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
516      LayerAnalyzer.Operators.Add(layerQualityAnalyzer, layerQualityAnalyzer.EnabledByDefault);
517
518      if (Problem != null) {
519        foreach (var analyzer in Problem.Operators.OfType<IAnalyzer>()) {
520          foreach (var parameter in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
521            parameter.Depth = 2;
522          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
523        }
524      }
525    }
526    #endregion
527
528    private void RecalculateAgeLimits() {
529      IEnumerable<int> scheme;
530      switch (AgingScheme.Value) {
531        case AgingSchemes.Linear: scheme = LinearAgingScheme(); break;
532        case AgingSchemes.Fibonacci: scheme = FibonacciAgingScheme(); break;
533        case AgingSchemes.Polynomial: scheme = PolynomialAgingScheme(2); break;
534        case AgingSchemes.Exponential: scheme = ExponentialAgingScheme(2); break;
535        default: throw new NotSupportedException("Aging Scheme " + AgingScheme.Value + " is not supported.");
536      }
537
538      int ageGap = AgeGap.Value;
539      AgeLimits = new IntArray(scheme.Select(a => a * ageGap).Take(NumberOfLayers.Value).ToArray());
540    }
541
542    // 1 2 3 4 5 6 7
543    private static IEnumerable<int> LinearAgingScheme() {
544      for (int i = 0; ; i++)
545        yield return i + 1;
546    }
547    // 1 2 3 5 8 13 21
548    private static IEnumerable<int> FibonacciAgingScheme() {
549      for (int i = 1, next = 2, temp; ; temp = next, next = i + next, i = temp)
550        yield return i;
551    }
552    // (n^2): 1 2 4 9 16 25 36
553    private static IEnumerable<int> PolynomialAgingScheme(double exp) {
554      yield return 1;
555      yield return 2;
556      for (int i = 2; ; i++)
557        yield return (int)Math.Pow(i, exp);
558
559    }
560    // 1 2 4 8 16 32 64
561    private static IEnumerable<int> ExponentialAgingScheme(double @base) {
562      for (int i = 0; ; i++)
563        yield return (int)Math.Pow(@base, i);
564    }
565  }
566}
Note: See TracBrowser for help on using the repository browser.