Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8010 was 8010, checked in by mkommend, 12 years ago

#1081: Corrected time series solution results and implemented new models.

File size: 7.5 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    [Storable(DefaultValue = double.MinValue)]
41    private double lowerEstimationLimit;
42    [Storable(DefaultValue = double.MaxValue)]
43    private double upperEstimationLimit;
44
45    #region properties
46
47    [Storable]
48    private ISymbolicExpressionTree symbolicExpressionTree;
49    public ISymbolicExpressionTree SymbolicExpressionTree {
50      get { return symbolicExpressionTree; }
51    }
52
53    [Storable]
54    private ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter;
55    public ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter Interpreter {
56      get { return interpreter; }
57    }
58
59    ISymbolicDataAnalysisExpressionTreeInterpreter ISymbolicDataAnalysisModel.Interpreter {
60      get { return (ISymbolicDataAnalysisExpressionTreeInterpreter)interpreter; }
61    }
62
63    #endregion
64
65    [StorableConstructor]
66    protected SymbolicTimeSeriesPrognosisModel(bool deserializing) : base(deserializing) { }
67    protected SymbolicTimeSeriesPrognosisModel(SymbolicTimeSeriesPrognosisModel original, Cloner cloner)
68      : base(original, cloner) {
69      this.symbolicExpressionTree = cloner.Clone(original.symbolicExpressionTree);
70      this.interpreter = cloner.Clone(original.interpreter);
71      this.lowerEstimationLimit = original.lowerEstimationLimit;
72      this.upperEstimationLimit = original.upperEstimationLimit;
73    }
74    public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue)
75      : base() {
76      this.name = ItemName;
77      this.description = ItemDescription;
78      this.symbolicExpressionTree = tree;
79      this.interpreter = interpreter;
80      this.lowerEstimationLimit = lowerLimit;
81      this.upperEstimationLimit = upperLimit;
82    }
83
84    public override IDeepCloneable Clone(Cloner cloner) {
85      return new SymbolicTimeSeriesPrognosisModel(this, cloner);
86    }
87
88    public IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
89      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows, horizons);
90      return estimatedValues.Select(predictionPerRow => predictionPerRow.LimitToRange(lowerEstimationLimit, upperEstimationLimit));
91    }
92
93    public ISymbolicTimeSeriesPrognosisSolution CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
94      return new SymbolicTimeSeriesPrognosisSolution(this, problemData);
95    }
96    ITimeSeriesPrognosisSolution ITimeSeriesPrognosisModel.CreateTimeSeriesPrognosisSolution(ITimeSeriesPrognosisProblemData problemData) {
97      return CreateTimeSeriesPrognosisSolution(problemData);
98    }
99
100    public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) {
101      var dataset = problemData.Dataset;
102      var targetVariable = problemData.TargetVariable;
103      var rows = problemData.TrainingIndizes;
104      var estimatedValuesEnumerator = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
105      var targetValuesEnumerator = problemData.Dataset.GetDoubleValues(targetVariable, rows);
106
107      double alpha, beta;
108      OnlineCalculatorError error;
109      OnlineLinearScalingParameterCalculator.Calculate(estimatedValuesEnumerator, targetValuesEnumerator, out alpha, out beta, out error);
110      if (error != OnlineCalculatorError.None) return;
111
112      ConstantTreeNode alphaTreeNode = null;
113      ConstantTreeNode betaTreeNode = null;
114      // check if model has been scaled previously by analyzing the structure of the tree
115      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
116      if (startNode.GetSubtree(0).Symbol is Addition) {
117        var addNode = startNode.GetSubtree(0);
118        if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
119          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
120          var mulNode = addNode.GetSubtree(0);
121          if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
122            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
123          }
124        }
125      }
126      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
127      if (alphaTreeNode != null && betaTreeNode != null) {
128        betaTreeNode.Value *= beta;
129        alphaTreeNode.Value *= beta;
130        alphaTreeNode.Value += alpha;
131      } else {
132        var mainBranch = startNode.GetSubtree(0);
133        startNode.RemoveSubtree(0);
134        var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
135        startNode.AddSubtree(scaledMainBranch);
136      }
137    }
138
139    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
140      if (alpha.IsAlmost(0.0)) {
141        return treeNode;
142      } else {
143        var addition = new Addition();
144        var node = addition.CreateTreeNode();
145        var alphaConst = MakeConstant(alpha);
146        node.AddSubtree(treeNode);
147        node.AddSubtree(alphaConst);
148        return node;
149      }
150    }
151
152    private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode treeNode, double beta) {
153      if (beta.IsAlmost(1.0)) {
154        return treeNode;
155      } else {
156        var multipliciation = new Multiplication();
157        var node = multipliciation.CreateTreeNode();
158        var betaConst = MakeConstant(beta);
159        node.AddSubtree(treeNode);
160        node.AddSubtree(betaConst);
161        return node;
162      }
163    }
164
165    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
166      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
167      node.Value = c;
168      return node;
169    }
170  }
171}
Note: See TracBrowser for help on using the repository browser.