Index: trunk/sources/HeuristicLab.GP.StructureIdentification/BakedTreeEvaluator.cs
===================================================================
--- trunk/sources/HeuristicLab.GP.StructureIdentification/BakedTreeEvaluator.cs	(revision 755)
+++ trunk/sources/HeuristicLab.GP.StructureIdentification/BakedTreeEvaluator.cs	(revision 767)
@@ -41,12 +41,12 @@
     private class Instr {
       public double d_arg0;
-      public int i_arg0;
-      public int i_arg1;
-      public int arity;
-      public int symbol;
+      public short i_arg0;
+      public short i_arg1;
+      public byte arity;
+      public byte symbol;
+      public ushort exprLength;
       public IFunction function;
     }
 
-    private List<Instr> code;
     private Instr[] codeArr;
     private int PC;
@@ -54,14 +54,9 @@
     private int sampleIndex;
 
-
-    public BakedTreeEvaluator() {
-      code = new List<Instr>();
-    }
-
     public void ResetEvaluator(BakedFunctionTree functionTree, Dataset dataset, int targetVariable, int start, int end, double punishmentFactor) {
       this.dataset = dataset;
       double maximumPunishment = punishmentFactor * dataset.GetRange(targetVariable);
 
-      // get the mean of the values of the target variable to determin the max and min bounds of the estimated value
+      // get the mean of the values of the target variable to determine the max and min bounds of the estimated value
       double targetMean = dataset.GetMean(targetVariable, start, end - 1);
       estimatedValueMin = targetMean - maximumPunishment;
@@ -69,34 +64,66 @@
 
       List<LightWeightFunction> linearRepresentation = functionTree.LinearRepresentation;
-      code.Clear();
-      foreach(LightWeightFunction f in linearRepresentation) {
-        Instr curInstr = new Instr();
-        TranslateToInstr(f, curInstr);
-        code.Add(curInstr);
-      }
-
-      codeArr = code.ToArray<Instr>();
-    }
-
-    private void TranslateToInstr(LightWeightFunction f, Instr instr) {
+      codeArr = new Instr[linearRepresentation.Count];
+      int i = 0;
+      foreach (LightWeightFunction f in linearRepresentation) {
+        codeArr[i++] = TranslateToInstr(f);
+      }
+      exprIndex = 0;
+      ushort exprLength;
+      bool constExpr;
+      PatchExpressionLengthsAndConstants(0, out constExpr, out exprLength);
+    }
+
+    ushort exprIndex;
+    private void PatchExpressionLengthsAndConstants(ushort index, out bool constExpr, out ushort exprLength) {
+      exprLength = 1;
+      if (codeArr[index].arity == 0) {
+        // when no children then it's a constant expression only if the terminal is a constant
+        constExpr = codeArr[index].symbol == EvaluatorSymbolTable.CONSTANT;
+      } else {
+        constExpr = true; // when there are children it's a constant expression if all children are constant;
+      }
+      for (int i = 0; i < codeArr[index].arity; i++) {
+        exprIndex++;
+        ushort branchLength;
+        bool branchConstExpr;
+        PatchExpressionLengthsAndConstants(exprIndex, out branchConstExpr, out branchLength);
+        exprLength += branchLength;
+        constExpr &= branchConstExpr;
+      }
+      codeArr[index].exprLength = exprLength;
+
+      if (constExpr) {
+        codeArr[index].symbol = EvaluatorSymbolTable.CONSTANT;
+        PC = index;
+        codeArr[index].d_arg0 = EvaluateBakedCode();
+      }
+    }
+
+    private Instr TranslateToInstr(LightWeightFunction f) {
+      Instr instr = new Instr();
       instr.arity = f.arity;
       instr.symbol = EvaluatorSymbolTable.MapFunction(f.functionType);
-      switch(instr.symbol) {
+      switch (instr.symbol) {
         case EvaluatorSymbolTable.DIFFERENTIAL:
         case EvaluatorSymbolTable.VARIABLE: {
-            instr.i_arg0 = (int)f.data[0]; // var
+            instr.i_arg0 = (byte)f.data[0]; // var
             instr.d_arg0 = f.data[1]; // weight
-            instr.i_arg1 = (int)f.data[2]; // sample-offset
+            instr.i_arg1 = (byte)f.data[2]; // sample-offset
+            instr.exprLength = 1;
             break;
           }
         case EvaluatorSymbolTable.CONSTANT: {
             instr.d_arg0 = f.data[0]; // value
+            instr.exprLength = 1;
             break;
           }
         case EvaluatorSymbolTable.UNKNOWN: {
             instr.function = f.functionType;
+            instr.exprLength = 1;
             break;
           }
       }
+      return instr;
     }
 
@@ -106,9 +133,9 @@
 
       double estimated = EvaluateBakedCode();
-      if(double.IsNaN(estimated) || double.IsInfinity(estimated)) {
+      if (double.IsNaN(estimated) || double.IsInfinity(estimated)) {
         estimated = estimatedValueMax;
-      } else if(estimated > estimatedValueMax) {
+      } else if (estimated > estimatedValueMax) {
         estimated = estimatedValueMax;
-      } else if(estimated < estimatedValueMin) {
+      } else if (estimated < estimatedValueMin) {
         estimated = estimatedValueMin;
       }
@@ -118,30 +145,27 @@
     // skips a whole branch
     private void SkipBakedCode() {
-      int i = 1;
-      while(i > 0) {
-        i += code[PC++].arity;
-        i--;
-      }
+      PC += codeArr[PC].exprLength;
     }
 
     private double EvaluateBakedCode() {
       Instr currInstr = codeArr[PC++];
-      switch(currInstr.symbol) {
+      switch (currInstr.symbol) {
         case EvaluatorSymbolTable.VARIABLE: {
             int row = sampleIndex + currInstr.i_arg1;
-            if(row < 0 || row >= dataset.Rows) return double.NaN;
+            if (row < 0 || row >= dataset.Rows) return double.NaN;
             else return currInstr.d_arg0 * dataset.GetValue(row, currInstr.i_arg0);
           }
         case EvaluatorSymbolTable.CONSTANT: {
+            PC += currInstr.exprLength - 1;
             return currInstr.d_arg0;
           }
         case EvaluatorSymbolTable.DIFFERENTIAL: {
             int row = sampleIndex + currInstr.i_arg1;
-            if(row < 1 || row >= dataset.Rows) return double.NaN;
+            if (row < 1 || row >= dataset.Rows) return double.NaN;
             else return currInstr.d_arg0 * (dataset.GetValue(row, currInstr.i_arg0) - dataset.GetValue(row - 1, currInstr.i_arg0));
           }
         case EvaluatorSymbolTable.MULTIPLICATION: {
             double result = EvaluateBakedCode();
-            for(int i = 1; i < currInstr.arity; i++) {
+            for (int i = 1; i < currInstr.arity; i++) {
               result *= EvaluateBakedCode();
             }
@@ -150,5 +174,5 @@
         case EvaluatorSymbolTable.ADDITION: {
             double sum = EvaluateBakedCode();
-            for(int i = 1; i < currInstr.arity; i++) {
+            for (int i = 1; i < currInstr.arity; i++) {
               sum += EvaluateBakedCode();
             }
@@ -156,9 +180,9 @@
           }
         case EvaluatorSymbolTable.SUBTRACTION: {
-            if(currInstr.arity == 1) {
+            if (currInstr.arity == 1) {
               return -EvaluateBakedCode();
             } else {
               double result = EvaluateBakedCode();
-              for(int i = 1; i < currInstr.arity; i++) {
+              for (int i = 1; i < currInstr.arity; i++) {
                 result -= EvaluateBakedCode();
               }
@@ -168,18 +192,18 @@
         case EvaluatorSymbolTable.DIVISION: {
             double result;
-            if(currInstr.arity == 1) {
+            if (currInstr.arity == 1) {
               result = 1.0 / EvaluateBakedCode();
             } else {
               result = EvaluateBakedCode();
-              for(int i = 1; i < currInstr.arity; i++) {
+              for (int i = 1; i < currInstr.arity; i++) {
                 result /= EvaluateBakedCode();
               }
             }
-            if(double.IsInfinity(result)) return 0.0;
+            if (double.IsInfinity(result)) return 0.0;
             else return result;
           }
         case EvaluatorSymbolTable.AVERAGE: {
             double sum = EvaluateBakedCode();
-            for(int i = 1; i < currInstr.arity; i++) {
+            for (int i = 1; i < currInstr.arity; i++) {
               sum += EvaluateBakedCode();
             }
@@ -205,5 +229,5 @@
         case EvaluatorSymbolTable.SIGNUM: {
             double value = EvaluateBakedCode();
-            if(double.IsNaN(value)) return double.NaN;
+            if (double.IsNaN(value)) return double.NaN;
             else return Math.Sign(value);
           }
@@ -216,6 +240,6 @@
         case EvaluatorSymbolTable.AND: { // only defined for inputs 1 and 0
             double result = EvaluateBakedCode();
-            for(int i = 1; i < currInstr.arity; i++) {
-              if(result == 0.0) SkipBakedCode();
+            for (int i = 1; i < currInstr.arity; i++) {
+              if (result == 0.0) SkipBakedCode();
               else {
                 result = EvaluateBakedCode();
@@ -228,10 +252,10 @@
             double x = EvaluateBakedCode();
             double y = EvaluateBakedCode();
-            if(Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0;
+            if (Math.Abs(x - y) < EPSILON) return 1.0; else return 0.0;
           }
         case EvaluatorSymbolTable.GT: {
             double x = EvaluateBakedCode();
             double y = EvaluateBakedCode();
-            if(x > y) return 1.0;
+            if (x > y) return 1.0;
             else return 0.0;
           }
@@ -240,5 +264,5 @@
             Debug.Assert(condition == 0.0 || condition == 1.0);
             double result;
-            if(condition == 0.0) {
+            if (condition == 0.0) {
               result = EvaluateBakedCode(); SkipBakedCode();
             } else {
@@ -250,5 +274,5 @@
             double x = EvaluateBakedCode();
             double y = EvaluateBakedCode();
-            if(x < y) return 1.0;
+            if (x < y) return 1.0;
             else return 0.0;
           }
@@ -260,6 +284,6 @@
         case EvaluatorSymbolTable.OR: { // only defined for inputs 0 or 1
             double result = EvaluateBakedCode();
-            for(int i = 1; i < currInstr.arity; i++) {
-              if(result > 0.0) SkipBakedCode();
+            for (int i = 1; i < currInstr.arity; i++) {
+              if (result > 0.0) SkipBakedCode();
               else {
                 result = EvaluateBakedCode();
Index: trunk/sources/HeuristicLab.GP.StructureIdentification/SymbolTable.cs
===================================================================
--- trunk/sources/HeuristicLab.GP.StructureIdentification/SymbolTable.cs	(revision 755)
+++ trunk/sources/HeuristicLab.GP.StructureIdentification/SymbolTable.cs	(revision 767)
@@ -29,35 +29,35 @@
 namespace HeuristicLab.GP.StructureIdentification {
   class EvaluatorSymbolTable : StorableBase {
-    public const int ADDITION = 1;
-    public const int AND = 2;
-    public const int AVERAGE = 3;
-    public const int CONSTANT = 4;
-    public const int COSINUS = 5;
-    public const int DIFFERENTIAL = 25;
-    public const int DIVISION = 6;
-    public const int EQU = 7;
-    public const int EXP = 8;
-    public const int GT = 9;
-    public const int IFTE = 10;
-    public const int LT = 11;
-    public const int LOG = 12;
-    public const int MULTIPLICATION = 13;
-    public const int NOT = 14;
-    public const int OR = 15;
-    public const int POWER = 16;
-    public const int SIGNUM = 17;
-    public const int SINUS = 18;
-    public const int SQRT = 19;
-    public const int SUBTRACTION = 20;
-    public const int TANGENS = 21;
-    public const int VARIABLE = 22;
-    public const int XOR = 23;
-    public const int UNKNOWN = 24;
+    public const byte ADDITION = 1;
+    public const byte AND = 2;
+    public const byte AVERAGE = 3;
+    public const byte CONSTANT = 4;
+    public const byte COSINUS = 5;
+    public const byte DIFFERENTIAL = 25;
+    public const byte DIVISION = 6;
+    public const byte EQU = 7;
+    public const byte EXP = 8;
+    public const byte GT = 9;
+    public const byte IFTE = 10;
+    public const byte LT = 11;
+    public const byte LOG = 12;
+    public const byte MULTIPLICATION = 13;
+    public const byte NOT = 14;
+    public const byte OR = 15;
+    public const byte POWER = 16;
+    public const byte SIGNUM = 17;
+    public const byte SINUS = 18;
+    public const byte SQRT = 19;
+    public const byte SUBTRACTION = 20;
+    public const byte TANGENS = 21;
+    public const byte VARIABLE = 22;
+    public const byte XOR = 23;
+    public const byte UNKNOWN = 24;
 
-    private static Dictionary<Type, int> staticTypes = new Dictionary<Type, int>();
+    private static Dictionary<Type, byte> staticTypes = new Dictionary<Type, byte>();
 
     // needs to be public for persistence mechanism (Activator.CreateInstance needs empty constructor)
     static EvaluatorSymbolTable() {
-      staticTypes = new Dictionary<Type, int>();
+      staticTypes = new Dictionary<Type, byte>();
       staticTypes[typeof(Addition)] = ADDITION;
       staticTypes[typeof(And)] = AND;
@@ -86,5 +86,5 @@
     }
 
-    internal static int MapFunction(IFunction function) {
+    internal static byte MapFunction(IFunction function) {
       if(staticTypes.ContainsKey(function.GetType())) return staticTypes[function.GetType()];
       else return UNKNOWN;
