Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveProblem.cs @ 17709

Last change on this file since 17709 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

File size: 8.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Optimization;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
29  [Item("Symbolic Classification Problem (single-objective)", "Represents a single objective symbolic classfication problem.")]
30  [StorableClass]
31  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 120)]
32  public class SymbolicClassificationSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IClassificationProblemData, ISymbolicClassificationSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IClassificationProblem {
33    private const double PunishmentFactor = 10;
34    private const int InitialMaximumTreeDepth = 8;
35    private const int InitialMaximumTreeLength = 25;
36    private const string EstimationLimitsParameterName = "EstimationLimits";
37    private const string EstimationLimitsParameterDescription = "The lower and upper limit for the estimated value that can be returned by the symbolic classification model.";
38    private const string ModelCreatorParameterName = "ModelCreator";
39
40    #region parameter properties
41    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
42      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
43    }
44    public IValueParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
45      get { return (IValueParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
46    }
47    #endregion
48    #region properties
49    public DoubleLimit EstimationLimits {
50      get { return EstimationLimitsParameter.Value; }
51    }
52    public ISymbolicClassificationModelCreator ModelCreator {
53      get { return ModelCreatorParameter.Value; }
54    }
55    #endregion
56    [StorableConstructor]
57    protected SymbolicClassificationSingleObjectiveProblem(bool deserializing) : base(deserializing) { }
58    protected SymbolicClassificationSingleObjectiveProblem(SymbolicClassificationSingleObjectiveProblem original, Cloner cloner)
59      : base(original, cloner) {
60      RegisterEventHandlers();
61    }
62    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicClassificationSingleObjectiveProblem(this, cloner); }
63
64    public SymbolicClassificationSingleObjectiveProblem()
65      : base(new ClassificationProblemData(), new SymbolicClassificationSingleObjectiveMeanSquaredErrorEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
66      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
67      Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
68
69      ApplyLinearScalingParameter.Value.Value = false;
70      EstimationLimitsParameter.Hidden = true;
71
72      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
73      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
74
75      RegisterEventHandlers();
76      ConfigureGrammarSymbols();
77      InitializeOperators();
78      UpdateEstimationLimits();
79    }
80
81    [StorableHook(HookType.AfterDeserialization)]
82    private void AfterDeserialization() {
83      // BackwardsCompatibility3.4
84      #region Backwards compatible code, remove with 3.5
85      if (!Parameters.ContainsKey(ModelCreatorParameterName))
86        Parameters.Add(new ValueParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "", new AccuracyMaximizingThresholdsModelCreator()));
87
88      bool changed = false;
89      if (!Operators.OfType<SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
90        Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
91        changed = true;
92      }
93      if (!Operators.OfType<SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
94        Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
95        changed = true;
96      }
97      if (changed) ParameterizeOperators();
98      #endregion
99      RegisterEventHandlers();
100    }
101
102    private void RegisterEventHandlers() {
103      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
104      ModelCreatorParameter.NameChanged += (o, e) => ParameterizeOperators();
105    }
106
107    private void ConfigureGrammarSymbols() {
108      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
109      if (grammar != null) grammar.ConfigureAsDefaultClassificationGrammar();
110    }
111
112    private void InitializeOperators() {
113      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingBestSolutionAnalyzer());
114      Operators.Add(new SymbolicClassificationSingleObjectiveValidationBestSolutionAnalyzer());
115      Operators.Add(new SymbolicClassificationSingleObjectiveOverfittingAnalyzer());
116      Operators.Add(new SymbolicClassificationSingleObjectiveTrainingParetoBestSolutionAnalyzer());
117      Operators.Add(new SymbolicClassificationSingleObjectiveValidationParetoBestSolutionAnalyzer());
118      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
119      Operators.Add(new SymbolicClassificationPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()));
120      ParameterizeOperators();
121    }
122
123    private void UpdateEstimationLimits() {
124      if (ProblemData.TrainingIndices.Any()) {
125        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
126        var mean = targetValues.Average();
127        var range = targetValues.Max() - targetValues.Min();
128        EstimationLimits.Upper = mean + PunishmentFactor * range;
129        EstimationLimits.Lower = mean - PunishmentFactor * range;
130      } else {
131        EstimationLimits.Upper = double.MaxValue;
132        EstimationLimits.Lower = double.MinValue;
133      }
134    }
135
136    protected override void OnProblemDataChanged() {
137      base.OnProblemDataChanged();
138      UpdateEstimationLimits();
139    }
140
141    protected override void ParameterizeOperators() {
142      base.ParameterizeOperators();
143      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
144        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
145        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>())
146          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
147        foreach (var op in operators.OfType<ISymbolicClassificationModelCreatorOperator>())
148          op.ModelCreatorParameter.ActualName = ModelCreatorParameter.Name;
149      }
150
151      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
152        op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
153        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
154
155        if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator) {
156          var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
157          phenotypicSimilarityCalculator.ProblemData = ProblemData;
158          phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
159        }
160      }
161    }
162  }
163}
Note: See TracBrowser for help on using the repository browser.