- Timestamp:
- 12/07/11 22:47:12 (13 years ago)
- Location:
- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisSingleObjectiveTrainingBestSolutionAnalyzer.cs
r5882 r7154 85 85 86 86 var results = ResultCollection; 87 if ( TrainingBestSolutionQuality == null ||88 IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value)) {87 if (bestTree != null && (TrainingBestSolutionQuality == null || 88 IsBetter(bestQuality, TrainingBestSolutionQuality.Value, Maximization.Value))) { 89 89 TrainingBestSolution = CreateSolution(bestTree, bestQuality); 90 90 TrainingBestSolutionQuality = new DoubleValue(bestQuality); -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/ISymbolicTimeSeriesPrognogisExpressionTreeInterpreter.cs
r7120 r7154 25 25 26 26 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 27 public interface ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter : INamedItem { 28 IEnumerable<IEnumerable<IEnumerable<double>>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon); 27 public interface ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter : INamedItem { 28 IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, 29 string[] targetVariables, IEnumerable<int> rows); 30 IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon); 29 31 } 30 32 } -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r7129 r7154 46 46 private static MethodInfo round = typeof(Math).GetMethod("Round", new Type[] { typeof(double) }); 47 47 48 internal delegate double CompiledFunction(int sampleIndex, IList<double>[] columns );48 internal delegate double CompiledFunction(int sampleIndex, IList<double>[] columns, IList<double>[] cachedValues, int cacheStartIndex); 49 49 private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic"; 50 50 #region private classes … … 179 179 180 180 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) { 181 return from prog in GetSymbolicExpressionTreeValues(tree, dataset, new string[] { "#NOTHING#" }, rows, 1) 182 select prog.First().First(); 183 } 184 185 // for each row for each target variable one prognosis (=enumerable of future values) 186 public IEnumerable<IEnumerable<IEnumerable<double>>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon) { 181 return GetSymbolicExpressionTreeValues(tree, dataset, new string[] { "#NOTHING#" }, rows); 182 } 183 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows) { 184 return GetSymbolicExpressionTreeValues(tree, dataset, targetVariables, rows, 1); 185 } 186 // for each row for each horizon for each target variable one value 187 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon) { 187 188 if (CheckExpressionsWithIntervalArithmetic.Value) 188 189 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); … … 211 212 } 212 213 var state = new InterpreterState(code); 213 Type[] methodArgs = { typeof(int), typeof(IList<double>[]) };214 Type[] methodArgs = { typeof(int), typeof(IList<double>[]), typeof(IList<double>[]), typeof(int) }; 214 215 215 216 CompiledFunction[] function = new CompiledFunction[targetVariables.Length]; … … 222 223 function[i] = (CompiledFunction)testFun.CreateDelegate(typeof(CompiledFunction)); 223 224 } 224 int nComponents = tree.Root.GetSubtree(0).SubtreeCount;225 // produce a n-step forecast for each target variable for all rows226 225 var values = doubleVariableNames.Keys 227 .Select(v => targetVariables.Contains(v) ? 228 (IList<double>)dataset.GetDoubleValues(v).ToList() : 229 dataset.GetReadOnlyDoubleValues(v)) 230 .Concat(new List<double>[] { Enumerable.Repeat(0.0, dataset.Rows).ToList() }) // append list for calculated target variable 226 .Select(v => dataset.GetReadOnlyDoubleValues(v)) 231 227 .ToArray(); 232 doubleVariableNames.Add("#NOTHING#", values.Count() - 1); 233 foreach (var rowEnum in rows) { 234 int row = rowEnum; 235 foreach (var horizonRow in Enumerable.Range(row, horizon)) { 236 for (int i = 0; i < nComponents; i++) { 237 var componentProg = function[i](horizonRow, values); 228 var cachedValues = (from var in doubleVariableNames.Keys 229 select new double[horizon]).ToArray(); 230 foreach (var row in rows) { 231 // init first line of cache 232 int c = 0; 233 foreach (var var in doubleVariableNames.Keys) 234 cachedValues[c++][0] = dataset.GetDoubleValue(var, row); 235 for (int horizonRow = row; horizonRow < row + horizon; horizonRow++) { 236 for (int i = 0; i < function.Length; i++) { 237 var componentProg = function[i](horizonRow, values, cachedValues, row); 238 238 // set cachedValues for prognosis of future values 239 values[doubleVariableNames[targetVariables[i]]][horizonRow] = componentProg; 240 } 241 } 242 243 yield return from component in Enumerable.Range(0, nComponents) 244 select from horizonRow in Enumerable.Range(row, horizon) 245 select values[doubleVariableNames[targetVariables[component]]][horizonRow]; 246 // reset start value 247 foreach (var v in targetVariables) { 248 if (v != "#NOTHING#") 249 values[doubleVariableNames[v]][row] = dataset.GetDoubleValue(v, row); 239 if (horizon > 1) 240 cachedValues[doubleVariableNames[targetVariables[i]]][horizonRow - row] = componentProg; 241 yield return componentProg; 242 } 250 243 } 251 244 } … … 547 540 case OpCodes.Variable: { 548 541 VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode; 549 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array550 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0);551 // load correct column of the current variable552 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);553 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex554 542 if (!state.InLaggedContext) { 543 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array 544 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); 545 // load correct column of the current variable 546 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 547 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 555 548 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 556 549 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight … … 559 552 var nanResult = il.DefineLabel(); 560 553 var normalResult = il.DefineLabel(); 554 var cachedValue = il.DefineLabel(); 555 var multiplyValue = il.DefineLabel(); 556 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 561 557 il.Emit(System.Reflection.Emit.OpCodes.Dup); 562 558 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); … … 565 561 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows); 566 562 il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult); 563 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); 564 il.Emit(System.Reflection.Emit.OpCodes.Bge, cachedValue); 565 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array 566 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); 567 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 568 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 567 569 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 570 il.Emit(System.Reflection.Emit.OpCodes.Br, multiplyValue); 571 il.MarkLabel(cachedValue); 572 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); // load cached values array 573 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); 574 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 575 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 576 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startRow 577 il.Emit(System.Reflection.Emit.OpCodes.Sub); // startRow 578 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 579 il.MarkLabel(multiplyValue); 568 580 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 569 581 il.Emit(System.Reflection.Emit.OpCodes.Mul); … … 571 583 il.MarkLabel(nanResult); 572 584 il.Emit(System.Reflection.Emit.OpCodes.Pop); // rowIndex 573 il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference574 585 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN); 575 586 il.MarkLabel(normalResult); … … 580 591 var nanResult = il.DefineLabel(); 581 592 var normalResult = il.DefineLabel(); 593 var cachedValue = il.DefineLabel(); 594 var multiplyValue = il.DefineLabel(); 582 595 LaggedVariableTreeNode varNode = (LaggedVariableTreeNode)currentInstr.dynamicNode; 583 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array584 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable585 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);586 596 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag 587 597 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex … … 593 603 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows); 594 604 il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult); 605 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startindex 606 il.Emit(System.Reflection.Emit.OpCodes.Bge, cachedValue); 607 // normal value 608 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array 609 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable 610 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 611 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag 612 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 613 il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset 595 614 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 615 il.Emit(System.Reflection.Emit.OpCodes.Br, multiplyValue); 616 il.MarkLabel(cachedValue); 617 // cached value 618 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); // load cached values 619 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable 620 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 621 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag 622 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex 623 il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset 624 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startRow 625 il.Emit(System.Reflection.Emit.OpCodes.Sub); // startRow 626 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 627 628 il.MarkLabel(multiplyValue); 596 629 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 597 630 il.Emit(System.Reflection.Emit.OpCodes.Mul); 598 631 il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult); 599 632 il.MarkLabel(nanResult); 600 il.Emit(System.Reflection.Emit.OpCodes.Pop); // sample index 601 il.Emit(System.Reflection.Emit.OpCodes.Pop); // column reference 633 il.Emit(System.Reflection.Emit.OpCodes.Pop); // pop the row index 602 634 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN); 603 635 il.MarkLabel(normalResult); -
branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs
r7120 r7154 201 201 202 202 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) { 203 return from prog in GetSymbolicExpressionTreeValues(tree, dataset, new string[] { "#NOTHING#" }, rows, 1) 204 select prog.First().First(); 205 } 206 207 // for each row for each target variable one prognosis (=enumerable of future values) 208 public IEnumerable<IEnumerable<IEnumerable<double>>> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon) { 203 return GetSymbolicExpressionTreeValues(tree, dataset, new string[] { "#NOTHING#" }, rows); 204 } 205 206 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows) { 207 return GetSymbolicExpressionTreeValues(tree, dataset, targetVariables, rows, 1); 208 } 209 210 211 // for each row for each horizon for each target variable one value 212 public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon) { 209 213 if (CheckExpressionsWithIntervalArithmetic.Value) 210 214 throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter."); … … 238 242 foreach (var rowEnum in rows) { 239 243 int row = rowEnum; 240 List<double[]> vProgs = new List<double[]>();241 244 foreach (var horizonRow in Enumerable.Range(row, horizon)) { 242 245 int localRow = horizonRow; // create a local variable for the ref parameter 243 var vPrognosis = from i in Enumerable.Range(0, nComponents) 244 select Evaluate(dataset, ref localRow, row - 1, state, cachedPrognosedValues); 245 246 var vPrognosisArr = vPrognosis.ToArray(); 247 vProgs.Add(vPrognosisArr); 248 // set cachedValues for prognosis of future values 249 for (int i = 0; i < vPrognosisArr.Length; i++) 250 cachedPrognosedValues[targetVariables[i]][horizonRow - row] = vPrognosisArr[i]; 246 for (int c = 0; c < nComponents; c++) { 247 var prog = Evaluate(dataset, ref localRow, row - 1, state, cachedPrognosedValues); 248 yield return prog; 249 cachedPrognosedValues[targetVariables[c]][horizonRow - row] = prog; 250 } 251 251 252 252 state.Reset(); 253 253 } 254 255 yield return from component in Enumerable.Range(0, nComponents)256 select from prognosisStep in Enumerable.Range(0, vProgs.Count)257 select vProgs[prognosisStep][component];258 254 } 259 255 }
Note: See TracChangeset
for help on using the changeset viewer.