Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Mono/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/SymbolicRegressionSingleObjectiveProblem.cs @ 8365

Last change on this file since 8365 was 8175, checked in by mkommend, 12 years ago

#1810: Corrected event registration for grammar configuration and updated samples.

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