Changeset 6741
- Timestamp:
- 09/12/11 15:35:17 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
r6740 r6741 21 21 22 22 using System; 23 using System.Collections.ObjectModel; 24 using System.Linq; 23 25 using System.Collections.Generic; 24 26 using System.Reflection; … … 35 37 [Item("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.")] 36 38 public sealed class SymbolicDataAnalysisExpressionTreeILEmittingInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter { 37 private static MethodInfo datasetGetValue = typeof(ThresholdCalculator).Assembly.GetType("HeuristicLab.Problems.DataAnalysis.Dataset").GetProperty("Item", new Type[] { typeof(int),typeof(int) }).GetGetMethod();39 private static MethodInfo listGetValue = typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod(); 38 40 private static MethodInfo cos = typeof(Math).GetMethod("Cos", new Type[] { typeof(double) }); 39 41 private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) }); … … 41 43 private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) }); 42 44 private static MethodInfo log = typeof(Math).GetMethod("Log", new Type[] { typeof(double) }); 43 private static MethodInfo sign = typeof(Math).GetMethod("Sign", new Type[] { typeof(double) });44 45 private static MethodInfo power = typeof(Math).GetMethod("Pow", new Type[] { typeof(double), typeof(double) }); 45 private static MethodInfo sqrt = typeof(Math).GetMethod("Sqrt", new Type[] { typeof(double) }); 46 private static MethodInfo isNan = typeof(double).GetMethod("IsNaN", new Type[] { typeof(double) }); 47 private static MethodInfo abs = typeof(Math).GetMethod("Abs", new Type[] { typeof(double) }); 48 private const double EPSILON = 1.0E-6; 49 50 internal delegate double CompiledFunction(Dataset dataset, int sampleIndex); 46 47 internal delegate double CompiledFunction(int sampleIndex, IList<double>[] columns); 51 48 private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic"; 52 49 #region private classes … … 220 217 Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode); 221 218 int necessaryArgStackSize = 0; 219 220 Dictionary<string, int> doubleVariableNames = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i); 221 IList<double>[] columns = (from v in doubleVariableNames.Keys 222 select dataset.GetReadOnlyDoubleValues(v)) 223 .ToArray(); 224 222 225 for (int i = 0; i < code.Length; i++) { 223 226 Instruction instr = code[i]; 224 227 if (instr.opCode == OpCodes.Variable) { 225 228 var variableTreeNode = instr.dynamicNode as VariableTreeNode; 226 instr.iArg0 = d ataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);229 instr.iArg0 = doubleVariableNames[variableTreeNode.VariableName]; 227 230 code[i] = instr; 228 231 } else if (instr.opCode == OpCodes.LagVariable) { 229 232 var variableTreeNode = instr.dynamicNode as LaggedVariableTreeNode; 230 instr.iArg0 = d ataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);233 instr.iArg0 = doubleVariableNames[variableTreeNode.VariableName]; 231 234 code[i] = instr; 232 235 } else if (instr.opCode == OpCodes.VariableCondition) { 233 236 var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode; 234 instr.iArg0 = d ataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);237 instr.iArg0 = doubleVariableNames[variableConditionTreeNode.VariableName]; 235 238 } else if (instr.opCode == OpCodes.Call) { 236 239 necessaryArgStackSize += instr.nArguments + 1; … … 239 242 var state = new InterpreterState(code, necessaryArgStackSize); 240 243 241 Type[] methodArgs = { typeof( Dataset), typeof(int) };244 Type[] methodArgs = { typeof(int), typeof(IList<double>[]) }; 242 245 DynamicMethod testFun = new DynamicMethod("TestFun", typeof(double), methodArgs, typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter).Module); 243 246 … … 249 252 250 253 foreach (var row in rows) { 251 yield return function( dataset, row);254 yield return function(row, columns); 252 255 } 253 256 } … … 468 471 } 469 472 case OpCodes.Variable: { 470 //VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode; 471 //il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // load dataset 472 //il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, 0); // sampleOffset 473 //il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // sampleIndex 474 //il.Emit(System.Reflection.Emit.OpCodes.Add); // row = sampleIndex + sampleOffset 475 //il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, currentInstr.iArg0); // load var 476 //il.Emit(System.Reflection.Emit.OpCodes.Call, datasetGetValue); // dataset.GetValue 477 //il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 478 //il.Emit(System.Reflection.Emit.OpCodes.Mul); 473 VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode; 474 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array 475 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable 476 il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref); 477 il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, 0); // sampleOffset 478 il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // sampleIndex 479 il.Emit(System.Reflection.Emit.OpCodes.Add); // row = sampleIndex + sampleOffset 480 il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue); 481 il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight 482 il.Emit(System.Reflection.Emit.OpCodes.Mul); 479 483 return; 480 484 }
Note: See TracChangeset
for help on using the changeset viewer.