Index: /branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
===================================================================
--- /branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs	(revision 10386)
+++ /branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs	(revision 10387)
@@ -83,10 +83,4 @@
     public static readonly string[] symb = new string[] { ?SYMBOLNAMES? };
     
-    public static Tree CreateTerminalNode(int state, Random random, ?IDENT?Problem problem) {
-      switch(state) {
-        ?CREATETERMINALNODECODE?
-        default: { throw new ArgumentException(""Unknown state index"" + state); }
-      }
-    }
   }  
 #endregion
@@ -128,5 +122,4 @@
         .Replace("?SYMBOLNAMES?", grammar.Symbols.Select(s => s.Name).Aggregate(string.Empty, (str, symb) => str + "\"" + symb + "\", "))
         .Replace("?TRANSITIONTABLE?", GenerateTransitionTable(grammar))
-        .Replace("?CREATETERMINALNODECODE?", GenerateCreateTerminalCode(grammar))
         .Replace("?SUBTREECOUNTTABLE?", GenerateSubtreeCountTable(grammar))
        ;
@@ -136,5 +129,5 @@
       var grammar = CreateGrammarFromAst(ast);
       var randomSearchCodeGen = new RandomSearchCodeGen();
-      randomSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode);
+      randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
       //var bruteForceSearchCodeGen = new BruteForceCodeGen();
       //bruteForceSearchCodeGen.Generate(grammar, ast.FitnessFunctionNode.Maximization, solverSourceCode);
@@ -239,20 +232,5 @@
         sb.AppendFormat("public {0} {1};", att.Type, att.Identifier).AppendLine();
       }
-      sb.AppendFormat(" public {0}Tree(Random random, ?IDENT?Problem problem) : base() {{", terminal.Ident).BeginBlock();
-      foreach (var constr in terminal.Constraints) {
-        if (constr.Type == ConstraintNodeType.Set) {
-          sb.Append("{").BeginBlock();
-          sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine();
-          sb.AppendFormat("{0} = elements[random.Next(elements.Length)]; ", constr.Ident).AppendLine();
-          sb.Append("}").EndBlock();
-        } else {
-          sb.Append("{").BeginBlock();
-          sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine();
-          sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine();
-          sb.AppendFormat("{0} = random.NextDouble() * (max - min) + min;", constr.Ident).EndBlock();
-          sb.Append("}").EndBlock();
-        }
-      }
-      sb.Append("}").EndBlock();
+      sb.AppendFormat(" public {0}Tree() : base() {{ }}", terminal.Ident).AppendLine();
       sb.AppendLine("}");
     }
@@ -362,15 +340,5 @@
     }
 
-    private string GenerateCreateTerminalCode(IGrammar grammar) {
-      Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol));
-      var sb = new SourceBuilder();
-      var allSymbols = grammar.Symbols.ToList();
-      foreach (var s in grammar.Symbols) {
-        if (grammar.IsTerminal(s)) {
-          sb.AppendFormat("case {0}: {{ return new {1}Tree(random, problem); }}", allSymbols.IndexOf(s), s.Name).AppendLine();
-        }
-      }
-      return sb.ToString();
-    }
+    
 
     private string GenerateTransitionTable(IGrammar grammar) {
Index: /branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs
===================================================================
--- /branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs	(revision 10386)
+++ /branches/HeuristicLab.Problems.GPDL/CodeGenerator/RandomSearchCodeGen.cs	(revision 10387)
@@ -33,5 +33,5 @@
       // terminals
       if(Grammar.subtreeCount[state] == 0) {
-        t = Grammar.CreateTerminalNode(state, random, problem);
+        t = CreateTerminalNode(state, random, problem);
       } else {
         // if the symbol has alternatives then we must choose one randomly (only one sub-tree in this case)
@@ -52,4 +52,11 @@
       curDepth -=1;
       return t;
+    }
+
+    public static Tree CreateTerminalNode(int state, Random random, ?IDENT?Problem problem) {
+      switch(state) {
+        ?CREATETERMINALNODECODE?
+        default: { throw new ArgumentException(""Unknown state index"" + state); }
+      }
     }
 
@@ -144,9 +151,10 @@
 }";
 
-    public void Generate(IGrammar grammar, bool maximization, SourceBuilder problemSourceCode) {
+    public void Generate(IGrammar grammar, IEnumerable<TerminalNode> terminals, bool maximization, SourceBuilder problemSourceCode) {
       var solverSourceCode = new SourceBuilder();
       solverSourceCode.Append(solverTemplate)
         .Replace("?MAXIMIZATION?", maximization.ToString().ToLowerInvariant())
         .Replace("?SAMPLEALTERNATIVECODE?", GenerateSampleAlternativeSource(grammar))
+        .Replace("?CREATETERMINALNODECODE?", GenerateCreateTerminalCode(grammar, terminals))
       ;
 
@@ -188,4 +196,33 @@
       return sb.ToString();
     }
+    private string GenerateCreateTerminalCode(IGrammar grammar, IEnumerable<TerminalNode> terminals) {
+      Debug.Assert(grammar.Symbols.First().Equals(grammar.StartSymbol));
+      var sb = new SourceBuilder();
+      var allSymbols = grammar.Symbols.ToList();
+      foreach (var s in grammar.Symbols) {
+        if (grammar.IsTerminal(s)) {
+          sb.AppendFormat("case {0}: {{", allSymbols.IndexOf(s)).BeginBlock();
+          sb.AppendFormat("var t = new {0}Tree();", s.Name).AppendLine();
+          var terminal = terminals.Single(t => t.Ident == s.Name);
+          foreach (var constr in terminal.Constraints) {
+            if (constr.Type == ConstraintNodeType.Set) {
+              sb.Append("{").BeginBlock();
+              sb.AppendFormat("var elements = problem.GetAllowed{0}_{1}().ToArray();", terminal.Ident, constr.Ident).AppendLine();
+              sb.AppendFormat("t.{0} = elements[random.Next(elements.Length)]; ", constr.Ident).EndBlock();
+              sb.AppendLine("}");
+            } else {
+              sb.Append("{").BeginBlock();
+              sb.AppendFormat(" var max = problem.GetMax{0}_{1}();", terminal.Ident, constr.Ident).AppendLine();
+              sb.AppendFormat(" var min = problem.GetMin{0}_{1}();", terminal.Ident, constr.Ident).AppendLine();
+              sb.AppendFormat("t.{0} = random.NextDouble() * (max - min) + min;", constr.Ident).EndBlock();
+              sb.AppendLine("}");
+            }
+          }
+          sb.AppendLine("return t;").EndBlock();
+          sb.Append("}");
+        }
+      }
+      return sb.ToString();
+    }
     private void GenerateReturnStatement(IEnumerable<int> idxs, SourceBuilder sb) {
       if (idxs.Count() == 1) {
