Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis/3.3/Symbolic/SymbolicTimeSeriesPrognosisProblem.cs @ 4949

Last change on this file since 4949 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: 16.9 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.Symbolic;
34using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureManipulators;
35using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
36using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;
37using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
38using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Interfaces;
39using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Analyzers;
40using HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic.Interfaces;
41
42namespace HeuristicLab.Problems.DataAnalysis.MultiVariate.TimeSeriesPrognosis.Symbolic {
43  [Item("Symbolic Time Series Prognosis Problem", "Represents a symbolic time series prognosis problem.")]
44  [StorableClass]
45  public class SymbolicTimeSeriesPrognosisProblem : MultiVariateDataAnalysisProblem, IProblem {
46
47    #region Parameter Properties
48    public ValueParameter<SymbolicExpressionTreeCreator> SolutionCreatorParameter {
49      get { return (ValueParameter<SymbolicExpressionTreeCreator>)Parameters["SolutionCreator"]; }
50    }
51    IParameter IProblem.SolutionCreatorParameter {
52      get { return SolutionCreatorParameter; }
53    }
54    public ValueParameter<ISymbolicTimeSeriesExpressionInterpreter> SymbolicExpressionTreeInterpreterParameter {
55      get { return (ValueParameter<ISymbolicTimeSeriesExpressionInterpreter>)Parameters["SymbolicExpressionTreeInterpreter"]; }
56    }
57    public ValueParameter<ISymbolicExpressionGrammar> FunctionTreeGrammarParameter {
58      get { return (ValueParameter<ISymbolicExpressionGrammar>)Parameters["FunctionTreeGrammar"]; }
59    }
60    public ValueParameter<IntValue> MaxExpressionLengthParameter {
61      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionLength"]; }
62    }
63    public ValueParameter<IntValue> MaxExpressionDepthParameter {
64      get { return (ValueParameter<IntValue>)Parameters["MaxExpressionDepth"]; }
65    }
66    public ValueParameter<IntValue> MaxFunctionDefiningBranchesParameter {
67      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionDefiningBranches"]; }
68    }
69    public ValueParameter<IntValue> MaxFunctionArgumentsParameter {
70      get { return (ValueParameter<IntValue>)Parameters["MaxFunctionArguments"]; }
71    }
72    public ValueParameter<IntValue> PredictionHorizonParameter {
73      get { return (ValueParameter<IntValue>)Parameters["PredictionHorizon"]; }
74    }
75    public ValueParameter<DoubleArray> UpperEstimationLimitParameter {
76      get { return (ValueParameter<DoubleArray>)Parameters["UpperEstimationLimit"]; }
77    }
78    public ValueParameter<DoubleArray> LowerEstimationLimitParameter {
79      get { return (ValueParameter<DoubleArray>)Parameters["LowerEstimationLimit"]; }
80    }
81    #endregion
82
83    #region Properties
84    public IntValue MaxExpressionLength {
85      get { return MaxExpressionLengthParameter.Value; }
86      set { MaxExpressionLengthParameter.Value = value; }
87    }
88    public IntValue MaxExpressionDepth {
89      get { return MaxExpressionDepthParameter.Value; }
90      set { MaxExpressionDepthParameter.Value = value; }
91    }
92    public IntValue MaxFunctionDefiningBranches {
93      get { return MaxFunctionDefiningBranchesParameter.Value; }
94      set { MaxFunctionDefiningBranchesParameter.Value = value; }
95    }
96    public IntValue MaxFunctionArguments {
97      get { return MaxFunctionArgumentsParameter.Value; }
98      set { MaxFunctionArgumentsParameter.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    public new SymbolicExpressionTreeCreator SolutionCreator {
109      get { return SolutionCreatorParameter.Value; }
110      set { SolutionCreatorParameter.Value = value; }
111    }
112    ISolutionCreator IProblem.SolutionCreator {
113      get { return SolutionCreatorParameter.Value; }
114    }
115    public ISymbolicTimeSeriesExpressionInterpreter SymbolicExpressionTreeInterpreter {
116      get { return SymbolicExpressionTreeInterpreterParameter.Value; }
117      set { SymbolicExpressionTreeInterpreterParameter.Value = value; }
118    }
119
120    public ISymbolicExpressionGrammar FunctionTreeGrammar {
121      get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
122      set { FunctionTreeGrammarParameter.Value = value; }
123    }
124    public override IEnumerable<IOperator> Operators {
125      get { return operators; }
126    }
127    public IEnumerable<IAnalyzer> Analyzers {
128      get { return operators.OfType<IAnalyzer>(); }
129    }
130    public IntValue TrainingSamplesStart {
131      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value); }
132    }
133    public IntValue TrainingSamplesEnd {
134      get {
135        return new IntValue((MultiVariateDataAnalysisProblemData.TrainingSamplesStart.Value +
136          MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
137      }
138    }
139    public IntValue ValidationSamplesStart {
140      get { return TrainingSamplesEnd; }
141    }
142    public IntValue ValidationSamplesEnd {
143      get { return new IntValue(MultiVariateDataAnalysisProblemData.TrainingSamplesEnd.Value); }
144    }
145    public IntValue TestSamplesStart {
146      get { return MultiVariateDataAnalysisProblemData.TestSamplesStart; }
147    }
148    public IntValue TestSamplesEnd {
149      get { return MultiVariateDataAnalysisProblemData.TestSamplesEnd; }
150    }
151    public DoubleValue PunishmentFactor {
152      get { return new DoubleValue(10.0); }
153    }
154    #endregion
155
156    [Storable]
157    private SymbolicTimeSeriesPrognosisGrammar grammar;
158    [Storable]
159    private List<IOperator> operators;
160
161    [StorableConstructor]
162    protected SymbolicTimeSeriesPrognosisProblem(bool deserializing) : base(deserializing) { }
163    public SymbolicTimeSeriesPrognosisProblem()
164      : base() {
165      SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
166      grammar = new SymbolicTimeSeriesPrognosisGrammar(1);
167      var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
168      var interpreter = new SymbolicTimeSeriesExpressionInterpreter();
169      Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new symbolic time series prognosis solutions.", creator));
170      Parameters.Add(new ValueParameter<ISymbolicTimeSeriesExpressionInterpreter>("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 time series prognosis 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<IntValue>("PredictionHorizon", "The number of time steps for which to create a forecast.", new IntValue(1)));
177      Parameters.Add(new ValueParameter<DoubleArray>("UpperEstimationLimit", "The upper limit for the estimated values for each component."));
178      Parameters.Add(new ValueParameter<DoubleArray>("LowerEstimationLimit", "The lower limit for the estimated values for each component."));
179      creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicTimeSeriesPrognosisModel";
180
181      ParameterizeSolutionCreator();
182
183      UpdateGrammar();
184      InitializeOperators();
185      RegisterParameterEvents();
186      RegisterParameterValueEvents();
187    }
188
189    [StorableHook(HookType.AfterDeserialization)]
190    private void AfterDeserializationHook() {
191      // BackwardsCompatibility3.3
192      #region Backwards compatible code (remove with 3.4)
193      if (operators == null) InitializeOperators();
194      #endregion
195      RegisterParameterEvents();
196      RegisterParameterValueEvents();
197    }
198
199    public override IDeepCloneable Clone(Cloner cloner) {
200      SymbolicTimeSeriesPrognosisProblem clone = (SymbolicTimeSeriesPrognosisProblem)base.Clone(cloner);
201      clone.operators = operators.Select(x => (IOperator)cloner.Clone(x)).ToList();
202      clone.grammar = (SymbolicTimeSeriesPrognosisGrammar)cloner.Clone(grammar);
203      clone.RegisterParameterEvents();
204      clone.RegisterParameterValueEvents();
205      return clone;
206    }
207
208    private void RegisterParameterValueEvents() {
209      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
210      MaxFunctionDefiningBranchesParameter.ValueChanged += new EventHandler(ArchitectureParameter_ValueChanged);
211      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
212    }
213
214    private void RegisterParameterEvents() {
215      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
216      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
217      SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
218    }
219
220    #region event handling
221    protected override void OnMultiVariateDataAnalysisProblemChanged(EventArgs e) {
222      base.OnMultiVariateDataAnalysisProblemChanged(e);
223      // input variables could have been changed
224      UpdateGrammar();
225      UpdateEstimationLimits();
226    }
227    protected virtual void OnArchitectureParameterChanged(EventArgs e) {
228      UpdateGrammar();
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      ParameterizeOperators();
241    }
242    #endregion
243
244    #region event handlers
245    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
246      OnSolutionCreatorChanged(e);
247    }
248    private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
249      OnSolutionParameterNameChanged(e);
250    }
251    private void ArchitectureParameter_ValueChanged(object sender, EventArgs e) {
252      MaxFunctionArgumentsParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
253      MaxFunctionDefiningBranchesParameter.Value.ValueChanged += new EventHandler(ArchitectureParameterValue_ValueChanged);
254      OnArchitectureParameterChanged(e);
255    }
256    private void ArchitectureParameterValue_ValueChanged(object sender, EventArgs e) {
257      OnArchitectureParameterChanged(e);
258    }
259    #endregion
260
261    #region Helpers
262    protected void AddOperator(IOperator op) {
263      operators.Add(op);
264    }
265
266    private void UpdateGrammar() {
267      var selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems;
268      grammar.SetResultProducingBranches(selectedTargetVariables.Count());
269
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    private void UpdateEstimationLimits() {
280      IEnumerable<string> selectedTargetVariables = MultiVariateDataAnalysisProblemData.TargetVariables.CheckedItems.Select(x => x.Value.Value);
281      UpperEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
282      LowerEstimationLimit = new DoubleArray(selectedTargetVariables.Count());
283      int i = 0;
284      foreach (string targetVariable in selectedTargetVariables) {
285        if (TrainingSamplesStart.Value < TrainingSamplesEnd.Value) {
286          var targetValues = MultiVariateDataAnalysisProblemData.Dataset.GetVariableValues(targetVariable, TrainingSamplesStart.Value, TrainingSamplesEnd.Value);
287          var mean = targetValues.Average();
288          var range = targetValues.Max() - targetValues.Min();
289          UpperEstimationLimit[i] = mean + PunishmentFactor.Value * range;
290          LowerEstimationLimit[i] = mean - PunishmentFactor.Value * range;
291        } else {
292          UpperEstimationLimit[i] = 0;
293          LowerEstimationLimit[i] = 0;
294        }
295        i++;
296      }
297    }
298
299    private void InitializeOperators() {
300      operators = new List<IOperator>();
301      operators.AddRange(ApplicationManager.Manager.GetInstances<ISymbolicExpressionTreeOperator>().OfType<IOperator>());
302      operators.Add(new MinAverageMaxSymbolicExpressionTreeSizeAnalyzer());
303      ParameterizeOperators();
304    }
305
306    private void ParameterizeSolutionCreator() {
307      SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
308      SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
309      SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
310      SolutionCreator.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
311      SolutionCreator.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
312    }
313
314    private void ParameterizeOperators() {
315      foreach (ISymbolicExpressionTreeOperator op in Operators.OfType<ISymbolicExpressionTreeOperator>()) {
316        op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
317        op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
318        op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
319      }
320      foreach (ISymbolicExpressionTreeCrossover op in Operators.OfType<ISymbolicExpressionTreeCrossover>()) {
321        op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
322        op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
323      }
324      foreach (ISymbolicExpressionTreeManipulator op in Operators.OfType<ISymbolicExpressionTreeManipulator>()) {
325        op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
326      }
327      foreach (ISymbolicExpressionTreeArchitectureManipulator op in Operators.OfType<ISymbolicExpressionTreeArchitectureManipulator>()) {
328        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
329        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefiningBranchesParameter.Name;
330      }
331    }
332    #endregion
333  }
334}
Note: See TracBrowser for help on using the repository browser.