Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added new plugins for multi-variate regression. #1089

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