Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 3294 was 3294, checked in by gkronber, 12 years ago

Added first version of architecture altering operators for ADFs. #290 (Implement ADFs)

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