Changeset 7989 for branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs
- Timestamp:
- 06/12/12 10:31:56 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic.TimeSeriesPrognosis/3.4/SymbolicTimeSeriesPrognosisModel.cs
r7183 r7989 20 20 #endregion 21 21 22 using System;23 22 using System.Collections.Generic; 24 23 using System.Drawing; … … 64 63 #endregion 65 64 66 [Storable]67 private string[] targetVariables;68 69 70 65 [StorableConstructor] 71 66 protected SymbolicTimeSeriesPrognosisModel(bool deserializing) : base(deserializing) { } … … 74 69 this.symbolicExpressionTree = cloner.Clone(original.symbolicExpressionTree); 75 70 this.interpreter = cloner.Clone(original.interpreter); 76 this.targetVariables = new string[original.targetVariables.Length];77 Array.Copy(original.targetVariables, this.targetVariables, this.targetVariables.Length);78 71 this.lowerEstimationLimit = original.lowerEstimationLimit; 79 72 this.upperEstimationLimit = original.upperEstimationLimit; 80 73 } 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) 82 75 : base() { 83 76 this.name = ItemName; 84 77 this.description = ItemDescription; 85 78 this.symbolicExpressionTree = tree; 86 this.interpreter = interpreter; this.targetVariables = targetVariables.ToArray();79 this.interpreter = interpreter; 87 80 this.lowerEstimationLimit = lowerLimit; 88 81 this.upperEstimationLimit = upperLimit; … … 93 86 } 94 87 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)); 111 91 } 112 92 … … 120 100 public static void Scale(SymbolicTimeSeriesPrognosisModel model, ITimeSeriesPrognosisProblemData problemData) { 121 101 var dataset = problemData.Dataset; 122 var targetVariable s = problemData.TargetVariables.ToArray();102 var targetVariable = problemData.TargetVariable; 123 103 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); 130 106 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 } 138 124 } 139 125 } 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 } 172 137 } 173 138
Note: See TracChangeset
for help on using the changeset viewer.