Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3462 was 3462, checked in by gkronber, 15 years ago

Refactored symbolic expression tree encoding and problem classes for symbolic regression. #937 , #938

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