Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1081: Derived time series classes from regression classes to avoid code duplication.

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