Index: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
===================================================================
--- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs	(revision 8431)
+++ branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs	(revision 8431)
@@ -0,0 +1,967 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Reflection;
+using System.Reflection.Emit;
+using HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Parameters;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
+  [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();
+
+    private static MethodInfo cos = typeof(Math).GetMethod("Cos", new Type[] { typeof(double) });
+    private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) });
+    private static MethodInfo tan = typeof(Math).GetMethod("Tan", new Type[] { typeof(double) });
+    private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) });
+    private static MethodInfo log = typeof(Math).GetMethod("Log", new Type[] { typeof(double) });
+    private static MethodInfo power = typeof(Math).GetMethod("Pow", new Type[] { typeof(double), typeof(double) });
+    private static MethodInfo round = typeof(Math).GetMethod("Round", new Type[] { typeof(double) });
+    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) });
+    private static MethodInfo gamma = thisType.GetMethod("Gamma", new Type[] { typeof(double) });
+    private static MethodInfo psi = thisType.GetMethod("Psi", new Type[] { typeof(double) });
+    private static MethodInfo dawson = thisType.GetMethod("Dawson", new Type[] { typeof(double) });
+    private static MethodInfo expIntegralEi = thisType.GetMethod("ExpIntegralEi", new Type[] { typeof(double) });
+    private static MethodInfo sinIntegral = thisType.GetMethod("SinIntegral", new Type[] { typeof(double) });
+    private static MethodInfo cosIntegral = thisType.GetMethod("CosIntegral", new Type[] { typeof(double) });
+    private static MethodInfo hypSinIntegral = thisType.GetMethod("HypSinIntegral", new Type[] { typeof(double) });
+    private static MethodInfo hypCosIntegral = thisType.GetMethod("HypCosIntegral", new Type[] { typeof(double) });
+    private static MethodInfo fresnelCosIntegral = thisType.GetMethod("FresnelCosIntegral", new Type[] { typeof(double) });
+    private static MethodInfo fresnelSinIntegral = thisType.GetMethod("FresnelSinIntegral", new Type[] { typeof(double) });
+    private static MethodInfo norm = thisType.GetMethod("Norm", new Type[] { typeof(double) });
+    private static MethodInfo erf = thisType.GetMethod("Erf", new Type[] { typeof(double) });
+    private static MethodInfo bessel = thisType.GetMethod("Bessel", new Type[] { typeof(double) });
+
+    
+
+    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 {
+      get { return false; }
+    }
+
+    public override bool CanChangeDescription {
+      get { return false; }
+    }
+
+    #region parameter properties
+
+    public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
+      get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
+    }
+
+    public IValueParameter<IntValue> EvaluatedSolutionsParameter {
+      get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
+    }
+
+    #endregion
+
+    #region properties
+
+    public BoolValue CheckExpressionsWithIntervalArithmetic {
+      get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
+      set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
+    }
+
+    public IntValue EvaluatedSolutions {
+      get { return EvaluatedSolutionsParameter.Value; }
+      set { EvaluatedSolutionsParameter.Value = value; }
+    }
+
+    #endregion
+
+
+    [StorableConstructor]
+    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);
+    }
+
+    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)));
+    }
+
+    [StorableHook(HookType.AfterDeserialization)]
+    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)));
+    }
+
+    #region IStatefulItem
+
+    public void InitializeState() {
+      EvaluatedSolutions.Value = 0;
+    }
+
+    public void ClearState() {
+      EvaluatedSolutions.Value = 0;
+    }
+
+    #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) {
+      if (CheckExpressionsWithIntervalArithmetic.Value)
+        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);
+      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];
+        if (instr.opCode == OpCodes.Variable) {
+          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
+          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;
+        } else if (instr.opCode == OpCodes.VariableCondition) {
+          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
+          instr.iArg0 = doubleVariableNames[variableConditionTreeNode.VariableName];
+        } else if (instr.opCode == OpCodes.Call) {
+          necessaryArgStackSize += instr.nArguments + 1;
+        }
+      }
+      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;
+          }
+        }
+      }
+    }
+
+    private void CompileInstructions(ILGenerator il, InterpreterState state, Dataset ds) {
+      Instruction currentInstr = state.NextInstruction();
+      int nArgs = currentInstr.nArguments;
+
+      switch (currentInstr.opCode) {
+        case OpCodes.Add: {
+            if (nArgs > 0) {
+              CompileInstructions(il, state, ds);
+            }
+            for (int i = 1; i < nArgs; i++) {
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Add);
+            }
+            return;
+          }
+        case OpCodes.Sub: {
+            if (nArgs == 1) {
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Neg);
+              return;
+            }
+            if (nArgs > 0) {
+              CompileInstructions(il, state, ds);
+            }
+            for (int i = 1; i < nArgs; i++) {
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Sub);
+            }
+            return;
+          }
+        case OpCodes.Mul: {
+            if (nArgs > 0) {
+              CompileInstructions(il, state, ds);
+            }
+            for (int i = 1; i < nArgs; i++) {
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            }
+            return;
+          }
+        case OpCodes.Div: {
+            if (nArgs == 1) {
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0);
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Div);
+              return;
+            }
+            if (nArgs > 0) {
+              CompileInstructions(il, state, ds);
+            }
+            for (int i = 1; i < nArgs; i++) {
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Div);
+            }
+            return;
+          }
+        case OpCodes.Average: {
+            CompileInstructions(il, state, ds);
+            for (int i = 1; i < nArgs; i++) {
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Add);
+            }
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, nArgs);
+            il.Emit(System.Reflection.Emit.OpCodes.Div);
+            return;
+          }
+        case OpCodes.Cos: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, cos);
+            return;
+          }
+        case OpCodes.Sin: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, sin);
+            return;
+          }
+        case OpCodes.Tan: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, tan);
+            return;
+          }
+        case OpCodes.Power: {
+            CompileInstructions(il, state, ds);
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, round);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
+            return;
+          }
+        case OpCodes.Root: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1 / round(...)
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, round);
+            il.Emit(System.Reflection.Emit.OpCodes.Div);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
+            return;
+          }
+        case OpCodes.Exp: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, exp);
+            return;
+          }
+        case OpCodes.Log: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, log);
+            return;
+          }
+        case OpCodes.Square: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
+            return;
+          }
+        case OpCodes.SquareRoot: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt);
+            return;
+          }
+        case OpCodes.AiryA: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, airyA);
+            return;
+          }
+        case OpCodes.AiryB: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, airyB);
+            return;
+          }
+        case OpCodes.Bessel: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, bessel);
+            return;
+          }
+        case OpCodes.CosineIntegral: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, cosIntegral);
+            return;
+          }
+        case OpCodes.Dawson: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, dawson);
+            return;
+          }
+        case OpCodes.Erf: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, erf);
+            return;
+          }
+        case OpCodes.ExponentialIntegralEi: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, expIntegralEi);
+            return;
+          }
+        case OpCodes.FresnelCosineIntegral: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelCosIntegral);
+            return;
+          }
+        case OpCodes.FresnelSineIntegral: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelSinIntegral);
+            return;
+          }
+        case OpCodes.Gamma: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, gamma);
+            return;
+          }
+        case OpCodes.HyperbolicCosineIntegral: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, hypCosIntegral);
+            return;
+          }
+        case OpCodes.HyperbolicSineIntegral: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, hypSinIntegral);
+            return;
+          }
+        case OpCodes.Norm: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, norm);
+            return;
+          }
+        case OpCodes.Psi: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, psi);
+            return;
+          }
+        case OpCodes.SineIntegral: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Call, sinIntegral);
+            return;
+          }
+        case OpCodes.IfThenElse: {
+            Label end = il.DefineLabel();
+            Label c1 = il.DefineLabel();
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
+            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
+            il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1);
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
+            il.MarkLabel(c1);
+            CompileInstructions(il, state, ds);
+            il.MarkLabel(end);
+            return;
+          }
+        case OpCodes.AND: {
+            Label falseBranch = il.DefineLabel();
+            Label end = il.DefineLabel();
+            CompileInstructions(il, state, ds);
+            for (int i = 1; i < nArgs; i++) {
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
+              il.Emit(System.Reflection.Emit.OpCodes.Cgt);
+              il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch);
+              CompileInstructions(il, state, ds);
+            }
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
+            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
+            il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1
+            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
+            il.MarkLabel(falseBranch);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // -1
+            il.Emit(System.Reflection.Emit.OpCodes.Neg);
+            il.MarkLabel(end);
+            return;
+          }
+        case OpCodes.OR: {
+            Label trueBranch = il.DefineLabel();
+            Label end = il.DefineLabel();
+            Label resultBranch = il.DefineLabel();
+            CompileInstructions(il, state, ds);
+            for (int i = 1; i < nArgs; i++) {
+              Label nextArgBranch = il.DefineLabel();
+              // complex definition because of special properties of NaN  
+              il.Emit(System.Reflection.Emit.OpCodes.Dup);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // <= 0        
+              il.Emit(System.Reflection.Emit.OpCodes.Ble, nextArgBranch);
+              il.Emit(System.Reflection.Emit.OpCodes.Br, resultBranch);
+              il.MarkLabel(nextArgBranch);
+              il.Emit(System.Reflection.Emit.OpCodes.Pop);
+              CompileInstructions(il, state, ds);
+            }
+            il.MarkLabel(resultBranch);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
+            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
+            il.Emit(System.Reflection.Emit.OpCodes.Brtrue, trueBranch);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // -1
+            il.Emit(System.Reflection.Emit.OpCodes.Neg);
+            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
+            il.MarkLabel(trueBranch);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1
+            il.MarkLabel(end);
+            return;
+          }
+        case OpCodes.NOT: {
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
+            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
+            il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
+            il.Emit(System.Reflection.Emit.OpCodes.Sub);
+            il.Emit(System.Reflection.Emit.OpCodes.Neg); // * -1
+            return;
+          }
+        case OpCodes.GT: {
+            CompileInstructions(il, state, ds);
+            CompileInstructions(il, state, ds);
+
+            il.Emit(System.Reflection.Emit.OpCodes.Cgt); // 1 (>) / 0 (otherwise)
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
+            il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
+            il.Emit(System.Reflection.Emit.OpCodes.Sub);
+            return;
+          }
+        case OpCodes.LT: {
+            CompileInstructions(il, state, ds);
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Clt);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
+            il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
+            il.Emit(System.Reflection.Emit.OpCodes.Sub);
+            return;
+          }
+        case OpCodes.TimeLag: {
+            LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
+            il.Emit(System.Reflection.Emit.OpCodes.Add);
+            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+            var prevLaggedContext = state.InLaggedContext;
+            state.InLaggedContext = true;
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
+            il.Emit(System.Reflection.Emit.OpCodes.Sub);
+            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+            state.InLaggedContext = prevLaggedContext;
+            return;
+          }
+        case OpCodes.Integral: {
+            int savedPc = state.ProgramCounter;
+            LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
+            il.Emit(System.Reflection.Emit.OpCodes.Add);
+            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+            var prevLaggedContext = state.InLaggedContext;
+            state.InLaggedContext = true;
+            CompileInstructions(il, state, ds);
+            for (int l = laggedTreeNode.Lag; l < 0; l++) {
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_1);
+              il.Emit(System.Reflection.Emit.OpCodes.Add);
+              il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+              state.ProgramCounter = savedPc;
+              CompileInstructions(il, state, ds);
+              il.Emit(System.Reflection.Emit.OpCodes.Add);
+            }
+            state.InLaggedContext = prevLaggedContext;
+            return;
+          }
+
+        //mkommend: derivate calculation taken from: 
+        //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
+        //one sided smooth differentiatior, N = 4
+        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
+        case OpCodes.Derivative: {
+            int savedPc = state.ProgramCounter;
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row --
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1);
+            il.Emit(System.Reflection.Emit.OpCodes.Add);
+            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+            state.ProgramCounter = savedPc;
+            var prevLaggedContext = state.InLaggedContext;
+            state.InLaggedContext = true;
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1
+            il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            il.Emit(System.Reflection.Emit.OpCodes.Add);
+
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -=2
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_2);
+            il.Emit(System.Reflection.Emit.OpCodes.Sub);
+            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+            state.ProgramCounter = savedPc;
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1 - 2 * f_3
+            il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            il.Emit(System.Reflection.Emit.OpCodes.Sub);
+
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row --
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1);
+            il.Emit(System.Reflection.Emit.OpCodes.Add);
+            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+            state.ProgramCounter = savedPc;
+            CompileInstructions(il, state, ds);
+            il.Emit(System.Reflection.Emit.OpCodes.Sub); // f_0 + 2 * f_1 - 2 * f_3 - f_4
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 8.0); // / 8
+            il.Emit(System.Reflection.Emit.OpCodes.Div);
+
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row +=4
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_4);
+            il.Emit(System.Reflection.Emit.OpCodes.Add);
+            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
+            state.InLaggedContext = prevLaggedContext;
+            return;
+          }
+        case OpCodes.Call: {
+            throw new NotSupportedException(
+              "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
+          }
+        case OpCodes.Arg: {
+            throw new NotSupportedException(
+              "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
+          }
+        case OpCodes.Variable: {
+            VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode;
+            if (!state.InLaggedContext) {
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0);
+              // load correct column of the current variable
+              il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
+              il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
+              il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            } else {
+              var nanResult = il.DefineLabel();
+              var normalResult = il.DefineLabel();
+              var cachedValue = il.DefineLabel();
+              var multiplyValue = il.DefineLabel();
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
+              il.Emit(System.Reflection.Emit.OpCodes.Dup);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
+              il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult);
+              il.Emit(System.Reflection.Emit.OpCodes.Dup);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows);
+              il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3);
+              il.Emit(System.Reflection.Emit.OpCodes.Bge, cachedValue);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
+              il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
+              il.Emit(System.Reflection.Emit.OpCodes.Br, multiplyValue);
+              il.MarkLabel(cachedValue);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); // load cached values array
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
+              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startRow
+              il.Emit(System.Reflection.Emit.OpCodes.Sub); // startRow
+              il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
+              il.MarkLabel(multiplyValue);
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
+              il.Emit(System.Reflection.Emit.OpCodes.Mul);
+              il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);
+              il.MarkLabel(nanResult);
+              il.Emit(System.Reflection.Emit.OpCodes.Pop); // rowIndex
+              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN);
+              il.MarkLabel(normalResult);
+            }
+            return;
+          }
+        case OpCodes.LagVariable: {
+            var nanResult = il.DefineLabel();
+            var normalResult = il.DefineLabel();
+            var cachedValue = il.DefineLabel();
+            var multiplyValue = il.DefineLabel();
+            LaggedVariableTreeNode varNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
+            il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset
+            il.Emit(System.Reflection.Emit.OpCodes.Dup);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
+            il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult);
+            il.Emit(System.Reflection.Emit.OpCodes.Dup);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows);
+            il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startindex
+            il.Emit(System.Reflection.Emit.OpCodes.Bge, cachedValue);
+            // normal value
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable
+            il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
+            il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset
+            il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
+            il.Emit(System.Reflection.Emit.OpCodes.Br, multiplyValue);
+            il.MarkLabel(cachedValue);
+            // cached value
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); // load cached values
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable
+            il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
+            il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset
+            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startRow
+            il.Emit(System.Reflection.Emit.OpCodes.Sub); // startRow            
+            il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
+
+            il.MarkLabel(multiplyValue);
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
+            il.Emit(System.Reflection.Emit.OpCodes.Mul);
+            il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);
+            il.MarkLabel(nanResult);
+            il.Emit(System.Reflection.Emit.OpCodes.Pop); // pop the row index
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN);
+            il.MarkLabel(normalResult);
+            return;
+          }
+        case OpCodes.Constant: {
+            ConstantTreeNode constNode = (ConstantTreeNode)currentInstr.dynamicNode;
+            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, constNode.Value);
+            return;
+          }
+
+        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 
+        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
+        case OpCodes.VariableCondition: {
+            throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name +
+                                            " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter");
+          }
+        default:
+          throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name +
+                                          " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter");
+      }
+    }
+
+    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;
+      double ai, aip, bi, bip;
+      alglib.airy(x, out ai, out aip, out bi, out bip);
+      return ai;
+    }
+
+    public static double AiryB(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      double ai, aip, bi, bip;
+      alglib.airy(x, out ai, out aip, out bi, out bip);
+      return bi;
+    }
+    public static double Dawson(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      return alglib.dawsonintegral(x);
+    }
+
+    public static double Gamma(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      return alglib.gammafunction(x);
+    }
+
+    public static double Psi(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
+      return alglib.psi(x);
+    }
+
+    public static double ExpIntegralEi(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      return alglib.exponentialintegralei(x);
+    }
+
+    public static double SinIntegral(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      double si, ci;
+      alglib.sinecosineintegrals(x, out si, out ci);
+      return si;
+    }
+
+    public static double CosIntegral(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      double si, ci;
+      alglib.sinecosineintegrals(x, out si, out ci);
+      return ci;
+    }
+
+    public static double HypSinIntegral(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      double shi, chi;
+      alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
+      return shi;
+    }
+
+    public static double HypCosIntegral(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      double shi, chi;
+      alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
+      return chi;
+    }
+
+    public static double FresnelCosIntegral(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      double c = 0, s = 0;
+      alglib.fresnelintegral(x, ref c, ref s);
+      return c;
+    }
+
+    public static double FresnelSinIntegral(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      double c = 0, s = 0;
+      alglib.fresnelintegral(x, ref c, ref s);
+      return s;
+    }
+
+    public static double Norm(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      return alglib.normaldistribution(x);
+    }
+
+    public static double Erf(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      return alglib.errorfunction(x);
+    }
+
+    public static double Bessel(double x) {
+      if (double.IsNaN(x)) return double.NaN;
+      return alglib.besseli0(x);
+    }
+
+  }
+}
Index: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/SymbolicDataAnalysisExpressionTreeInterpreter.cs
===================================================================
--- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/SymbolicDataAnalysisExpressionTreeInterpreter.cs	(revision 8431)
+++ branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interfaces/SymbolicDataAnalysisExpressionTreeInterpreter.cs	(revision 8431)
@@ -0,0 +1,666 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Parameters;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
+  [StorableClass]
+  [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
+  public sealed class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem,
+    ISymbolicDataAnalysisExpressionTreeInterpreter, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter {
+    private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
+    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
+    #region private classes
+    private class InterpreterState {
+      private double[] argumentStack;
+      private int argumentStackPointer;
+      private Instruction[] code;
+      private int pc;
+      public int ProgramCounter {
+        get { return pc; }
+        set { pc = value; }
+      }
+      internal InterpreterState(Instruction[] code, int argumentStackSize) {
+        this.code = code;
+        this.pc = 0;
+        if (argumentStackSize > 0) {
+          this.argumentStack = new double[argumentStackSize];
+        }
+        this.argumentStackPointer = 0;
+      }
+
+      internal void Reset() {
+        this.pc = 0;
+        this.argumentStackPointer = 0;
+      }
+
+      internal Instruction NextInstruction() {
+        return code[pc++];
+      }
+      private void Push(double val) {
+        argumentStack[argumentStackPointer++] = val;
+      }
+      private double Pop() {
+        return argumentStack[--argumentStackPointer];
+      }
+
+      internal void CreateStackFrame(double[] argValues) {
+        // push in reverse order to make indexing easier
+        for (int i = argValues.Length - 1; i >= 0; i--) {
+          argumentStack[argumentStackPointer++] = argValues[i];
+        }
+        Push(argValues.Length);
+      }
+
+      internal void RemoveStackFrame() {
+        int size = (int)Pop();
+        argumentStackPointer -= size;
+      }
+
+      internal double GetStackFrameValue(ushort index) {
+        // layout of stack:
+        // [0]   <- argumentStackPointer
+        // [StackFrameSize = N + 1]
+        // [Arg0] <- argumentStackPointer - 2 - 0
+        // [Arg1] <- argumentStackPointer - 2 - 1
+        // [...]
+        // [ArgN] <- argumentStackPointer - 2 - N
+        // <Begin of stack frame>
+        return argumentStack[argumentStackPointer - index - 2];
+      }
+    }
+    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(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 {
+      get { return false; }
+    }
+    public override bool CanChangeDescription {
+      get { return false; }
+    }
+
+    #region parameter properties
+    public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
+      get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
+    }
+
+    public IValueParameter<IntValue> EvaluatedSolutionsParameter {
+      get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
+    }
+    #endregion
+
+    #region properties
+    public BoolValue CheckExpressionsWithIntervalArithmetic {
+      get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
+      set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
+    }
+
+    public IntValue EvaluatedSolutions {
+      get { return EvaluatedSolutionsParameter.Value; }
+      set { EvaluatedSolutionsParameter.Value = value; }
+    }
+    #endregion
+
+    [StorableConstructor]
+    private SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
+    private SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
+    }
+
+    public SymbolicDataAnalysisExpressionTreeInterpreter()
+      : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
+      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)));
+    }
+
+    [StorableHook(HookType.AfterDeserialization)]
+    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)));
+    }
+
+    #region IStatefulItem
+    public void InitializeState() {
+      EvaluatedSolutions.Value = 0;
+    }
+
+    public void ClearState() {
+    }
+    #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) {
+      if (CheckExpressionsWithIntervalArithmetic.Value)
+        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);
+      int necessaryArgStackSize = 0;
+      for (int i = 0; i < code.Length; i++) {
+        Instruction instr = code[i];
+        if (instr.opCode == OpCodes.Variable) {
+          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
+          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
+          code[i] = instr;
+        } else if (instr.opCode == OpCodes.LagVariable) {
+          var laggedVariableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
+          instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
+          code[i] = instr;
+        } else if (instr.opCode == OpCodes.VariableCondition) {
+          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
+          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
+        } else if (instr.opCode == OpCodes.Call) {
+          necessaryArgStackSize += instr.nArguments + 1;
+        }
+      }
+      var state = new InterpreterState(code, necessaryArgStackSize);
+
+      int nComponents = tree.Root.GetSubtree(0).SubtreeCount;
+      // produce a n-step forecast for each target variable for all rows
+      var cachedPrognosedValues = new Dictionary<string, double[]>();
+      foreach (var targetVariable in targetVariables)
+        cachedPrognosedValues[targetVariable] = new double[horizon];
+      foreach (var rowEnum in rows) {
+        int row = rowEnum;
+        foreach (var horizonRow in Enumerable.Range(row, horizon)) {
+          int localRow = horizonRow; // create a local variable for the ref parameter
+          for (int c = 0; c < nComponents; c++) {
+            var prog = Evaluate(dataset, ref localRow, row - 1, state, cachedPrognosedValues);
+            yield return prog;
+            cachedPrognosedValues[targetVariables[c]][horizonRow - row] = prog;
+          }
+
+          state.Reset();
+        }
+      }
+    }
+
+    private double Evaluate(Dataset dataset, ref int row, int lastObservedRow, InterpreterState state, Dictionary<string, double[]> cachedPrognosedValues) {
+      Instruction currentInstr = state.NextInstruction();
+      switch (currentInstr.opCode) {
+        case OpCodes.Add: {
+            double s = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            for (int i = 1; i < currentInstr.nArguments; i++) {
+              s += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            }
+            return s;
+          }
+        case OpCodes.Sub: {
+            double s = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            for (int i = 1; i < currentInstr.nArguments; i++) {
+              s -= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            }
+            if (currentInstr.nArguments == 1) s = -s;
+            return s;
+          }
+        case OpCodes.Mul: {
+            double p = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            for (int i = 1; i < currentInstr.nArguments; i++) {
+              p *= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            }
+            return p;
+          }
+        case OpCodes.Div: {
+            double p = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            for (int i = 1; i < currentInstr.nArguments; i++) {
+              p /= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            }
+            if (currentInstr.nArguments == 1) p = 1.0 / p;
+            return p;
+          }
+        case OpCodes.Average: {
+            double sum = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            for (int i = 1; i < currentInstr.nArguments; i++) {
+              sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            }
+            return sum / currentInstr.nArguments;
+          }
+        case OpCodes.Cos: {
+            return Math.Cos(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+          }
+        case OpCodes.Sin: {
+            return Math.Sin(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+          }
+        case OpCodes.Tan: {
+            return Math.Tan(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+          }
+        case OpCodes.Square: {
+            return Math.Pow(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues), 2);
+          }
+        case OpCodes.Power: {
+            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            double y = Math.Round(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+            return Math.Pow(x, y);
+          }
+        case OpCodes.SquareRoot: {
+            return Math.Sqrt(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+          }
+        case OpCodes.Root: {
+            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            double y = Math.Round(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+            return Math.Pow(x, 1 / y);
+          }
+        case OpCodes.Exp: {
+            return Math.Exp(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+          }
+        case OpCodes.Log: {
+            return Math.Log(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
+          }
+        case OpCodes.Gamma: {
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else return alglib.gammafunction(x);
+          }
+        case OpCodes.Psi: {
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
+            return alglib.psi(x);
+          }
+        case OpCodes.Dawson: {
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            return alglib.dawsonintegral(x);
+          }
+        case OpCodes.ExponentialIntegralEi: {
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            return alglib.exponentialintegralei(x);
+          }
+        case OpCodes.SineIntegral: {
+            double si, ci;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.sinecosineintegrals(x, out si, out ci);
+              return si;
+            }
+          }
+        case OpCodes.CosineIntegral: {
+            double si, ci;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.sinecosineintegrals(x, out si, out ci);
+              return ci;
+            }
+          }
+        case OpCodes.HyperbolicSineIntegral: {
+            double shi, chi;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
+              return shi;
+            }
+          }
+        case OpCodes.HyperbolicCosineIntegral: {
+            double shi, chi;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
+              return chi;
+            }
+          }
+        case OpCodes.FresnelCosineIntegral: {
+            double c = 0, s = 0;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.fresnelintegral(x, ref c, ref s);
+              return c;
+            }
+          }
+        case OpCodes.FresnelSineIntegral: {
+            double c = 0, s = 0;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.fresnelintegral(x, ref c, ref s);
+              return s;
+            }
+          }
+        case OpCodes.AiryA: {
+            double ai, aip, bi, bip;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.airy(x, out ai, out aip, out bi, out bip);
+              return ai;
+            }
+          }
+        case OpCodes.AiryB: {
+            double ai, aip, bi, bip;
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else {
+              alglib.airy(x, out ai, out aip, out bi, out bip);
+              return bi;
+            }
+          }
+        case OpCodes.Norm: {
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else return alglib.normaldistribution(x);
+          }
+        case OpCodes.Erf: {
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else return alglib.errorfunction(x);
+          }
+        case OpCodes.Bessel: {
+            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (double.IsNaN(x)) return double.NaN;
+            else return alglib.besseli0(x);
+          }
+        case OpCodes.IfThenElse: {
+            double condition = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            double result;
+            if (condition > 0.0) {
+              result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); SkipInstructions(state);
+            } else {
+              SkipInstructions(state); result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            }
+            return result;
+          }
+        case OpCodes.AND: {
+            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            for (int i = 1; i < currentInstr.nArguments; i++) {
+              if (result > 0.0) result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+              else {
+                SkipInstructions(state);
+              }
+            }
+            return result > 0.0 ? 1.0 : -1.0;
+          }
+        case OpCodes.OR: {
+            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            for (int i = 1; i < currentInstr.nArguments; i++) {
+              if (result <= 0.0) result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+              else {
+                SkipInstructions(state);
+              }
+            }
+            return result > 0.0 ? 1.0 : -1.0;
+          }
+        case OpCodes.NOT: {
+            return Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues) > 0.0 ? -1.0 : 1.0;
+          }
+        case OpCodes.GT: {
+            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            double y = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (x > y) return 1.0;
+            else return -1.0;
+          }
+        case OpCodes.LT: {
+            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            double y = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            if (x < y) return 1.0;
+            else return -1.0;
+          }
+        case OpCodes.TimeLag: {
+            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
+            row += timeLagTreeNode.Lag;
+            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            row -= timeLagTreeNode.Lag;
+            return result;
+          }
+        case OpCodes.Integral: {
+            int savedPc = state.ProgramCounter;
+            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
+            double sum = 0.0;
+            for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) {
+              row += Math.Sign(timeLagTreeNode.Lag);
+              sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+              state.ProgramCounter = savedPc;
+            }
+            row -= timeLagTreeNode.Lag;
+            sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            return sum;
+          }
+
+        //mkommend: derivate calculation taken from: 
+        //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
+        //one sided smooth differentiatior, N = 4
+        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
+        case OpCodes.Derivative: {
+            int savedPc = state.ProgramCounter;
+            double f_0 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row--;
+            state.ProgramCounter = savedPc;
+            double f_1 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row -= 2;
+            state.ProgramCounter = savedPc;
+            double f_3 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row--;
+            state.ProgramCounter = savedPc;
+            double f_4 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            row += 4;
+
+            return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
+          }
+        case OpCodes.Call: {
+            // evaluate sub-trees
+            double[] argValues = new double[currentInstr.nArguments];
+            for (int i = 0; i < currentInstr.nArguments; i++) {
+              argValues[i] = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            }
+            // push on argument values on stack 
+            state.CreateStackFrame(argValues);
+
+            // save the pc
+            int savedPc = state.ProgramCounter;
+            // set pc to start of function  
+            state.ProgramCounter = (ushort)currentInstr.iArg0;
+            // evaluate the function
+            double v = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+
+            // delete the stack frame
+            state.RemoveStackFrame();
+
+            // restore the pc => evaluation will continue at point after my subtrees  
+            state.ProgramCounter = savedPc;
+            return v;
+          }
+        case OpCodes.Arg: {
+            return state.GetStackFrameValue((ushort)currentInstr.iArg0);
+          }
+        case OpCodes.Variable: {
+            if (row < 0 || row >= dataset.Rows)
+              return double.NaN;
+            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
+            if (row <= lastObservedRow || !cachedPrognosedValues.ContainsKey(variableTreeNode.VariableName)) return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
+            else return cachedPrognosedValues[variableTreeNode.VariableName][row - lastObservedRow - 1] * variableTreeNode.Weight;
+          }
+        case OpCodes.LagVariable: {
+            var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
+            int actualRow = row + laggedVariableTreeNode.Lag;
+            if (actualRow < 0 || actualRow >= dataset.Rows)
+              return double.NaN;
+            if (actualRow <= lastObservedRow || !cachedPrognosedValues.ContainsKey(laggedVariableTreeNode.VariableName)) return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
+            else return cachedPrognosedValues[laggedVariableTreeNode.VariableName][actualRow - lastObservedRow - 1] * laggedVariableTreeNode.Weight;
+          }
+        case OpCodes.Constant: {
+            var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
+            return constTreeNode.Value;
+          }
+
+        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 
+        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
+        case OpCodes.VariableCondition: {
+            if (row < 0 || row >= dataset.Rows)
+              return double.NaN;
+            var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
+            double variableValue;
+            if (row <= lastObservedRow || !cachedPrognosedValues.ContainsKey(variableConditionTreeNode.VariableName))
+              variableValue = ((IList<double>)currentInstr.iArg0)[row];
+            else
+              variableValue = cachedPrognosedValues[variableConditionTreeNode.VariableName][row - lastObservedRow - 1];
+
+            double x = variableValue - variableConditionTreeNode.Threshold;
+            double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
+
+            double trueBranch = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+            double falseBranch = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
+
+            return trueBranch * p + falseBranch * (1 - p);
+          }
+        default: throw new NotSupportedException();
+      }
+    }
+
+    private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
+      byte opCode;
+      if (!symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out opCode))
+        throw new NotSupportedException("Symbol: " + treeNode.Symbol);
+      return opCode;
+    }
+
+    // skips a whole branch
+    private void SkipInstructions(InterpreterState state) {
+      int i = 1;
+      while (i > 0) {
+        i += state.NextInstruction().nArguments;
+        i--;
+      }
+    }
+  }
+}
Index: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/InterpreterState.cs
===================================================================
--- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/InterpreterState.cs	(revision 8431)
+++ branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/InterpreterState.cs	(revision 8431)
@@ -0,0 +1,92 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
+  public class InterpreterState {
+    private double[] argumentStack;
+    private int argumentStackPointer;
+    private Instruction[] code;
+    private int pc;
+    public int ProgramCounter {
+      get { return pc; }
+      set { pc = value; }
+    }
+    public InterpreterState(Instruction[] code, int argumentStackSize) {
+      this.code = code;
+      this.pc = 0;
+      if (argumentStackSize > 0) {
+        this.argumentStack = new double[argumentStackSize];
+      }
+      this.argumentStackPointer = 0;
+    }
+
+    public void Reset() {
+      this.pc = 0;
+      this.argumentStackPointer = 0;
+    }
+
+    public Instruction NextInstruction() {
+      return code[pc++];
+    }
+    // skips a whole branch
+    public void SkipInstructions() {
+      int i = 1;
+      while (i > 0) {
+        i += NextInstruction().nArguments;
+        i--;
+      }
+    }
+
+    private void Push(double val) {
+      argumentStack[argumentStackPointer++] = val;
+    }
+    private double Pop() {
+      return argumentStack[--argumentStackPointer];
+    }
+
+    public void CreateStackFrame(double[] argValues) {
+      // push in reverse order to make indexing easier
+      for (int i = argValues.Length - 1; i >= 0; i--) {
+        argumentStack[argumentStackPointer++] = argValues[i];
+      }
+      Push(argValues.Length);
+    }
+
+    public void RemoveStackFrame() {
+      int size = (int)Pop();
+      argumentStackPointer -= size;
+    }
+
+    public double GetStackFrameValue(ushort index) {
+      // layout of stack:
+      // [0]   <- argumentStackPointer
+      // [StackFrameSize = N + 1]
+      // [Arg0] <- argumentStackPointer - 2 - 0
+      // [Arg1] <- argumentStackPointer - 2 - 1
+      // [...]
+      // [ArgN] <- argumentStackPointer - 2 - N
+      // <Begin of stack frame>
+      return argumentStack[argumentStackPointer - index - 2];
+    }
+  }
+}
Index: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs
===================================================================
--- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs	(revision 8431)
+++ branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/OpCodes.cs	(revision 8431)
@@ -0,0 +1,138 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Collections.Generic;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
+  public static 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;
+
+    private static 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(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 static byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
+      byte opCode;
+      if (symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out opCode)) return opCode;
+      else throw new NotSupportedException("Symbol: " + treeNode.Symbol);
+    }
+  }
+}
Index: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs
===================================================================
--- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeILEmittingInterpreter.cs	(revision 8430)
+++ 	(revision )
@@ -1,967 +1,0 @@
-﻿#region License Information
-/* HeuristicLab
- * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
- *
- * This file is part of HeuristicLab.
- *
- * HeuristicLab is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * HeuristicLab is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
- */
-#endregion
-
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Reflection;
-using System.Reflection.Emit;
-using HeuristicLab.Common;
-using HeuristicLab.Core;
-using HeuristicLab.Data;
-using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
-using HeuristicLab.Parameters;
-using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
-
-namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
-  [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();
-
-    private static MethodInfo cos = typeof(Math).GetMethod("Cos", new Type[] { typeof(double) });
-    private static MethodInfo sin = typeof(Math).GetMethod("Sin", new Type[] { typeof(double) });
-    private static MethodInfo tan = typeof(Math).GetMethod("Tan", new Type[] { typeof(double) });
-    private static MethodInfo exp = typeof(Math).GetMethod("Exp", new Type[] { typeof(double) });
-    private static MethodInfo log = typeof(Math).GetMethod("Log", new Type[] { typeof(double) });
-    private static MethodInfo power = typeof(Math).GetMethod("Pow", new Type[] { typeof(double), typeof(double) });
-    private static MethodInfo round = typeof(Math).GetMethod("Round", new Type[] { typeof(double) });
-    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) });
-    private static MethodInfo gamma = thisType.GetMethod("Gamma", new Type[] { typeof(double) });
-    private static MethodInfo psi = thisType.GetMethod("Psi", new Type[] { typeof(double) });
-    private static MethodInfo dawson = thisType.GetMethod("Dawson", new Type[] { typeof(double) });
-    private static MethodInfo expIntegralEi = thisType.GetMethod("ExpIntegralEi", new Type[] { typeof(double) });
-    private static MethodInfo sinIntegral = thisType.GetMethod("SinIntegral", new Type[] { typeof(double) });
-    private static MethodInfo cosIntegral = thisType.GetMethod("CosIntegral", new Type[] { typeof(double) });
-    private static MethodInfo hypSinIntegral = thisType.GetMethod("HypSinIntegral", new Type[] { typeof(double) });
-    private static MethodInfo hypCosIntegral = thisType.GetMethod("HypCosIntegral", new Type[] { typeof(double) });
-    private static MethodInfo fresnelCosIntegral = thisType.GetMethod("FresnelCosIntegral", new Type[] { typeof(double) });
-    private static MethodInfo fresnelSinIntegral = thisType.GetMethod("FresnelSinIntegral", new Type[] { typeof(double) });
-    private static MethodInfo norm = thisType.GetMethod("Norm", new Type[] { typeof(double) });
-    private static MethodInfo erf = thisType.GetMethod("Erf", new Type[] { typeof(double) });
-    private static MethodInfo bessel = thisType.GetMethod("Bessel", new Type[] { typeof(double) });
-
-    
-
-    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 {
-      get { return false; }
-    }
-
-    public override bool CanChangeDescription {
-      get { return false; }
-    }
-
-    #region parameter properties
-
-    public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
-      get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
-    }
-
-    public IValueParameter<IntValue> EvaluatedSolutionsParameter {
-      get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
-    }
-
-    #endregion
-
-    #region properties
-
-    public BoolValue CheckExpressionsWithIntervalArithmetic {
-      get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
-      set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
-    }
-
-    public IntValue EvaluatedSolutions {
-      get { return EvaluatedSolutionsParameter.Value; }
-      set { EvaluatedSolutionsParameter.Value = value; }
-    }
-
-    #endregion
-
-
-    [StorableConstructor]
-    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);
-    }
-
-    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)));
-    }
-
-    [StorableHook(HookType.AfterDeserialization)]
-    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)));
-    }
-
-    #region IStatefulItem
-
-    public void InitializeState() {
-      EvaluatedSolutions.Value = 0;
-    }
-
-    public void ClearState() {
-      EvaluatedSolutions.Value = 0;
-    }
-
-    #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) {
-      if (CheckExpressionsWithIntervalArithmetic.Value)
-        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);
-      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];
-        if (instr.opCode == OpCodes.Variable) {
-          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
-          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;
-        } else if (instr.opCode == OpCodes.VariableCondition) {
-          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
-          instr.iArg0 = doubleVariableNames[variableConditionTreeNode.VariableName];
-        } else if (instr.opCode == OpCodes.Call) {
-          necessaryArgStackSize += instr.nArguments + 1;
-        }
-      }
-      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;
-          }
-        }
-      }
-    }
-
-    private void CompileInstructions(ILGenerator il, InterpreterState state, Dataset ds) {
-      Instruction currentInstr = state.NextInstruction();
-      int nArgs = currentInstr.nArguments;
-
-      switch (currentInstr.opCode) {
-        case OpCodes.Add: {
-            if (nArgs > 0) {
-              CompileInstructions(il, state, ds);
-            }
-            for (int i = 1; i < nArgs; i++) {
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Add);
-            }
-            return;
-          }
-        case OpCodes.Sub: {
-            if (nArgs == 1) {
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Neg);
-              return;
-            }
-            if (nArgs > 0) {
-              CompileInstructions(il, state, ds);
-            }
-            for (int i = 1; i < nArgs; i++) {
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Sub);
-            }
-            return;
-          }
-        case OpCodes.Mul: {
-            if (nArgs > 0) {
-              CompileInstructions(il, state, ds);
-            }
-            for (int i = 1; i < nArgs; i++) {
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            }
-            return;
-          }
-        case OpCodes.Div: {
-            if (nArgs == 1) {
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0);
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Div);
-              return;
-            }
-            if (nArgs > 0) {
-              CompileInstructions(il, state, ds);
-            }
-            for (int i = 1; i < nArgs; i++) {
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Div);
-            }
-            return;
-          }
-        case OpCodes.Average: {
-            CompileInstructions(il, state, ds);
-            for (int i = 1; i < nArgs; i++) {
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Add);
-            }
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, nArgs);
-            il.Emit(System.Reflection.Emit.OpCodes.Div);
-            return;
-          }
-        case OpCodes.Cos: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, cos);
-            return;
-          }
-        case OpCodes.Sin: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, sin);
-            return;
-          }
-        case OpCodes.Tan: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, tan);
-            return;
-          }
-        case OpCodes.Power: {
-            CompileInstructions(il, state, ds);
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, round);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
-            return;
-          }
-        case OpCodes.Root: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1 / round(...)
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, round);
-            il.Emit(System.Reflection.Emit.OpCodes.Div);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
-            return;
-          }
-        case OpCodes.Exp: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, exp);
-            return;
-          }
-        case OpCodes.Log: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, log);
-            return;
-          }
-        case OpCodes.Square: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, power);
-            return;
-          }
-        case OpCodes.SquareRoot: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, sqrt);
-            return;
-          }
-        case OpCodes.AiryA: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, airyA);
-            return;
-          }
-        case OpCodes.AiryB: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, airyB);
-            return;
-          }
-        case OpCodes.Bessel: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, bessel);
-            return;
-          }
-        case OpCodes.CosineIntegral: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, cosIntegral);
-            return;
-          }
-        case OpCodes.Dawson: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, dawson);
-            return;
-          }
-        case OpCodes.Erf: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, erf);
-            return;
-          }
-        case OpCodes.ExponentialIntegralEi: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, expIntegralEi);
-            return;
-          }
-        case OpCodes.FresnelCosineIntegral: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelCosIntegral);
-            return;
-          }
-        case OpCodes.FresnelSineIntegral: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, fresnelSinIntegral);
-            return;
-          }
-        case OpCodes.Gamma: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, gamma);
-            return;
-          }
-        case OpCodes.HyperbolicCosineIntegral: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, hypCosIntegral);
-            return;
-          }
-        case OpCodes.HyperbolicSineIntegral: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, hypSinIntegral);
-            return;
-          }
-        case OpCodes.Norm: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, norm);
-            return;
-          }
-        case OpCodes.Psi: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, psi);
-            return;
-          }
-        case OpCodes.SineIntegral: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Call, sinIntegral);
-            return;
-          }
-        case OpCodes.IfThenElse: {
-            Label end = il.DefineLabel();
-            Label c1 = il.DefineLabel();
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
-            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
-            il.Emit(System.Reflection.Emit.OpCodes.Brfalse, c1);
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
-            il.MarkLabel(c1);
-            CompileInstructions(il, state, ds);
-            il.MarkLabel(end);
-            return;
-          }
-        case OpCodes.AND: {
-            Label falseBranch = il.DefineLabel();
-            Label end = il.DefineLabel();
-            CompileInstructions(il, state, ds);
-            for (int i = 1; i < nArgs; i++) {
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
-              il.Emit(System.Reflection.Emit.OpCodes.Cgt);
-              il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch);
-              CompileInstructions(il, state, ds);
-            }
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
-            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
-            il.Emit(System.Reflection.Emit.OpCodes.Brfalse, falseBranch);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1
-            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
-            il.MarkLabel(falseBranch);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // -1
-            il.Emit(System.Reflection.Emit.OpCodes.Neg);
-            il.MarkLabel(end);
-            return;
-          }
-        case OpCodes.OR: {
-            Label trueBranch = il.DefineLabel();
-            Label end = il.DefineLabel();
-            Label resultBranch = il.DefineLabel();
-            CompileInstructions(il, state, ds);
-            for (int i = 1; i < nArgs; i++) {
-              Label nextArgBranch = il.DefineLabel();
-              // complex definition because of special properties of NaN  
-              il.Emit(System.Reflection.Emit.OpCodes.Dup);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // <= 0        
-              il.Emit(System.Reflection.Emit.OpCodes.Ble, nextArgBranch);
-              il.Emit(System.Reflection.Emit.OpCodes.Br, resultBranch);
-              il.MarkLabel(nextArgBranch);
-              il.Emit(System.Reflection.Emit.OpCodes.Pop);
-              CompileInstructions(il, state, ds);
-            }
-            il.MarkLabel(resultBranch);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
-            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
-            il.Emit(System.Reflection.Emit.OpCodes.Brtrue, trueBranch);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // -1
-            il.Emit(System.Reflection.Emit.OpCodes.Neg);
-            il.Emit(System.Reflection.Emit.OpCodes.Br, end);
-            il.MarkLabel(trueBranch);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // 1
-            il.MarkLabel(end);
-            return;
-          }
-        case OpCodes.NOT: {
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0); // > 0
-            il.Emit(System.Reflection.Emit.OpCodes.Cgt);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
-            il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
-            il.Emit(System.Reflection.Emit.OpCodes.Sub);
-            il.Emit(System.Reflection.Emit.OpCodes.Neg); // * -1
-            return;
-          }
-        case OpCodes.GT: {
-            CompileInstructions(il, state, ds);
-            CompileInstructions(il, state, ds);
-
-            il.Emit(System.Reflection.Emit.OpCodes.Cgt); // 1 (>) / 0 (otherwise)
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
-            il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
-            il.Emit(System.Reflection.Emit.OpCodes.Sub);
-            return;
-          }
-        case OpCodes.LT: {
-            CompileInstructions(il, state, ds);
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Clt);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // * 2
-            il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 1.0); // - 1
-            il.Emit(System.Reflection.Emit.OpCodes.Sub);
-            return;
-          }
-        case OpCodes.TimeLag: {
-            LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
-            il.Emit(System.Reflection.Emit.OpCodes.Add);
-            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-            var prevLaggedContext = state.InLaggedContext;
-            state.InLaggedContext = true;
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
-            il.Emit(System.Reflection.Emit.OpCodes.Sub);
-            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-            state.InLaggedContext = prevLaggedContext;
-            return;
-          }
-        case OpCodes.Integral: {
-            int savedPc = state.ProgramCounter;
-            LaggedTreeNode laggedTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -= lag
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, laggedTreeNode.Lag);
-            il.Emit(System.Reflection.Emit.OpCodes.Add);
-            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-            var prevLaggedContext = state.InLaggedContext;
-            state.InLaggedContext = true;
-            CompileInstructions(il, state, ds);
-            for (int l = laggedTreeNode.Lag; l < 0; l++) {
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row += lag
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_1);
-              il.Emit(System.Reflection.Emit.OpCodes.Add);
-              il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-              state.ProgramCounter = savedPc;
-              CompileInstructions(il, state, ds);
-              il.Emit(System.Reflection.Emit.OpCodes.Add);
-            }
-            state.InLaggedContext = prevLaggedContext;
-            return;
-          }
-
-        //mkommend: derivate calculation taken from: 
-        //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
-        //one sided smooth differentiatior, N = 4
-        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
-        case OpCodes.Derivative: {
-            int savedPc = state.ProgramCounter;
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row --
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1);
-            il.Emit(System.Reflection.Emit.OpCodes.Add);
-            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-            state.ProgramCounter = savedPc;
-            var prevLaggedContext = state.InLaggedContext;
-            state.InLaggedContext = true;
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1
-            il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            il.Emit(System.Reflection.Emit.OpCodes.Add);
-
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row -=2
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_2);
-            il.Emit(System.Reflection.Emit.OpCodes.Sub);
-            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-            state.ProgramCounter = savedPc;
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 2.0); // f_0 + 2 * f_1 - 2 * f_3
-            il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            il.Emit(System.Reflection.Emit.OpCodes.Sub);
-
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row --
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_M1);
-            il.Emit(System.Reflection.Emit.OpCodes.Add);
-            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-            state.ProgramCounter = savedPc;
-            CompileInstructions(il, state, ds);
-            il.Emit(System.Reflection.Emit.OpCodes.Sub); // f_0 + 2 * f_1 - 2 * f_3 - f_4
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, 8.0); // / 8
-            il.Emit(System.Reflection.Emit.OpCodes.Div);
-
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // row +=4
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_4);
-            il.Emit(System.Reflection.Emit.OpCodes.Add);
-            il.Emit(System.Reflection.Emit.OpCodes.Starg, 0);
-            state.InLaggedContext = prevLaggedContext;
-            return;
-          }
-        case OpCodes.Call: {
-            throw new NotSupportedException(
-              "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
-          }
-        case OpCodes.Arg: {
-            throw new NotSupportedException(
-              "Automatically defined functions are not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter. Either turn of ADFs or change the interpeter.");
-          }
-        case OpCodes.Variable: {
-            VariableTreeNode varNode = (VariableTreeNode)currentInstr.dynamicNode;
-            if (!state.InLaggedContext) {
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0);
-              // load correct column of the current variable
-              il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
-              il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
-              il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            } else {
-              var nanResult = il.DefineLabel();
-              var normalResult = il.DefineLabel();
-              var cachedValue = il.DefineLabel();
-              var multiplyValue = il.DefineLabel();
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
-              il.Emit(System.Reflection.Emit.OpCodes.Dup);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
-              il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult);
-              il.Emit(System.Reflection.Emit.OpCodes.Dup);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows);
-              il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3);
-              il.Emit(System.Reflection.Emit.OpCodes.Bge, cachedValue);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
-              il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
-              il.Emit(System.Reflection.Emit.OpCodes.Br, multiplyValue);
-              il.MarkLabel(cachedValue);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); // load cached values array
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
-              il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startRow
-              il.Emit(System.Reflection.Emit.OpCodes.Sub); // startRow
-              il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
-              il.MarkLabel(multiplyValue);
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
-              il.Emit(System.Reflection.Emit.OpCodes.Mul);
-              il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);
-              il.MarkLabel(nanResult);
-              il.Emit(System.Reflection.Emit.OpCodes.Pop); // rowIndex
-              il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN);
-              il.MarkLabel(normalResult);
-            }
-            return;
-          }
-        case OpCodes.LagVariable: {
-            var nanResult = il.DefineLabel();
-            var normalResult = il.DefineLabel();
-            var cachedValue = il.DefineLabel();
-            var multiplyValue = il.DefineLabel();
-            LaggedVariableTreeNode varNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
-            il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset
-            il.Emit(System.Reflection.Emit.OpCodes.Dup);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4_0);
-            il.Emit(System.Reflection.Emit.OpCodes.Blt, nanResult);
-            il.Emit(System.Reflection.Emit.OpCodes.Dup);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, ds.Rows);
-            il.Emit(System.Reflection.Emit.OpCodes.Bge, nanResult);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startindex
-            il.Emit(System.Reflection.Emit.OpCodes.Bge, cachedValue);
-            // normal value
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_1); // load columns array
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable
-            il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
-            il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset
-            il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
-            il.Emit(System.Reflection.Emit.OpCodes.Br, multiplyValue);
-            il.MarkLabel(cachedValue);
-            // cached value
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_2); // load cached values
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, (int)currentInstr.iArg0); // load correct column of the current variable
-            il.Emit(System.Reflection.Emit.OpCodes.Ldelem_Ref);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_I4, varNode.Lag); // lag
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_0); // rowIndex
-            il.Emit(System.Reflection.Emit.OpCodes.Add); // actualRowIndex = rowIndex + sampleOffset
-            il.Emit(System.Reflection.Emit.OpCodes.Ldarg_3); // startRow
-            il.Emit(System.Reflection.Emit.OpCodes.Sub); // startRow            
-            il.Emit(System.Reflection.Emit.OpCodes.Call, listGetValue);
-
-            il.MarkLabel(multiplyValue);
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, varNode.Weight); // load weight
-            il.Emit(System.Reflection.Emit.OpCodes.Mul);
-            il.Emit(System.Reflection.Emit.OpCodes.Br, normalResult);
-            il.MarkLabel(nanResult);
-            il.Emit(System.Reflection.Emit.OpCodes.Pop); // pop the row index
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, double.NaN);
-            il.MarkLabel(normalResult);
-            return;
-          }
-        case OpCodes.Constant: {
-            ConstantTreeNode constNode = (ConstantTreeNode)currentInstr.dynamicNode;
-            il.Emit(System.Reflection.Emit.OpCodes.Ldc_R8, constNode.Value);
-            return;
-          }
-
-        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 
-        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
-        case OpCodes.VariableCondition: {
-            throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name +
-                                            " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter");
-          }
-        default:
-          throw new NotSupportedException("Interpretation of symbol " + currentInstr.dynamicNode.Symbol.Name +
-                                          " is not supported by the SymbolicDataAnalysisTreeILEmittingInterpreter");
-      }
-    }
-
-    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;
-      double ai, aip, bi, bip;
-      alglib.airy(x, out ai, out aip, out bi, out bip);
-      return ai;
-    }
-
-    public static double AiryB(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      double ai, aip, bi, bip;
-      alglib.airy(x, out ai, out aip, out bi, out bip);
-      return bi;
-    }
-    public static double Dawson(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      return alglib.dawsonintegral(x);
-    }
-
-    public static double Gamma(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      return alglib.gammafunction(x);
-    }
-
-    public static double Psi(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
-      return alglib.psi(x);
-    }
-
-    public static double ExpIntegralEi(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      return alglib.exponentialintegralei(x);
-    }
-
-    public static double SinIntegral(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      double si, ci;
-      alglib.sinecosineintegrals(x, out si, out ci);
-      return si;
-    }
-
-    public static double CosIntegral(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      double si, ci;
-      alglib.sinecosineintegrals(x, out si, out ci);
-      return ci;
-    }
-
-    public static double HypSinIntegral(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      double shi, chi;
-      alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
-      return shi;
-    }
-
-    public static double HypCosIntegral(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      double shi, chi;
-      alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
-      return chi;
-    }
-
-    public static double FresnelCosIntegral(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      double c = 0, s = 0;
-      alglib.fresnelintegral(x, ref c, ref s);
-      return c;
-    }
-
-    public static double FresnelSinIntegral(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      double c = 0, s = 0;
-      alglib.fresnelintegral(x, ref c, ref s);
-      return s;
-    }
-
-    public static double Norm(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      return alglib.normaldistribution(x);
-    }
-
-    public static double Erf(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      return alglib.errorfunction(x);
-    }
-
-    public static double Bessel(double x) {
-      if (double.IsNaN(x)) return double.NaN;
-      return alglib.besseli0(x);
-    }
-
-  }
-}
Index: branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs
===================================================================
--- branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeInterpreter.cs	(revision 8430)
+++ 	(revision )
@@ -1,666 +1,0 @@
-﻿#region License Information
-/* HeuristicLab
- * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
- *
- * This file is part of HeuristicLab.
- *
- * HeuristicLab is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * HeuristicLab is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
- */
-#endregion
-
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using HeuristicLab.Common;
-using HeuristicLab.Core;
-using HeuristicLab.Data;
-using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
-using HeuristicLab.Parameters;
-using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
-
-namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
-  [StorableClass]
-  [Item("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.")]
-  public sealed class SymbolicDataAnalysisExpressionTreeInterpreter : ParameterizedNamedItem,
-    ISymbolicDataAnalysisExpressionTreeInterpreter, ISymbolicTimeSeriesPrognosisExpressionTreeInterpreter {
-    private const string CheckExpressionsWithIntervalArithmeticParameterName = "CheckExpressionsWithIntervalArithmetic";
-    private const string EvaluatedSolutionsParameterName = "EvaluatedSolutions";
-    #region private classes
-    private class InterpreterState {
-      private double[] argumentStack;
-      private int argumentStackPointer;
-      private Instruction[] code;
-      private int pc;
-      public int ProgramCounter {
-        get { return pc; }
-        set { pc = value; }
-      }
-      internal InterpreterState(Instruction[] code, int argumentStackSize) {
-        this.code = code;
-        this.pc = 0;
-        if (argumentStackSize > 0) {
-          this.argumentStack = new double[argumentStackSize];
-        }
-        this.argumentStackPointer = 0;
-      }
-
-      internal void Reset() {
-        this.pc = 0;
-        this.argumentStackPointer = 0;
-      }
-
-      internal Instruction NextInstruction() {
-        return code[pc++];
-      }
-      private void Push(double val) {
-        argumentStack[argumentStackPointer++] = val;
-      }
-      private double Pop() {
-        return argumentStack[--argumentStackPointer];
-      }
-
-      internal void CreateStackFrame(double[] argValues) {
-        // push in reverse order to make indexing easier
-        for (int i = argValues.Length - 1; i >= 0; i--) {
-          argumentStack[argumentStackPointer++] = argValues[i];
-        }
-        Push(argValues.Length);
-      }
-
-      internal void RemoveStackFrame() {
-        int size = (int)Pop();
-        argumentStackPointer -= size;
-      }
-
-      internal double GetStackFrameValue(ushort index) {
-        // layout of stack:
-        // [0]   <- argumentStackPointer
-        // [StackFrameSize = N + 1]
-        // [Arg0] <- argumentStackPointer - 2 - 0
-        // [Arg1] <- argumentStackPointer - 2 - 1
-        // [...]
-        // [ArgN] <- argumentStackPointer - 2 - N
-        // <Begin of stack frame>
-        return argumentStack[argumentStackPointer - index - 2];
-      }
-    }
-    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(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 {
-      get { return false; }
-    }
-    public override bool CanChangeDescription {
-      get { return false; }
-    }
-
-    #region parameter properties
-    public IValueParameter<BoolValue> CheckExpressionsWithIntervalArithmeticParameter {
-      get { return (IValueParameter<BoolValue>)Parameters[CheckExpressionsWithIntervalArithmeticParameterName]; }
-    }
-
-    public IValueParameter<IntValue> EvaluatedSolutionsParameter {
-      get { return (IValueParameter<IntValue>)Parameters[EvaluatedSolutionsParameterName]; }
-    }
-    #endregion
-
-    #region properties
-    public BoolValue CheckExpressionsWithIntervalArithmetic {
-      get { return CheckExpressionsWithIntervalArithmeticParameter.Value; }
-      set { CheckExpressionsWithIntervalArithmeticParameter.Value = value; }
-    }
-
-    public IntValue EvaluatedSolutions {
-      get { return EvaluatedSolutionsParameter.Value; }
-      set { EvaluatedSolutionsParameter.Value = value; }
-    }
-    #endregion
-
-    [StorableConstructor]
-    private SymbolicDataAnalysisExpressionTreeInterpreter(bool deserializing) : base(deserializing) { }
-    private SymbolicDataAnalysisExpressionTreeInterpreter(SymbolicDataAnalysisExpressionTreeInterpreter original, Cloner cloner) : base(original, cloner) { }
-    public override IDeepCloneable Clone(Cloner cloner) {
-      return new SymbolicDataAnalysisExpressionTreeInterpreter(this, cloner);
-    }
-
-    public SymbolicDataAnalysisExpressionTreeInterpreter()
-      : base("SymbolicDataAnalysisExpressionTreeInterpreter", "Interpreter for symbolic expression trees including automatically defined functions.") {
-      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)));
-    }
-
-    [StorableHook(HookType.AfterDeserialization)]
-    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)));
-    }
-
-    #region IStatefulItem
-    public void InitializeState() {
-      EvaluatedSolutions.Value = 0;
-    }
-
-    public void ClearState() {
-    }
-    #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) {
-      if (CheckExpressionsWithIntervalArithmetic.Value)
-        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);
-      int necessaryArgStackSize = 0;
-      for (int i = 0; i < code.Length; i++) {
-        Instruction instr = code[i];
-        if (instr.opCode == OpCodes.Variable) {
-          var variableTreeNode = instr.dynamicNode as VariableTreeNode;
-          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableTreeNode.VariableName);
-          code[i] = instr;
-        } else if (instr.opCode == OpCodes.LagVariable) {
-          var laggedVariableTreeNode = instr.dynamicNode as LaggedVariableTreeNode;
-          instr.iArg0 = dataset.GetReadOnlyDoubleValues(laggedVariableTreeNode.VariableName);
-          code[i] = instr;
-        } else if (instr.opCode == OpCodes.VariableCondition) {
-          var variableConditionTreeNode = instr.dynamicNode as VariableConditionTreeNode;
-          instr.iArg0 = dataset.GetReadOnlyDoubleValues(variableConditionTreeNode.VariableName);
-        } else if (instr.opCode == OpCodes.Call) {
-          necessaryArgStackSize += instr.nArguments + 1;
-        }
-      }
-      var state = new InterpreterState(code, necessaryArgStackSize);
-
-      int nComponents = tree.Root.GetSubtree(0).SubtreeCount;
-      // produce a n-step forecast for each target variable for all rows
-      var cachedPrognosedValues = new Dictionary<string, double[]>();
-      foreach (var targetVariable in targetVariables)
-        cachedPrognosedValues[targetVariable] = new double[horizon];
-      foreach (var rowEnum in rows) {
-        int row = rowEnum;
-        foreach (var horizonRow in Enumerable.Range(row, horizon)) {
-          int localRow = horizonRow; // create a local variable for the ref parameter
-          for (int c = 0; c < nComponents; c++) {
-            var prog = Evaluate(dataset, ref localRow, row - 1, state, cachedPrognosedValues);
-            yield return prog;
-            cachedPrognosedValues[targetVariables[c]][horizonRow - row] = prog;
-          }
-
-          state.Reset();
-        }
-      }
-    }
-
-    private double Evaluate(Dataset dataset, ref int row, int lastObservedRow, InterpreterState state, Dictionary<string, double[]> cachedPrognosedValues) {
-      Instruction currentInstr = state.NextInstruction();
-      switch (currentInstr.opCode) {
-        case OpCodes.Add: {
-            double s = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            for (int i = 1; i < currentInstr.nArguments; i++) {
-              s += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            }
-            return s;
-          }
-        case OpCodes.Sub: {
-            double s = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            for (int i = 1; i < currentInstr.nArguments; i++) {
-              s -= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            }
-            if (currentInstr.nArguments == 1) s = -s;
-            return s;
-          }
-        case OpCodes.Mul: {
-            double p = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            for (int i = 1; i < currentInstr.nArguments; i++) {
-              p *= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            }
-            return p;
-          }
-        case OpCodes.Div: {
-            double p = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            for (int i = 1; i < currentInstr.nArguments; i++) {
-              p /= Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            }
-            if (currentInstr.nArguments == 1) p = 1.0 / p;
-            return p;
-          }
-        case OpCodes.Average: {
-            double sum = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            for (int i = 1; i < currentInstr.nArguments; i++) {
-              sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            }
-            return sum / currentInstr.nArguments;
-          }
-        case OpCodes.Cos: {
-            return Math.Cos(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-          }
-        case OpCodes.Sin: {
-            return Math.Sin(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-          }
-        case OpCodes.Tan: {
-            return Math.Tan(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-          }
-        case OpCodes.Square: {
-            return Math.Pow(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues), 2);
-          }
-        case OpCodes.Power: {
-            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            double y = Math.Round(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-            return Math.Pow(x, y);
-          }
-        case OpCodes.SquareRoot: {
-            return Math.Sqrt(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-          }
-        case OpCodes.Root: {
-            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            double y = Math.Round(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-            return Math.Pow(x, 1 / y);
-          }
-        case OpCodes.Exp: {
-            return Math.Exp(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-          }
-        case OpCodes.Log: {
-            return Math.Log(Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues));
-          }
-        case OpCodes.Gamma: {
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else return alglib.gammafunction(x);
-          }
-        case OpCodes.Psi: {
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else if (x <= 0 && (Math.Floor(x) - x).IsAlmost(0)) return double.NaN;
-            return alglib.psi(x);
-          }
-        case OpCodes.Dawson: {
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            return alglib.dawsonintegral(x);
-          }
-        case OpCodes.ExponentialIntegralEi: {
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            return alglib.exponentialintegralei(x);
-          }
-        case OpCodes.SineIntegral: {
-            double si, ci;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.sinecosineintegrals(x, out si, out ci);
-              return si;
-            }
-          }
-        case OpCodes.CosineIntegral: {
-            double si, ci;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.sinecosineintegrals(x, out si, out ci);
-              return ci;
-            }
-          }
-        case OpCodes.HyperbolicSineIntegral: {
-            double shi, chi;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
-              return shi;
-            }
-          }
-        case OpCodes.HyperbolicCosineIntegral: {
-            double shi, chi;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.hyperbolicsinecosineintegrals(x, out shi, out chi);
-              return chi;
-            }
-          }
-        case OpCodes.FresnelCosineIntegral: {
-            double c = 0, s = 0;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.fresnelintegral(x, ref c, ref s);
-              return c;
-            }
-          }
-        case OpCodes.FresnelSineIntegral: {
-            double c = 0, s = 0;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.fresnelintegral(x, ref c, ref s);
-              return s;
-            }
-          }
-        case OpCodes.AiryA: {
-            double ai, aip, bi, bip;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.airy(x, out ai, out aip, out bi, out bip);
-              return ai;
-            }
-          }
-        case OpCodes.AiryB: {
-            double ai, aip, bi, bip;
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else {
-              alglib.airy(x, out ai, out aip, out bi, out bip);
-              return bi;
-            }
-          }
-        case OpCodes.Norm: {
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else return alglib.normaldistribution(x);
-          }
-        case OpCodes.Erf: {
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else return alglib.errorfunction(x);
-          }
-        case OpCodes.Bessel: {
-            var x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (double.IsNaN(x)) return double.NaN;
-            else return alglib.besseli0(x);
-          }
-        case OpCodes.IfThenElse: {
-            double condition = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            double result;
-            if (condition > 0.0) {
-              result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); SkipInstructions(state);
-            } else {
-              SkipInstructions(state); result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            }
-            return result;
-          }
-        case OpCodes.AND: {
-            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            for (int i = 1; i < currentInstr.nArguments; i++) {
-              if (result > 0.0) result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-              else {
-                SkipInstructions(state);
-              }
-            }
-            return result > 0.0 ? 1.0 : -1.0;
-          }
-        case OpCodes.OR: {
-            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            for (int i = 1; i < currentInstr.nArguments; i++) {
-              if (result <= 0.0) result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-              else {
-                SkipInstructions(state);
-              }
-            }
-            return result > 0.0 ? 1.0 : -1.0;
-          }
-        case OpCodes.NOT: {
-            return Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues) > 0.0 ? -1.0 : 1.0;
-          }
-        case OpCodes.GT: {
-            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            double y = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (x > y) return 1.0;
-            else return -1.0;
-          }
-        case OpCodes.LT: {
-            double x = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            double y = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            if (x < y) return 1.0;
-            else return -1.0;
-          }
-        case OpCodes.TimeLag: {
-            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
-            row += timeLagTreeNode.Lag;
-            double result = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            row -= timeLagTreeNode.Lag;
-            return result;
-          }
-        case OpCodes.Integral: {
-            int savedPc = state.ProgramCounter;
-            var timeLagTreeNode = (LaggedTreeNode)currentInstr.dynamicNode;
-            double sum = 0.0;
-            for (int i = 0; i < Math.Abs(timeLagTreeNode.Lag); i++) {
-              row += Math.Sign(timeLagTreeNode.Lag);
-              sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-              state.ProgramCounter = savedPc;
-            }
-            row -= timeLagTreeNode.Lag;
-            sum += Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            return sum;
-          }
-
-        //mkommend: derivate calculation taken from: 
-        //http://www.holoborodko.com/pavel/numerical-methods/numerical-derivative/smooth-low-noise-differentiators/
-        //one sided smooth differentiatior, N = 4
-        // y' = 1/8h (f_i + 2f_i-1, -2 f_i-3 - f_i-4)
-        case OpCodes.Derivative: {
-            int savedPc = state.ProgramCounter;
-            double f_0 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row--;
-            state.ProgramCounter = savedPc;
-            double f_1 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row -= 2;
-            state.ProgramCounter = savedPc;
-            double f_3 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues); row--;
-            state.ProgramCounter = savedPc;
-            double f_4 = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            row += 4;
-
-            return (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1
-          }
-        case OpCodes.Call: {
-            // evaluate sub-trees
-            double[] argValues = new double[currentInstr.nArguments];
-            for (int i = 0; i < currentInstr.nArguments; i++) {
-              argValues[i] = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            }
-            // push on argument values on stack 
-            state.CreateStackFrame(argValues);
-
-            // save the pc
-            int savedPc = state.ProgramCounter;
-            // set pc to start of function  
-            state.ProgramCounter = (ushort)currentInstr.iArg0;
-            // evaluate the function
-            double v = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-
-            // delete the stack frame
-            state.RemoveStackFrame();
-
-            // restore the pc => evaluation will continue at point after my subtrees  
-            state.ProgramCounter = savedPc;
-            return v;
-          }
-        case OpCodes.Arg: {
-            return state.GetStackFrameValue((ushort)currentInstr.iArg0);
-          }
-        case OpCodes.Variable: {
-            if (row < 0 || row >= dataset.Rows)
-              return double.NaN;
-            var variableTreeNode = (VariableTreeNode)currentInstr.dynamicNode;
-            if (row <= lastObservedRow || !cachedPrognosedValues.ContainsKey(variableTreeNode.VariableName)) return ((IList<double>)currentInstr.iArg0)[row] * variableTreeNode.Weight;
-            else return cachedPrognosedValues[variableTreeNode.VariableName][row - lastObservedRow - 1] * variableTreeNode.Weight;
-          }
-        case OpCodes.LagVariable: {
-            var laggedVariableTreeNode = (LaggedVariableTreeNode)currentInstr.dynamicNode;
-            int actualRow = row + laggedVariableTreeNode.Lag;
-            if (actualRow < 0 || actualRow >= dataset.Rows)
-              return double.NaN;
-            if (actualRow <= lastObservedRow || !cachedPrognosedValues.ContainsKey(laggedVariableTreeNode.VariableName)) return ((IList<double>)currentInstr.iArg0)[actualRow] * laggedVariableTreeNode.Weight;
-            else return cachedPrognosedValues[laggedVariableTreeNode.VariableName][actualRow - lastObservedRow - 1] * laggedVariableTreeNode.Weight;
-          }
-        case OpCodes.Constant: {
-            var constTreeNode = currentInstr.dynamicNode as ConstantTreeNode;
-            return constTreeNode.Value;
-          }
-
-        //mkommend: this symbol uses the logistic function f(x) = 1 / (1 + e^(-alpha * x) ) 
-        //to determine the relative amounts of the true and false branch see http://en.wikipedia.org/wiki/Logistic_function
-        case OpCodes.VariableCondition: {
-            if (row < 0 || row >= dataset.Rows)
-              return double.NaN;
-            var variableConditionTreeNode = (VariableConditionTreeNode)currentInstr.dynamicNode;
-            double variableValue;
-            if (row <= lastObservedRow || !cachedPrognosedValues.ContainsKey(variableConditionTreeNode.VariableName))
-              variableValue = ((IList<double>)currentInstr.iArg0)[row];
-            else
-              variableValue = cachedPrognosedValues[variableConditionTreeNode.VariableName][row - lastObservedRow - 1];
-
-            double x = variableValue - variableConditionTreeNode.Threshold;
-            double p = 1 / (1 + Math.Exp(-variableConditionTreeNode.Slope * x));
-
-            double trueBranch = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-            double falseBranch = Evaluate(dataset, ref row, lastObservedRow, state, cachedPrognosedValues);
-
-            return trueBranch * p + falseBranch * (1 - p);
-          }
-        default: throw new NotSupportedException();
-      }
-    }
-
-    private byte MapSymbolToOpCode(ISymbolicExpressionTreeNode treeNode) {
-      byte opCode;
-      if (!symbolToOpcode.TryGetValue(treeNode.Symbol.GetType(), out opCode))
-        throw new NotSupportedException("Symbol: " + treeNode.Symbol);
-      return opCode;
-    }
-
-    // skips a whole branch
-    private void SkipInstructions(InterpreterState state) {
-      int i = 1;
-      while (i > 0) {
-        i += state.NextInstruction().nArguments;
-        i--;
-      }
-    }
-  }
-}
