Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs @ 18057

Last change on this file since 18057 was 18053, checked in by gkronber, 3 years ago

#3135 added a new analyzer which allows to configure the solution result name, and renamed and marked the existing SolutionRSquaredAnalyzer as obsolete.

File size: 7.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Parameters;
27using HEAL.Attic;
28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
30  [Item("Symbolic Regression Problem (single-objective)", "Represents a single objective symbolic regression problem.")]
31  [StorableType("7DDCF683-96FC-4F70-BF4F-FE3A0B0DE6E0")]
32  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 100)]
33  public class SymbolicRegressionSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem {
34    private const double PunishmentFactor = 10;
35    private const int InitialMaximumTreeDepth = 8;
36    private const int InitialMaximumTreeLength = 25;
37    private const string EstimationLimitsParameterName = "EstimationLimits";
38    private const string EstimationLimitsParameterDescription = "The limits for the estimated value that can be returned by the symbolic regression model.";
39
40    #region parameter properties
41    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
42      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
43    }
44    #endregion
45    #region properties
46    public DoubleLimit EstimationLimits {
47      get { return EstimationLimitsParameter.Value; }
48    }
49    #endregion
50    [StorableConstructor]
51    protected SymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
52    protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner)
53      : base(original, cloner) {
54      RegisterEventHandlers();
55    }
56    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveProblem(this, cloner); }
57
58    public SymbolicRegressionSingleObjectiveProblem() : this(new RegressionProblemData(), new SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator(), new SymbolicDataAnalysisExpressionTreeCreator()) {
59    }
60    public SymbolicRegressionSingleObjectiveProblem(IRegressionProblemData problemData, ISymbolicRegressionSingleObjectiveEvaluator evaluator, ISymbolicDataAnalysisSolutionCreator solutionCreator) :
61      base(problemData, evaluator, solutionCreator) {
62
63      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
64
65      EstimationLimitsParameter.Hidden = true;
66
67
68      ApplyLinearScalingParameter.Value.Value = true;
69      Maximization.Value = true;
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      RegisterEventHandlers();
82      // compatibility
83      bool changed = false;
84      if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
85        Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
86        changed = true;
87      }
88      if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
89        Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
90        changed = true;
91      }
92      if (!Operators.OfType<SolutionQualityAnalyzer>().Any()) {
93        Operators.Add(new SolutionQualityAnalyzer());
94        changed = true;
95      }
96
97      if (!Operators.OfType<ShapeConstraintsAnalyzer>().Any()) {
98        Operators.Add(new ShapeConstraintsAnalyzer());
99        changed = true;
100      }
101      if (changed) {
102        ParameterizeOperators();
103      }
104    }
105
106    private void RegisterEventHandlers() {
107      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
108    }
109
110    private void ConfigureGrammarSymbols() {
111      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
112      if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
113    }
114
115    private void InitializeOperators() {
116      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
117      Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
118      Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
119      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
120      Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
121      Operators.Add(new SolutionQualityAnalyzer());
122      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
123      Operators.Add(new ShapeConstraintsAnalyzer());
124      Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
125      ParameterizeOperators();
126    }
127
128    private void UpdateEstimationLimits() {
129      if (ProblemData.TrainingIndices.Any()) {
130        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
131        var mean = targetValues.Average();
132        var range = targetValues.Max() - targetValues.Min();
133        EstimationLimits.Upper = mean + PunishmentFactor * range;
134        EstimationLimits.Lower = mean - PunishmentFactor * range;
135      } else {
136        EstimationLimits.Upper = double.MaxValue;
137        EstimationLimits.Lower = double.MinValue;
138      }
139    }
140
141    protected override void OnProblemDataChanged() {
142      base.OnProblemDataChanged();
143      UpdateEstimationLimits();
144    }
145
146    protected override void ParameterizeOperators() {
147      base.ParameterizeOperators();
148      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
149        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
150        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
151          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
152        }
153      }
154
155      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
156        op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
157        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
158
159        if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator) {
160          var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
161          phenotypicSimilarityCalculator.ProblemData = ProblemData;
162          phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
163        }
164      }
165    }
166  }
167}
Note: See TracBrowser for help on using the repository browser.