Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1081 worked on multi-variate time series prognosis

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