Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs @ 7120

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

#1081 implemented multi-variate symbolic expression tree interpreter for time series prognosis.

File size: 7.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Collections.Generic;
23using System.Drawing;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
31  /// <summary>
32  /// Represents a symbolic time-series prognosis model
33  /// </summary>
34  [StorableClass]
35  [Item(Name = "Symbolic Time-Series Prognosis Model", Description = "Represents a symbolic time series prognosis model.")]
36  public class SymbolicTimeSeriesPrognosisModel : NamedItem, ISymbolicTimeSeriesPrognosisModel {
37    public override Image ItemImage {
38      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
39    }
40
41    #region properties
42
43    [Storable]
44    private ISymbolicExpressionTree symbolicExpressionTree;
45    public ISymbolicExpressionTree SymbolicExpressionTree {
46      get { return symbolicExpressionTree; }
47    }
48
49    [Storable]
50    private ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter;
51    public ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter Interpreter {
52      get { return interpreter; }
53    }
54
55    #endregion
56
57    [Storable]
58    private string[] targetVariables;
59
60
61    [StorableConstructor]
62    protected SymbolicTimeSeriesPrognosisModel(bool deserializing) : base(deserializing) { }
63    protected SymbolicTimeSeriesPrognosisModel(SymbolicTimeSeriesPrognosisModel original, Cloner cloner)
64      : base(original, cloner) {
65      this.symbolicExpressionTree = cloner.Clone(original.symbolicExpressionTree);
66      this.interpreter = cloner.Clone(original.interpreter);
67    }
68    public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, IEnumerable<string> targetVariables)
69      : base() {
70      this.name = ItemName;
71      this.description = ItemDescription;
72      this.symbolicExpressionTree = tree;
73      this.interpreter = interpreter; this.targetVariables = targetVariables.ToArray();
74    }
75
76    public override IDeepCloneable Clone(Cloner cloner) {
77      return new SymbolicTimeSeriesPrognosisModel(this, cloner);
78    }
79
80    public IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, int horizon) {
81      return Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, targetVariables, rows, horizon);
82    }
83
84    public ISymbolicTimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
85      return new SymbolicTimeSeriesPrognosisSolution(this, problemData);
86    }
87    ITimeSeriesPrognosisSolution ITimeSeriesPrognosisModel.CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
88      return CreateTimeSeriesPrognosisSolution(problemData);
89    }
90
91    public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) {
92      var dataset = problemData.Dataset;
93      var targetVariables = problemData.TargetVariables;
94      var rows = problemData.TrainingIndizes;
95      int i = 0;
96      int horizon = 1;
97      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, problemData.TargetVariables.ToArray(), rows, horizon)
98        .ToArray();
99      foreach (var targetVariable in targetVariables) {
100        var targetValues = dataset.GetDoubleValues(targetVariable, rows);
101        double alpha;
102        double beta;
103        OnlineCalculatorError errorState;
104        OnlineLinearScalingParameterCalculator.Calculate(estimatedValues[i].Select(x => x.First()), targetValues,
105          out alpha, out beta, out errorState);
106        if (errorState != OnlineCalculatorError.None) return;
107
108        ConstantTreeNode alphaTreeNode = null;
109        ConstantTreeNode betaTreeNode = null;
110        // check if model has been scaled previously by analyzing the structure of the tree
111        var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
112        if (startNode.GetSubtree(i).Symbol is Addition) {
113          var addNode = startNode.GetSubtree(i);
114          if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication &&
115              addNode.GetSubtree(1).Symbol is Constant) {
116            alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
117            var mulNode = addNode.GetSubtree(0);
118            if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
119              betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
120            }
121          }
122        }
123        // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
124        if (alphaTreeNode != null && betaTreeNode != null) {
125          betaTreeNode.Value *= beta;
126          alphaTreeNode.Value *= beta;
127          alphaTreeNode.Value += alpha;
128        } else {
129          var mainBranch = startNode.GetSubtree(i);
130          startNode.RemoveSubtree(i);
131          var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
132          startNode.InsertSubtree(i, scaledMainBranch);
133        }
134        i++;
135      } // foreach
136    }
137
138    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
139      if (alpha.IsAlmost(0.0)) {
140        return treeNode;
141      } else {
142        var addition = new Addition();
143        var node = addition.CreateTreeNode();
144        var alphaConst = MakeConstant(alpha);
145        node.AddSubtree(treeNode);
146        node.AddSubtree(alphaConst);
147        return node;
148      }
149    }
150
151    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
152      if (beta.IsAlmost(1.0)) {
153        return treeNode;
154      } else {
155        var multipliciation = new Multiplication();
156        var node = multipliciation.CreateTreeNode();
157        var betaConst = MakeConstant(beta);
158        node.AddSubtree(treeNode);
159        node.AddSubtree(betaConst);
160        return node;
161      }
162    }
163
164    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
165      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
166      node.Value = c;
167      return node;
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.