Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
06/12/12 10:31:56 (12 years ago)
Author:
mkommend
Message:

#1081: Improved performance of time series prognosis.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs

    r7183 r7989  
    2020#endregion
    2121
    22 using System;
    2322using System.Collections.Generic;
    2423using System.Drawing;
     
    6463    #endregion
    6564
    66     [Storable]
    67     private string[] targetVariables;
    68 
    69 
    7065    [StorableConstructor]
    7166    protected SymbolicTimeSeriesPrognosisModel(bool deserializing) : base(deserializing) { }
     
    7469      this.symbolicExpressionTree = cloner.Clone(original.symbolicExpressionTree);
    7570      this.interpreter = cloner.Clone(original.interpreter);
    76       this.targetVariables = new string[original.targetVariables.Length];
    77       Array.Copy(original.targetVariables, this.targetVariables, this.targetVariables.Length);
    7871      this.lowerEstimationLimit = original.lowerEstimationLimit;
    7972      this.upperEstimationLimit = original.upperEstimationLimit;
    8073    }
    81     public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, IEnumerable<string> targetVariables, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue)
     74    public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue)
    8275      : base() {
    8376      this.name = ItemName;
    8477      this.description = ItemDescription;
    8578      this.symbolicExpressionTree = tree;
    86       this.interpreter = interpreter; this.targetVariables = targetVariables.ToArray();
     79      this.interpreter = interpreter;
    8780      this.lowerEstimationLimit = lowerLimit;
    8881      this.upperEstimationLimit = upperLimit;
     
    9386    }
    9487
    95     public IEnumerable<IEnumerable<IEnumerable<double>>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, int horizon) {
    96       var enumerator =
    97         Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, targetVariables, rows, horizon).
    98           GetEnumerator();
    99       foreach (var r in rows) {
    100         var l = new List<double[]>();
    101         for (int h = 0; h < horizon; h++) {
    102           double[] components = new double[targetVariables.Length];
    103           for (int c = 0; c < components.Length; c++) {
    104             enumerator.MoveNext();
    105             components[c] = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, enumerator.Current));
    106           }
    107           l.Add(components);
    108         }
    109         yield return l;
    110       }
     88    public IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, int horizon) {
     89      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows, horizon);
     90      return estimatedValues.Select(predictionPerRow => predictionPerRow.LimitToRange(lowerEstimationLimit, upperEstimationLimit));
    11191    }
    11292
     
    120100    public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) {
    121101      var dataset = problemData.Dataset;
    122       var targetVariables = problemData.TargetVariables.ToArray();
     102      var targetVariable = problemData.TargetVariable;
    123103      var rows = problemData.TrainingIndizes;
    124       var estimatedValuesEnumerator = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset,
    125                                                                               targetVariables,
    126                                                                               rows).GetEnumerator();
    127       var scalingParameterCalculators =
    128         problemData.TargetVariables.Select(v => new OnlineLinearScalingParameterCalculator()).ToArray();
    129       var targetValuesEnumerator = problemData.Dataset.GetVectorEnumerable(targetVariables, rows).GetEnumerator();
     104      var estimatedValuesEnumerator = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
     105      var targetValuesEnumerator = problemData.Dataset.GetDoubleValues(targetVariable, rows);
    130106
    131       var more = targetValuesEnumerator.MoveNext() & estimatedValuesEnumerator.MoveNext();
    132       // foreach row
    133       while (more) {
    134         // foreach component
    135         for (int i = 0; i < targetVariables.Length; i++) {
    136           scalingParameterCalculators[i].Add(estimatedValuesEnumerator.Current, targetValuesEnumerator.Current);
    137           more = estimatedValuesEnumerator.MoveNext() & targetValuesEnumerator.MoveNext();
     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          }
    138124        }
    139125      }
    140 
    141       for (int i = 0; i < targetVariables.Count(); i++) {
    142         if (scalingParameterCalculators[i].ErrorState != OnlineCalculatorError.None) continue;
    143         double alpha = scalingParameterCalculators[i].Alpha;
    144         double beta = scalingParameterCalculators[i].Beta;
    145         ConstantTreeNode alphaTreeNode = null;
    146         ConstantTreeNode betaTreeNode = null;
    147         // check if model has been scaled previously by analyzing the structure of the tree
    148         var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
    149         if (startNode.GetSubtree(i).Symbol is Addition) {
    150           var addNode = startNode.GetSubtree(i);
    151           if (addNode.SubtreeCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication &&
    152               addNode.GetSubtree(1).Symbol is Constant) {
    153             alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
    154             var mulNode = addNode.GetSubtree(0);
    155             if (mulNode.SubtreeCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
    156               betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
    157             }
    158           }
    159         }
    160         // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
    161         if (alphaTreeNode != null && betaTreeNode != null) {
    162           betaTreeNode.Value *= beta;
    163           alphaTreeNode.Value *= beta;
    164           alphaTreeNode.Value += alpha;
    165         } else {
    166           var mainBranch = startNode.GetSubtree(i);
    167           startNode.RemoveSubtree(i);
    168           var scaledMainBranch = MakeSum(MakeProduct(mainBranch, beta), alpha);
    169           startNode.InsertSubtree(i, scaledMainBranch);
    170         }
    171       } // foreach
     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      }
    172137    }
    173138
Note: See TracChangeset for help on using the changeset viewer.