Index: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
===================================================================
--- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs	(revision 8432)
+++ branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs	(revision 8436)
@@ -35,8 +35,10 @@
   [StorableClass]
   [Item("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.")]
-  public sealed class SymbolicDataAnalysisExpressionTreeILEmittingInterpreter : ParameterizedNamedItem,
-                                                                                ISymbolicDataAnalysisExpressionTreeInterpreter {
-    private static MethodInfo listGetValue =
-      typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod();
+  public sealed class SymbolicDataAnalysisExpressionTreeILEmittingInterpreter : ParameterizedNamedItem, ISymbolicDataAnalysisExpressionTreeInterpreter {
+    private static readonly Type thisType = typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter);
+    internal delegate double CompiledFunction(int sampleIndex, IList<double>[] columns);
+
+    #region method infos
+    private static MethodInfo listGetValue = typeof(IList<double>).GetProperty("Item", new Type[] { typeof(int) }).GetGetMethod();
 
     private static MethodInfo cos = typeof(Math).GetMethod("Cos", new Type[] { typeof(double) });
@@ -49,6 +51,4 @@
     private static MethodInfo sqrt = typeof(Math).GetMethod("Sqrt", new Type[] { typeof(double) });
 
-    internal delegate double CompiledFunction(int sampleIndex, IList<double>[] columns, IList<double>[] cachedValues, int cacheStartIndex);
-    private static Type thisType = typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter);
     private static MethodInfo airyA = thisType.GetMethod("AiryA", new Type[] { typeof(double) });
     private static MethodInfo airyB = thisType.GetMethod("AiryB", new Type[] { typeof(double) });
@@ -66,155 +66,8 @@
     private static MethodInfo erf = thisType.GetMethod("Erf", new Type[] { typeof(double) });
     private static MethodInfo bessel = thisType.GetMethod("Bessel", new Type[] { typeof(double) });
-
-    
+    #endregion
 
     private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
     private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
-
-    #region private classes
-
-    private class InterpreterState {
-      private Instruction[] code;
-      private int pc;
-
-      public int ProgramCounter {
-        get { return pc; }
-        set { pc = value; }
-      }
-
-      private bool inLaggedContext;
-
-      public bool InLaggedContext {
-        get { return inLaggedContext; }
-        set { inLaggedContext = value; }
-      }
-
-      internal InterpreterState(Instruction[] code) {
-        this.inLaggedContext = false;
-        this.code = code;
-        this.pc = 0;
-      }
-
-      internal Instruction NextInstruction() {
-        return code[pc++];
-      }
-    }
-
-    private class OpCodes {
-      public const byte Add = 1;
-      public const byte Sub = 2;
-      public const byte Mul = 3;
-      public const byte Div = 4;
-
-      public const byte Sin = 5;
-      public const byte Cos = 6;
-      public const byte Tan = 7;
-
-      public const byte Log = 8;
-      public const byte Exp = 9;
-
-      public const byte IfThenElse = 10;
-
-      public const byte GT = 11;
-      public const byte LT = 12;
-
-      public const byte AND = 13;
-      public const byte OR = 14;
-      public const byte NOT = 15;
-
-
-      public const byte Average = 16;
-
-      public const byte Call = 17;
-
-      public const byte Variable = 18;
-      public const byte LagVariable = 19;
-      public const byte Constant = 20;
-      public const byte Arg = 21;
-
-      public const byte Power = 22;
-      public const byte Root = 23;
-      public const byte TimeLag = 24;
-      public const byte Integral = 25;
-      public const byte Derivative = 26;
-
-      public const byte VariableCondition = 27;
-
-      public const byte Square = 28;
-      public const byte SquareRoot = 29;
-      public const byte Gamma = 30;
-      public const byte Psi = 31;
-      public const byte Dawson = 32;
-      public const byte ExponentialIntegralEi = 33;
-      public const byte CosineIntegral = 34;
-      public const byte SineIntegral = 35;
-      public const byte HyperbolicCosineIntegral = 36;
-      public const byte HyperbolicSineIntegral = 37;
-      public const byte FresnelCosineIntegral = 38;
-      public const byte FresnelSineIntegral = 39;
-      public const byte AiryA = 40;
-      public const byte AiryB = 41;
-      public const byte Norm = 42;
-      public const byte Erf = 43;
-      public const byte Bessel = 44;
-    }
-
-    #endregion
-
-    private Dictionary<Type, byte> symbolToOpcode = new Dictionary<Type, byte>()
-                                                      {
-                                                        {typeof (Addition), OpCodes.Add},
-                                                        {typeof (Subtraction), OpCodes.Sub},
-                                                        {typeof (Multiplication), OpCodes.Mul},
-                                                        {typeof (Division), OpCodes.Div},
-                                                        {typeof (Sine), OpCodes.Sin},
-                                                        {typeof (Cosine), OpCodes.Cos},
-                                                        {typeof (Tangent), OpCodes.Tan},
-                                                        {typeof (Logarithm), OpCodes.Log},
-                                                        {typeof (Exponential), OpCodes.Exp},
-                                                        {typeof (IfThenElse), OpCodes.IfThenElse},
-                                                        {typeof (GreaterThan), OpCodes.GT},
-                                                        {typeof (LessThan), OpCodes.LT},
-                                                        {typeof (And), OpCodes.AND},
-                                                        {typeof (Or), OpCodes.OR},
-                                                        {typeof (Not), OpCodes.NOT},
-                                                        {typeof (Average), OpCodes.Average},
-                                                        {typeof (InvokeFunction), OpCodes.Call},
-                                                        {
-                                                          typeof (HeuristicLab.Problems.DataAnalysis.Symbolic.Variable),
-                                                          OpCodes.Variable
-                                                          },
-                                                        {typeof (LaggedVariable), OpCodes.LagVariable},
-                                                        {typeof (Constant), OpCodes.Constant},
-                                                        {typeof (Argument), OpCodes.Arg},
-                                                        {typeof (Power), OpCodes.Power},
-                                                        {typeof (Root), OpCodes.Root},
-                                                        {typeof (TimeLag), OpCodes.TimeLag},
-                                                        {typeof (Integral), OpCodes.Integral},
-                                                        {typeof (Derivative), OpCodes.Derivative},
-                                                        {typeof (VariableCondition), OpCodes.VariableCondition},
-                                                        {typeof (Square), OpCodes.Square},
-                                                        {typeof (SquareRoot), OpCodes.SquareRoot},
-                                                        {typeof (Gamma), OpCodes.Gamma},
-                                                        {typeof (Psi), OpCodes.Psi},
-                                                        {typeof (Dawson), OpCodes.Dawson},
-                                                        {typeof (ExponentialIntegralEi), OpCodes.ExponentialIntegralEi},
-                                                        {typeof (CosineIntegral), OpCodes.CosineIntegral},
-                                                        {typeof (SineIntegral), OpCodes.SineIntegral},
-                                                        {
-                                                          typeof (HyperbolicCosineIntegral),
-                                                          OpCodes.HyperbolicCosineIntegral
-                                                          },
-                                                        {
-                                                          typeof (HyperbolicSineIntegral), OpCodes.HyperbolicSineIntegral
-                                                          },
-                                                        {typeof (FresnelCosineIntegral), OpCodes.FresnelCosineIntegral},
-                                                        {typeof (FresnelSineIntegral), OpCodes.FresnelSineIntegral},
-                                                        {typeof (AiryA), OpCodes.AiryA},
-                                                        {typeof (AiryB), OpCodes.AiryB},
-                                                        {typeof (Norm), OpCodes.Norm},
-                                                        {typeof (Erf), OpCodes.Erf},
-                                                        {typeof (Bessel), OpCodes.Bessel}
-                                                      };
 
     public override bool CanChangeName {
@@ -254,13 +107,7 @@
 
     [StorableConstructor]
-    private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(bool deserializing)
-      : base(deserializing) {
-    }
-
-    private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(
-      SymbolicDataAnalysisExpressionTreeILEmittingInterpreter original, Cloner cloner)
-      : base(original, cloner) {
-    }
-
+    private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(bool deserializing) : base(deserializing) { }
+
+    private SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter original, Cloner cloner) : base(original, cloner) { }
     public override IDeepCloneable Clone(Cloner cloner) {
       return new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(this, cloner);
@@ -269,10 +116,6 @@
     public SymbolicDataAnalysisExpressionTreeILEmittingInterpreter()
       : base("SymbolicDataAnalysisExpressionTreeILEmittingInterpreter", "Interpreter for symbolic expression trees.") {
-      Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName,
-                                                   "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.",
-                                                   new BoolValue(false)));
-      Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName,
-                                                  "A counter for the total number of solutions the interpreter has evaluated",
-                                                  new IntValue(0)));
+      Parameters.Add(new ValueParameter<BoolValue>(CheckExpressionsWithIntervalArithmeticParameterName, "Switch that determines if the interpreter checks the validity of expressions with interval arithmetic before evaluating the expression.", new BoolValue(false)));
+      Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     }
 
@@ -280,7 +123,5 @@
     private void AfterDeserialization() {
       if (!Parameters.ContainsKey(EvaluatedSolutionsParameterName))
-        Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName,
-                                                    "A counter for the total number of solutions the interpreter has evaluated",
-                                                    new IntValue(0)));
+        Parameters.Add(new ValueParameter<IntValue>(EvaluatedSolutionsParameterName, "A counter for the total number of solutions the interpreter has evaluated", new IntValue(0)));
     }
 
@@ -297,36 +138,40 @@
     #endregion
 
-    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset,
-                                                               IEnumerable<int> rows) {
-      return GetSymbolicExpressionTreeValues(tree, dataset, new string[] { "#NOTHING#" }, rows);
-    }
-    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows) {
-      return GetSymbolicExpressionTreeValues(tree, dataset, targetVariables, rows, 1);
-    }
-    // for each row for each horizon for each target variable one value
-    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, string[] targetVariables, IEnumerable<int> rows, int horizon) {
+    public IEnumerable<double> GetSymbolicExpressionTreeValues(ISymbolicExpressionTree tree, Dataset dataset, IEnumerable<int> rows) {
       if (CheckExpressionsWithIntervalArithmetic.Value)
-        throw new NotSupportedException(
-          "Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
+        throw new NotSupportedException("Interval arithmetic is not yet supported in the symbolic data analysis interpreter.");
+
       EvaluatedSolutions.Value++; // increment the evaluated solutions counter
-      var compiler = new SymbolicExpressionTreeCompiler();
-      Instruction[] code = compiler.Compile(tree, MapSymbolToOpCode);
+      var state = PrepareInterpreterState(tree, dataset);
+
+      Type[] methodArgs = { typeof(int), typeof(IList<double>[]) };
+      DynamicMethod testFun = new DynamicMethod("TestFun", typeof(double), methodArgs, typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter).Module);
+
+      ILGenerator il = testFun.GetILGenerator();
+      CompileInstructions(il, state, dataset);
+      il.Emit(System.Reflection.Emit.OpCodes.Conv_R8);
+      il.Emit(System.Reflection.Emit.OpCodes.Ret);
+      var function = (CompiledFunction)testFun.CreateDelegate(typeof(CompiledFunction));
+
+      IList<double>[] columns = dataset.DoubleVariables.Select(v => dataset.GetReadOnlyDoubleValues(v)).ToArray();
+
+      foreach (var row in rows) {
+        yield return function(row, columns);
+      }
+    }
+
+    private InterpreterState PrepareInterpreterState(ISymbolicExpressionTree tree, Dataset dataset) {
+      Instruction[] code = SymbolicExpressionTreeCompiler.Compile(tree, OpCodes.MapSymbolToOpCode);
+      Dictionary<string, int> doubleVariableNames = dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i);
       int necessaryArgStackSize = 0;
-
-      Dictionary<string, int> doubleVariableNames =
-        dataset.DoubleVariables.Select((x, i) => new { x, i }).ToDictionary(e => e.x, e => e.i);
-
-      for (int i = 0; i < code.Length; i++) {
-        Instruction instr = code[i];
+      foreach (Instruction instr in code) {
         if (instr.opCode == OpCodes.Variable) {
-          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
+          var variableTreeNode = (VariableTreeNode)instr.dynamicNode;
           instr.iArg0 = doubleVariableNames[variableTreeNode.VariableName];
-          code[i] = instr;
         } else if (instr.opCode == OpCodes.LagVariable) {
-          var variableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
-          instr.iArg0 = doubleVariableNames[variableTreeNode.VariableName];
-          code[i] = instr;
+          var laggedVariableTreeNode = (LaggedVariableTreeNode)instr.dynamicNode;
+          instr.iArg0 = doubleVariableNames[laggedVariableTreeNode.VariableName];
         } else if (instr.opCode == OpCodes.VariableCondition) {
-          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
+          var variableConditionTreeNode = (VariableConditionTreeNode)instr.dynamicNode;
           instr.iArg0 = doubleVariableNames[variableConditionTreeNode.VariableName];
         } else if (instr.opCode == OpCodes.Call) {
@@ -334,36 +179,5 @@
         }
       }
-      var state = new InterpreterState(code);
-      Type[] methodArgs = { typeof(int), typeof(IList<double>[]), typeof(IList<double>[]), typeof(int) };
-
-      CompiledFunction[] function = new CompiledFunction[targetVariables.Length];
-      for (int i = 0; i < function.Length; i++) {
-        DynamicMethod testFun = new DynamicMethod("TestFun", typeof(double), methodArgs, typeof(SymbolicDataAnalysisExpressionTreeILEmittingInterpreter).Module);
-        ILGenerator il = testFun.GetILGenerator();
-        CompileInstructions(il, state, dataset);
-        il.Emit(System.Reflection.Emit.OpCodes.Conv_R8);
-        il.Emit(System.Reflection.Emit.OpCodes.Ret);
-        function[i] = (CompiledFunction)testFun.CreateDelegate(typeof(CompiledFunction));
-      }
-      var values = doubleVariableNames.Keys
-        .Select(v => dataset.GetReadOnlyDoubleValues(v))
-        .ToArray();
-      var cachedValues = (from var in doubleVariableNames.Keys
-                          select new double[horizon]).ToArray();
-      foreach (var row in rows) {
-        // init first line of cache 
-        int c = 0;
-        foreach (var var in doubleVariableNames.Keys)
-          cachedValues[c++][0] = dataset.GetDoubleValue(var, row);
-        for (int horizonRow = row; horizonRow < row + horizon; horizonRow++) {
-          for (int i = 0; i < function.Length; i++) {
-            var componentProg = function[i](horizonRow, values, cachedValues, row);
-            // set cachedValues for prognosis of future values
-            if (horizon > 1)
-              cachedValues[doubleVariableNames[targetVariables[i]]][horizonRow - row] = componentProg;
-            yield return componentProg;
-          }
-        }
-      }
+      return new InterpreterState(code, necessaryArgStackSize);
     }
 
@@ -865,12 +679,4 @@
     }
 
-    private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
-      byte opCode;
-      if (!symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out opCode))
-        throw new NotSupportedException("Symbol: " + treeNode.Symbol);
-      return opCode;
-    }
-
-
     public static double AiryA(double x) {
       if (double.IsNaN(x)) return double.NaN;
