Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/09/12 13:48:43 (12 years ago)
Author:
mkommend
Message:

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

Location:
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis-3.4.csproj

    r7989 r8458  
    107107    <Reference Include="HeuristicLab.Persistence-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    108108    <Reference Include="HeuristicLab.PluginInfrastructure-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
     109    <Reference Include="HeuristicLab.Problems.DataAnalysis.Symbolic.Regression-3.4, Version=3.4.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    109110    <Reference Include="HeuristicLab.Problems.Instances-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" />
    110111    <Reference Include="System" />
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveMeanSquaredErrorEvaluator.cs

    r8114 r8458  
    7676      } else if (applyLinearScaling) { //first create model to perform linear scaling and afterwards calculate fitness for the scaled model
    7777        var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)solution.Clone(), interpreter, lowerEstimationLimit, upperEstimationLimit);
    78         SymbolicTimeSeriesPrognosisModel.Scale(model, problemData, rows);
     78        SymbolicTimeSeriesPrognosisModel.Scale(model, problemData);
    7979        var scaledSolution = model.SymbolicExpressionTree;
    8080        estimatedValues = interpreter.GetSymbolicExpressionTreeValues(scaledSolution, problemData.Dataset, rows, horizions).SelectMany(x => x);
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveTrainingBestSolutionAnalyzer.cs

    r8430 r8458  
    7676    protected override ISymbolicTimeSeriesPrognosisSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    7777      var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue as ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    78       if (ApplyLinearScaling.Value)
    79         SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     78      if (ApplyLinearScaling.Value) SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue);
    8079      return new SymbolicTimeSeriesPrognosisSolution(model, (ITimeSeriesPrognosisProblemData)ProblemDataParameter.ActualValue.Clone());
    8180    }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SingleObjective/SymbolicTimeSeriesPrognosisSingleObjectiveValidationBestSolutionAnalyzer.cs

    r8430 r8458  
    6565    protected override ISymbolicTimeSeriesPrognosisSolution CreateSolution(ISymbolicExpressionTree bestTree, double bestQuality) {
    6666      var model = new SymbolicTimeSeriesPrognosisModel((ISymbolicExpressionTree)bestTree.Clone(), SymbolicDataAnalysisTreeInterpreterParameter.ActualValue as ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper);
    67       if (ApplyLinearScaling.Value)
    68         SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue, ProblemDataParameter.ActualValue.TrainingIndices);
     67      if (ApplyLinearScaling.Value) SymbolicTimeSeriesPrognosisModel.Scale(model, ProblemDataParameter.ActualValue);
     68
    6969      return new SymbolicTimeSeriesPrognosisSolution(model, (ITimeSeriesPrognosisProblemData)ProblemDataParameter.ActualValue.Clone());
    7070    }
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs

    r8114 r8458  
    2121
    2222using System.Collections.Generic;
    23 using System.Drawing;
    2423using System.Linq;
    2524using HeuristicLab.Common;
     
    2726using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    2827using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
     28using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
    2929
    3030namespace HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis {
     
    3434  [StorableClass]
    3535  [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; }
     36  public class SymbolicTimeSeriesPrognosisModel : SymbolicRegressionModel, ISymbolicTimeSeriesPrognosisModel {
     37
     38    public new ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter Interpreter {
     39      get { return (ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter)base.Interpreter; }
    3940    }
    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
    6441
    6542    [StorableConstructor]
    6643    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 
     44    protected SymbolicTimeSeriesPrognosisModel(SymbolicTimeSeriesPrognosisModel original, Cloner cloner) : base(original, cloner) { }
    8445    public override IDeepCloneable Clone(Cloner cloner) {
    8546      return new SymbolicTimeSeriesPrognosisModel(this, cloner);
    8647    }
    8748
     49    public SymbolicTimeSeriesPrognosisModel(ISymbolicExpressionTree tree, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter interpreter, double lowerLimit = double.MinValue, double upperLimit = double.MaxValue) : base(tree, interpreter, lowerLimit, upperLimit) { }
     50
     51
     52
    8853    public IEnumerable<IEnumerable<double>> GetPrognosedValues(Dataset dataset, IEnumerable<int> rows, IEnumerable<int> horizons) {
    8954      var estimatedValues = Interpreter.GetSymbolicExpressionTreeValues(SymbolicExpressionTree, dataset, rows, horizons);
    90       return estimatedValues.Select(predictionPerRow => predictionPerRow.LimitToRange(lowerEstimationLimit, upperEstimationLimit));
     55      return estimatedValues.Select(predictionPerRow => predictionPerRow.LimitToRange(LowerEstimationLimit, UpperEstimationLimit));
    9156    }
    9257
     
    9863    }
    9964
    100     public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData, IEnumerable<int> rows) {
    101       var dataset = problemData.Dataset;
    102       var targetVariable = problemData.TargetVariable;
    103       var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
    104       var boundedEstimatedValues = estimatedValues.LimitToRange(model.lowerEstimationLimit, model.upperEstimationLimit);
    105       var targetValues = problemData.Dataset.GetDoubleValues(targetVariable, rows);
     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);
    10671
    107       double alpha, beta;
    108       OnlineCalculatorError error;
    109       OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, targetValues, out alpha, out beta, out error);
    110       if (error != OnlineCalculatorError.None) return;
     72    //  double alpha, beta;
     73    //  OnlineCalculatorError error;
     74    //  OnlineLinearScalingParameterCalculator.Calculate(boundedEstimatedValues, targetValues, out alpha, out beta, out error);
     75    //  if (error != OnlineCalculatorError.None) return;
    11176
    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     }
     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    //}
    138103
    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     }
     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    //}
    151116
    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     }
     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    //}
    164129
    165     private static ISymbolicExpressionTreeNode MakeConstant(double c) {
    166       var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
    167       node.Value = c;
    168       return node;
    169     }
     130    //private static ISymbolicExpressionTreeNode MakeConstant(double c) {
     131    //  var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     132    //  node.Value = c;
     133    //  return node;
     134    //}
    170135  }
    171136}
Note: See TracChangeset for help on using the changeset viewer.