Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/12/11 15:35:17 (13 years ago)
Author:
gkronber
Message:

#1640 adapted IL emitting interpreter to work with previous (r6740) changes of the dataset.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs

    r6740 r6741  
    2121
    2222using System;
     23using System.Collections.ObjectModel;
     24using System.Linq;
    2325using System.Collections.Generic;
    2426using System.Reflection;
     
    3537  [Item("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.")]
    3638  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();
    3840    private static MethodInfo cos = typeof(Math).GetMethod("Cos", new Type[] { typeof(double) });
    3941    private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) });
     
    4143    private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) });
    4244    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) });
    4445    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);
    5148    private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
    5249    #region private classes
     
    220217      Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode);
    221218      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
    222225      for (int i = 0; i < code.Length; i++) {
    223226        Instruction instr = code[i];
    224227        if (instr.opCode == OpCodes.Variable) {
    225228          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
    226           instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
     229          instr.iArg0 = doubleVariableNames[variableTreeNode.VariableName];
    227230          code[i] = instr;
    228231        } else if (instr.opCode == OpCodes.LagVariable) {
    229232          var variableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
    230           instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
     233          instr.iArg0 = doubleVariableNames[variableTreeNode.VariableName];
    231234          code[i] = instr;
    232235        } else if (instr.opCode == OpCodes.VariableCondition) {
    233236          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
    234           instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
     237          instr.iArg0 = doubleVariableNames[variableConditionTreeNode.VariableName];
    235238        } else if (instr.opCode == OpCodes.Call) {
    236239          necessaryArgStackSize += instr.nArguments + 1;
     
    239242      var state = new InterpreterState(code, necessaryArgStackSize);
    240243
    241       Type[] methodArgs = { typeof(Dataset), typeof(int) };
     244      Type[] methodArgs = { typeof(int), typeof(IList<double>[]) };
    242245      DynamicMethod testFun = new DynamicMethod("TestFun", typeof(double), methodArgs, typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter).Module);
    243246
     
    249252
    250253      foreach (var row in rows) {
    251         yield return function(dataset, row);
     254        yield return function(row, columns);
    252255      }
    253256    }
     
    468471          }
    469472        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);
    479483            return;
    480484          }
Note: See TracChangeset for help on using the changeset viewer.