Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 6593 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

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