Free cookie consent management tool by TermsFeed Policy Generator

source: branches/SymbolicExpressionTreeDiversityAnalyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs @ 12068

Last change on this file since 12068 was 12068, checked in by bburlacu, 9 years ago

#2326: Fixed mistakes and wired similarity calculators directly into the problem and correctly initialized the properties of the phenotypic similarity calculator.

File size: 8.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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      ApplyLinearScalingParameter.Value.Value = false;
69      EstimationLimitsParameter.Hidden = true;
70
71      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
72      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
73
74      RegisterEventHandlers();
75      ConfigureGrammarSymbols();
76      InitializeOperators();
77      UpdateEstimationLimits();
78    }
79
80    [StorableHook(HookType.AfterDeserialization)]
81    private void AfterDeserialization() {
82      // BackwardsCompatibility3.4
83      #region Backwards compatible code, remove with 3.5
84      if (!Parameters.ContainsKey(ModelCreatorParameterName))
85        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
86
87      bool changed = false;
88      if (!Operators.OfType<SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
89        Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
90        changed = true;
91      }
92      if (!Operators.OfType<SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
93        Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
94        changed = true;
95      }
96      if (changed) ParameterizeOperators();
97      #endregion
98      RegisterEventHandlers();
99    }
100
101    private void RegisterEventHandlers() {
102      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
103      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
104    }
105
106    private void ConfigureGrammarSymbols() {
107      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
108      if (grammar != null) grammar.ConfigureAsDefaultClassificationGrammar();
109    }
110
111    private void InitializeOperators() {
112      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer());
113      Operators.Add(new SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer());
114      Operators.Add(new SymbolicClassificationSingleObjectiveOverfittingAnalyzer());
115      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
116      Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
117      Operators.Add(new SymbolicClassificationPhenotypicDiversityAnalyzer());
118      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
119      ParameterizeOperators();
120    }
121
122    private void UpdateEstimationLimits() {
123      if (ProblemData.TrainingIndices.Any()) {
124        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
125        var mean = targetValues.Average();
126        var range = targetValues.Max() - targetValues.Min();
127        EstimationLimits.Upper = mean + PunishmentFactor * range;
128        EstimationLimits.Lower = mean - PunishmentFactor * range;
129      } else {
130        EstimationLimits.Upper = double.MaxValue;
131        EstimationLimits.Lower = double.MinValue;
132      }
133    }
134
135    protected override void OnProblemDataChanged() {
136      base.OnProblemDataChanged();
137      UpdateEstimationLimits();
138    }
139
140    protected override void ParameterizeOperators() {
141      base.ParameterizeOperators();
142      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
143        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
144        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
145          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
146        foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
147          op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
148      }
149
150      foreach (var op in Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) {
151        op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
152        op.ProblemData = ProblemData;
153        op.Interpreter = SymbolicExpressionTreeInterpreter;
154      }
155
156      foreach (var op in Operators.OfType<SymbolicClassificationPhenotypicDiversityAnalyzer>()) {
157        var sim = Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>().FirstOrDefault();
158        if (sim != null)
159          op.SimilarityCalculator = sim;
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.