Free cookie consent management tool by TermsFeed Policy Generator

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

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

Refactored HeuristicLab.Problems.DataAnalysis namespace. #938 (Data types and operators for regression problems)

File size: 15.1 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 ValueParameter<DoubleValue> BestKnownQualityParameter {
87      get { return (ValueParameter<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 ArithmeticExpressionGrammar FunctionTreeGrammar {
121      get { return (ArithmeticExpressionGrammar)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      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", new BoolValue(false)));
145      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
146      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
147      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that can be reached by symbolic regression models.", new DoubleValue(0)));
148      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", grammar));
149      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
150      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
151      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", new IntValue(3)));
152      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", new IntValue(3)));
153      Parameters.Add(new ValueParameter<DoubleValue>("NumberOfEvaluatedNodes", "The total number of evaluated function tree nodes (for performance measurements.)", new DoubleValue()));
154      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize artificial ant solutions.", null));
155
156      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";
157      evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
158      DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged);
159      DataAnalysisProblemData.InputVariablesChanged += new EventHandler(DataAnalysisProblemData_InputVariablesChanged);
160      ParameterizeSolutionCreator();
161      ParameterizeEvaluator();
162      ParameterizeVisualizer();
163
164      Initialize();
165    }
166
167    void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) {
168      DataAnalysisProblemData.InputVariablesChanged += new EventHandler(DataAnalysisProblemData_InputVariablesChanged);
169    }
170
171    void DataAnalysisProblemData_InputVariablesChanged(object sender, EventArgs e) {
172      FunctionTreeGrammar.VariableNames = DataAnalysisProblemData.InputVariables.Select(x => x.Value);
173    }
174
175    [StorableConstructor]
176    private SymbolicRegressionProblem(bool deserializing) : base() { }
177
178    public override IDeepCloneable Clone(Cloner cloner) {
179      SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
180      clone.Initialize();
181      return clone;
182    }
183
184    #region Events
185    public event EventHandler SolutionCreatorChanged;
186    private void OnSolutionCreatorChanged() {
187      var changed = SolutionCreatorChanged;
188      if (changed != null)
189        changed(this, EventArgs.Empty);
190    }
191    public event EventHandler EvaluatorChanged;
192    private void OnEvaluatorChanged() {
193      var changed = EvaluatorChanged;
194      if (changed != null)
195        changed(this, EventArgs.Empty);
196    }
197    public event EventHandler VisualizerChanged;
198    private void OnVisualizerChanged() {
199      var changed = VisualizerChanged;
200      if (changed != null)
201        changed(this, EventArgs.Empty);
202    }
203
204    public event EventHandler OperatorsChanged;
205    private void OnOperatorsChanged() {
206      var changed = OperatorsChanged;
207      if (changed != null)
208        changed(this, EventArgs.Empty);
209    }
210
211    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
212      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
213      ParameterizeSolutionCreator();
214      ParameterizeEvaluator();
215      ParameterizeVisualizer();
216      ParameterizeOperators();
217      OnSolutionCreatorChanged();
218    }
219    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
220      ParameterizeEvaluator();
221      ParameterizeVisualizer();
222      ParameterizeOperators();
223    }
224    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
225      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
226      ParameterizeEvaluator();
227      ParameterizeVisualizer();
228      OnEvaluatorChanged();
229    }
230
231    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
232      ParameterizeVisualizer();
233      OnVisualizerChanged();
234    }
235
236    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
237      ParameterizeVisualizer();
238    }
239
240    #endregion
241
242    #region Helpers
243    [StorableHook(HookType.AfterDeserialization)]
244    private void Initialize() {
245      InitializeOperators();
246      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
247      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
248      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
249      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
250      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
251    }
252
253    private void InitializeOperators() {
254      operators = new List<ISymbolicExpressionTreeOperator>();
255      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
256      ParameterizeOperators();
257    }
258
259    private void ParameterizeSolutionCreator() {
260      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
261      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
262      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
263    }
264    private void ParameterizeEvaluator() {
265      Evaluator.FunctionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
266      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
267    }
268    private void ParameterizeVisualizer() {
269      if (Visualizer != null) {
270        //Visualizer.QualityParameter.ActualName = Evaluator.QualityParameter.ActualName;
271        //var antTrailVisualizer = Visualizer as IAntTrailVisualizer;
272        //if (antTrailVisualizer != null) {
273        //  antTrailVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
274        //  antTrailVisualizer.WorldParameter.ActualName = WorldParameter.Name;
275        //  antTrailVisualizer.MaxTimeStepsParameter.ActualName = MaxTimeStepsParameter.Name;
276        //}
277        //var bestSymExpressionVisualizer = Visualizer as BestSymbolicExpressionTreeVisualizer;
278        //if (bestSymExpressionVisualizer != null) {
279        //  bestSymExpressionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
280        //}
281      }
282    }
283
284    private void ParameterizeOperators() {
285      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
286        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
287        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
288        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
289      }
290      foreach (ISymbolicRegressionEvaluator op in Operators.OfType<ISymbolicRegressionEvaluator>()) {
291        op.FunctionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
292        op.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
293        op.NumberOfEvaluatedNodesParameter.ActualName = NumberOfEvaluatedNodesParameter.Name;
294      }
295      foreach (SymbolicExpressionTreeCrossover op in Operators.OfType<SymbolicExpressionTreeCrossover>()) {
296        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
297        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
298      }
299      foreach (SymbolicExpressionTreeManipulator op in Operators.OfType<SymbolicExpressionTreeManipulator>()) {
300        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
301      }
302      foreach (SymbolicExpressionTreeArchitectureAlteringOperator op in Operators.OfType<SymbolicExpressionTreeArchitectureAlteringOperator>()) {
303      }
304    }
305    #endregion
306  }
307}
Note: See TracBrowser for help on using the repository browser.