Index: /branches/HeuristicLab.Problems.GPDL/CodeGenerator/MonteCarloTreeSearchCodeGen.cs
===================================================================
--- /branches/HeuristicLab.Problems.GPDL/CodeGenerator/MonteCarloTreeSearchCodeGen.cs	(revision 10414)
+++ /branches/HeuristicLab.Problems.GPDL/CodeGenerator/MonteCarloTreeSearchCodeGen.cs	(revision 10415)
@@ -15,7 +15,13 @@
     public double sumQuality = 0.0;
     public double bestQuality = double.NegativeInfinity;
-    public bool ready;
+    public bool done;
     public SearchTreeNode[] children;
-
+    public double[] Ucb {
+      get {
+        return (from c in children
+                select ?IDENT?Solver.UCB(this, c)
+               ).ToArray();
+      }
+    }
     public SearchTreeNode() {
     }
@@ -37,6 +43,9 @@
 
     private void SampleTree(SearchTreeNode searchTree, Stack<Tuple<Tree, int, int>> extensionPoints) {
-      const int RANDOM_TRIES = 100;
-      if(extensionPoints.Count == 0) return; // nothing to do
+      const int RANDOM_TRIES = 1000;
+      if(extensionPoints.Count == 0) {
+        searchTree.done = true;
+        return; // nothing to do
+      }
       var extensionPoint = extensionPoints.Pop();
       Tree parent = extensionPoint.Item1;
@@ -44,5 +53,4 @@
       int subtreeIdx = extensionPoint.Item3;
       Tree t = null;
-      
       if(searchTree.tries < RANDOM_TRIES || Grammar.subtreeCount[state] == 0) {
         t = SampleTreeRandom(state);
@@ -53,4 +61,5 @@
             searchTree.children = new SearchTreeNode[] { new SearchTreeNode() } ;
           SampleTree(searchTree.children[0], extensionPoints);
+          if(searchTree.children[0].done) searchTree.done = true;
         } else {
           // fill up all remaining slots randomly
@@ -81,7 +90,7 @@
             extensionPoints.Push(Tuple.Create(t, Grammar.transition[state][i], i));
           }
-          SampleTree(searchTree, extensionPoints);
-        }
-      }
+          SampleTree(searchTree, extensionPoints);          
+        }
+      }      
       Debug.Assert(parent.subtrees[subtreeIdx] == null);
       parent.subtrees[subtreeIdx] = t;
@@ -95,29 +104,33 @@
         return altIdx;
       } else {
+        altIdx = Array.FindIndex(searchTree.children, (e) => !e.done && e.tries < 1000);
+        if(altIdx >= 0) return altIdx;
         // select the least sampled alternative
+        //altIdx = 0;
+        //int minSamples = searchTree.children[altIdx].tries;
+        //for(int idx = 1; idx < searchTree.children.Length; idx++) {
+        //  if(!searchTree.children[idx].done && searchTree.children[idx].tries < minSamples) {
+        //    minSamples = searchTree.children[idx].tries;
+        //    altIdx = idx;
+        //  }
+        //}
+        // select the alternative with the largest average
         altIdx = 0;
-        int minSamples = searchTree.children[altIdx].tries;
+        double bestAverage = UCB(searchTree, searchTree.children[altIdx]);
         for(int idx = 1; idx < searchTree.children.Length; idx++) {
-          if(searchTree.children[idx].tries < minSamples) {
-            minSamples = searchTree.children[idx].tries;
+          if (!searchTree.children[idx].done && UCB(searchTree, searchTree.children[idx]) > UCB(searchTree, searchTree.children[altIdx])) {
             altIdx = idx;
           }
         }
-        // select the alternative with the largest average
-        // altIdx = 0;
-        // double bestAverage = UCB(searchTree, searchTree.children[altIdx]);
-        // for(int idx = 1; idx < searchTree.children.Length; idx++) {
-        //   if (UCB(searchTree, searchTree.children[idx]) > UCB(searchTree, searchTree.children[altIdx])) {
-        //     altIdx = idx;
-        //   }
-        // }
+
+        searchTree.done = searchTree.children.All(c=>c.done);
         return altIdx;
       }
     }
 
-    private double UCB(SearchTreeNode parent, SearchTreeNode n) {
+    public static double UCB(SearchTreeNode parent, SearchTreeNode n) {
       Debug.Assert(parent.tries >= n.tries);
       Debug.Assert(n.tries > 0);
-      return n.sumQuality / n.tries + Math.Sqrt((2 * Math.Log(parent.tries)) / n.tries ); // constant is dependent fitness function values
+      return n.sumQuality / n.tries + Math.Sqrt((10 * Math.Log(parent.tries)) / n.tries ); // constant is dependent fitness function values
     }
 
@@ -144,10 +157,10 @@
         if(t.subtrees != null) { 
           Debug.Assert(t.subtrees.Length == 1);
-          if(searchTree.children!=null) {
+          if(searchTree.children != null) {
             trees.Push(t.subtrees[0]);
             UpdateSearchTree(searchTree.children[t.altIdx], trees, quality);
           }
         } else {
-          if(searchTree.children!=null) {
+          if(searchTree.children != null) {
             Debug.Assert(searchTree.children.Length == 1);
             UpdateSearchTree(searchTree.children[0], trees, quality);
@@ -225,5 +238,5 @@
       var sw = new System.Diagnostics.Stopwatch();
       sw.Start();
-      while (true) {
+      while (!searchTree.done) {
 
         int steps, depth;
@@ -297,5 +310,5 @@
             GenerateReturnStatement(terminalAltIndexes, sb);
             sb.Append("} else {");
-            GenerateReturnStatement(nonTerminalAltIndexes, sb);
+            GenerateReturnStatement(nonTerminalAltIndexes.Concat(terminalAltIndexes), sb);
             sb.Append("}").EndBlock();
           } else {
@@ -317,8 +330,9 @@
           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("}");
+              throw new NotImplementedException("Support for terminal symbols with attributes is not yet implemented.");
+              // 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 {
               throw new NotSupportedException("The MTCS solver does not support RANGE constraints.");
Index: /branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs
===================================================================
--- /branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs	(revision 10414)
+++ /branches/HeuristicLab.Problems.GPDL/CodeGenerator/ProblemCodeGen.cs	(revision 10415)
@@ -151,6 +151,6 @@
       // var randomSearchCodeGen = new RandomSearchCodeGen();
       // randomSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
-      //var bruteForceSearchCodeGen = new BruteForceCodeGen();
-      //bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
+      // var bruteForceSearchCodeGen = new BruteForceCodeGen();
+      // bruteForceSearchCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
       var mctsCodeGen = new MonteCarloTreeSearchCodeGen();
       mctsCodeGen.Generate(grammar, ast.Terminals.OfType<TerminalNode>(), ast.FitnessFunctionNode.Maximization, solverSourceCode);
Index: /branches/HeuristicLab.Problems.GPDL/Examples/OneMaxBinary.txt
===================================================================
--- /branches/HeuristicLab.Problems.GPDL/Examples/OneMaxBinary.txt	(revision 10415)
+++ /branches/HeuristicLab.Problems.GPDL/Examples/OneMaxBinary.txt	(revision 10415)
@@ -0,0 +1,57 @@
+// special adaptation of the one-max problem
+// must find maximal number of 1-terminals
+// optimal solution = 32, number of solutions 2^32
+PROBLEM OneMaxBinary
+
+NONTERMINALS
+  S<<out int n>>.
+  U<<out int n>>.
+  V<<out int n>>.
+  W<<out int n>>.
+  X<<out int n>>.
+  Y<<out int n>>.
+  T<<out int n>>.
+
+TERMINALS
+  A.
+  B.
+
+RULES
+  S<<out int n>> =                                LOCAL << int n1, n2; >>
+    U<<out n1>> U<<out n2>>                       SEM << n = n1 + n2; >>
+  .
+  U<<out int n>> =                                LOCAL << int n1, n2; >>
+    V<<out n1>> V<<out n2>>                       SEM << n = n1 + n2; >>
+  .
+  V<<out int n>> =                                LOCAL << int n1, n2; >>
+    W<<out n1>> W<<out n2>>                       SEM << n = n1 + n2; >>
+  .
+  W<<out int n>> =                                LOCAL << int n1, n2; >>
+    X<<out n1>> X<<out n2>>                       SEM << n = n1 + n2; >>
+  .
+  // 2^32 solutions
+  X<<out int n>> =                                LOCAL << int n1, n2; >>
+    T<<out n1>> T<<out n2>>                       SEM << n = n1 + n2; >>
+  .
+
+  // uncomment for 2^64 solutions
+  // X<<out int n>> =                                LOCAL << int n1, n2; >>
+  //   Y<<out n1>> Y<<out n2>>                       SEM << n = n1 + n2; >>
+  // .
+
+  Y<<out int n>> =                                LOCAL << int n1, n2; >>
+    T<<out n1>> T<<out n2>>                       SEM << n = n1 + n2; >>
+  .
+
+  T<<out int n>> = 
+    A                                             SEM << n = 1; >>
+    | B                                           SEM << n = 0; >>
+  .
+
+MAXIMIZE
+  << 
+    int n;
+    S(out n);
+    return (double) n;
+  >>
+END OneMaxBinary.
Index: /branches/HeuristicLab.Problems.GPDL/Examples/OneMaxTernary.txt
===================================================================
--- /branches/HeuristicLab.Problems.GPDL/Examples/OneMaxTernary.txt	(revision 10415)
+++ /branches/HeuristicLab.Problems.GPDL/Examples/OneMaxTernary.txt	(revision 10415)
@@ -0,0 +1,47 @@
+// special adaptation of the one-max problem
+// must find maximal number of 1-terminals
+// optimal solution = 243, number of solutions 3^243
+PROBLEM OneMaxTernary
+
+NONTERMINALS
+  S<<out int n>>.
+  U<<out int n>>.
+  V<<out int n>>.
+  W<<out int n>>.
+  X<<out int n>>.
+  T<<out int n>>.
+
+TERMINALS
+  A. B. C.
+
+RULES
+  S<<out int n>> =                                LOCAL << int n1, n2, n3; >>
+    U<<out n1>> U<<out n2>> U<<out n3>>           SEM << n = n1 + n2 + n3; >>
+  .
+  U<<out int n>> =                                LOCAL << int n1, n2, n3; >>
+    V<<out n1>> V<<out n2>> V<<out n3>>           SEM << n = n1 + n2 + n3; >>
+  .
+  V<<out int n>> =                                LOCAL << int n1, n2, n3; >>
+    W<<out n1>> W<<out n2>> W<<out n3>>           SEM << n = n1 + n2 + n3; >>
+  .
+  W<<out int n>> =                                LOCAL << int n1, n2, n3; >>
+    X<<out n1>> X<<out n2>> X<<out n3>>           SEM << n = n1 + n2 + n3; >>
+  .
+
+  X<<out int n>> =                                LOCAL << int n1, n2, n3; >>
+    T<<out n1>> T<<out n2>> T<<out n3>>           SEM << n = n1 + n2 + n3; >>
+  .
+
+  T<<out int n>> =     
+    A                                             SEM << n = 0; >>
+    | B                                           SEM << n = 1; >>
+    | C                                           SEM << n = 0; >>
+  .
+
+MAXIMIZE
+  << 
+    int n;
+    S(out n);
+    return (double) n;
+  >>
+END OneMaxTernary.
Index: /branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.sln
===================================================================
--- /branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.sln	(revision 10414)
+++ /branches/HeuristicLab.Problems.GPDL/HeuristicLab.Problems.GPDL.sln	(revision 10415)
@@ -43,4 +43,5 @@
 		Examples\multi-output-multiplier.txt = Examples\multi-output-multiplier.txt
 		Examples\Multiplexer.txt = Examples\Multiplexer.txt
+		Examples\OneMaxBinary.txt = Examples\OneMaxBinary.txt
 		Examples\OnlyTerminals.txt = Examples\OnlyTerminals.txt
 		Examples\Rastrigin.txt = Examples\Rastrigin.txt
