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
RevLine 
[5618]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5618]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;
[12103]25using HeuristicLab.Optimization;
[5716]26using HeuristicLab.Parameters;
[16565]27using HEAL.Attic;
[5618]28
29namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[12504]30  [Item("Symbolic Regression Problem (single-objective)", "Represents a single objective symbolic regression problem.")]
[16565]31  [StorableType("7DDCF683-96FC-4F70-BF4F-FE3A0B0DE6E0")]
[12504]32  [Creatable(CreatableAttribute.Categories.GeneticProgrammingProblems, Priority = 100)]
[5759]33  public class SymbolicRegressionSingleObjectiveProblem : SymbolicDataAnalysisSingleObjectiveProblem<IRegressionProblemData, ISymbolicRegressionSingleObjectiveEvaluator, ISymbolicDataAnalysisSolutionCreator>, IRegressionProblem {
[5618]34    private const double PunishmentFactor = 10;
[5685]35    private const int InitialMaximumTreeDepth = 8;
36    private const int InitialMaximumTreeLength = 25;
[5770]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.";
[5716]39
[5685]40    #region parameter properties
[5770]41    public IFixedValueParameter<DoubleLimit> EstimationLimitsParameter {
42      get { return (IFixedValueParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
[5685]43    }
44    #endregion
45    #region properties
[5770]46    public DoubleLimit EstimationLimits {
47      get { return EstimationLimitsParameter.Value; }
[5685]48    }
49    #endregion
[5618]50    [StorableConstructor]
[16565]51    protected SymbolicRegressionSingleObjectiveProblem(StorableConstructorFlag _) : base(_) { }
[8175]52    protected SymbolicRegressionSingleObjectiveProblem(SymbolicRegressionSingleObjectiveProblem original, Cloner cloner)
53      : base(original, cloner) {
54      RegisterEventHandlers();
55    }
[5618]56    public override IDeepCloneable Clone(Cloner cloner) { return new SymbolicRegressionSingleObjectiveProblem(this, cloner); }
57
[17958]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
[5847]63      Parameters.Add(new FixedValueParameter<DoubleLimit>(EstimationLimitsParameterName, EstimationLimitsParameterDescription));
[5685]64
[5854]65      EstimationLimitsParameter.Hidden = true;
66
[8664]67
68      ApplyLinearScalingParameter.Value.Value = true;
[5618]69      Maximization.Value = true;
[5685]70      MaximumSymbolicExpressionTreeDepth.Value = InitialMaximumTreeDepth;
71      MaximumSymbolicExpressionTreeLength.Value = InitialMaximumTreeLength;
72
[8175]73      RegisterEventHandlers();
[6803]74      ConfigureGrammarSymbols();
[5685]75      InitializeOperators();
[5716]76      UpdateEstimationLimits();
[5618]77    }
78
[8130]79    [StorableHook(HookType.AfterDeserialization)]
80    private void AfterDeserialization() {
[8175]81      RegisterEventHandlers();
[8130]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      }
[18053]92      if (!Operators.OfType<SolutionQualityAnalyzer>().Any()) {
93        Operators.Add(new SolutionQualityAnalyzer());
[10596]94        changed = true;
95      }
[17903]96
97      if (!Operators.OfType<ShapeConstraintsAnalyzer>().Any()) {
98        Operators.Add(new ShapeConstraintsAnalyzer());
99        changed = true;
100      }
[8130]101      if (changed) {
102        ParameterizeOperators();
103      }
104    }
105
[8175]106    private void RegisterEventHandlers() {
107      SymbolicExpressionTreeGrammarParameter.ValueChanged += (o, e) => ConfigureGrammarSymbols();
108    }
109
[6803]110    private void ConfigureGrammarSymbols() {
111      var grammar = SymbolicExpressionTreeGrammar as TypeCoherentExpressionGrammar;
112      if (grammar != null) grammar.ConfigureAsDefaultRegressionGrammar();
113    }
114
[5685]115    private void InitializeOperators() {
116      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingBestSolutionAnalyzer());
117      Operators.Add(new SymbolicRegressionSingleObjectiveValidationBestSolutionAnalyzer());
[5747]118      Operators.Add(new SymbolicRegressionSingleObjectiveOverfittingAnalyzer());
[7726]119      Operators.Add(new SymbolicRegressionSingleObjectiveTrainingParetoBestSolutionAnalyzer());
[7734]120      Operators.Add(new SymbolicRegressionSingleObjectiveValidationParetoBestSolutionAnalyzer());
[18053]121      Operators.Add(new SolutionQualityAnalyzer());
[12103]122      Operators.Add(new SymbolicExpressionTreePhenotypicSimilarityCalculator());
[17903]123      Operators.Add(new ShapeConstraintsAnalyzer());
[12103]124      Operators.Add(new SymbolicRegressionPhenotypicDiversityAnalyzer(Operators.OfType<SymbolicExpressionTreePhenotypicSimilarityCalculator>()) { DiversityResultName = "Phenotypic Diversity" });
[5685]125      ParameterizeOperators();
126    }
[5716]127
[5685]128    private void UpdateEstimationLimits() {
[8139]129      if (ProblemData.TrainingIndices.Any()) {
130        var targetValues = ProblemData.Dataset.GetDoubleValues(ProblemData.TargetVariable, ProblemData.TrainingIndices).ToList();
[5618]131        var mean = targetValues.Average();
132        var range = targetValues.Max() - targetValues.Min();
[5770]133        EstimationLimits.Upper = mean + PunishmentFactor * range;
134        EstimationLimits.Lower = mean - PunishmentFactor * range;
[6754]135      } else {
136        EstimationLimits.Upper = double.MaxValue;
137        EstimationLimits.Lower = double.MinValue;
[5618]138      }
139    }
[5623]140
[5685]141    protected override void OnProblemDataChanged() {
142      base.OnProblemDataChanged();
143      UpdateEstimationLimits();
144    }
145
146    protected override void ParameterizeOperators() {
147      base.ParameterizeOperators();
[5770]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        }
[5685]153      }
[12103]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      }
[5685]165    }
[5618]166  }
167}
Note: See TracBrowser for help on using the repository browser.