Free cookie consent management tool by TermsFeed Policy Generator

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

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

Included tracking of best of run solution (based on validation set) and calculation of MSE, R² and rel. Error on training and test sets. #938 (Data types and operators for regression problems)

File size: 16.6 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.Collections.Generic;
24using System.Linq;
25using System.Drawing;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32using HeuristicLab.PluginInfrastructure;
33using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
34using HeuristicLab.Problems.DataAnalysis.Regression;
35using HeuristicLab.Problems.DataAnalysis.Symbolic;
36
37namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
38  [Item("SymbolicRegressionProblem", "Represents a symbolic regression problem.")]
39  [Creatable("Problems")]
40  [StorableClass]
41  public sealed class SymbolicRegressionProblem : DataAnalysisProblem, ISingleObjectiveProblem {
42
43    #region Parameter Properties
44    public ValueParameter<BoolValue> MaximizationParameter {
45      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
46    }
47    IParameter ISingleObjectiveProblem.MaximizationParameter {
48      get { return MaximizationParameter; }
49    }
50    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
51      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
52    }
53    IParameter IProblem.SolutionCreatorParameter {
54      get { return SolutionCreatorParameter; }
55    }
56    public ValueParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {
57      get { return (ValueParameter<ISymbolicRegressionEvaluator>)Parameters["Evaluator"]; }
58    }
59    IParameter IProblem.EvaluatorParameter {
60      get { return EvaluatorParameter; }
61    }
62    public ValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
63      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["FunctionTreeGrammar"]; }
64    }
65    public ValueParameter<IntValue> MaxExpressionLengthParameter {
66      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
67    }
68    public ValueParameter<IntValue> MaxExpressionDepthParameter {
69      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
70    }
71    public ValueParameter<DoubleValue> NumberOfEvaluatedNodesParameter {
72      get { return (ValueParameter<DoubleValue>)Parameters["NumberOfEvaluatedNodes"]; }
73    }
74    public ValueParameter<IntValue> MaxFunctionDefiningBranchesParameter {
75      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefiningBranches"]; }
76    }
77    public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
78      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
79    }
80    public OptionalValueParameter<ISingleObjectiveSolutionsVisualizer> VisualizerParameter {
81      get { return (OptionalValueParameter<ISingleObjectiveSolutionsVisualizer>)Parameters["Visualizer"]; }
82    }
83    IParameter IProblem.VisualizerParameter {
84      get { return VisualizerParameter; }
85    }
86    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
87      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
88    }
89    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
90      get { return BestKnownQualityParameter; }
91    }
92    #endregion
93
94    #region Properties
95    public IntValue MaxExpressionLength {
96      get { return MaxExpressionLengthParameter.Value; }
97      set { MaxExpressionLengthParameter.Value = value; }
98    }
99    public IntValue MaxExpressionDepth {
100      get { return MaxExpressionDepthParameter.Value; }
101      set { MaxExpressionDepthParameter.Value = value; }
102    }
103    public SymbolicExpressionTreeCreator SolutionCreator {
104      get { return SolutionCreatorParameter.Value; }
105      set { SolutionCreatorParameter.Value = value; }
106    }
107    ISolutionCreator IProblem.SolutionCreator {
108      get { return SolutionCreatorParameter.Value; }
109    }
110    public ISymbolicRegressionEvaluator Evaluator {
111      get { return EvaluatorParameter.Value; }
112      set { EvaluatorParameter.Value = value; }
113    }
114    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
115      get { return EvaluatorParameter.Value; }
116    }
117    IEvaluator IProblem.Evaluator {
118      get { return EvaluatorParameter.Value; }
119    }
120    public ISymbolicExpressionGrammar FunctionTreeGrammar {
121      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
122    }
123    public ISingleObjectiveSolutionsVisualizer Visualizer {
124      get { return VisualizerParameter.Value; }
125      set { VisualizerParameter.Value = value; }
126    }
127    ISolutionsVisualizer IProblem.Visualizer {
128      get { return VisualizerParameter.Value; }
129    }
130    public DoubleValue BestKnownQuality {
131      get { return BestKnownQualityParameter.Value; }
132    }
133    private List<ISymbolicExpressionTreeOperator> operators;
134    public IEnumerable<IOperator> Operators {
135      get { return operators.Cast<IOperator>(); }
136    }
137    #endregion
138
139    public SymbolicRegressionProblem()
140      : base() {
141      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
142      var evaluator = new SymbolicRegressionMeanSquaredErrorEvaluator();
143      var grammar = new ArithmeticExpressionGrammar();
144      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
145      var visualizer = new BestValidationSymbolicRegressionSolutionVisualizer();
146      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", new BoolValue(false)));
147      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
148      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
149      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
150      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", globalGrammar));
151      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
152      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
153      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", new IntValue(3)));
154      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", new IntValue(3)));
155      Parameters.Add(new ValueParameter<DoubleValue>("NumberOfEvaluatedNodes", "The total number of evaluated function tree nodes (for performance measurements.)", new DoubleValue()));
156      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize symbolic regression solutions.", visualizer));
157
158      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";
159      creator.MaxFunctionArgumentsParameter.ActualName = "MaxFunctionArguments";
160      creator.MaxFunctionDefinitionsParameter.ActualName = "MaxFunctionDefiningBranches";
161      DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged);
162      DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
163      ParameterizeSolutionCreator();
164      ParameterizeEvaluator();
165      ParameterizeVisualizer();
166
167      Initialize();
168    }
169
170
171    [StorableConstructor]
172    private SymbolicRegressionProblem(bool deserializing) : base() { }
173
174    public override IDeepCloneable Clone(Cloner cloner) {
175      SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
176      clone.Initialize();
177      return clone;
178    }
179
180    #region Events
181    void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) {
182      DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
183    }
184
185    void DataAnalysisProblemData_Changed(object sender, EventArgs e) {
186      foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
187        varSymbol.VariableNames = DataAnalysisProblemData.InputVariables.Select(x => x.Value);
188      }
189      UpdatePartitioningParameters();
190    }
191
192    private void UpdatePartitioningParameters() {
193      int trainingStart = DataAnalysisProblemData.TrainingSamplesStart.Value;
194      int validationEnd = DataAnalysisProblemData.TrainingSamplesEnd.Value;
195      int trainingEnd = trainingStart + (validationEnd - trainingStart) / 2;
196      int validationStart = trainingEnd;
197      var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
198      if (solutionVisualizer != null) {
199        solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue(validationStart);
200        solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(validationEnd);
201      }
202      Evaluator.SamplesStartParameter.Value = new IntValue(trainingStart);
203      Evaluator.SamplesEndParameter.Value = new IntValue(trainingEnd);
204    }
205
206    public event EventHandler SolutionCreatorChanged;
207    private void OnSolutionCreatorChanged() {
208      var changed = SolutionCreatorChanged;
209      if (changed != null)
210        changed(this, EventArgs.Empty);
211    }
212    public event EventHandler EvaluatorChanged;
213    private void OnEvaluatorChanged() {
214      var changed = EvaluatorChanged;
215      if (changed != null)
216        changed(this, EventArgs.Empty);
217    }
218    public event EventHandler VisualizerChanged;
219    private void OnVisualizerChanged() {
220      var changed = VisualizerChanged;
221      if (changed != null)
222        changed(this, EventArgs.Empty);
223    }
224
225    public event EventHandler OperatorsChanged;
226    private void OnOperatorsChanged() {
227      var changed = OperatorsChanged;
228      if (changed != null)
229        changed(this, EventArgs.Empty);
230    }
231
232    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
233      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
234      ParameterizeSolutionCreator();
235      ParameterizeEvaluator();
236      ParameterizeVisualizer();
237      ParameterizeOperators();
238      OnSolutionCreatorChanged();
239    }
240    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
241      ParameterizeEvaluator();
242      ParameterizeVisualizer();
243      ParameterizeOperators();
244    }
245    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
246      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
247      ParameterizeEvaluator();
248      ParameterizeVisualizer();
249      OnEvaluatorChanged();
250    }
251
252    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
253      ParameterizeVisualizer();
254      OnVisualizerChanged();
255    }
256
257    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
258      ParameterizeVisualizer();
259    }
260
261    #endregion
262
263    #region Helpers
264    [StorableHook(HookType.AfterDeserialization)]
265    private void Initialize() {
266      InitializeOperators();
267      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
268      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
269      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
270      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
271      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
272    }
273
274    private void InitializeOperators() {
275      operators = new List<ISymbolicExpressionTreeOperator>();
276      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
277      ParameterizeOperators();
278    }
279
280    private void ParameterizeSolutionCreator() {
281      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
282      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
283      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
284    }
285    private void ParameterizeEvaluator() {
286      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
287      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
288      Evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
289      Evaluator.SamplesStartParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value);
290      Evaluator.SamplesEndParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
291    }
292    private void ParameterizeVisualizer() {
293      if (Visualizer != null) {
294        var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
295        if (solutionVisualizer != null) {
296          solutionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
297          solutionVisualizer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
298          solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
299          solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value);
300        }
301      }
302    }
303
304    private void ParameterizeOperators() {
305      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
306        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
307        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
308        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
309      }
310      foreach (ISymbolicRegressionEvaluator op in Operators.OfType<ISymbolicRegressionEvaluator>()) {
311        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
312        op.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
313        op.NumberOfEvaluatedNodesParameter.ActualName = NumberOfEvaluatedNodesParameter.Name;
314      }
315      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
316        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
317        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
318      }
319      foreach (SymbolicExpressionTreeManipulator op in Operators.OfType<SymbolicExpressionTreeManipulator>()) {
320        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
321      }
322      foreach (SymbolicExpressionTreeArchitectureAlteringOperator op in Operators.OfType<SymbolicExpressionTreeArchitectureAlteringOperator>()) {
323      }
324    }
325    #endregion
326  }
327}
Note: See TracBrowser for help on using the repository browser.