Free cookie consent management tool by TermsFeed Policy Generator

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

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

minor bug fixes #937 (Data types and operators for symbolic expression tree encoding)

File size: 17.3 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<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    }
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    }
84    public OptionalValueParameter<ISingleObjectiveSolutionsVisualizer> VisualizerParameter {
85      get { return (OptionalValueParameter<ISingleObjectiveSolutionsVisualizer>)Parameters["Visualizer"]; }
86    }
87    IParameter IProblem.VisualizerParameter {
88      get { return VisualizerParameter; }
89    }
90    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
91      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
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    }
124    public ISymbolicExpressionGrammar FunctionTreeGrammar {
125      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
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();
147      var grammar = new ArithmeticExpressionGrammar();
148      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
149      var visualizer = new BestValidationSymbolicRegressionSolutionVisualizer();
150      var interpreter = new SimpleArithmeticExpressionInterpreter();
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));
153      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter));
154      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
155      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
156      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", globalGrammar));
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)));
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)));
161      Parameters.Add(new ValueParameter<DoubleValue>("NumberOfEvaluatedNodes", "The total number of evaluated function tree nodes (for performance measurements.)", new DoubleValue()));
162      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize symbolic regression solutions.", visualizer));
163
164      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";
165      creator.MaxFunctionArgumentsParameter.ActualName = "MaxFunctionArguments";
166      creator.MaxFunctionDefinitionsParameter.ActualName = "MaxFunctionDefiningBranches";
167      DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged);
168      DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
169      ParameterizeSolutionCreator();
170      ParameterizeEvaluator();
171      ParameterizeVisualizer();
172
173      Initialize();
174    }
175
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
187    void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) {
188      DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
189    }
190
191    void DataAnalysisProblemData_Changed(object sender, EventArgs e) {
192      UpdateGrammar();
193      UpdatePartitioningParameters();
194    }
195
196    private void UpdateGrammar() {
197      foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
198        varSymbol.VariableNames = DataAnalysisProblemData.InputVariables.Select(x => x.Value);
199      }
200    }
201
202    private void UpdatePartitioningParameters() {
203      int trainingStart = DataAnalysisProblemData.TrainingSamplesStart.Value;
204      int validationEnd = DataAnalysisProblemData.TrainingSamplesEnd.Value;
205      int trainingEnd = trainingStart + (validationEnd - trainingStart) / 2;
206      int validationStart = trainingEnd;
207      var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
208      if (solutionVisualizer != null) {
209        solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue(validationStart);
210        solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(validationEnd);
211      }
212      Evaluator.SamplesStartParameter.Value = new IntValue(trainingStart);
213      Evaluator.SamplesEndParameter.Value = new IntValue(trainingEnd);
214    }
215
216    public event EventHandler SolutionCreatorChanged;
217    private void OnSolutionCreatorChanged() {
218      var changed = SolutionCreatorChanged;
219      if (changed != null)
220        changed(this, EventArgs.Empty);
221    }
222    public event EventHandler EvaluatorChanged;
223    private void OnEvaluatorChanged() {
224      var changed = EvaluatorChanged;
225      if (changed != null)
226        changed(this, EventArgs.Empty);
227    }
228    public event EventHandler VisualizerChanged;
229    private void OnVisualizerChanged() {
230      var changed = VisualizerChanged;
231      if (changed != null)
232        changed(this, EventArgs.Empty);
233    }
234
235    public event EventHandler OperatorsChanged;
236    private void OnOperatorsChanged() {
237      var changed = OperatorsChanged;
238      if (changed != null)
239        changed(this, EventArgs.Empty);
240    }
241
242    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
243      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
244      ParameterizeSolutionCreator();
245      ParameterizeEvaluator();
246      ParameterizeVisualizer();
247      ParameterizeOperators();
248      OnSolutionCreatorChanged();
249    }
250    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
251      ParameterizeEvaluator();
252      ParameterizeVisualizer();
253      ParameterizeOperators();
254    }
255    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
256      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
257      ParameterizeEvaluator();
258      ParameterizeVisualizer();
259      OnEvaluatorChanged();
260    }
261
262    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
263      ParameterizeVisualizer();
264      OnVisualizerChanged();
265    }
266
267    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
268      ParameterizeVisualizer();
269    }
270
271    #endregion
272
273    #region Helpers
274    [StorableHook(HookType.AfterDeserialization)]
275    private void Initialize() {
276      InitializeOperators();
277      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
278      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
279      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
280      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
281      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
282    }
283
284    private void InitializeOperators() {
285      operators = new List<ISymbolicExpressionTreeOperator>();
286      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
287      ParameterizeOperators();
288      UpdateGrammar();
289      UpdatePartitioningParameters();
290    }
291
292    private void ParameterizeSolutionCreator() {
293      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
294      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
295      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
296    }
297    private void ParameterizeEvaluator() {
298      Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
299      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
300      Evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
301      Evaluator.SamplesStartParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value);
302      Evaluator.SamplesEndParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
303    }
304    private void ParameterizeVisualizer() {
305      if (Visualizer != null) {
306        var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
307        if (solutionVisualizer != null) {
308          solutionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
309          solutionVisualizer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
310          solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
311          solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value);
312        }
313      }
314    }
315
316    private void ParameterizeOperators() {
317      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
318        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
319        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
320        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
321      }
322      foreach (ISymbolicRegressionEvaluator op in Operators.OfType<ISymbolicRegressionEvaluator>()) {
323        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
324        op.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
325        op.NumberOfEvaluatedNodesParameter.ActualName = NumberOfEvaluatedNodesParameter.Name;
326      }
327      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
328        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
329        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
330      }
331      foreach (SymbolicExpressionTreeManipulator op in Operators.OfType<SymbolicExpressionTreeManipulator>()) {
332        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
333      }
334      foreach (SymbolicExpressionTreeArchitectureAlteringOperator op in Operators.OfType<SymbolicExpressionTreeArchitectureAlteringOperator>()) {
335      }
336    }
337    #endregion
338  }
339}
Note: See TracBrowser for help on using the repository browser.