Index: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs
===================================================================
--- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs	(revision 13645)
+++ stable/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs	(revision 15060)
@@ -2,5 +2,4 @@
 /* HeuristicLab
  * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
- * and the BEACON Center for the Study of Evolution in Action.
  * 
  * This file is part of HeuristicLab.
@@ -27,5 +26,18 @@
 namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression {
   // this is the core class for generating expressions. 
-  // the automaton determines which expressions are allowed
+  // it represents a finite state automaton, each state transition can be associated with an action (e.g. to produce code).
+  // the automaton determines the possible structures for expressions.
+  // 
+  // to understand this code it is worthwile to generate a graphical visualization of the automaton (see PrintAutomaton).
+  // If the code is compiled in debug mode the automaton produces a Graphviz file into the folder of the application
+  // whenever an instance of the automaton is constructed.
+  //
+  // This class relies on two other classes: 
+  // - CodeGenerator to produce code for a stack-based evaluator and
+  // - ConstraintHandler to restrict the allowed set of expressions. 
+  //
+  // The ConstraintHandler extends the automaton and adds semantic restrictions for expressions produced by the automaton.
+  // 
+  // 
   internal class Automaton {
     public const int StateExpr = 1;
@@ -53,5 +65,6 @@
     public const int StateInvTFStart = 23;
     public const int StateInvTFEnd = 24;
-    private const int FirstDynamicState = 25;
+    public const int FirstDynamicState = 25;
+    // more states for individual variables are created dynamically
 
     private const int StartState = StateExpr;
@@ -222,6 +235,7 @@
         () => {
           codeGenerator.Emit1(OpCodes.LoadConst0);
-        },
-        "0");
+          constraintHandler.StartNewTermInPoly();
+        },
+        "0, StartTermInPoly");
       AddTransition(StateLogTEnd, StateLogFactorEnd,
         () => {
@@ -272,6 +286,7 @@
         () => {
           codeGenerator.Emit1(OpCodes.LoadConst1);
-        },
-        "c");
+          constraintHandler.StartNewTermInPoly();
+        },
+        "c, StartTermInPoly");
       AddTransition(StateInvTEnd, StateInvFactorEnd,
         () => {
@@ -338,22 +353,13 @@
     private readonly int[] followStatesBuf = new int[1000];
     public void FollowStates(int state, out int[] buf, out int nElements) {
-      // return followStates[state]
-      //   .Where(s => s < FirstDynamicState || s >= minVarIdx) // for variables we only allow non-decreasing state sequences
-      //   // the following states imply an additional variable being added to the expression
-      //   // F, Sum, Prod
-      //   .Where(s => (s != StateF && s != StateSum && s != StateProd) || variablesRemaining > 0);
-
       // for loop instead of where iterator
       var fs = followStates[state];
       int j = 0;
-      //Console.Write(stateNames[CurrentState] + " allowed: ");
       for (int i = 0; i < fs.Count; i++) {
         var s = fs[i];
         if (constraintHandler.IsAllowedFollowState(state, s)) {
-          //Console.Write(s + " ");
           followStatesBuf[j++] = s;
         }
       }
-      //Console.WriteLine();
       buf = followStatesBuf;
       nElements = j;
@@ -362,7 +368,4 @@
 
     public void Goto(int targetState) {
-      //Console.WriteLine("->{0}", stateNames[targetState]);
-      // Contract.Assert(FollowStates(CurrentState).Contains(targetState));
-
       if (actions[CurrentState, targetState] != null)
         actions[CurrentState, targetState].ForEach(a => a()); // execute all actions
@@ -371,5 +374,5 @@
 
     public bool IsFinalState(int s) {
-      return s == StateExprEnd;
+      return s == StateExprEnd && !constraintHandler.IsInvalidExpression;
     }
 
@@ -389,6 +392,4 @@
         writer.WriteLine("digraph {");
         // writer.WriteLine("rankdir=LR");
-        int[] fs;
-        int nFs;
         for (int s = StartState; s < stateNames.Count; s++) {
           for (int i = 0; i < followStates[s].Count; i++) {
