Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added upper and lower estimation limits. #938 (Data types and operators for regression problems)

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