Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5785 was 5625, checked in by mkommend, 14 years ago

#1418: Reorganized branch and removed CreateAble-Attribute from outdated classes.

File size: 9.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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  [StorableClass]
35  public sealed class SymbolicRegressionProblem : SymbolicRegressionProblemBase, ISingleObjectiveDataAnalysisProblem {
36
37    #region Parameter Properties
38    public ValueParameter<BoolValue> MaximizationParameter {
39      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
40    }
41    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
42      get { return MaximizationParameter; }
43    }
44    public new ValueParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {
45      get { return (ValueParameter<ISymbolicRegressionEvaluator>)Parameters["Evaluator"]; }
46    }
47    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
48      get { return EvaluatorParameter; }
49    }
50    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
51      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
52    }
53    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
54      get { return BestKnownQualityParameter; }
55    }
56    #endregion
57
58    #region Properties
59    public new ISymbolicRegressionEvaluator Evaluator {
60      get { return EvaluatorParameter.Value; }
61      set { EvaluatorParameter.Value = value; }
62    }
63    ISingleObjectiveEvaluator ISingleObjectiveHeuristicOptimizationProblem.Evaluator {
64      get { return EvaluatorParameter.Value; }
65    }
66    IEvaluator IHeuristicOptimizationProblem.Evaluator {
67      get { return EvaluatorParameter.Value; }
68    }
69    public DoubleValue BestKnownQuality {
70      get { return BestKnownQualityParameter.Value; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    private SymbolicRegressionProblem(bool deserializing) : base(deserializing) { }
76    private SymbolicRegressionProblem(SymbolicRegressionProblem original, Cloner cloner)
77      : base(original, cloner) {
78      RegisterParameterEvents();
79      RegisterParameterValueEvents();
80    }
81
82    public SymbolicRegressionProblem()
83      : base() {
84      var evaluator = new SymbolicRegressionPearsonsRSquaredEvaluator();
85      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", (BoolValue)new BoolValue(true)));
86      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
87      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
88
89      InitializeOperators();
90      ParameterizeEvaluator();
91
92      RegisterParameterEvents();
93      RegisterParameterValueEvents();
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new SymbolicRegressionProblem(this, cloner);
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      ParameterizeProblem();
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      AddOperator(new TrainingBestScaledSymbolicRegressionSolutionAnalyzer());
151      ParameterizeAnalyzers();
152    }
153
154    private void ParameterizeEvaluator() {
155      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
156      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
157      Evaluator.SamplesStartParameter.Value = TrainingSamplesStart;
158      Evaluator.SamplesEndParameter.Value = TrainingSamplesEnd;
159    }
160
161    private void ParameterizeAnalyzers() {
162      foreach (var analyzer in Analyzers) {
163        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
164        var validationSolutionAnalyzer = analyzer as SymbolicRegressionValidationAnalyzer;
165        if (validationSolutionAnalyzer != null) {
166          validationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
167          validationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
168          validationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
169          validationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
170          validationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
171          validationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
172          validationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
173        }
174
175        var fixedBestValidationSolutionAnalyzer = analyzer as FixedValidationBestScaledSymbolicRegressionSolutionAnalyzer;
176        if (fixedBestValidationSolutionAnalyzer != null) {
177          fixedBestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
178        }
179
180        var bestValidationSolutionAnalyzer = analyzer as ValidationBestScaledSymbolicRegressionSolutionAnalyzer;
181        if (bestValidationSolutionAnalyzer != null) {
182          bestValidationSolutionAnalyzer.ProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
183          bestValidationSolutionAnalyzer.UpperEstimationLimitParameter.ActualName = UpperEstimationLimitParameter.Name;
184          bestValidationSolutionAnalyzer.LowerEstimationLimitParameter.ActualName = LowerEstimationLimitParameter.Name;
185          bestValidationSolutionAnalyzer.SymbolicExpressionTreeInterpreterParameter.ActualName = SymbolicExpressionTreeInterpreterParameter.Name;
186          bestValidationSolutionAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
187          bestValidationSolutionAnalyzer.ValidationSamplesStartParameter.Value = ValidationSamplesStart;
188          bestValidationSolutionAnalyzer.ValidationSamplesEndParameter.Value = ValidationSamplesEnd;
189          bestValidationSolutionAnalyzer.BestKnownQualityParameter.ActualName = BestKnownQualityParameter.Name;
190          bestValidationSolutionAnalyzer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
191        }
192      }
193    }
194
195    private void ParameterizeProblem() {
196      if (MaximizationParameter.Value != null) {
197        MaximizationParameter.Value.Value = Evaluator.Maximization;
198      } else {
199        MaximizationParameter.Value = new BoolValue(Evaluator.Maximization);
200      }
201    }
202    #endregion
203  }
204}
Note: See TracBrowser for help on using the repository browser.