Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Algorithms.GAssist/3.3/GAssist.cs @ 13797

Last change on this file since 13797 was 9605, checked in by sforsten, 12 years ago

#1980:

  • set plugin dependencies
  • added smart initialization
  • added hierarchical selection
  • fixed major and minor default rule
  • fixed several smaller bugs
  • some refactoring has been done
File size: 34.1 KB
RevLine 
[9334]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;
[9342]31using HeuristicLab.Optimization.Operators.LCS;
[9334]32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
35using HeuristicLab.Random;
36
37namespace HeuristicLab.Algorithms.GAssist {
38  /// <summary>
39  /// A genetic algorithm.
40  /// </summary>
41  [Item("GAssist", "A learning classifier system.")]
42  [Creatable("Algorithms")]
43  [StorableClass]
44  public sealed class GAssist : HeuristicOptimizationEngineAlgorithm, IStorableContent {
45    public string Filename { get; set; }
46
47    #region Problem Properties
48    public override Type ProblemType {
[9352]49      get { return typeof(IGAssistProblem); }
[9334]50    }
[9352]51    public new IGAssistProblem Problem {
52      get { return (IGAssistProblem)base.Problem; }
[9334]53      set { base.Problem = value; }
54    }
55    #endregion
56
57    #region Parameter Properties
58    private ValueParameter<IntValue> SeedParameter {
59      get { return (ValueParameter<IntValue>)Parameters["Seed"]; }
60    }
61    private ValueParameter<BoolValue> SetSeedRandomlyParameter {
62      get { return (ValueParameter<BoolValue>)Parameters["SetSeedRandomly"]; }
63    }
64    private ValueParameter<IntValue> PopulationSizeParameter {
65      get { return (ValueParameter<IntValue>)Parameters["PopulationSize"]; }
66    }
[9392]67    public ValueParameter<MDLIterationOperator> MDLIterationOperatorParameter {
68      get { return (ValueParameter<MDLIterationOperator>)Parameters["MDLIterationOperator"]; }
69    }
[9352]70    public IConstrainedValueParameter<IDefaultRuleOperator> DefaultRuleParameter {
71      get { return (IConstrainedValueParameter<IDefaultRuleOperator>)Parameters["DefaultRule"]; }
[9334]72    }
[9352]73    public IConstrainedValueParameter<INichingSingleObjectiveSelector> SelectorParameter {
74      get { return (IConstrainedValueParameter<INichingSingleObjectiveSelector>)Parameters["Selector"]; }
75    }
[9334]76    private ValueParameter<PercentValue> CrossoverProbabilityParameter {
77      get { return (ValueParameter<PercentValue>)Parameters["CrossoverProbability"]; }
78    }
79    public IConstrainedValueParameter<ICrossover> CrossoverParameter {
80      get { return (IConstrainedValueParameter<ICrossover>)Parameters["Crossover"]; }
81    }
82    private ValueParameter<PercentValue> MutationProbabilityParameter {
83      get { return (ValueParameter<PercentValue>)Parameters["MutationProbability"]; }
84    }
85    public IConstrainedValueParameter<IManipulator> MutatorParameter {
86      get { return (IConstrainedValueParameter<IManipulator>)Parameters["Mutator"]; }
87    }
88    private ValueParameter<IntValue> ElitesParameter {
89      get { return (ValueParameter<IntValue>)Parameters["Elites"]; }
90    }
91    private ValueParameter<MultiAnalyzer> AnalyzerParameter {
92      get { return (ValueParameter<MultiAnalyzer>)Parameters["Analyzer"]; }
93    }
94    private ValueParameter<GAssistSpecialStageMultiOperator> SpecialStagesParameter {
95      get { return (ValueParameter<GAssistSpecialStageMultiOperator>)Parameters["SpecialStages"]; }
96    }
97    private ValueParameter<IntValue> MaximumGenerationsParameter {
98      get { return (ValueParameter<IntValue>)Parameters["MaximumGenerations"]; }
99    }
100    private ValueParameter<PercentValue> StartReinitializeProbabilityParameter {
101      get { return (ValueParameter<PercentValue>)Parameters["StartReinitializeProbability"]; }
102    }
103    private ValueParameter<PercentValue> EndReinitializeProbabilityParameter {
104      get { return (ValueParameter<PercentValue>)Parameters["EndReinitializeProbability"]; }
105    }
106    public IConstrainedValueParameter<IDiscreteDoubleValueModifier> ReinitializeCurveOperatorParameter {
107      get { return (IConstrainedValueParameter<IDiscreteDoubleValueModifier>)Parameters["ReinitializeCurveOperator"]; }
108    }
109    private ValueParameter<PercentValue> SplitProbabilityParameter {
110      get { return (ValueParameter<PercentValue>)Parameters["SplitProbability"]; }
111    }
112    private ValueParameter<PercentValue> MergeProbabilityParameter {
113      get { return (ValueParameter<PercentValue>)Parameters["MergeProbability"]; }
114    }
115    private ValueParameter<PercentValue> OneProbabilityParameter {
116      get { return (ValueParameter<PercentValue>)Parameters["OneProbability"]; }
117    }
118    private ValueParameter<IntValue> MaximumNumberOfIntervalsParameter {
119      get { return (ValueParameter<IntValue>)Parameters["MaximumNumberOfIntervals"]; }
120    }
121    private ValueParameter<IntValue> InitialNumberOfRulesParameter {
122      get { return (ValueParameter<IntValue>)Parameters["InitialNumberOfRules"]; }
123    }
124    private ValueParameter<ItemCollection<IDiscretizer>> DiscretizersParameter {
125      get { return (ValueParameter<ItemCollection<IDiscretizer>>)Parameters["Discretizers"]; }
126    }
[9392]127    public ValueParameter<IntValue> MDLActivationIterationParameter {
128      get { return (ValueParameter<IntValue>)Parameters["MDLActivationIteration"]; }
129    }
130    public ValueParameter<DoubleValue> InitialTheoryLengthRatioParameter {
131      get { return (ValueParameter<DoubleValue>)Parameters["InitialTheoryLengthRatio"]; }
132    }
133    public ValueParameter<DoubleValue> WeightRelaxFactorParameter {
134      get { return (ValueParameter<DoubleValue>)Parameters["WeightRelaxFactor"]; }
135    }
136    public ValueParameter<IntValue> WeightAdaptionIterationsParameter {
137      get { return (ValueParameter<IntValue>)Parameters["WeightAdaptionIterations"]; }
138    }
139    public ValueParameter<IntValue> NumberOfStrataParameter {
140      get { return (ValueParameter<IntValue>)Parameters["NumberOfStrata"]; }
141    }
[9334]142    #endregion
143
144    #region Properties
145    public IntValue Seed {
146      get { return SeedParameter.Value; }
147      set { SeedParameter.Value = value; }
148    }
149    public BoolValue SetSeedRandomly {
150      get { return SetSeedRandomlyParameter.Value; }
151      set { SetSeedRandomlyParameter.Value = value; }
152    }
153    public IntValue PopulationSize {
154      get { return PopulationSizeParameter.Value; }
155      set { PopulationSizeParameter.Value = value; }
156    }
[9352]157    public INichingSingleObjectiveSelector Selector {
[9334]158      get { return SelectorParameter.Value; }
159      set { SelectorParameter.Value = value; }
160    }
161    public PercentValue CrossoverProbability {
162      get { return CrossoverProbabilityParameter.Value; }
163      set { CrossoverProbabilityParameter.Value = value; }
164    }
165    public ICrossover Crossover {
166      get { return CrossoverParameter.Value; }
167      set { CrossoverParameter.Value = value; }
168    }
169    public PercentValue MutationProbability {
170      get { return MutationProbabilityParameter.Value; }
171      set { MutationProbabilityParameter.Value = value; }
172    }
173    public IManipulator Mutator {
174      get { return MutatorParameter.Value; }
175      set { MutatorParameter.Value = value; }
176    }
177    public IntValue Elites {
178      get { return ElitesParameter.Value; }
179      set { ElitesParameter.Value = value; }
180    }
181    public MultiAnalyzer Analyzer {
182      get { return AnalyzerParameter.Value; }
183      set { AnalyzerParameter.Value = value; }
184    }
185    public GAssistSpecialStageMultiOperator SpecialStages {
186      get { return SpecialStagesParameter.Value; }
187      set { SpecialStagesParameter.Value = value; }
188    }
189    public IDiscreteDoubleValueModifier ReinitializeCurveOperator {
190      get { return ReinitializeCurveOperatorParameter.Value; }
191      set { ReinitializeCurveOperatorParameter.Value = value; }
192    }
193    public IntValue MaximumGenerations {
194      get { return MaximumGenerationsParameter.Value; }
195      set { MaximumGenerationsParameter.Value = value; }
196    }
197    private RandomCreator RandomCreator {
198      get { return (RandomCreator)OperatorGraph.InitialOperator; }
199    }
[9392]200    private VariableCreator VariableCreator {
201      get { return (VariableCreator)RandomCreator.Successor; }
202    }
[9605]203    private Placeholder InitialDefaultRuleExecution {
[9392]204      get { return (Placeholder)VariableCreator.Successor; }
205    }
[9605]206    private Placeholder MDLIterationPlaceholder {
207      get { return (Placeholder)InitialDefaultRuleExecution.Successor; }
208    }
[9392]209    private ILASOperator ILASOperator {
210      get { return (ILASOperator)MDLIterationPlaceholder.Successor; }
211    }
212    private InitializeDiscretizersOperator InitializeDiscretizers {
213      get { return (InitializeDiscretizersOperator)ILASOperator.Successor; }
214    }
[9605]215    private NicheSolutionCreator SolutionsCreator {
216      get { return (NicheSolutionCreator)InitializeDiscretizers.Successor; }
[9334]217    }
218    private GAssistMainLoop GeneticAlgorithmMainLoop {
219      get { return FindMainLoop(SolutionsCreator.Successor); }
220    }
221    [Storable]
222    private BestAverageWorstQualityAnalyzer qualityAnalyzer;
223    #endregion
224
225    public GAssist()
226      : base() {
227      Parameters.Add(new ValueParameter<IntValue>("Seed", "The random seed used to initialize the new pseudo random number generator.", new IntValue(0)));
228      Parameters.Add(new ValueParameter<BoolValue>("SetSeedRandomly", "True if the random seed should be set to a random value, otherwise false.", new BoolValue(true)));
229      Parameters.Add(new ValueParameter<IntValue>("PopulationSize", "The size of the population of solutions.", new IntValue(100)));
[9352]230      Parameters.Add(new ConstrainedValueParameter<INichingSingleObjectiveSelector>("Selector", "The operator used to select solutions for reproduction."));
[9334]231      Parameters.Add(new ValueParameter<PercentValue>("CrossoverProbability", "The probability that the Crossover operator is applied on a solution.", new PercentValue(0.9)));
232      Parameters.Add(new ConstrainedValueParameter<ICrossover>("Crossover", "The operator used to cross solutions."));
233      Parameters.Add(new ValueParameter<PercentValue>("MutationProbability", "The probability that the mutation operator is applied on a solution.", new PercentValue(0.05)));
234      Parameters.Add(new OptionalConstrainedValueParameter<IManipulator>("Mutator", "The operator used to mutate solutions."));
235      Parameters.Add(new ValueParameter<IntValue>("Elites", "The numer of elite solutions which are kept in each generation.", new IntValue(1)));
236      Parameters.Add(new ValueParameter<MultiAnalyzer>("Analyzer", "The operator used to analyze each generation.", new MultiAnalyzer()));
237      Parameters.Add(new ValueParameter<GAssistSpecialStageMultiOperator>("SpecialStages", "", new GAssistSpecialStageMultiOperator()));
238      Parameters.Add(new ValueParameter<IntValue>("MaximumGenerations", "The maximum number of generations which should be processed.", new IntValue(1000)));
[9392]239      Parameters.Add(new ValueParameter<PercentValue>("SplitProbability", "", new PercentValue(0.05)));
240      Parameters.Add(new ValueParameter<PercentValue>("MergeProbability", "", new PercentValue(0.05)));
241      Parameters.Add(new ValueParameter<PercentValue>("StartReinitializeProbability", "", new PercentValue(0.05)));
242      Parameters.Add(new ValueParameter<PercentValue>("EndReinitializeProbability", "", new PercentValue(Double.Epsilon)));
243      Parameters.Add(new ValueParameter<PercentValue>("OneProbability", "", new PercentValue(0.75)));
244      Parameters.Add(new ValueParameter<IntValue>("MaximumNumberOfIntervals", "", new IntValue(5)));
245      Parameters.Add(new ValueParameter<IntValue>("InitialNumberOfRules", "", new IntValue(20)));
246      Parameters.Add(new ValueParameter<MDLIterationOperator>("MDLIterationOperator", "", new MDLIterationOperator()));
[9352]247      Parameters.Add(new ConstrainedValueParameter<IDefaultRuleOperator>("DefaultRule", ""));
[9334]248      Parameters.Add(new ConstrainedValueParameter<IDiscreteDoubleValueModifier>("ReinitializeCurveOperator", ""));
249      Parameters.Add(new ValueParameter<ItemCollection<IDiscretizer>>("Discretizers", "", new ItemCollection<IDiscretizer>()));
250
[9392]251      Parameters.Add(new ValueParameter<IntValue>("MDLActivationIteration", "", new IntValue(25)));
252      Parameters.Add(new ValueParameter<DoubleValue>("InitialTheoryLengthRatio", "", new DoubleValue(0.075)));
253      Parameters.Add(new ValueParameter<DoubleValue>("WeightRelaxFactor", "", new DoubleValue(0.9)));
254      Parameters.Add(new ValueParameter<IntValue>("WeightAdaptionIterations", "", new IntValue(10)));
255
256      Parameters.Add(new ValueParameter<IntValue>("NumberOfStrata", "", new IntValue(2)));
257
[9334]258      RandomCreator randomCreator = new RandomCreator();
[9392]259      VariableCreator variableCreator = new VariableCreator();
[9605]260      Placeholder initialDefaultRuleExecution = new Placeholder();
[9392]261      Placeholder mdlIterationPlaceholder = new Placeholder();
262      ILASOperator ilasOperator = new ILASOperator();
263      InitializeDiscretizersOperator initializeDiscretizers = new InitializeDiscretizersOperator();
[9605]264      NicheSolutionCreator solutionsCreator = new NicheSolutionCreator();
[9334]265      SubScopesCounter subScopesCounter = new SubScopesCounter();
266      ResultsCollector resultsCollector = new ResultsCollector();
267      GAssistMainLoop mainLoop = new GAssistMainLoop();
268      OperatorGraph.InitialOperator = randomCreator;
269
270      randomCreator.RandomParameter.ActualName = "Random";
271      randomCreator.SeedParameter.ActualName = SeedParameter.Name;
272      randomCreator.SeedParameter.Value = null;
273      randomCreator.SetSeedRandomlyParameter.ActualName = SetSeedRandomlyParameter.Name;
274      randomCreator.SetSeedRandomlyParameter.Value = null;
[9392]275      randomCreator.Successor = variableCreator;
[9334]276
[9392]277      variableCreator.CollectedValues.Add(new ValueParameter<IntValue>("Generations", new IntValue(0))); // Class GAssistMainLoop expects this to be called Generations
[9605]278      variableCreator.Successor = initialDefaultRuleExecution;
[9392]279
[9605]280      initialDefaultRuleExecution.Name = "Initial Default Rule Execution";
281      initialDefaultRuleExecution.OperatorParameter.ActualName = DefaultRuleParameter.Name;
282      initialDefaultRuleExecution.Successor = mdlIterationPlaceholder;
283
[9392]284      mdlIterationPlaceholder.Name = "MDL Iteration Operator";
285      mdlIterationPlaceholder.OperatorParameter.ActualName = MDLIterationOperatorParameter.Name;
286      mdlIterationPlaceholder.Successor = ilasOperator;
287
288      ilasOperator.RandomParameter.ActualName = randomCreator.RandomParameter.ActualName;
289      ilasOperator.NumberOfStrataParameter.ActualName = NumberOfStrataParameter.Name;
290      ilasOperator.Successor = initializeDiscretizers;
291
292      initializeDiscretizers.DiscretizersParameter.ActualName = DiscretizersParameter.Name;
293      initializeDiscretizers.Successor = solutionsCreator;
294
[9334]295      solutionsCreator.NumberOfSolutionsParameter.ActualName = PopulationSizeParameter.Name;
296      solutionsCreator.Successor = subScopesCounter;
297
298      subScopesCounter.Name = "Initialize EvaluatedSolutions";
299      subScopesCounter.ValueParameter.ActualName = "EvaluatedSolutions";
300      subScopesCounter.Successor = resultsCollector;
301
302      resultsCollector.CollectedValues.Add(new LookupParameter<IntValue>("Evaluated Solutions", null, "EvaluatedSolutions"));
303      resultsCollector.ResultsParameter.ActualName = "Results";
304      resultsCollector.Successor = mainLoop;
305
[9392]306      mainLoop.MDLIterationParameter.ActualName = MDLIterationOperatorParameter.Name;
[9352]307      mainLoop.DefaultRuleParameter.ActualName = DefaultRuleParameter.Name;
[9334]308      mainLoop.SelectorParameter.ActualName = SelectorParameter.Name;
309      mainLoop.CrossoverParameter.ActualName = CrossoverParameter.Name;
310      mainLoop.CrossoverProbabilityParameter.ActualName = CrossoverProbabilityParameter.Name;
311      mainLoop.ElitesParameter.ActualName = ElitesParameter.Name;
312      mainLoop.MaximumGenerationsParameter.ActualName = MaximumGenerationsParameter.Name;
313      mainLoop.MutatorParameter.ActualName = MutatorParameter.Name;
314      mainLoop.MutationProbabilityParameter.ActualName = MutationProbabilityParameter.Name;
315      mainLoop.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
316      mainLoop.AnalyzerParameter.ActualName = AnalyzerParameter.Name;
317      mainLoop.SpecialStagesParameter.ActualName = SpecialStagesParameter.Name;
318      mainLoop.EvaluatedSolutionsParameter.ActualName = "EvaluatedSolutions";
319      mainLoop.PopulationSizeParameter.ActualName = PopulationSizeParameter.Name;
320      mainLoop.ResultsParameter.ActualName = "Results";
[9342]321      mainLoop.ReinitializationProbabilityOperatorParameter.ActualName = ReinitializeCurveOperatorParameter.Name;
[9334]322
[9352]323      foreach (INichingSingleObjectiveSelector selector in ApplicationManager.Manager.GetInstances<INichingSingleObjectiveSelector>().Where(x => !(x is IMultiObjectiveSelector)).OrderBy(x => x.Name))
[9334]324        SelectorParameter.ValidValues.Add(selector);
[9605]325
[9334]326      ParameterizeSelectors();
327
328      qualityAnalyzer = new BestAverageWorstQualityAnalyzer();
329      ParameterizeAnalyzers();
330      UpdateAnalyzers();
331
[9342]332      foreach (IDiscreteDoubleValueModifier op in ApplicationManager.Manager.GetInstances<IDiscreteDoubleValueModifier>().OrderBy(x => x.Name)) {
[9334]333        ReinitializeCurveOperatorParameter.ValidValues.Add(op);
[9342]334      }
335      ReinitializeCurveOperatorParameter.Value = ReinitializeCurveOperatorParameter.ValidValues.First(x => x.GetType().Equals(typeof(LinearDiscreteDoubleValueModifier)));
[9334]336      ParameterizeReinitializeCurveOperators();
337
338      InitializeSpecialStages();
339
340      UpdateDiscretizers();
341
342      Initialize();
343    }
344
345    private void UpdateDiscretizers() {
346      // change to add more
[9411]347      //DiscretizersParameter.Value.AddRange(ApplicationManager.Manager.GetInstances<IDiscretizer>());
348      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(4));
349      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(5));
350      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(6));
351      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(7));
352      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(8));
353      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(10));
354      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(15));
355      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(20));
356      DiscretizersParameter.Value.Add(new UniformWidthDiscretizer(25));
[9334]357    }
358
359    private void ParameterizeReinitializeCurveOperators() {
360      foreach (IDiscreteDoubleValueModifier op in ReinitializeCurveOperatorParameter.ValidValues) {
361        op.IndexParameter.ActualName = "Generations";
362        op.IndexParameter.Hidden = true;
363        op.StartIndexParameter.Value = new IntValue(0);
364        op.EndIndexParameter.ActualName = MaximumGenerationsParameter.Name;
365        op.ValueParameter.ActualName = "ReinitializeProbability";
366        op.ValueParameter.Hidden = true;
367        op.StartValueParameter.ActualName = StartReinitializeProbabilityParameter.Name;
368        op.StartValueParameter.Hidden = true;
369        op.EndValueParameter.ActualName = EndReinitializeProbabilityParameter.Name;
370        op.EndValueParameter.Hidden = true;
371        ParameterizeStochasticOperator(op);
372      }
373    }
374
375    private void InitializeSpecialStages() {
376      SpecialStages.Operators.Clear();
377      var splitOperator = new SplitOperator();
[9605]378      splitOperator.ProbabilityParameter.ActualName = SplitProbabilityParameter.Name;
[9342]379      //change
380      splitOperator.IndividualParameter.ActualName = "DecisionList";
[9334]381      SpecialStages.Operators.Add(splitOperator);
382      var mergeOperator = new MergeOperator();
[9605]383      mergeOperator.ProbabilityParameter.ActualName = MergeProbabilityParameter.Name;
[9342]384      //change
385      mergeOperator.IndividualParameter.ActualName = "DecisionList";
[9334]386      SpecialStages.Operators.Add(mergeOperator);
387      var reinitializeOperator = new ReinitializeOperator();
388      reinitializeOperator.ProbabilityParameter.ActualName = "ReinitializeProbability";
389      reinitializeOperator.DiscretizersParameter.ActualName = "Discretizers";
[9605]390      reinitializeOperator.OneProbabilityParameter.ActualName = OneProbabilityParameter.Name;
[9342]391      //change
392      reinitializeOperator.IndividualParameter.ActualName = "DecisionList";
[9334]393      SpecialStages.Operators.Add(reinitializeOperator);
394      foreach (var op in SpecialStages.Operators) {
395        op.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
396      }
397    }
398
399    [StorableConstructor]
400    private GAssist(bool deserializing) : base(deserializing) { }
401    [StorableHook(HookType.AfterDeserialization)]
402    private void AfterDeserialization() {
403      Initialize();
404    }
405
406    private GAssist(GAssist original, Cloner cloner)
407      : base(original, cloner) {
408      qualityAnalyzer = cloner.Clone(original.qualityAnalyzer);
409      Initialize();
410    }
411    public override IDeepCloneable Clone(Cloner cloner) {
412      return new GAssist(this, cloner);
413    }
414
415    public override void Prepare() {
416      if (Problem != null) base.Prepare();
417    }
418
419    #region Events
420    protected override void OnProblemChanged() {
421      ParameterizeStochasticOperator(Problem.SolutionCreator);
422      ParameterizeStochasticOperator(Problem.Evaluator);
[9392]423      ParameterizeMDLOperator(Problem.Evaluator);
424      ParameterizeIterationBasedOperators(Problem.Evaluator);
425      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) {
426        ParameterizeStochasticOperator(op);
427      }
[9334]428      ParameterizeSolutionsCreator();
429      ParameterizeGeneticAlgorithmMainLoop();
[9392]430      ParameterizeMDL();
[9334]431      ParameterizeSelectors();
432      ParameterizeAnalyzers();
433      ParameterizeIterationBasedOperators();
[9352]434      UpdateDefaultRuleOperators();
[9334]435      UpdateCrossovers();
436      UpdateMutators();
437      UpdateAnalyzers();
438      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[9392]439      ILASOperator.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
440      InitializeDiscretizers.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
[9334]441      base.OnProblemChanged();
442    }
443
[9392]444    private void ParameterizeMDL() {
445      MDLIterationOperatorParameter.Value.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
446      //change
447      MDLIterationOperatorParameter.Value.IndividualParameter.ActualName = "DecisionList";
448      MDLIterationOperatorParameter.Value.ProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
449      MDLIterationOperatorParameter.Value.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
450
451      MDLIterationOperatorParameter.Value.InitialTheoryLengthRatioParameter.ActualName = InitialTheoryLengthRatioParameter.Name;
452      MDLIterationOperatorParameter.Value.MDLActivationIterationParameter.ActualName = MDLActivationIterationParameter.Name;
453      MDLIterationOperatorParameter.Value.WeightAdaptionIterationsParameter.ActualName = WeightAdaptionIterationsParameter.Name;
454      MDLIterationOperatorParameter.Value.WeightRelaxFactorParameter.ActualName = WeightRelaxFactorParameter.Name;
455      MDLIterationOperatorParameter.Value.IterationsParameter.ActualName = "Generations";
456    }
457
[9334]458    protected override void Problem_SolutionCreatorChanged(object sender, EventArgs e) {
459      ParameterizeStochasticOperator(Problem.SolutionCreator);
460      ParameterizeSolutionsCreator();
461      base.Problem_SolutionCreatorChanged(sender, e);
462    }
463    protected override void Problem_EvaluatorChanged(object sender, EventArgs e) {
464      ParameterizeStochasticOperator(Problem.Evaluator);
465      ParameterizeSolutionsCreator();
466      ParameterizeGeneticAlgorithmMainLoop();
467      ParameterizeSelectors();
468      ParameterizeAnalyzers();
469      Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
[9392]470      Problem.Evaluator.StrataParameter.ActualName = ILASOperator.StrataParameter.ActualName;
[9334]471      base.Problem_EvaluatorChanged(sender, e);
472    }
473    protected override void Problem_OperatorsChanged(object sender, EventArgs e) {
474      foreach (IOperator op in Problem.Operators.OfType<IOperator>()) ParameterizeStochasticOperator(op);
475      ParameterizeIterationBasedOperators();
476      UpdateCrossovers();
477      UpdateMutators();
478      UpdateAnalyzers();
479      base.Problem_OperatorsChanged(sender, e);
480    }
481    private void ElitesParameter_ValueChanged(object sender, EventArgs e) {
482      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
483      ParameterizeSelectors();
484    }
485    private void Elites_ValueChanged(object sender, EventArgs e) {
486      ParameterizeSelectors();
487    }
488
489    private void PopulationSizeParameter_ValueChanged(object sender, EventArgs e) {
490      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
491      ParameterizeSelectors();
492    }
493    private void PopulationSize_ValueChanged(object sender, EventArgs e) {
494      ParameterizeSelectors();
495    }
496    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
497      ParameterizeGeneticAlgorithmMainLoop();
498      ParameterizeSelectors();
499      ParameterizeAnalyzers();
500    }
501    #endregion
502
503    #region Helpers
504    private void Initialize() {
505      PopulationSizeParameter.ValueChanged += new EventHandler(PopulationSizeParameter_ValueChanged);
506      PopulationSize.ValueChanged += new EventHandler(PopulationSize_ValueChanged);
507      ElitesParameter.ValueChanged += new EventHandler(ElitesParameter_ValueChanged);
508      Elites.ValueChanged += new EventHandler(Elites_ValueChanged);
509      if (Problem != null) {
510        Problem.Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
511      }
512    }
513
514    private void ParameterizeSolutionsCreator() {
515      SolutionsCreator.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
516      SolutionsCreator.SolutionCreatorParameter.ActualName = Problem.SolutionCreatorParameter.Name;
[9605]517      SolutionsCreator.GAssistNichesProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
[9334]518      // change!
[9605]519      ((IGAssistSolutionCreator)Problem.SolutionCreatorParameter.ActualValue).DiscretizersParameter.ActualName = "Discretizers";
[9334]520    }
521    private void ParameterizeGeneticAlgorithmMainLoop() {
522      GeneticAlgorithmMainLoop.EvaluatorParameter.ActualName = Problem.EvaluatorParameter.Name;
523      GeneticAlgorithmMainLoop.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
524      GeneticAlgorithmMainLoop.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
525    }
526    private void ParameterizeStochasticOperator(IOperator op) {
527      IStochasticOperator stochasticOp = op as IStochasticOperator;
528      if (stochasticOp != null) {
529        stochasticOp.RandomParameter.ActualName = RandomCreator.RandomParameter.ActualName;
530        stochasticOp.RandomParameter.Hidden = true;
531      }
532    }
[9392]533    private void ParameterizeMDLOperator(IOperator op) {
534      IMDLCalculatorBasedOperator stochasticOp = op as IMDLCalculatorBasedOperator;
535      if (stochasticOp != null) {
536        stochasticOp.MDLCalculatorParameter.ActualName = MDLIterationOperatorParameter.Value.MDLCalculatorParameter.ActualName;
537      }
538    }
[9334]539    private void ParameterizeSelectors() {
[9352]540      foreach (INichingSingleObjectiveSelector selector in SelectorParameter.ValidValues) {
[9334]541        selector.CopySelected = new BoolValue(true);
542        selector.NumberOfSelectedSubScopesParameter.Value = new IntValue(2 * (PopulationSizeParameter.Value.Value - ElitesParameter.Value.Value));
543        selector.NumberOfSelectedSubScopesParameter.Hidden = true;
[9352]544        selector.ParentsPerChildParameter.Value = new IntValue(2);
[9334]545        ParameterizeStochasticOperator(selector);
[9605]546        ParameterizeIterationBasedOperators(selector);
[9334]547      }
548      if (Problem != null) {
[9605]549        foreach (INichingSingleObjectiveSelector selector in SelectorParameter.ValidValues) {
[9334]550          selector.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
551          selector.MaximizationParameter.Hidden = true;
552          selector.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
553          selector.QualityParameter.Hidden = true;
[9352]554          selector.NichingParameter.ActualName = Problem.NichingParameterName;
555          selector.GAssistNichesProblemDataParameter.ActualName = Problem.ProblemDataParameter.Name;
556          //change
557          selector.IndividualParameter.ActualName = "DecisionList";
[9334]558        }
[9605]559        foreach (IHierarchicalSingleObjectiveSelector selector in SelectorParameter.ValidValues.OfType<IHierarchicalSingleObjectiveSelector>()) {
560          selector.LengthParameter.ActualName = Problem.Evaluator.LengthParameter.ActualName;
561        }
[9334]562      }
563    }
564    private void ParameterizeAnalyzers() {
565      qualityAnalyzer.ResultsParameter.ActualName = "Results";
566      qualityAnalyzer.ResultsParameter.Hidden = true;
567      if (Problem != null) {
568        qualityAnalyzer.MaximizationParameter.ActualName = Problem.MaximizationParameter.Name;
569        qualityAnalyzer.MaximizationParameter.Hidden = true;
570        qualityAnalyzer.QualityParameter.ActualName = Problem.Evaluator.QualityParameter.ActualName;
571        qualityAnalyzer.QualityParameter.Depth = 1;
572        qualityAnalyzer.QualityParameter.Hidden = true;
573        qualityAnalyzer.BestKnownQualityParameter.ActualName = Problem.BestKnownQualityParameter.Name;
574        qualityAnalyzer.BestKnownQualityParameter.Hidden = true;
575      }
576    }
[9392]577    private void ParameterizeIterationBasedOperators(IOperator op) {
578      IIterationBasedOperator iterationOp = op as IIterationBasedOperator;
579      if (iterationOp != null) {
580        ParameterizeIterationBasedOperators(iterationOp);
581      }
582    }
583    private void ParameterizeIterationBasedOperators(IIterationBasedOperator op) {
584      op.IterationsParameter.ActualName = "Generations";
585      op.IterationsParameter.Hidden = true;
586      op.MaximumIterationsParameter.ActualName = "MaximumGenerations";
587      op.MaximumIterationsParameter.Hidden = true;
588    }
[9334]589    private void ParameterizeIterationBasedOperators() {
590      if (Problem != null) {
591        foreach (IIterationBasedOperator op in Problem.Operators.OfType<IIterationBasedOperator>()) {
[9392]592          ParameterizeIterationBasedOperators(op);
[9334]593        }
594      }
595    }
[9352]596    private void UpdateDefaultRuleOperators() {
597      IDefaultRuleOperator oldDefaultRule = DefaultRuleParameter.Value;
598      DefaultRuleParameter.ValidValues.Clear();
599      IDefaultRuleOperator defaultdefaultRule = Problem.Operators.OfType<IDefaultRuleOperator>().FirstOrDefault();
600
601      foreach (IDefaultRuleOperator defaultRule in Problem.Operators.OfType<IDefaultRuleOperator>().OrderBy(x => x.Name))
602        DefaultRuleParameter.ValidValues.Add(defaultRule);
603
604      if (oldDefaultRule != null) {
605        IDefaultRuleOperator defaultRule = DefaultRuleParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldDefaultRule.GetType());
606        if (defaultRule != null) DefaultRuleParameter.Value = defaultRule;
607        else oldDefaultRule = null;
608      }
[9605]609      if (oldDefaultRule == null && defaultdefaultRule != null) {
[9352]610        DefaultRuleParameter.Value = defaultdefaultRule;
[9605]611        SolutionsCreator.NichingParameter.ActualName = defaultdefaultRule.NichingParameter.ActualName;
612      }
[9352]613    }
[9334]614    private void UpdateCrossovers() {
615      ICrossover oldCrossover = CrossoverParameter.Value;
616      CrossoverParameter.ValidValues.Clear();
617      ICrossover defaultCrossover = Problem.Operators.OfType<ICrossover>().FirstOrDefault();
618
619      foreach (ICrossover crossover in Problem.Operators.OfType<ICrossover>().OrderBy(x => x.Name))
620        CrossoverParameter.ValidValues.Add(crossover);
621
622      if (oldCrossover != null) {
623        ICrossover crossover = CrossoverParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldCrossover.GetType());
624        if (crossover != null) CrossoverParameter.Value = crossover;
625        else oldCrossover = null;
626      }
627      if (oldCrossover == null && defaultCrossover != null)
628        CrossoverParameter.Value = defaultCrossover;
629    }
630    private void UpdateMutators() {
631      IManipulator oldMutator = MutatorParameter.Value;
632      MutatorParameter.ValidValues.Clear();
[9342]633      IManipulator defaultMutator = Problem.Operators.OfType<IManipulator>().FirstOrDefault();
634
[9334]635      foreach (IManipulator mutator in Problem.Operators.OfType<IManipulator>().OrderBy(x => x.Name))
636        MutatorParameter.ValidValues.Add(mutator);
637      if (oldMutator != null) {
638        IManipulator mutator = MutatorParameter.ValidValues.FirstOrDefault(x => x.GetType() == oldMutator.GetType());
639        if (mutator != null) MutatorParameter.Value = mutator;
640      }
[9342]641      if (oldMutator == null && defaultMutator != null)
642        MutatorParameter.Value = defaultMutator;
[9334]643    }
644    private void UpdateAnalyzers() {
645      Analyzer.Operators.Clear();
646      if (Problem != null) {
647        foreach (IAnalyzer analyzer in Problem.Operators.OfType<IAnalyzer>()) {
648          foreach (IScopeTreeLookupParameter param in analyzer.Parameters.OfType<IScopeTreeLookupParameter>())
649            param.Depth = 1;
650          Analyzer.Operators.Add(analyzer, analyzer.EnabledByDefault);
651        }
652      }
653      Analyzer.Operators.Add(qualityAnalyzer, qualityAnalyzer.EnabledByDefault);
654    }
655    private GAssistMainLoop FindMainLoop(IOperator start) {
656      IOperator mainLoop = start;
657      while (mainLoop != null && !(mainLoop is GAssistMainLoop))
658        mainLoop = ((SingleSuccessorOperator)mainLoop).Successor;
659      if (mainLoop == null) return null;
660      else return (GAssistMainLoop)mainLoop;
661    }
662    #endregion
663  }
664}
Note: See TracBrowser for help on using the repository browser.