Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1951:

  • Added linear scaling parameter to data analysis problems.
  • Adapted interfaces, evaluators and analyzers accordingly.
  • Added OnlineBoundedMeanSquaredErrorCalculator.
  • Adapted symbolic regression sample unit test.
File size: 7.3 KB
RevLine 
[5618]1#region License Information
2/* HeuristicLab
[7259]3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5618]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;
[5716]24using HeuristicLab.Parameters;
[5618]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")]
[5733]31  public class SymbolicClassificationSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IClassificationProblemData, ISymbolicClassificationSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IClassificationProblem {
[5618]32    private const double PunishmentFactor = 10;
[5685]33    private const int InitialMaximumTreeDepth = 8;
34    private const int InitialMaximumTreeLength = 25;
[5770]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.";
[8594]37    private const string ModelCreatorParameterName = "ModelCreator";
[5618]38
[5685]39    #region parameter properties
[5770]40    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
41      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
[5685]42    }
[8594]43    public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
44      get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
45    }
[5685]46    #endregion
47    #region properties
[5770]48    public DoubleLimit EstimationLimits {
49      get { return EstimationLimitsParameter.Value; }
[5685]50    }
[8594]51    public ISymbolicClassificationModelCreator ModelCreator {
52      get { return ModelCreatorParameter.Value; }
53    }
[5685]54    #endregion
[5618]55    [StorableConstructor]
56    protected SymbolicClassificationSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
[8175]57    protected SymbolicClassificationSingleObjectiveProblem(SymbolicClassificationSingleObjectiveProblem original, Cloner cloner)
58      : base(original, cloner) {
59      RegisterEventHandlers();
60    }
[5618]61    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationSingleObjectiveProblem(this, cloner); }
62
63    public SymbolicClassificationSingleObjectiveProblem()
64      : base(new ClassificationProblemData(), new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
[5847]65      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
[8594]66      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
[5685]67
[8664]68      ApplyLinearScalingParameter.Value.Value = false;
[5854]69      EstimationLimitsParameter.Hidden = true;
70
[5685]71      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
72      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
73
[8175]74      RegisterEventHandlers();
[6803]75      ConfigureGrammarSymbols();
[5685]76      InitializeOperators();
[5716]77      UpdateEstimationLimits();
[5618]78    }
79
[8130]80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
[8594]82
83      if (!Parameters.ContainsKey(ModelCreatorParameterName))
84        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
85
[8130]86      bool changed = false;
87      if (!Operators.OfType<SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
88        Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
89        changed = true;
90      }
91      if (!Operators.OfType<SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
92        Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
93        changed = true;
94      }
95      if (changed) ParameterizeOperators();
[8594]96      RegisterEventHandlers();
[8130]97    }
98
[8175]99    private void RegisterEventHandlers() {
100      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
[8594]101      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
[8175]102    }
103
[6803]104    private void ConfigureGrammarSymbols() {
105      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
106      if (grammar != null) grammar.ConfigureAsDefaultClassificationGrammar();
107    }
108
[5685]109    private void InitializeOperators() {
110      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer());
111      Operators.Add(new SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer());
[5747]112      Operators.Add(new SymbolicClassificationSingleObjectiveOverfittingAnalyzer());
[7734]113      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
114      Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
[5685]115      ParameterizeOperators();
116    }
117
118    private void UpdateEstimationLimits() {
[8139]119      if (ProblemData.TrainingIndices.Any()) {
120        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
[5618]121        var mean = targetValues.Average();
122        var range = targetValues.Max() - targetValues.Min();
[5770]123        EstimationLimits.Upper = mean + PunishmentFactor * range;
124        EstimationLimits.Lower = mean - PunishmentFactor * range;
[6754]125      } else {
126        EstimationLimits.Upper = double.MaxValue;
127        EstimationLimits.Lower = double.MinValue;
[5618]128      }
129    }
[5623]130
[5685]131    protected override void OnProblemDataChanged() {
132      base.OnProblemDataChanged();
133      UpdateEstimationLimits();
134    }
135
136    protected override void ParameterizeOperators() {
137      base.ParameterizeOperators();
[5770]138      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
139        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
[8594]140        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
[5770]141          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
[8594]142        foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
143          op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
[5685]144      }
145    }
[5618]146  }
147}
Note: See TracBrowser for help on using the repository browser.