Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs @ 16641

Last change on this file since 16641 was 16641, checked in by gkronber, 5 years ago

#2971: merged r16527:16625 from trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression to branch/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression (resolving all conflicts)

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