Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs	(revision 3296)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs	(revision 3297)
@@ -119,5 +119,4 @@
       // add new function name to original branch
       selectedBody.AddDynamicSymbol(functionName, argumentBranches.Count);
-      Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree));
       return true;
     }
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 3296)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 3297)
@@ -57,5 +57,4 @@
         }
       } while (tree.Root.SubTrees.Count == 0 || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
-      System.Diagnostics.Debug.Assert(grammar.IsValidExpression(tree));
       return tree;
     }
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs	(revision 3296)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs	(revision 3297)
@@ -74,5 +74,8 @@
                             select branch;
 
-      if (allowedBranches.Count() > 0) {
+      if (allowedBranches.Count() == 0) {
+        success = false;
+        return parent0;
+      } else {
         var selectedBranch = SelectRandomBranch(random, allowedBranches, internalCrossoverPointProbability);
 
@@ -84,7 +87,4 @@
         return parent0;
       }
-
-      success = false;
-      return parent0;
     }
 
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.Tests.csproj
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.Tests.csproj	(revision 3296)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.Tests.csproj	(revision 3297)
@@ -39,4 +39,6 @@
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="SubroutineCreaterTest.cs" />
+    <Compile Include="SubtreeCrossoverTest.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="ProbabilisticTreeCreaterTest.cs" />
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs	(revision 3296)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs	(revision 3297)
@@ -6,4 +6,5 @@
 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
 using HeuristicLab.Random;
+using System.Diagnostics;
 
 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
@@ -18,4 +19,6 @@
         randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
       }
+      foreach (var tree in randomTrees)
+        Assert.IsTrue(grammar.IsValidExpression(tree));
     }
 
@@ -77,5 +80,5 @@
 
     [TestMethod()]
-    public void SizeDistributionTest() {
+    public void ProbabilisticTreeCreaterSizeDistributionTest() {
       int[] histogram = new int[105 / 5];
       for (int i = 0; i < randomTrees.Count; i++) {
@@ -92,5 +95,5 @@
 
     [TestMethod()]
-    public void FunctionDistributionTest() {
+    public void ProbabilisticTreeCreaterFunctionDistributionTest() {
       Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
       double n = 0.0;
@@ -115,5 +118,5 @@
 
     [TestMethod()]
-    public void NumberOfSubTreesDistributionTest() {
+    public void ProbabilisticTreeCreaterNumberOfSubTreesDistributionTest() {
       Dictionary<int, int> occurances = new Dictionary<int, int>();
       double n = 0.0;
@@ -137,5 +140,5 @@
 
     [TestMethod()]
-    public void TerminalDistributionTest() {
+    public void ProbabilisticTreeCreaterTerminalDistributionTest() {
       Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
       double n = 0.0;
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs	(revision 3297)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs	(revision 3297)
@@ -0,0 +1,177 @@
+﻿using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Random;
+using System.Diagnostics;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
+  [TestClass]
+  public class SubroutineCreaterTest {
+    public SubroutineCreaterTest() {
+    }
+
+    private TestContext testContextInstance;
+
+    /// <summary>
+    ///Gets or sets the test context which provides
+    ///information about and functionality for the current test run.
+    ///</summary>
+    public TestContext TestContext {
+      get {
+        return testContextInstance;
+      }
+      set {
+        testContextInstance = value;
+      }
+    }
+
+    [ClassInitialize()]
+    public static void SubroutineCreaterTestInitialize(TestContext testContext) {
+      var randomTrees = new List<SymbolicExpressionTree>();
+      subroutineTrees = new List<SymbolicExpressionTree>();
+      int populationSize = 1000;
+      var grammar = new TestGrammar();
+      var random = new MersenneTwister();
+      for (int i = 0; i < populationSize; i++) {
+        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
+      }
+      var newPopulation = new List<SymbolicExpressionTree>();
+      for (int i = 0; i < populationSize; i++) {
+        var par0 = (SymbolicExpressionTree)randomTrees[random.Next(populationSize)].Clone();
+        bool success = SubroutineCreater.CreateSubroutine(random, par0, grammar, 100, 10, 3, 3);
+        Assert.IsTrue(grammar.IsValidExpression(par0));
+        subroutineTrees.Add(par0);
+      }
+    }
+
+
+
+    private static List<SymbolicExpressionTree> subroutineTrees;
+    private static int failedEvents;
+
+    private class Addition : Symbol { }
+    private class Subtraction : Symbol { }
+    private class Multiplication : Symbol { }
+    private class Division : Symbol { }
+    private class Terminal : Symbol { }
+
+    private class TestGrammar : DefaultSymbolicExpressionGrammar {
+      public TestGrammar()
+        : base(0, 0, 0, 0) {
+        Initialize();
+      }
+
+      private void Initialize() {
+        var add = new Addition();
+        var sub = new Subtraction();
+        var mul = new Multiplication();
+        var div = new Division();
+        var terminal = new Terminal();
+
+        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
+        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
+        allSymbols.ForEach(s => AddAllowedSymbols(StartSymbol, 0, s));
+
+        SetMinSubTreeCount(terminal, 0);
+        SetMaxSubTreeCount(terminal, 0);
+        int maxSubTrees = 3;
+        foreach (var functionSymbol in functionSymbols) {
+          SetMinSubTreeCount(functionSymbol, 1);
+          SetMaxSubTreeCount(functionSymbol, maxSubTrees);
+          foreach (var childSymbol in allSymbols) {
+            for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
+              AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
+            }
+          }
+        }
+      }
+    }
+
+    [TestMethod()]
+    public void SubroutineCreaterSizeDistributionTest() {
+      int[] histogram = new int[105 / 5];
+      for (int i = 0; i < subroutineTrees.Count; i++) {
+        histogram[subroutineTrees[i].Size / 5]++;
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      for (int i = 0; i < histogram.Length; i++) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
+        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)subroutineTrees.Count);
+      }
+      Assert.Inconclusive("Size distribution of SubroutineCreater: " + strBuilder);
+    }
+
+    [TestMethod()]
+    public void SubroutineCreaterFunctionDistributionTest() {
+      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
+      double n = 0.0;
+      for (int i = 0; i < subroutineTrees.Count; i++) {
+        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
+          if (node.SubTrees.Count > 0) {
+            if (!occurances.ContainsKey(node.Symbol))
+              occurances[node.Symbol] = 0;
+            occurances[node.Symbol]++;
+            n++;
+          }
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var function in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(function.Name); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
+      }
+      Assert.Inconclusive("Function distribution of SubroutineCreater: " + strBuilder);
+    }
+
+    [TestMethod()]
+    public void SubroutineCreaterNumberOfSubTreesDistributionTest() {
+      Dictionary<int, int> occurances = new Dictionary<int, int>();
+      double n = 0.0;
+      for (int i = 0; i < subroutineTrees.Count; i++) {
+        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
+          if (!occurances.ContainsKey(node.SubTrees.Count))
+            occurances[node.SubTrees.Count] = 0;
+          occurances[node.SubTrees.Count]++;
+          n++;
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var arity in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(arity); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
+      }
+      Assert.Inconclusive("Distribution of function arities of SubroutineCreater: " + strBuilder);
+    }
+
+
+    [TestMethod()]
+    public void SubroutineCreaterTerminalDistributionTest() {
+      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
+      double n = 0.0;
+      for (int i = 0; i < subroutineTrees.Count; i++) {
+        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
+          if (node.SubTrees.Count == 0) {
+            if (!occurances.ContainsKey(node.Symbol))
+              occurances[node.Symbol] = 0;
+            occurances[node.Symbol]++;
+            n++;
+          }
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var function in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(function.Name); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
+      }
+      Assert.Inconclusive("Terminal distribution of SubroutineCreater: " + strBuilder);
+    }
+  }
+}
Index: trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs
===================================================================
--- trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs	(revision 3297)
+++ trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs	(revision 3297)
@@ -0,0 +1,191 @@
+﻿using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Random;
+using System.Diagnostics;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
+  [TestClass]
+  public class SubtreeCrossoverTest {
+    public SubtreeCrossoverTest() {
+    }
+
+    private TestContext testContextInstance;
+
+    /// <summary>
+    ///Gets or sets the test context which provides
+    ///information about and functionality for the current test run.
+    ///</summary>
+    public TestContext TestContext {
+      get {
+        return testContextInstance;
+      }
+      set {
+        testContextInstance = value;
+      }
+    }
+
+    [ClassInitialize()]
+    public static void SubtreeCrossoverTestInitialize(TestContext testContext) {
+      crossoverTrees = new List<SymbolicExpressionTree>();
+      int populationSize = 1000;
+      int generations = 5;
+      var grammar = new TestGrammar();
+      var random = new MersenneTwister();
+      for (int i = 0; i < populationSize; i++) {
+        crossoverTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
+      }
+      Stopwatch stopwatch = new Stopwatch();
+      stopwatch.Start();
+      for (int gCount = 0; gCount < generations; gCount++) {
+        var newPopulation = new List<SymbolicExpressionTree>();
+        for (int i = 0; i < populationSize; i++) {
+          var par0 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
+          var par1 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
+          bool success;
+          newPopulation.Add(SubtreeCrossover.Cross(random, grammar, par0, par1, 0.9, 100, 10, out success));
+        }
+        crossoverTrees = newPopulation;
+      }
+      stopwatch.Stop();
+      foreach (var tree in crossoverTrees)
+        Assert.IsTrue(grammar.IsValidExpression(tree));
+      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;
+    }
+
+
+
+    private static List<SymbolicExpressionTree> crossoverTrees;
+    private static double msPerCrossoverEvent;
+
+    private class Addition : Symbol { }
+    private class Subtraction : Symbol { }
+    private class Multiplication : Symbol { }
+    private class Division : Symbol { }
+    private class Terminal : Symbol { }
+
+    private class TestGrammar : DefaultSymbolicExpressionGrammar {
+      public TestGrammar()
+        : base(0, 0, 0, 0) {
+        Initialize();
+      }
+
+      private void Initialize() {
+        var add = new Addition();
+        var sub = new Subtraction();
+        var mul = new Multiplication();
+        var div = new Division();
+        var terminal = new Terminal();
+
+        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
+        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
+        allSymbols.ForEach(s => AddAllowedSymbols(StartSymbol, 0, s));
+
+        SetMinSubTreeCount(terminal, 0);
+        SetMaxSubTreeCount(terminal, 0);
+        int maxSubTrees = 3;
+        foreach (var functionSymbol in functionSymbols) {
+          SetMinSubTreeCount(functionSymbol, 1);
+          SetMaxSubTreeCount(functionSymbol, maxSubTrees);
+          foreach (var childSymbol in allSymbols) {
+            for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
+              AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
+            }
+          }
+        }
+      }
+    }
+
+    [TestMethod()]
+    public void SubtreeCrossoverSpeed() {
+      Assert.Inconclusive(msPerCrossoverEvent + " ms per crossover event (~" +
+        Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)");
+    }
+
+    [TestMethod()]
+    public void SubtreeCrossoverSizeDistributionTest() {
+      int[] histogram = new int[105 / 5];
+      for (int i = 0; i < crossoverTrees.Count; i++) {
+        histogram[crossoverTrees[i].Size / 5]++;
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      for (int i = 0; i < histogram.Length; i++) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
+        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)crossoverTrees.Count);
+      }
+      Assert.Inconclusive("Size distribution of SubtreeCrossover: " + strBuilder);
+    }
+
+    [TestMethod()]
+    public void SubtreeCrossoverFunctionDistributionTest() {
+      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
+      double n = 0.0;
+      for (int i = 0; i < crossoverTrees.Count; i++) {
+        foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
+          if (node.SubTrees.Count > 0) {
+            if (!occurances.ContainsKey(node.Symbol))
+              occurances[node.Symbol] = 0;
+            occurances[node.Symbol]++;
+            n++;
+          }
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var function in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(function.Name); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
+      }
+      Assert.Inconclusive("Function distribution of SubtreeCrossover: " + strBuilder);
+    }
+
+    [TestMethod()]
+    public void SubtreeCrossoverNumberOfSubTreesDistributionTest() {
+      Dictionary<int, int> occurances = new Dictionary<int, int>();
+      double n = 0.0;
+      for (int i = 0; i < crossoverTrees.Count; i++) {
+        foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
+          if (!occurances.ContainsKey(node.SubTrees.Count))
+            occurances[node.SubTrees.Count] = 0;
+          occurances[node.SubTrees.Count]++;
+          n++;
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var arity in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(arity); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
+      }
+      Assert.Inconclusive("Distribution of function arities of SubtreeCrossover: " + strBuilder);
+    }
+
+
+    [TestMethod()]
+    public void SubtreeCrossoverTerminalDistributionTest() {
+      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
+      double n = 0.0;
+      for (int i = 0; i < crossoverTrees.Count; i++) {
+        foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
+          if (node.SubTrees.Count == 0) {
+            if (!occurances.ContainsKey(node.Symbol))
+              occurances[node.Symbol] = 0;
+            occurances[node.Symbol]++;
+            n++;
+          }
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var function in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(function.Name); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
+      }
+      Assert.Inconclusive("Terminal distribution of SubtreeCrossover: " + strBuilder);
+    }
+  }
+}
