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

Last change on this file since 3442 was 3442, checked in by gkronber, 11 years ago

Implemented views for DataAnalysisProblems and DataAnalysisSolutions. #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 ISymbolicExpressionGrammar FunctionTreeGrammar {
121      get { return (ISymbolicExpressionGrammar)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      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
145      var visualizer = new BestValidationSymbolicRegressionSolutionVisualizer();
146      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the error of the regression model should be minimized.", new BoolValue(false)));
147      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
148      Parameters.Add(new ValueParameter<ISymbolicRegressionEvaluator>("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
149      Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The minimal error value that can be reached by symbolic regression models.", new DoubleValue(0)));
150      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", globalGrammar));
151      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
152      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
153      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", new IntValue(3)));
154      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", new IntValue(3)));
155      Parameters.Add(new ValueParameter<DoubleValue>("NumberOfEvaluatedNodes", "The total number of evaluated function tree nodes (for performance measurements.)", new DoubleValue()));
156      Parameters.Add(new ValueParameter<ISingleObjectiveSolutionsVisualizer>("Visualizer", "The operator which should be used to visualize symbolic regression solutions.", visualizer));
157
158      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";
159      creator.MaxFunctionArgumentsParameter.ActualName = "MaxFunctionArguments";
160      creator.MaxFunctionDefinitionsParameter.ActualName = "MaxFunctionDefiningBranches";
161      evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
162      DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged);
163      DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
164      ParameterizeSolutionCreator();
165      ParameterizeEvaluator();
166      ParameterizeVisualizer();
167
168      Initialize();
169    }
170
171    void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) {
172      DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
173    }
174
175    void DataAnalysisProblemData_Changed(object sender, EventArgs e) {
176      foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
177        varSymbol.VariableNames = DataAnalysisProblemData.InputVariables.Select(x => x.Value);
178      }
179    }
180
181    [StorableConstructor]
182    private SymbolicRegressionProblem(bool deserializing) : base() { }
183
184    public override IDeepCloneable Clone(Cloner cloner) {
185      SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
186      clone.Initialize();
187      return clone;
188    }
189
190    #region Events
191    public event EventHandler SolutionCreatorChanged;
192    private void OnSolutionCreatorChanged() {
193      var changed = SolutionCreatorChanged;
194      if (changed != null)
195        changed(this, EventArgs.Empty);
196    }
197    public event EventHandler EvaluatorChanged;
198    private void OnEvaluatorChanged() {
199      var changed = EvaluatorChanged;
200      if (changed != null)
201        changed(this, EventArgs.Empty);
202    }
203    public event EventHandler VisualizerChanged;
204    private void OnVisualizerChanged() {
205      var changed = VisualizerChanged;
206      if (changed != null)
207        changed(this, EventArgs.Empty);
208    }
209
210    public event EventHandler OperatorsChanged;
211    private void OnOperatorsChanged() {
212      var changed = OperatorsChanged;
213      if (changed != null)
214        changed(this, EventArgs.Empty);
215    }
216
217    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
218      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
219      ParameterizeSolutionCreator();
220      ParameterizeEvaluator();
221      ParameterizeVisualizer();
222      ParameterizeOperators();
223      OnSolutionCreatorChanged();
224    }
225    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
226      ParameterizeEvaluator();
227      ParameterizeVisualizer();
228      ParameterizeOperators();
229    }
230    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
231      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
232      ParameterizeEvaluator();
233      ParameterizeVisualizer();
234      OnEvaluatorChanged();
235    }
236
237    private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
238      ParameterizeVisualizer();
239      OnVisualizerChanged();
240    }
241
242    private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
243      ParameterizeVisualizer();
244    }
245
246    #endregion
247
248    #region Helpers
249    [StorableHook(HookType.AfterDeserialization)]
250    private void Initialize() {
251      InitializeOperators();
252      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
253      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
254      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
255      Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
256      VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
257    }
258
259    private void InitializeOperators() {
260      operators = new List<ISymbolicExpressionTreeOperator>();
261      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>());
262      ParameterizeOperators();
263    }
264
265    private void ParameterizeSolutionCreator() {
266      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
267      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
268      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
269    }
270    private void ParameterizeEvaluator() {
271      Evaluator.FunctionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
272      Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
273    }
274    private void ParameterizeVisualizer() {
275      if (Visualizer != null) {
276        var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
277        if (solutionVisualizer != null) {
278          solutionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
279          solutionVisualizer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
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.