Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs @ 5477

Last change on this file since 5477 was 5275, checked in by gkronber, 14 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers;
31
32namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
33  [Item("Symbolic Regression Problem (single objective)", "Represents a single objective symbolic regression problem.")]
34  [Creatable("Problems")]
35  [StorableClass]
36  public sealed class SymbolicRegressionProblem : SymbolicRegressionProblemBase, ISingleObjectiveDataAnalysisProblem {
37
38    #region Parameter Properties
39    public ValueParameter<BoolValue> MaximizationParameter {
40      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
41    }
42    IParameter ISingleObjectiveProblem.MaximizationParameter {
43      get { return MaximizationParameter; }
44    }
45    public new ValueParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {
46      get { return (ValueParameter<ISymbolicRegressionEvaluator>)Parameters["Evaluator"]; }
47    }
48    IParameter IProblem.EvaluatorParameter {
49      get { return EvaluatorParameter; }
50    }
51    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
52      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
53    }
54    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
55      get { return BestKnownQualityParameter; }
56    }
57    #endregion
58
59    #region Properties
60    public new ISymbolicRegressionEvaluator Evaluator {
61      get { return EvaluatorParameter.Value; }
62      set { EvaluatorParameter.Value = value; }
63    }
64    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
65      get { return EvaluatorParameter.Value; }
66    }
67    IEvaluator IProblem.Evaluator {
68      get { return EvaluatorParameter.Value; }
69    }
70    public DoubleValue BestKnownQuality {
71      get { return BestKnownQualityParameter.Value; }
72    }
73    #endregion
74
75    [StorableConstructor]
76    private SymbolicRegressionProblem(bool deserializing) : base(deserializing) { }
77    private SymbolicRegressionProblem(SymbolicRegressionProblem original, Cloner cloner)
78      : base(original, cloner) {
79      RegisterParameterEvents();
80      RegisterParameterValueEvents();
81    }
82
83    public SymbolicRegressionProblem()
84      : base() {
85      var evaluator = new SymbolicRegressionPearsonsRSquaredEvaluator();
86      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", (BoolValue)new BoolValue(true)));
87      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
88      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
89
90      InitializeOperators();
91      ParameterizeEvaluator();
92
93      RegisterParameterEvents();
94      RegisterParameterValueEvents();
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new SymbolicRegressionProblem(this, cloner);
99    }
100
101    private void RegisterParameterValueEvents() {
102      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
103    }
104
105    private void RegisterParameterEvents() { }
106
107    #region event handling
108    protected override void OnDataAnalysisProblemChanged(EventArgs e) {
109      base.OnDataAnalysisProblemChanged(e);
110      BestKnownQualityParameter.Value = null;
111      // paritions could be changed
112      ParameterizeEvaluator();
113      ParameterizeAnalyzers();
114    }
115
116    protected override void OnSolutionParameterNameChanged(EventArgs e) {
117      base.OnSolutionParameterNameChanged(e);
118      ParameterizeEvaluator();
119      ParameterizeAnalyzers();
120    }
121
122    protected override void OnEvaluatorChanged(EventArgs e) {
123      base.OnEvaluatorChanged(e);
124      ParameterizeEvaluator();
125      ParameterizeAnalyzers();
126      RaiseEvaluatorChanged(e);
127    }
128    #endregion
129
130    #region event handlers
131    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
132      OnEvaluatorChanged(e);
133    }
134    #endregion
135
136    #region Helpers
137    [StorableHook(HookType.AfterDeserialization)]
138    private void AfterDeserializationHook() {
139      // BackwardsCompatibility3.3
140      #region Backwards compatible code (remove with 3.4)
141      if (Operators == null || Operators.Count() == 0) InitializeOperators();
142      #endregion
143      RegisterParameterEvents();
144      RegisterParameterValueEvents();
145    }
146
147    private void InitializeOperators() {
148      AddOperator(new FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer());
149      AddOperator(new SymbolicRegressionOverfittingAnalyzer());
150      ParameterizeAnalyzers();
151    }
152
153    private void ParameterizeEvaluator() {
154      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
155      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
156      Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
157      Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
158    }
159
160    private void ParameterizeAnalyzers() {
161      foreach (var analyzer in Analyzers) {
162        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
163        var validationSolutionAnalyzer = analyzer as SymbolicRegressionValidationAnalyzer;
164        if (validationSolutionAnalyzer != null) {
165          validationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
166          validationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
167          validationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
168          validationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
169          validationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
170          validationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
171          validationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
172        }
173
174        var fixedBestValidationSolutionAnalyzer = analyzer as FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer;
175        if (fixedBestValidationSolutionAnalyzer != null) {
176          fixedBestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
177        }
178
179        var bestValidationSolutionAnalyzer = analyzer as ValidationBestScaledSymbolicRegressionSolutionAnalyzer;
180        if (bestValidationSolutionAnalyzer != null) {
181          bestValidationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
182          bestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
183          bestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
184          bestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
185          bestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
186          bestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
187          bestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
188          bestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
189          bestValidationSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
190        }
191      }
192    }
193    #endregion
194  }
195}
Note: See TracBrowser for help on using the repository browser.