Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs @ 18086

Last change on this file since 18086 was 18086, checked in by mkommend, 3 years ago

#2521: Merged trunk changes into branch.

File size: 7.7 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 HEAL.Attic;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization;
27using HeuristicLab.Parameters;
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>, 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()) {
59    }
60
61    public SymbolicRegressionSingleObjectiveProblem(IRegressionProblemData problemData, ISymbolicRegressionSingleObjectiveEvaluator evaluator) : base(problemData, evaluator) {
62      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
63
64      EstimationLimitsParameter.Hidden = true;
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
78    [StorableHook(HookType.AfterDeserialization)]
79    private void AfterDeserialization() {
80      RegisterEventHandlers();
81      // compatibility
82      bool changed = false;
83      if (!Operators.OfType<SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer>().Any()) {
84        Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
85        changed = true;
86      }
87      if (!Operators.OfType<SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer>().Any()) {
88        Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
89        changed = true;
90      }
91      if (!Operators.OfType<SolutionQualityAnalyzer>().Any()) {
92        Operators.Add(new SolutionQualityAnalyzer());
93        changed = true;
94      }
95
96      if (!Operators.OfType<ShapeConstraintsAnalyzer>().Any()) {
97        Operators.Add(new ShapeConstraintsAnalyzer());
98        changed = true;
99      }
100      if (changed) {
101        ParameterizeOperators();
102      }
103    }
104
105    private void RegisterEventHandlers() {
106      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
107    }
108
109    private void ConfigureGrammarSymbols() {
110      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
111      if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
112    }
113
114    private void InitializeOperators() {
115      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
116      Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
117      Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
118      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
119      Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
120      Operators.Add(new SolutionQualityAnalyzer());
121      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
122      Operators.Add(new ShapeConstraintsAnalyzer());
123      Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
124      ParameterizeOperators();
125    }
126
127    private void UpdateEstimationLimits() {
128      if (ProblemData.TrainingIndices.Any()) {
129        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
130        var mean = targetValues.Average();
131        var range = targetValues.Max() - targetValues.Min();
132        EstimationLimits.Upper = mean + PunishmentFactor * range;
133        EstimationLimits.Lower = mean - PunishmentFactor * range;
134      } else {
135        EstimationLimits.Upper = double.MaxValue;
136        EstimationLimits.Lower = double.MinValue;
137      }
138    }
139
140    protected override void OnProblemDataChanged() {
141      base.OnProblemDataChanged();
142      UpdateEstimationLimits();
143    }
144
145    protected override void ParameterizeOperators() {
146      base.ParameterizeOperators();
147      if (Parameters.ContainsKey(EstimationLimitsParameterName)) {
148        var operators = Parameters.OfType<IValueParameter>().Select(p => p.Value).OfType<IOperator>().Union(Operators);
149        foreach (var op in operators.OfType<ISymbolicDataAnalysisBoundedOperator>()) {
150          op.EstimationLimitsParameter.ActualName = EstimationLimitsParameter.Name;
151        }
152      }
153
154      foreach (var op in Operators.OfType<ISolutionSimilarityCalculator>()) {
155        //op.SolutionVariableName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
156        op.QualityVariableName = Evaluator.QualityParameter.ActualName;
157
158        if (op is SymbolicExpressionTreePhenotypicSimilarityCalculator) {
159          var phenotypicSimilarityCalculator = (SymbolicExpressionTreePhenotypicSimilarityCalculator)op;
160          phenotypicSimilarityCalculator.ProblemData = ProblemData;
161          phenotypicSimilarityCalculator.Interpreter = SymbolicExpressionTreeInterpreter;
162        }
163      }
164    }
165  }
166}
Note: See TracBrowser for help on using the repository browser.