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/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs

    r7183 r7989  
    2222using System;
    2323using System.Collections.Generic;
    24 using System.Drawing.Printing;
    2524using System.Linq;
    2625using HeuristicLab.Common;
     
    5150      IEnumerable<int> rows = GenerateRowsToEvaluate();
    5251
    53       double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue,
    54         solution,
     52      var interpreter = (ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter)SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
     53
     54      double quality = Calculate(interpreter, solution,
    5555        EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
    5656        ProblemDataParameter.ActualValue,
    57         rows, HorizonParameter.ActualValue.Value);
     57        rows, HorizonParameter.ActualValue.Value, ApplyLinearScaling);
    5858      QualityParameter.ActualValue = new DoubleValue(quality);
    5959
     
    6161    }
    6262
    63     public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows, int horizon) {
    64       double[] alpha;
    65       double[] beta;
    66       DetermineScalingFactors(solution, problemData, interpreter, rows, out alpha, out beta);
    67       var scaledSolution = Scale(solution, alpha, beta);
    68       string[] targetVariables = problemData.TargetVariables.ToArray();
    69       var meanSquaredErrorCalculators = Enumerable.Range(0, problemData.TargetVariables.Count())
    70         .Select(i => new OnlineMeanSquaredErrorCalculator()).ToArray();
     63    public static double Calculate(ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree solution, double lowerEstimationLimit, double upperEstimationLimit, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows, int horizon, bool applyLinearScaling) {
     64      IEnumerable<double> targetValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows.SelectMany(r => Enumerable.Range(r, horizon)));
     65      IEnumerable<double> estimatedValues = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, rows, horizon).SelectMany(x => x);
     66      IEnumerable<double> boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit);
     67      OnlineCalculatorError errorState;
    7168
    72       var allContinuationsEnumerator = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset,
    73                                                                                   targetVariables,
    74                                                                                   rows, horizon).GetEnumerator();
    75       allContinuationsEnumerator.MoveNext();
    76       // foreach row
    77       foreach (var row in rows) {
    78         // foreach horizon
    79         for (int h = 0; h < horizon; h++) {
    80           // foreach component
    81           for (int i = 0; i < meanSquaredErrorCalculators.Length; i++) {
    82             double e = Math.Min(upperEstimationLimit, Math.Max(lowerEstimationLimit, allContinuationsEnumerator.Current));
    83             meanSquaredErrorCalculators[i].Add(problemData.Dataset.GetDoubleValue(targetVariables[i], row + h), e);
    84             if (meanSquaredErrorCalculators[i].ErrorState == OnlineCalculatorError.InvalidValueAdded)
    85               return double.MaxValue;
    86             allContinuationsEnumerator.MoveNext();
    87           }
    88         }
    89       }
    90       var meanCalculator = new OnlineMeanAndVarianceCalculator();
    91       foreach (var calc in meanSquaredErrorCalculators) {
    92         if (calc.ErrorState != OnlineCalculatorError.None) return double.MaxValue;
    93         meanCalculator.Add(calc.MeanSquaredError);
    94       }
     69      double mse;
     70      if (applyLinearScaling) {
     71        var mseCalculator = new OnlineMeanSquaredErrorCalculator();
     72        CalculateWithScaling(targetValues, boundedEstimatedValues, mseCalculator, problemData.Dataset.Rows);
     73        errorState = mseCalculator.ErrorState;
     74        mse = mseCalculator.MeanSquaredError;
     75      } else
     76        mse = OnlineMeanSquaredErrorCalculator.Calculate(targetValues, boundedEstimatedValues, out errorState);
    9577
    96       return meanCalculator.MeanErrorState == OnlineCalculatorError.None ? meanCalculator.Mean : double.MaxValue;
     78      if (errorState != OnlineCalculatorError.None) return Double.NaN;
     79      else return mse;
    9780    }
    9881
    99     private static ISymbolicExpressionTree Scale(ISymbolicExpressionTree solution, double[] alpha, double[] beta) {
    100       var clone = (ISymbolicExpressionTree)solution.Clone();
    101       int n = alpha.Length;
    102       for (int i = 0; i < n; i++) {
    103         var parent = clone.Root.GetSubtree(0);
    104         var rpb = clone.Root.GetSubtree(0).GetSubtree(i);
    105         var scaledRpb = MakeSum(
    106           MakeProduct(rpb,
    107             MakeConstant(beta[i], clone.Root.Grammar), clone.Root.Grammar),
    108             MakeConstant(alpha[i], clone.Root.Grammar), clone.Root.Grammar);
    109         parent.RemoveSubtree(i);
    110         parent.InsertSubtree(i, scaledRpb);
    111       }
    112       return clone;
    113     }
    114 
    115     private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeGrammar grammar) {
    116       var sum = grammar.Symbols.Where(s => s is Addition).First().CreateTreeNode();
    117       sum.AddSubtree(a);
    118       sum.AddSubtree(b);
    119       return sum;
    120     }
    121 
    122     private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b, ISymbolicExpressionTreeGrammar grammar) {
    123       var prod = grammar.Symbols.Where(s => s is Multiplication).First().CreateTreeNode();
    124       prod.AddSubtree(a);
    125       prod.AddSubtree(b);
    126       return prod;
    127     }
    128 
    129     private static ISymbolicExpressionTreeNode MakeConstant(double c, ISymbolicExpressionTreeGrammar grammar) {
    130       var node = (ConstantTreeNode)grammar.Symbols.Where(s => s is Constant).First().CreateTreeNode();
    131       node.Value = c;
    132       return node;
    133     }
    134 
    135     private static void DetermineScalingFactors(ISymbolicExpressionTree solution, ITimeSeriesPrognosisProblemData problemData, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, IEnumerable<int> rows, out double[] alpha, out double[] beta) {
    136       string[] targetVariables = problemData.TargetVariables.ToArray();
    137       int nComponents = targetVariables.Length;
    138       alpha = new double[nComponents];
    139       beta = new double[nComponents];
    140       var oneStepPredictionsEnumerator = interpreter.GetSymbolicExpressionTreeValues(solution, problemData.Dataset, targetVariables, rows).GetEnumerator();
    141       var scalingParameterCalculators =
    142         Enumerable.Repeat(0, nComponents).Select(x => new OnlineLinearScalingParameterCalculator()).ToArray();
    143       var targetValues = problemData.Dataset.GetVectorEnumerable(targetVariables, rows);
    144       var targetValueEnumerator = targetValues.GetEnumerator();
    145 
    146       var more = oneStepPredictionsEnumerator.MoveNext() & targetValueEnumerator.MoveNext();
    147       while (more) {
    148         for (int i = 0; i < nComponents; i++) {
    149           scalingParameterCalculators[i].Add(oneStepPredictionsEnumerator.Current, targetValueEnumerator.Current);
    150           more = oneStepPredictionsEnumerator.MoveNext() & targetValueEnumerator.MoveNext();
    151         }
    152       }
    153 
    154       for (int i = 0; i < nComponents; i++) {
    155         if (scalingParameterCalculators[i].ErrorState == OnlineCalculatorError.None) {
    156           alpha[i] = scalingParameterCalculators[i].Alpha;
    157           beta[i] = scalingParameterCalculators[i].Beta;
    158         } else {
    159           alpha[i] = 0.0;
    160           beta[i] = 1.0;
    161         }
    162       }
    163     }
    16482
    16583    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) {
     
    16886      HorizonParameter.ExecutionContext = context;
    16987
    170       double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, HorizonParameter.ActualValue.Value);
     88      double mse = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue as ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper, problemData, rows, HorizonParameter.ActualValue.Value, ApplyLinearScaling);
    17189
    17290      HorizonParameter.ExecutionContext = null;
Note: See TracChangeset for help on using the changeset viewer.