Index: /branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs
===================================================================
--- /branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs	(revision 15419)
+++ /branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs	(revision 15420)
@@ -385,4 +385,5 @@
 
     public void Goto(int targetState) {
+      Debug.Assert(followStates[CurrentState].Contains(targetState));
       if (actions[CurrentState, targetState] != null)
         actions[CurrentState, targetState].ForEach(a => a()); // execute all actions
Index: /branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs
===================================================================
--- /branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs	(revision 15419)
+++ /branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs	(revision 15420)
@@ -71,4 +71,5 @@
     // TODO: is it OK to initialize all constants to 1 (Obj 2)?
     // TODO: improve memory usage
+    // TODO: support empty test partition
     #region static API
 
@@ -138,4 +139,10 @@
       private readonly double[] predBuf, testPredBuf;
       private readonly double[][] gradBuf;
+
+      // debugging stats
+      // calculate for each level the number of alternatives the average 'inequality' of tries and 'inequality' of quality over the alternatives for each trie
+      // inequality can be calculated using the Gini coefficient
+      internal readonly double[] giniCoeffs = new double[100];
+      
 
       public State(IRegressionProblemData problemData, uint randSeed, int maxVariables, bool scaleVariables,
@@ -436,5 +443,17 @@
         }
       }
+
       #endregion
+
+#if DEBUG
+      internal void ClearStats() {
+        for (int i = 0; i < giniCoeffs.Length; i++) giniCoeffs[i] = -1;
+      }
+      internal void WriteStats() {
+        Console.WriteLine(string.Join("\t", giniCoeffs.TakeWhile(x => x >= 0).Select(x => string.Format("{0:N3}", x))));
+      }
+
+#endif
+
     }
 
@@ -496,4 +515,7 @@
       bool success = false;
       do {
+#if DEBUG
+        mctsState.ClearStats();
+#endif
         automaton.Reset();
         success = TryTreeSearchRec2(rand, tree, automaton, eval, treePolicy, mctsState, out q);
@@ -502,11 +524,13 @@
       mctsState.effectiveRollouts++;
 
+#if DEBUG
+      mctsState.WriteStats();
+#endif
       //if (mctsState.effectiveRollouts % 100 == 1) {
-        // Console.WriteLine(WriteTree(tree, mctsState));
-        // Console.WriteLine(TraceTree(tree, mctsState));
+      // Console.WriteLine(WriteTree(tree, mctsState));
+      // Console.WriteLine(TraceTree(tree, mctsState));
       //}
       return q;
     }
-
 
     // search forward
@@ -540,4 +564,8 @@
             selectedIdx = treePolicy.Select(state.children[tree].Select(ch => ch.actionStatistics), rand);
           }
+
+          // STATS
+          state.giniCoeffs[tree.level] = InequalityCoefficient(state.children[tree].Select(ch => (double)ch.actionStatistics.AverageQuality));
+
           tree = state.children[tree][selectedIdx];
 
@@ -548,5 +576,6 @@
           int nFs;
           automaton.FollowStates(automaton.CurrentState, out possibleFollowStates, out nFs);
-          while (nFs == 1 && !automaton.IsEvalState(possibleFollowStates[0]) && !automaton.IsFinalState(possibleFollowStates[0])) {
+          while (automaton.CurrentState != tree.state && nFs == 1 && 
+            !automaton.IsEvalState(possibleFollowStates[0]) && !automaton.IsFinalState(possibleFollowStates[0])) {
             automaton.Goto(possibleFollowStates[0]);
             automaton.FollowStates(automaton.CurrentState, out possibleFollowStates, out nFs);
@@ -658,4 +687,17 @@
     }
 
+    private static double InequalityCoefficient(IEnumerable<double> xs) {
+      var arr = xs.ToArray();
+      var sad = 0.0;
+      var sum = 0.0;
+
+      for(int i=0;i<arr.Length;i++) {
+        for(int j=0;j<arr.Length;j++) {
+          sad += Math.Abs(arr[i] - arr[j]);
+          sum += arr[j];
+        }
+      }
+      return 0.5 * sad / sum;      
+    }
 
     private static double TransformQuality(double q) {
@@ -871,4 +913,5 @@
       return Disassembler.CodeToString(code);
     }
+
 
     private static string WriteStatistics(Tree tree, State state) {
Index: /branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs
===================================================================
--- /branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs	(revision 15419)
+++ /branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs	(revision 15420)
@@ -1,3 +1,4 @@
 ﻿using System;
+using System.Collections.Generic;
 using System.Linq;
 using System.Threading;
@@ -758,9 +759,48 @@
     [TestMethod]
     [TestCategory("Algorithms.DataAnalysis")]
-    [TestProperty("Time", "short")]
-    public void MctsSymbReg_NoConstants_Poly10() {
+    [TestProperty("Time", "long")]
+    public void MctsSymbReg_NoConstants_Poly10_250rows() {
       var provider = new HeuristicLab.Problems.Instances.DataAnalysis.VariousInstanceProvider(seed: 1234);
       var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("Poly-10")));
+      regProblem.TrainingPartition.Start = 0;
+      regProblem.TrainingPartition.End = regProblem.Dataset.Rows;
+      regProblem.TestPartition.Start = 0;
+      regProblem.TestPartition.End = 2;
       TestMctsWithoutConstants(regProblem, nVarRefs: 15, iterations: 200000, allowExp: false, allowLog: false, allowInv: false);
+    }
+    [TestMethod]
+    [TestCategory("Algorithms.DataAnalysis")]
+    [TestProperty("Time", "long")]
+    public void MctsSymbReg_NoConstants_Poly10_10000rows() {
+      // as poly-10 but more rows
+      var rand = new FastRandom(1234);
+      var x1 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x2 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x3 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x4 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x5 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x6 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x7 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x8 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x9 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var x10 = Enumerable.Range(0, 10000).Select(_ => rand.NextDouble()).ToList();
+      var ys = new List<double>();
+      for (int i = 0; i < x1.Count; i++) {
+        ys.Add(x1[i] * x2[i] + x3[i] * x4[i] + x5[i] * x6[i] + x1[i] * x7[i] * x9[i] + x3[i] * x6[i] * x10[i]);
+      }
+
+      var ds = new Dataset(new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "y" }, 
+        new[] { x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, ys });
+
+
+      var problemData = new RegressionProblemData(ds, new string[] { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j"}, "y");
+
+      problemData.TrainingPartition.Start = 0;
+      problemData.TrainingPartition.End = problemData.Dataset.Rows;
+      problemData.TestPartition.Start = 0;
+      problemData.TestPartition.End = 2; // must not be empty
+
+
+      TestMctsWithoutConstants(problemData, nVarRefs: 15, iterations: 100000, allowExp: false, allowLog: false, allowInv: false);
     }
 
@@ -797,14 +837,14 @@
       var @as = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
       var bs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
-      var cs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() *1.0e-3).ToList();
-      var ds = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() ).ToList();
-      var es = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() ).ToList();
+      var cs = Enumerable.Range(0, 100).Select(_ => rand.NextDouble() * 1.0e-3).ToList();
+      var ds = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
+      var es = Enumerable.Range(0, 100).Select(_ => rand.NextDouble()).ToList();
       var ys = new double[@as.Count];
-      for(int i=0;i<ys.Length;i++)
-        ys[i] = @as[i] + bs[i] + @as[i]*bs[i]*cs[i];
+      for (int i = 0; i < ys.Length; i++)
+        ys[i] = @as[i] + bs[i] + @as[i] * bs[i] * cs[i];
 
       var dataset = new Dataset(new string[] { "a", "b", "c", "d", "e", "y" }, new[] { @as, bs, cs, ds, es, ys.ToList() });
 
-      var problemData = new RegressionProblemData(dataset, new string[] { "a", "b","c","d","e" }, "y");
+      var problemData = new RegressionProblemData(dataset, new string[] { "a", "b", "c", "d", "e" }, "y");
 
 
@@ -854,5 +894,5 @@
       var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
       var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F7 ")));
-      TestMcts(regProblem, maxVariableReferences:5, allowExp: false, allowLog: true, allowInv: false);
+      TestMcts(regProblem, maxVariableReferences: 5, allowExp: false, allowLog: true, allowInv: false);
     }
     [TestMethod]
@@ -895,5 +935,5 @@
       var provider = new HeuristicLab.Problems.Instances.DataAnalysis.NguyenInstanceProvider(seed: 1234);
       var regProblem = provider.LoadData(provider.GetDataDescriptors().Single(x => x.Name.Contains("F11 ")));
-      TestMcts(regProblem, maxVariableReferences: 5, allowExp: true, allowLog: true, allowInv: false); 
+      TestMcts(regProblem, maxVariableReferences: 5, allowExp: true, allowLog: true, allowInv: false);
     }
     [TestMethod]
