Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs @ 8594

Last change on this file since 8594 was 8594, checked in by mkommend, 12 years ago

#1940: Added support in symbolic classification for different methods to create the classification ModelCreator.

  • Added ModelCreators
  • Refactored SymbolicClassificationModel and SymbolicDiscriminantFunctionClassificationModel
  • Added ModelCreatorParameter to Analyzers and Evaluators if needed
  • Corrected wiring in symbolic classification problems (single- and multiobjective
  • Adapted simplifier
File size: 7.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21using System.Linq;
22using HeuristicLab.Common;
23using HeuristicLab.Core;
24using HeuristicLab.Parameters;
25using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
26
27namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
28  [Item("Symbolic Classification Problem (single objective)", "Represents a single objective symbolic classfication problem.")]
29  [StorableClass]
30  [Creatable("Problems")]
31  public class SymbolicClassificationSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IClassificationProblemData, ISymbolicClassificationSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IClassificationProblem {
32    private const double PunishmentFactor = 10;
33    private const int InitialMaximumTreeDepth = 8;
34    private const int InitialMaximumTreeLength = 25;
35    private const string EstimationLimitsParameterName = "EstimationLimits";
36    private const string EstimationLimitsParameterDescription = "The lower and upper limit for the estimated value that can be returned by the symbolic classification model.";
37    private const string ModelCreatorParameterName = "ModelCreator";
38
39    #region parameter properties
40    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
41      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
42    }
43    public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
44      get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
45    }
46    #endregion
47    #region properties
48    public DoubleLimit EstimationLimits {
49      get { return EstimationLimitsParameter.Value; }
50    }
51    public ISymbolicClassificationModelCreator ModelCreator {
52      get { return ModelCreatorParameter.Value; }
53    }
54    #endregion
55    [StorableConstructor]
56    protected SymbolicClassificationSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
57    protected SymbolicClassificationSingleObjectiveProblem(SymbolicClassificationSingleObjectiveProblem original, Cloner cloner)
58      : base(original, cloner) {
59      RegisterEventHandlers();
60    }
61    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationSingleObjectiveProblem(this, cloner); }
62
63    public SymbolicClassificationSingleObjectiveProblem()
64      : base(new ClassificationProblemData(), new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
65      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
66      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
67
68      EstimationLimitsParameter.Hidden = true;
69
70      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
71      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
72
73      RegisterEventHandlers();
74      ConfigureGrammarSymbols();
75      InitializeOperators();
76      UpdateEstimationLimits();
77    }
78
79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
81
82      if (!Parameters.ContainsKey(ModelCreatorParameterName))
83        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
84
85      bool changed = false;
86      if (!Operators.OfType<SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
87        Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
88        changed = true;
89      }
90      if (!Operators.OfType<SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
91        Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
92        changed = true;
93      }
94      if (changed) ParameterizeOperators();
95      RegisterEventHandlers();
96    }
97
98    private void RegisterEventHandlers() {
99      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
100      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
101    }
102
103    private void ConfigureGrammarSymbols() {
104      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
105      if (grammar != null) grammar.ConfigureAsDefaultClassificationGrammar();
106    }
107
108    private void InitializeOperators() {
109      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer());
110      Operators.Add(new SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer());
111      Operators.Add(new SymbolicClassificationSingleObjectiveOverfittingAnalyzer());
112      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
113      Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
114      ParameterizeOperators();
115    }
116
117    private void UpdateEstimationLimits() {
118      if (ProblemData.TrainingIndices.Any()) {
119        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
120        var mean = targetValues.Average();
121        var range = targetValues.Max() - targetValues.Min();
122        EstimationLimits.Upper = mean + PunishmentFactor * range;
123        EstimationLimits.Lower = mean - PunishmentFactor * range;
124      } else {
125        EstimationLimits.Upper = double.MaxValue;
126        EstimationLimits.Lower = double.MinValue;
127      }
128    }
129
130    protected override void OnProblemDataChanged() {
131      base.OnProblemDataChanged();
132      UpdateEstimationLimits();
133    }
134
135    protected override void ParameterizeOperators() {
136      base.ParameterizeOperators();
137      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
138        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
139        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
140          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
141        foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
142          op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
143      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.