source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/SymbolicRegressionProblem.cs @ 4468

Last change on this file since 4468 was 4468, checked in by mkommend, 9 years ago

Preparation for cross validation - removed the test samples from the trainining samples and added ValidationPercentage parameter (ticket #1199).

File size: 8.8 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 class SymbolicRegressionProblem : SymbolicRegressionProblemBase, ISingleObjectiveProblem {
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    protected SymbolicRegressionProblem(bool deserializing) : base(deserializing) { }
77    public SymbolicRegressionProblem()
78      : base() {
79      var evaluator = new SymbolicRegressionPearsonsRSquaredEvaluator();
80      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", (BoolValue)new BoolValue(true)));
81      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
82      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
83
84      evaluator.QualityParameter.ActualName = "TrainingPearsonR2";
85
86      InitializeOperators();
87      ParameterizeEvaluator();
88
89      RegisterParameterEvents();
90      RegisterParameterValueEvents();
91    }
92
93    public override IDeepCloneable Clone(Cloner cloner) {
94      SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
95      clone.RegisterParameterEvents();
96      clone.RegisterParameterValueEvents();
97      return clone;
98    }
99
100    private void RegisterParameterValueEvents() {
101      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
102    }
103
104    private void RegisterParameterEvents() { }
105
106    #region event handling
107    protected override void OnDataAnalysisProblemChanged(EventArgs e) {
108      base.OnDataAnalysisProblemChanged(e);
109      BestKnownQualityParameter.Value = null;
110      // paritions could be changed
111      ParameterizeEvaluator();
112      ParameterizeAnalyzers();
113    }
114
115    protected override void OnSolutionParameterNameChanged(EventArgs e) {
116      base.OnSolutionParameterNameChanged(e);
117      ParameterizeEvaluator();
118      ParameterizeAnalyzers();
119    }
120
121    protected override void OnEvaluatorChanged(EventArgs e) {
122      base.OnEvaluatorChanged(e);
123      ParameterizeEvaluator();
124      ParameterizeAnalyzers();
125      RaiseEvaluatorChanged(e);
126    }
127    #endregion
128
129    #region event handlers
130    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
131      OnEvaluatorChanged(e);
132    }
133    #endregion
134
135    #region Helpers
136    [StorableHook(HookType.AfterDeserialization)]
137    private void AfterDeserializationHook() {
138      // BackwardsCompatibility3.3
139      #region Backwards compatible code (remove with 3.4)
140      if (Operators == null || Operators.Count() == 0) InitializeOperators();
141      #endregion
142      RegisterParameterEvents();
143      RegisterParameterValueEvents();
144    }
145
146    private void InitializeOperators() {
147      AddOperator(new FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer());
148      ParameterizeAnalyzers();
149    }
150
151    private void ParameterizeEvaluator() {
152      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
153      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
154      Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
155      Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
156    }
157
158    private void ParameterizeAnalyzers() {
159      foreach (var analyzer in Analyzers) {
160        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
161        var fixedBestValidationSolutionAnalyzer = analyzer as FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer;
162        if (fixedBestValidationSolutionAnalyzer != null) {
163          fixedBestValidationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
164          fixedBestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
165          fixedBestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
166          fixedBestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
167          fixedBestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
168          fixedBestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
169          fixedBestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
170          fixedBestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
171        }
172
173        var bestValidationSolutionAnalyzer = analyzer as ValidationBestScaledSymbolicRegressionSolutionAnalyzer;
174        if (bestValidationSolutionAnalyzer != null) {
175          bestValidationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
176          bestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
177          bestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
178          bestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
179          bestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
180          bestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
181          bestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
182          bestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
183          bestValidationSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
184        }
185      }
186    }
187    #endregion
188  }
189}
Note: See TracBrowser for help on using the repository browser.