Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3257 was 3257, checked in by gkronber, 13 years ago

Moved general set of symbols and evaluator into Encodings.SymbolicExpressionTreeEncoding. #937 (Data types and operators for symbolic expression tree encoding)

File size: 13.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;
35
36namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
37  [Item("SymbolicRegressionProblem", "Represents a symbolic regression problem.")]
38  [Creatable("Problems")]
39  [StorableClass]
40  public sealed class SymbolicRegressionProblem : RegressionProblem, ISingleObjectiveProblem {
41
42    #region Parameter Properties
43    public ValueParameter<BoolValue> MaximizationParameter {
44      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
45    }
46    IParameter ISingleObjectiveProblem.MaximizationParameter {
47      get { return MaximizationParameter; }
48    }
49    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
50      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
51    }
52    IParameter IProblem.SolutionCreatorParameter {
53      get { return SolutionCreatorParameter; }
54    }
55    public ValueParameter<ISymbolicRegressionEvaluator> EvaluatorParameter {
56      get { return (ValueParameter<ISymbolicRegressionEvaluator>)Parameters["Evaluator"]; }
57    }
58    IParameter IProblem.EvaluatorParameter {
59      get { return EvaluatorParameter; }
60    }
61    public ValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
62      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["FunctionTreeGrammar"]; }
63    }
64    public ValueParameter<IntValue> MaxExpressionLengthParameter {
65      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
66    }
67    public ValueParameter<IntValue> MaxExpressionDepthParameter {
68      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
69    }
70    public ValueParameter<DoubleValue> NumberOfEvaluatedNodesParameter {
71      get { return (ValueParameter<DoubleValue>)Parameters["NumberOfEvaluatedNodes"]; }
72    }
73    public OptionalValueParameter<ISingleObjectiveSolutionsVisualizer> VisualizerParameter {
74      get { return (OptionalValueParameter<ISingleObjectiveSolutionsVisualizer>)Parameters["Visualizer"]; }
75    }
76    IParameter IProblem.VisualizerParameter {
77      get { return VisualizerParameter; }
78    }
79    public ValueParameter<DoubleValue> BestKnownQualityParameter {
80      get { return (ValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
81    }
82    IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
83      get { return BestKnownQualityParameter; }
84    }
85    #endregion
86
87    #region Properties
88    public IntValue MaxExpressionLength {
89      get { return MaxExpressionLengthParameter.Value; }
90      set { MaxExpressionLengthParameter.Value = value; }
91    }
92    public IntValue MaxExpressionDepth {
93      get { return MaxExpressionDepthParameter.Value; }
94      set { MaxExpressionDepthParameter.Value = value; }
95    }
96    public SymbolicExpressionTreeCreator SolutionCreator {
97      get { return SolutionCreatorParameter.Value; }
98      set { SolutionCreatorParameter.Value = value; }
99    }
100    ISolutionCreator IProblem.SolutionCreator {
101      get { return SolutionCreatorParameter.Value; }
102    }
103    public ISymbolicRegressionEvaluator Evaluator {
104      get { return EvaluatorParameter.Value; }
105      set { EvaluatorParameter.Value = value; }
106    }
107    ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
108      get { return EvaluatorParameter.Value; }
109    }
110    IEvaluator IProblem.Evaluator {
111      get { return EvaluatorParameter.Value; }
112    }
113    public ISymbolicExpressionGrammar FunctionTreeGrammar {
114      get { return FunctionTreeGrammarParameter.Value; }
115    }
116    public ISingleObjectiveSolutionsVisualizer Visualizer {
117      get { return VisualizerParameter.Value; }
118      set { VisualizerParameter.Value = value; }
119    }
120    ISolutionsVisualizer IProblem.Visualizer {
121      get { return VisualizerParameter.Value; }
122    }
123    public DoubleValue BestKnownQuality {
124      get { return BestKnownQualityParameter.Value; }
125    }
126    private List<ISymbolicExpressionTreeOperator> operators;
127    public IEnumerable<IOperator> Operators {
128      get { return operators.Cast<IOperator>(); }
129    }
130    #endregion
131
132    public SymbolicRegressionProblem()
133      : base() {
134      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
135      var evaluator = new SymbolicRegressionMeanSquaredErrorEvaluator();
136      var grammar = new ArithmeticExpressionGrammar();
137      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", new BoolValue(false)));
138      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
139      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
140      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that can be reached by symbolic regression models.", new DoubleValue(0)));
141      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", grammar));
142      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
143      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
144      Parameters.Add(new ValueParameter<DoubleValue>("NumberOfEvaluatedNodes", "The total number of evaluated function tree nodes (for performance measurements.)", new DoubleValue()));
145      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize artificial ant solutions.", null));
146
147      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";
148      evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
149      ParameterizeSolutionCreator();
150      ParameterizeEvaluator();
151      ParameterizeVisualizer();
152
153      Initialize();
154    }
155
156    [StorableConstructor]
157    private SymbolicRegressionProblem(bool deserializing) : base() { }
158
159    public override IDeepCloneable Clone(Cloner cloner) {
160      SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
161      clone.Initialize();
162      return clone;
163    }
164
165    #region Events
166    public event EventHandler SolutionCreatorChanged;
167    private void OnSolutionCreatorChanged() {
168      var changed = SolutionCreatorChanged;
169      if (changed != null)
170        changed(this, EventArgs.Empty);
171    }
172    public event EventHandler EvaluatorChanged;
173    private void OnEvaluatorChanged() {
174      var changed = EvaluatorChanged;
175      if (changed != null)
176        changed(this, EventArgs.Empty);
177    }
178    public event EventHandler VisualizerChanged;
179    private void OnVisualizerChanged() {
180      var changed = VisualizerChanged;
181      if (changed != null)
182        changed(this, EventArgs.Empty);
183    }
184
185    public event EventHandler OperatorsChanged;
186    private void OnOperatorsChanged() {
187      var changed = OperatorsChanged;
188      if (changed != null)
189        changed(this, EventArgs.Empty);
190    }
191
192    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
193      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
194      ParameterizeSolutionCreator();
195      ParameterizeEvaluator();
196      ParameterizeVisualizer();
197      ParameterizeOperators();
198      OnSolutionCreatorChanged();
199    }
200    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
201      ParameterizeEvaluator();
202      ParameterizeVisualizer();
203      ParameterizeOperators();
204    }
205    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
206      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
207      ParameterizeEvaluator();
208      ParameterizeVisualizer();
209      OnEvaluatorChanged();
210    }
211
212    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
213      ParameterizeVisualizer();
214      OnVisualizerChanged();
215    }
216
217    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
218      ParameterizeVisualizer();
219    }
220
221    #endregion
222
223    #region Helpers
224    [StorableHook(HookType.AfterDeserialization)]
225    private void Initialize() {
226      InitializeOperators();
227      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
228      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
229      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
230      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
231      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
232    }
233
234    private void InitializeOperators() {
235      operators = new List<ISymbolicExpressionTreeOperator>();
236      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
237      ParameterizeOperators();
238    }
239
240    private void ParameterizeSolutionCreator() {
241      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
242      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
243      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
244    }
245    private void ParameterizeEvaluator() {
246      Evaluator.FunctionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
247      Evaluator.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
248      Evaluator.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
249    }
250    private void ParameterizeVisualizer() {
251      if (Visualizer != null) {
252        //Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
253        //var antTrailVisualizer = Visualizer as IAntTrailVisualizer;
254        //if (antTrailVisualizer != null) {
255        //  antTrailVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
256        //  antTrailVisualizer.WorldParameter.ActualName = WorldParameter.Name;
257        //  antTrailVisualizer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
258        //}
259        //var bestSymExpressionVisualizer = Visualizer as BestSymbolicExpressionTreeVisualizer;
260        //if (bestSymExpressionVisualizer != null) {
261        //  bestSymExpressionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
262        //}
263      }
264    }
265
266    private void ParameterizeOperators() {
267      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
268        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
269        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
270        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
271      }
272      foreach (ISymbolicRegressionEvaluator op in Operators.OfType<ISymbolicRegressionEvaluator>()) {
273        op.FunctionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
274        op.DatasetParameter.ActualName = DatasetParameter.Name;
275        op.NumberOfEvaluatedNodesParameter.ActualName = NumberOfEvaluatedNodesParameter.Name;
276        op.TargetVariableParameter.ActualName = TargetVariableParameter.Name;
277        op.SamplesStartParameter.ActualName = TrainingSamplesStartParameter.Name;
278        op.SamplesEndParameter.ActualName = TrainingSamplesEndParameter.Name;
279      }
280      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
281        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
282        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
283      }
284    }
285    #endregion
286  }
287}
Note: See TracBrowser for help on using the repository browser.