Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3076_IA_evaluators_analyzers/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs @ 18242

Last change on this file since 18242 was 17821, checked in by chaider, 4 years ago

#3076 Refactoring Evaluators and Analyzers

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