Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression/3.3/Symbolic/SymbolicVectorRegressionProblem.cs @ 4118

Last change on this file since 4118 was 4118, checked in by abeham, 14 years ago

#1090

  • Fixed problem plugins reloading their operators on deserialization in following problems (forgot on them in the first commit)
    • SupportVectorRegressionProblem
    • SymbolicTimeSeriesPrognosisProblem
  • Fixed a bug in the FeatureSelectionProblem introduced in r4098
  • Fixed the issues mentioned in the code review of mkommend
File size: 17.2 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 HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;
30using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
31using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
35using HeuristicLab.PluginInfrastructure;
36using HeuristicLab.Problems.DataAnalysis.Symbolic;
37
38namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.Regression.Symbolic {
39  [StorableClass]
40  public class SymbolicVectorRegressionProblem : MultiVariateDataAnalysisProblem, IProblem {
41
42    #region Parameter Properties
43    public new ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
44      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
45    }
46
47    IParameter IProblem.SolutionCreatorParameter {
48      get {
49        return SolutionCreatorParameter;
50      }
51    }
52    public ValueParameter<ISymbolicExpressionTreeInterpreter> SymbolicExpressionTreeInterpreterParameter {
53      get { return (ValueParameter<ISymbolicExpressionTreeInterpreter>)Parameters["SymbolicExpressionTreeInterpreter"]; }
54    }
55
56    public ValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
57      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["FunctionTreeGrammar"]; }
58    }
59    public ValueParameter<IntValue> MaxExpressionLengthParameter {
60      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
61    }
62    public ValueParameter<IntValue> MaxExpressionDepthParameter {
63      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
64    }
65    public ValueParameter<IntValue> MaxFunctionDefiningBranchesParameter {
66      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefiningBranches"]; }
67    }
68    public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
69      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
70    }
71    public ValueParameter<DoubleArray> UpperEstimationLimitParameter {
72      get { return (ValueParameter<DoubleArray>)Parameters["UpperEstimationLimit"]; }
73    }
74    public ValueParameter<DoubleArray> LowerEstimationLimitParameter {
75      get { return (ValueParameter<DoubleArray>)Parameters["LowerEstimationLimit"]; }
76    }
77    #endregion
78
79    #region Properties
80    public IntValue MaxExpressionLength {
81      get { return MaxExpressionLengthParameter.Value; }
82      set { MaxExpressionLengthParameter.Value = value; }
83    }
84    public IntValue MaxExpressionDepth {
85      get { return MaxExpressionDepthParameter.Value; }
86      set { MaxExpressionDepthParameter.Value = value; }
87    }
88    public IntValue MaxFunctionDefiningBranches {
89      get { return MaxFunctionDefiningBranchesParameter.Value; }
90      set { MaxFunctionDefiningBranchesParameter.Value = value; }
91    }
92    public IntValue MaxFunctionArguments {
93      get { return MaxFunctionArgumentsParameter.Value; }
94      set { MaxFunctionArgumentsParameter.Value = value; }
95    }
96    public new SymbolicExpressionTreeCreator SolutionCreator {
97      get { return SolutionCreatorParameter.Value; }
98      set { SolutionCreatorParameter.Value = value; }
99    }
100    public DoubleArray UpperEstimationLimit {
101      get { return UpperEstimationLimitParameter.Value; }
102      set { UpperEstimationLimitParameter.Value = value; }
103    }
104    public DoubleArray LowerEstimationLimit {
105      get { return LowerEstimationLimitParameter.Value; }
106      set { LowerEstimationLimitParameter.Value = value; }
107    }
108    ISolutionCreator IProblem.SolutionCreator {
109      get { return SolutionCreatorParameter.Value; }
110    }
111    public ISymbolicExpressionTreeInterpreter SymbolicExpressionTreeInterpreter {
112      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
113      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
114    }
115
116    public ISymbolicExpressionGrammar FunctionTreeGrammar {
117      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
118      set { FunctionTreeGrammarParameter.Value = value; }
119    }
120
121    public override IEnumerable<IOperator> Operators {
122      get { return operators; }
123    }
124    public IEnumerable<IAnalyzer> Analyzers {
125      get { return operators.OfType<IAnalyzer>(); }
126    }
127    public IntValue TrainingSamplesStart {
128      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value); }
129    }
130    public IntValue TrainingSamplesEnd {
131      get {
132        return new IntValue((MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value +
133          MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
134      }
135    }
136    public IntValue ValidationSamplesStart {
137      get { return TrainingSamplesEnd; }
138    }
139    public IntValue ValidationSamplesEnd {
140      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value); }
141    }
142    public IntValue TestSamplesStart {
143      get { return MultiVariateDataAnalysisProblemData.TestSamplesStart; }
144    }
145    public IntValue TestSamplesEnd {
146      get { return MultiVariateDataAnalysisProblemData.TestSamplesEnd; }
147    }
148    public DoubleValue PunishmentFactor {
149      get { return new DoubleValue(10.0); }
150    }
151    #endregion
152
153    [Storable]
154    private SymbolicVectorRegressionGrammar grammar;
155    [Storable]
156    private List<IOperator> operators;
157
158    [StorableConstructor]
159    protected SymbolicVectorRegressionProblem(bool deserializing) : base(deserializing) { }
160    public SymbolicVectorRegressionProblem()
161      : base() {
162      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
163      grammar = new SymbolicVectorRegressionGrammar(MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems.Count());
164      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
165      var interpreter = new SimpleArithmeticExpressionInterpreter();
166      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
167      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeInterpreter>("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter));
168      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", globalGrammar));
169      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
170      Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
171      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly()));
172      Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", (IntValue)new IntValue(0).AsReadOnly()));
173      Parameters.Add(new ValueParameter<DoubleArray>("UpperEstimationLimit", "The upper limit for the estimated values for each component."));
174      Parameters.Add(new ValueParameter<DoubleArray>("LowerEstimationLimit", "The lower limit for the estimated values for each component."));
175      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicVectorRegressionModel";
176
177      ParameterizeSolutionCreator();
178      UpdateGrammar();
179      UpdateEstimationLimits();
180      InitializeOperators();
181      RegisterParameterEvents();
182      RegisterParameterValueEvents();
183    }
184
185    [StorableHook(HookType.AfterDeserialization)]
186    private void AfterDeserializationHook() {
187      // BackwardsCompatibility3.3
188      #region Backwards compatible code (remove with 3.4)
189      if (operators == null) InitializeOperators();
190      #endregion
191      RegisterParameterEvents();
192      RegisterParameterValueEvents();
193    }
194
195    public override IDeepCloneable Clone(Cloner cloner) {
196      SymbolicVectorRegressionProblem clone = (SymbolicVectorRegressionProblem)base.Clone(cloner);
197      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
198      clone.RegisterParameterEvents();
199      clone.RegisterParameterValueEvents();
200      return clone;
201    }
202
203    private void RegisterParameterValueEvents() {
204      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
205      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
206      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
207    }
208
209    private void RegisterParameterEvents() {
210      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
211      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
212      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
213    }
214
215    #region event handling
216    protected override void OnMultiVariateDataAnalysisProblemChanged(EventArgs e) {
217      base.OnMultiVariateDataAnalysisProblemChanged(e);
218      int dimension = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems.Count();
219      // paritions should be updated
220      ParameterizeAnalyzers();
221      // input variables should be updated
222      UpdateGrammar();
223      UpdateEstimationLimits();
224    }
225
226    protected virtual void OnArchitectureParameterChanged(EventArgs e) {
227      UpdateGrammar();
228    }
229
230    protected virtual void OnGrammarChanged(EventArgs e) { }
231    protected virtual void OnOperatorsChanged(EventArgs e) { RaiseOperatorsChanged(e); }
232    protected virtual void OnSolutionCreatorChanged(EventArgs e) {
233      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
234      ParameterizeSolutionCreator();
235      OnSolutionParameterNameChanged(e);
236      RaiseSolutionCreatorChanged(e);
237    }
238
239    protected virtual void OnSolutionParameterNameChanged(EventArgs e) {
240      ParameterizeAnalyzers();
241      ParameterizeOperators();
242    }
243    #endregion
244
245    #region event handlers
246    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
247      OnSolutionCreatorChanged(e);
248    }
249    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
250      OnSolutionParameterNameChanged(e);
251    }
252    private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) {
253      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
254      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
255      OnArchitectureParameterChanged(e);
256    }
257    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
258      OnArchitectureParameterChanged(e);
259    }
260    #endregion
261
262    #region Helpers
263    protected void AddOperator(IOperator op) {
264      operators.Add(op);
265    }
266
267    private void UpdateGrammar() {
268      var selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems;
269      grammar.SetDimension(selectedTargetVariables.Count());
270      foreach (var varSymbol in grammar.Symbols.OfType<HeuristicLab.Problems.DataAnalysis.Symbolic.Symbols.Variable>()) {
271        varSymbol.VariableNames = MultiVariateDataAnalysisProblemData.InputVariables.CheckedItems.Select(x => x.Value.Value);
272      }
273
274      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
275      globalGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
276      globalGrammar.MaxFunctionDefinitions = MaxFunctionDefiningBranches.Value;
277      FunctionTreeGrammar = globalGrammar;
278    }
279
280    private void UpdateEstimationLimits() {
281      IEnumerable<string> selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value);
282      UpperEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
283      LowerEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
284      int i = 0;
285      foreach (string targetVariable in selectedTargetVariables) {
286        if (TrainingSamplesStart.Value < TrainingSamplesEnd.Value) {
287          var targetValues = MultiVariateDataAnalysisProblemData.Dataset.GetVariableValues(targetVariable, TrainingSamplesStart.Value, TrainingSamplesEnd.Value);
288          var mean = targetValues.Average();
289          var range = targetValues.Max() - targetValues.Min();
290          UpperEstimationLimit[i] = mean + PunishmentFactor.Value * range;
291          LowerEstimationLimit[i] = mean - PunishmentFactor.Value * range;
292        } else {
293          UpperEstimationLimit[i] = 0;
294          LowerEstimationLimit[i] = 0;
295        }
296        i++;
297      }
298    }
299
300
301    private void InitializeOperators() {
302      operators = new List<IOperator>();
303      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
304      operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
305      // operators.Add(new SymbolicVectorRegressionVariableFrequencyAnalyzer());
306      ParameterizeOperators();
307      ParameterizeAnalyzers();
308    }
309
310    private void ParameterizeSolutionCreator() {
311      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
312      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
313      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
314      SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
315      SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
316    }
317
318    private void ParameterizeAnalyzers() {
319      foreach (ISymbolicExpressionTreeAnalyzer analyzer in Analyzers.OfType<ISymbolicExpressionTreeAnalyzer>()) {
320        analyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
321      }
322      //foreach (var analyzer in Analyzers) {
323      //  var varFreqAnalyzer = analyzer as SymbolicVectorRegressionVariableFrequencyAnalyzer;
324      //  if (varFreqAnalyzer != null) {
325      //    varFreqAnalyzer.ProblemDataParameter.ActualName = MultiVariateDataAnalysisProblemDataParameter.Name;
326      //    varFreqAnalyzer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
327      //  }
328      //}
329    }
330
331    private void ParameterizeOperators() {
332      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
333        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
334        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
335        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
336      }
337      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
338        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
339        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
340      }
341      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
342        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
343      }
344      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
345        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
346        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
347      }
348    }
349    #endregion
350
351  }
352}
Note: See TracBrowser for help on using the repository browser.