Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentCreater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentCreater.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentCreater.cs	(revision 3360)
@@ -96,5 +96,5 @@
         // append a new argument branch after expanding all argument nodes
         var clonedBranch = (SymbolicExpressionTreeNode)replacedBranch.Clone();
-        ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees);
+        clonedBranch = ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees);
         invocationNode.InsertSubTree(newArgumentIndex, clonedBranch);
       }
@@ -113,8 +113,13 @@
       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
         // when the changed function is known in the branch then update the number of arguments
-        var matchingSymbol = subtree.Grammar.Symbols.Where(s => s.Name == selectedDefunBranch.FunctionName).SingleOrDefault();
+        var matchingSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).SingleOrDefault();
         if (matchingSymbol != null) {
           subtree.Grammar.SetMinSubtreeCount(matchingSymbol, selectedDefunBranch.NumberOfArguments);
           subtree.Grammar.SetMaxSubtreeCount(matchingSymbol, selectedDefunBranch.NumberOfArguments);
+          foreach (var child in subtree.GetAllowedSymbols(0)) {
+            for (int i = 0; i < subtree.Grammar.GetMaxSubtreeCount(matchingSymbol); i++) {
+              subtree.Grammar.SetAllowedChild(matchingSymbol, child, i);
+            }
+          }
         }
       }
@@ -122,16 +127,17 @@
     }
 
-    private static void ReplaceArgumentsInBranch(SymbolicExpressionTreeNode branch, IList<SymbolicExpressionTreeNode> argumentTrees) {
-      // check if any subtree is an argument node 
-      for (int subtreeIndex = 0; subtreeIndex < branch.SubTrees.Count; subtreeIndex++) {
-        var subtree = branch.SubTrees[subtreeIndex];
-        var argNode = subtree as ArgumentTreeNode;
-        if (argNode != null) {
-          // replace argument nodes by a clone of the original subtree that provided the result for the argument node
-          branch.SubTrees[subtreeIndex] = (SymbolicExpressionTreeNode)argumentTrees[argNode.Symbol.ArgumentIndex].Clone();
-        } else {
-          // recursively replace arguments in all branches
-          ReplaceArgumentsInBranch(subtree, argumentTrees);
+    private static SymbolicExpressionTreeNode ReplaceArgumentsInBranch(SymbolicExpressionTreeNode branch, IList<SymbolicExpressionTreeNode> argumentTrees) {
+      if (branch is ArgumentTreeNode) {
+        var argNode = branch as ArgumentTreeNode;
+        // replace argument nodes by a clone of the original subtree that provided the result for the argument node
+        return (SymbolicExpressionTreeNode)argumentTrees[argNode.Symbol.ArgumentIndex].Clone();
+      } else {
+        // call recursively for all subtree
+        List<SymbolicExpressionTreeNode> subtrees = new List<SymbolicExpressionTreeNode>(branch.SubTrees);
+        while (branch.SubTrees.Count > 0) branch.SubTrees.RemoveAt(0);
+        foreach (var subtree in subtrees) {
+          branch.AddSubTree(ReplaceArgumentsInBranch(subtree, argumentTrees));
         }
+        return branch;
       }
     }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDeleter.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDeleter.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDeleter.cs	(revision 3360)
@@ -65,10 +65,12 @@
         // argument deletion by consolidation is not possible => abort
         return false;
+      // the argument to be removed is always the one with the largest index 
+      // (otherwise we would have to decrement the index of the larger argument symbols)
       var removedArgument = (from sym in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
-                             select sym.ArgumentIndex).Distinct().SelectRandom(random);
+                             select sym.ArgumentIndex).Distinct().OrderBy(x => x).Last();
       // find invocations of the manipulated funcion and remove the specified argument tree
-      var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
-                            where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
-                            select node;
+      var invocationNodes = (from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
+                             where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
+                             select node).ToList();
       foreach (var invokeNode in invocationNodes) {
         invokeNode.RemoveSubTree(removedArgument);
@@ -78,15 +80,15 @@
 
       // delete the dynamic argument symbol that matches the argument to be removed
-      var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Where(s => s.ArgumentIndex == removedArgument).First();
+      var matchingSymbol = selectedDefunBranch.Grammar.Symbols.OfType<Argument>().Where(s => s.ArgumentIndex == removedArgument).Single();
       selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol);
+      selectedDefunBranch.NumberOfArguments--;
       // reduce arity in known functions of all root branches
       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
-        var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).FirstOrDefault();
+        var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).SingleOrDefault();
         if (matchingInvokeSymbol != null) {
-          subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments - 1);
-          subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments - 1);
+          subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
+          subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
         }
       }
-      selectedDefunBranch.NumberOfArguments--;
       return true;
     }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDuplicater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDuplicater.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDuplicater.cs	(revision 3360)
@@ -64,44 +64,59 @@
         return false;
 
-      var selectedDefunBranch = SelectRandomBranch(random, functionDefiningBranches);
-      var argumentNodes = from node in IterateNodesPrefix(selectedDefunBranch)
-                          where node is ArgumentTreeNode
-                          select (ArgumentTreeNode)node;
-      if (argumentNodes.Count() == 0 || argumentNodes.Count() >= maxFunctionArguments)
+      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
+      var argumentSymbols = selectedDefunBranch.Grammar.Symbols.OfType<Argument>();
+      if (argumentSymbols.Count() == 0 || argumentSymbols.Count() >= maxFunctionArguments)
         // when no argument or number of arguments is already at max allowed value => abort
         return false;
-      var distinctArgumentIndexes = (from node in argumentNodes
-                                     select node.ArgumentIndex).Distinct().ToList();
-      var selectedArgumentIndex = distinctArgumentIndexes[random.Next(distinctArgumentIndexes.Count)];
-      var newArgumentIndex = allowedArgumentIndexes.Except(from argNode in argumentNodes select argNode.ArgumentIndex).First();
+      var selectedArgumentSymbol = argumentSymbols.SelectRandom(random);
+      var takenIndexes = argumentSymbols.Select(s => s.ArgumentIndex);
+      var newArgumentIndex = allowedArgumentIndexes.Except(takenIndexes).First();
+
+      var newArgSymbol = new Argument(newArgumentIndex);
+
       // replace existing references to the original argument with references to the new argument randomly in the selectedBranch
+      var argumentNodes = selectedDefunBranch.IterateNodesPrefix().OfType<ArgumentTreeNode>();
       foreach (var argNode in argumentNodes) {
-        if (argNode.ArgumentIndex == selectedArgumentIndex) {
+        if (argNode.Symbol == selectedArgumentSymbol) {
           if (random.NextDouble() < 0.5) {
-            argNode.ArgumentIndex = newArgumentIndex;
+            argNode.Symbol = newArgSymbol;
           }
         }
       }
       // find invocations of the functions and duplicate the matching argument branch
-      var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix()
-                            let invokeNode = node as InvokeFunctionTreeNode
-                            where invokeNode != null
-                            where invokeNode.InvokedFunctionName == selectedDefunBranch.Name
-                            select invokeNode;
+      var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
+                            where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
+                            select node;
       foreach (var invokeNode in invocationNodes) {
-        var argumentBranch = invokeNode.SubTrees[selectedArgumentIndex];
+        var argumentBranch = invokeNode.SubTrees[selectedArgumentSymbol.ArgumentIndex];
         var clonedArgumentBranch = (SymbolicExpressionTreeNode)argumentBranch.Clone();
         invokeNode.InsertSubTree(newArgumentIndex, clonedArgumentBranch);
       }
-      // register the new argument node and increase the number of arguments of the ADF
-      selectedDefunBranch.AddDynamicSymbol("ARG" + newArgumentIndex);
+      // register the new argument symbol and increase the number of arguments of the ADF
+      selectedDefunBranch.Grammar.AddSymbol(newArgSymbol);
+      selectedDefunBranch.Grammar.SetMinSubtreeCount(newArgSymbol, 0);
+      selectedDefunBranch.Grammar.SetMaxSubtreeCount(newArgSymbol, 0);
+      // allow the argument as child of any other symbol
+      foreach (var symb in selectedDefunBranch.Grammar.Symbols)
+        for (int i = 0; i < selectedDefunBranch.Grammar.GetMaxSubtreeCount(symb); i++) {
+          selectedDefunBranch.Grammar.SetAllowedChild(symb, newArgSymbol, i);
+        }
       selectedDefunBranch.NumberOfArguments++;
+
       // increase the arity of the changed ADF in all branches that can use this ADF
       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
-        if (subtree.DynamicSymbols.Contains(selectedDefunBranch.Name)) {
-          subtree.SetDynamicSymbolArgumentCount(selectedDefunBranch.Name, selectedDefunBranch.NumberOfArguments);
+        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
+                                    where symb.FunctionName == selectedDefunBranch.FunctionName
+                                    select symb).SingleOrDefault();
+        if (matchingInvokeSymbol != null) {
+          subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
+          subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments);
+          foreach (var child in subtree.GetAllowedSymbols(0)) {
+            for (int i = 0; i < subtree.Grammar.GetMaxSubtreeCount(matchingInvokeSymbol); i++) {
+              subtree.Grammar.SetAllowedChild(matchingInvokeSymbol, child, i);
+            }
+          }
         }
       }
-      Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree));
       return true;
     }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/GrammarModifier.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/GrammarModifier.cs	(revision 3360)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/GrammarModifier.cs	(revision 3360)
@@ -0,0 +1,79 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Linq;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
+using HeuristicLab.Operators;
+using HeuristicLab.Optimization;
+using HeuristicLab.Parameters;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
+using System.Collections.Generic;
+using System.Text;
+using System.Diagnostics;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators {
+  public static class GrammarModifier {
+    public static void AddDynamicSymbol(ISymbolicExpressionGrammar grammar, Symbol classRepresentative, string symbolName, int nArgs) {
+      var invokeSym = new InvokeFunction(symbolName);
+      grammar.AddSymbol(invokeSym);
+      grammar.SetMinSubtreeCount(invokeSym, nArgs);
+      grammar.SetMaxSubtreeCount(invokeSym, nArgs);
+      SetSyntaxConstraints(grammar, classRepresentative, invokeSym);
+    }
+
+
+    public static void AddDynamicArguments(ISymbolicExpressionGrammar grammar, Symbol classRepresentative, IEnumerable<int> argumentIndexes) {
+      foreach (int argIndex in argumentIndexes) {
+        var argSymbol = new Argument(argIndex);
+        grammar.AddSymbol(argSymbol);
+        grammar.SetMinSubtreeCount(argSymbol, 0);
+        grammar.SetMaxSubtreeCount(argSymbol, 0);
+        SetSyntaxConstraints(grammar, classRepresentative, argSymbol);
+      }
+    }
+
+    private static void SetSyntaxConstraints(ISymbolicExpressionGrammar grammar, Symbol classRepresentative, Symbol newSymbol) {
+      // allow symbol as child of the representative first to make sure that the symbol 
+      // is in the list of parents and children
+      for (int i = 0; i < grammar.GetMaxSubtreeCount(classRepresentative); i++) {
+        grammar.SetAllowedChild(classRepresentative, newSymbol, i);
+      }
+      // for all available symbols add the new symbol as allowed child iff the available symbol is an allowed child of the class representative
+      foreach (var parent in grammar.Symbols) {
+        if (grammar.IsAllowedChild(classRepresentative, parent, 0))
+          for (int arg = 0; arg < grammar.GetMaxSubtreeCount(parent); arg++) {
+            grammar.SetAllowedChild(parent, newSymbol, arg);
+          }
+      }
+      // for all available symbols add the new symbol as allowed parent iff the available symbol is an allowed child of the class representative
+      foreach (var child in grammar.Symbols) {
+        if (grammar.IsAllowedChild(classRepresentative, child, 0))
+          for (int arg = 0; arg < grammar.GetMaxSubtreeCount(newSymbol); arg++) {
+            grammar.SetAllowedChild(newSymbol, child, arg);
+          }
+      }
+    }
+
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/RandomArchitectureAlteringOperator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/RandomArchitectureAlteringOperator.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/RandomArchitectureAlteringOperator.cs	(revision 3360)
@@ -65,5 +65,5 @@
 
     public override void ModifyArchitecture(IRandom random, SymbolicExpressionTree tree, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight, IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments, out bool success) {
-      var selectedOperator = Operators[random.Next(Operators.Count)];
+      var selectedOperator = Operators.SelectRandom(random);
       selectedOperator.ModifyArchitecture(random, tree, grammar, maxTreeSize, maxTreeHeight, maxFunctionDefiningBranches, maxFunctionArguments, out success);
     }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs	(revision 3360)
@@ -42,4 +42,6 @@
   [StorableClass]
   public sealed class SubroutineCreater : SymbolicExpressionTreeArchitectureAlteringOperator {
+    private const double ARGUMENT_CUTOFF_PROBABILITY = 0.05;
+
     public override sealed void ModifyArchitecture(
       IRandom random,
@@ -62,13 +64,15 @@
         // allowed maximum number of ADF reached => abort
         return false;
-      string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefiningBranches) + 1).ToString(); // >= 100 functions => ###
+      if (symbolicExpressionTree.Size + 4 > maxTreeSize)
+        // defining a new function causes an size increase by 4 nodes (max) if the max tree size is reached => abort
+        return false;
+      string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefiningBranches * 10 - 1)).ToString(); // >= 100 functions => ###
       var allowedFunctionNames = from index in Enumerable.Range(0, maxFunctionDefiningBranches)
                                  select "ADF" + index.ToString(formatString);
-      // find all body branches
-      var bodies = from node in symbolicExpressionTree.IterateNodesPrefix()
-                   where node.Symbol is Defun || node.Symbol is StartSymbol
+
+      // select a random body (either the result producing branch or an ADF branch)
+      var bodies = from node in symbolicExpressionTree.Root.SubTrees
                    select new { Tree = node, Size = node.GetSize() };
       var totalNumberOfBodyNodes = bodies.Select(x => x.Size).Sum();
-      // select a random body
       int r = random.Next(totalNumberOfBodyNodes);
       int aggregatedNumberOfBodyNodes = 0;
@@ -81,24 +85,25 @@
       // sanity check
       if (selectedBody == null) throw new InvalidOperationException();
-      // select a random node in the selected branch
-      var allCutPoints = (from parent in selectedBody.IterateNodesPrefix()
-                          from subtree in parent.SubTrees
-                          select new { Parent = parent, ReplacedBranchIndex = parent.SubTrees.IndexOf(subtree), ReplacedBranch = subtree }).ToList();
-      if (allCutPoints.Count == 0)
+
+      // select a random cut point in the selected branch
+      var allCutPoints = from parent in selectedBody.IterateNodesPrefix()
+                         from subtree in parent.SubTrees
+                         select new { Parent = parent, ReplacedBranchIndex = parent.SubTrees.IndexOf(subtree), ReplacedBranch = subtree };
+      if (allCutPoints.Count() == 0)
         // no cut points => abort
         return false;
-      var selectedCutPoint = allCutPoints[random.Next(allCutPoints.Count)];
+      string newFunctionName = allowedFunctionNames.Except(functionDefiningBranches.Select(x => x.FunctionName)).First();
+      var selectedCutPoint = allCutPoints.SelectRandom(random);
       // select random branches as argument cut-off points (replaced by argument terminal nodes in the function)
-      List<SymbolicExpressionTreeNode> argumentBranches = SelectRandomArgumentBranches(selectedCutPoint.ReplacedBranch, random, 0.25, maxFunctionArguments);
-      string functionName = allowedFunctionNames.Except(functionDefiningBranches.Select(x => x.Name)).First();
+      List<SymbolicExpressionTreeNode> argumentBranches = SelectRandomArgumentBranches(selectedCutPoint.ReplacedBranch, random, ARGUMENT_CUTOFF_PROBABILITY, maxFunctionArguments);
       SymbolicExpressionTreeNode functionBody = selectedCutPoint.ReplacedBranch;
       // disconnect the function body from the tree
       selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint.ReplacedBranchIndex);
       // disconnect the argument branches from the function
-      DisconnectBranches(functionBody, argumentBranches);
-      // and insert a function invocation symbol instead
-      var invokeNode = (InvokeFunctionTreeNode)(new InvokeFunction()).CreateTreeNode();
-      invokeNode.InvokedFunctionName = functionName;
+      functionBody = DisconnectBranches(functionBody, argumentBranches);
+      // insert a function invocation symbol instead
+      var invokeNode = (InvokeFunctionTreeNode)(new InvokeFunction(newFunctionName)).CreateTreeNode();
       selectedCutPoint.Parent.InsertSubTree(selectedCutPoint.ReplacedBranchIndex, invokeNode);
+      // add the branches selected as argument as subtrees of the function invocation node
       foreach (var argumentBranch in argumentBranches)
         invokeNode.AddSubTree(argumentBranch);
@@ -106,21 +111,58 @@
       // insert a new function defining branch
       var defunNode = (DefunTreeNode)(new Defun()).CreateTreeNode();
-      defunNode.Name = functionName;
+      defunNode.FunctionName = newFunctionName;
       defunNode.AddSubTree(functionBody);
       symbolicExpressionTree.Root.AddSubTree(defunNode);
-      // copy known symbols from originating branch into new branch
-      foreach (var knownSymbol in selectedBody.DynamicSymbols) {
-        defunNode.AddDynamicSymbol(knownSymbol, selectedBody.GetDynamicSymbolArgumentCount(knownSymbol));
-      }
-      // add function arguments as known symbols to new branch
-      for (int i = 0; i < argumentBranches.Count; i++) {
-        defunNode.AddDynamicSymbol("ARG" + i);
-      }
-      // add new function name to original branch
-      selectedBody.AddDynamicSymbol(functionName, argumentBranches.Count);
+      // the grammar in the newly defined function is a clone of the grammar of the originating branch
+      defunNode.Grammar = (ISymbolicExpressionGrammar)selectedBody.Grammar.Clone();
+      // remove all argument symbols from grammar
+      var oldArgumentSymbols = defunNode.Grammar.Symbols.OfType<Argument>().ToList();
+      foreach (var oldArgSymb in oldArgumentSymbols)
+        defunNode.Grammar.RemoveSymbol(oldArgSymb);
+      // find unique argument indexes and matching symbols in the function defining branch 
+      var newArgumentIndexes = (from node in defunNode.IterateNodesPrefix().OfType<ArgumentTreeNode>()
+                                select node.Symbol.ArgumentIndex).Distinct();
+      // add argument symbols to grammar of function defining branch
+      GrammarModifier.AddDynamicArguments(defunNode.Grammar, defunNode.Symbol, newArgumentIndexes);
+      defunNode.NumberOfArguments = newArgumentIndexes.Count();
+      if (defunNode.NumberOfArguments != argumentBranches.Count) throw new InvalidOperationException();
+      // add invoke symbol for newly defined function to the original branch 
+      var allowedParents = from symb in selectedBody.Grammar.Symbols
+                           where !(symb is ProgramRootSymbol)
+                           select symb;
+      var allowedChildren = from symb in selectedBody.Grammar.Symbols
+                            where selectedBody.Grammar.IsAllowedChild(selectedBody.Symbol, symb, 0)
+                            select symb;
+      GrammarModifier.AddDynamicSymbol(selectedBody.Grammar, selectedBody.Symbol, defunNode.FunctionName, defunNode.NumberOfArguments);
+
+      // when the new function body was taken from another function definition
+      // add invoke symbol for newly defined function to all branches that are allowed to invoke the original branch
+      if (selectedBody.Symbol is Defun) {
+        var originalFunctionDefinition = selectedBody as DefunTreeNode;
+        foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
+          var originalBranchInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
+                                            where symb.FunctionName == originalFunctionDefinition.FunctionName
+                                            select symb).SingleOrDefault();
+          // when the original branch can be invoked from the subtree then also allow invocation of the function
+          if (originalBranchInvokeSymbol != null) {
+            allowedParents = from symb in subtree.Grammar.Symbols
+                             where !(symb is ProgramRootSymbol)
+                             select symb;
+            allowedChildren = from symb in subtree.Grammar.Symbols
+                              where subtree.Grammar.IsAllowedChild(subtree.Symbol, symb, 0)
+                              select symb;
+            GrammarModifier.AddDynamicSymbol(subtree.Grammar, subtree.Symbol, defunNode.FunctionName, defunNode.NumberOfArguments);
+          }
+        }
+      }
       return true;
     }
 
-    private static void DisconnectBranches(SymbolicExpressionTreeNode node, List<SymbolicExpressionTreeNode> argumentBranches) {
+    private static SymbolicExpressionTreeNode DisconnectBranches(SymbolicExpressionTreeNode node, List<SymbolicExpressionTreeNode> argumentBranches) {
+      if (argumentBranches.Contains(node)) {
+        var argumentIndex = argumentBranches.IndexOf(node);
+        var argSymbol = new Argument(argumentIndex);
+        return argSymbol.CreateTreeNode();
+      }
       // remove the subtrees so that we can clone only the root node
       List<SymbolicExpressionTreeNode> subtrees = new List<SymbolicExpressionTreeNode>(node.SubTrees);
@@ -128,16 +170,6 @@
       // recursively apply function for subtrees or append a argument terminal node
       foreach (var subtree in subtrees) {
-        if (argumentBranches.Contains(subtree)) {
-          node.AddSubTree(MakeArgumentNode(argumentBranches.IndexOf(subtree)));
-        } else {
-          DisconnectBranches(subtree, argumentBranches);
-          node.AddSubTree(subtree);
-        }
-      }
-    }
-
-    private static SymbolicExpressionTreeNode MakeArgumentNode(int argIndex) {
-      var node = (ArgumentTreeNode)(new Argument()).CreateTreeNode();
-      node.ArgumentIndex = argIndex;
+        node.AddSubTree(DisconnectBranches(subtree, argumentBranches));
+      }
       return node;
     }
@@ -145,24 +177,47 @@
     private static List<SymbolicExpressionTreeNode> SelectRandomArgumentBranches(SymbolicExpressionTreeNode selectedRoot,
       IRandom random,
-      double argumentProbability,
+      double cutProbability,
       int maxArguments) {
+      // breadth first determination of argument cut-off points
+      // we must make sure that we cut off all original argument nodes and that the number of new argument is smaller than the limit
       List<SymbolicExpressionTreeNode> argumentBranches = new List<SymbolicExpressionTreeNode>();
-      foreach (var subTree in selectedRoot.SubTrees) {
-        if (random.NextDouble() < argumentProbability) {
-          if (argumentBranches.Count < maxArguments)
-            argumentBranches.Add(subTree);
-        } else {
-          foreach (var argBranch in SelectRandomArgumentBranches(subTree, random, argumentProbability, maxArguments))
-            if (argumentBranches.Count < maxArguments)
-              argumentBranches.Add(argBranch);
+      if (selectedRoot is ArgumentTreeNode) {
+        argumentBranches.Add(selectedRoot);
+        return argumentBranches;
+      } else {
+        // get the number of argument nodes (which must be cut-off) in the sub-trees
+        var numberOfArgumentsInSubtrees = (from subtree in selectedRoot.SubTrees
+                                           let nArgumentsInTree = subtree.IterateNodesPrefix().OfType<ArgumentTreeNode>().Count()
+                                           select nArgumentsInTree).ToList();
+        // determine the minimal number of new argument nodes for each sub-tree
+        var minNewArgumentsForSubtrees = numberOfArgumentsInSubtrees.Select(x => x > 0 ? 1 : 0).ToList();
+        if (minNewArgumentsForSubtrees.Sum() > maxArguments) {
+          argumentBranches.Add(selectedRoot);
+          return argumentBranches;
         }
-      }
-      return argumentBranches;
-    }
-    
-    private static IEnumerable<string> UsedFunctionNames(SymbolicExpressionTree symbolicExpressionTree) {
-      return from node in symbolicExpressionTree.IterateNodesPrefix()
-             where node.Symbol is Defun
-             select ((DefunTreeNode)node).Name;
+        // cut-off in the sub-trees in random order
+        var randomIndexes = (from index in Enumerable.Range(0, selectedRoot.SubTrees.Count)
+                             select new { Index = index, OrderValue = random.NextDouble() }).OrderBy(x => x.OrderValue).Select(x => x.Index);
+        foreach (var subtreeIndex in randomIndexes) {
+          var subtree = selectedRoot.SubTrees[subtreeIndex];
+          minNewArgumentsForSubtrees[subtreeIndex] = 0;
+          // => cut-off at 0..n points somewhere in the current sub-tree
+          // determine the maximum number of new arguments that should be created in the branch
+          // as the maximum for the whole branch minus already added arguments minus minimal number of arguments still left
+          int maxArgumentsFromBranch = maxArguments - argumentBranches.Count - minNewArgumentsForSubtrees.Sum();
+          // when no argument is allowed from the current branch then we have to include the whole branch into the function
+          // otherwise: choose randomly wether to cut off immediately or wether to extend the function body into the branch
+          if (maxArgumentsFromBranch == 0) {
+            // don't cut at all => the whole sub-tree branch is included in the function body
+            // (we already checked ahead of time that there are no arguments left over in the subtree)
+          } else if (random.NextDouble() >= cutProbability) {
+            argumentBranches.AddRange(SelectRandomArgumentBranches(subtree, random, cutProbability, maxArgumentsFromBranch));
+          } else {
+            // cut-off at current sub-tree
+            argumentBranches.Add(subtree);
+          }
+        }
+        return argumentBranches;
+      }
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDeleter.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDeleter.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDeleter.cs	(revision 3360)
@@ -41,5 +41,4 @@
   [StorableClass]
   public sealed class SubroutineDeleter : SymbolicExpressionTreeArchitectureAlteringOperator {
-    private const int MAX_TRIES = 100;
     public override sealed void ModifyArchitecture(
       IRandom random,
@@ -68,39 +67,46 @@
       symbolicExpressionTree.Root.RemoveSubTree(defunSubtreeIndex);
 
-      // get all cut points that contain an invokation of the deleted defun
-      var invocationCutPoints = from node in symbolicExpressionTree.IterateNodesPrefix()
-                                where node.SubTrees.Count > 0
-                                from argIndex in Enumerable.Range(0, node.SubTrees.Count)
-                                let subtree = node.SubTrees[argIndex] as InvokeFunctionTreeNode
-                                where subtree != null
-                                where subtree.InvokedFunctionName == selectedDefunBranch.Name
-                                select new { Parent = node, ReplacedChildIndex = argIndex, ReplacedChild = subtree };
-      // deletion by random regeneration
-      foreach (var cutPoint in invocationCutPoints) {
-        SymbolicExpressionTreeNode replacementTree = null;
-        int targetSize = random.Next(cutPoint.ReplacedChild.GetSize());
-        int targetHeight = cutPoint.ReplacedChild.GetHeight();
-        int tries = 0;
-        do {
-          try {
-            replacementTree = ProbabilisticTreeCreator.PTC2(random, grammar, cutPoint.Parent.Symbol, targetSize, targetHeight);
-          }
-          catch (ArgumentException) {
-            // try different size
-            targetSize = random.Next(cutPoint.ReplacedChild.GetSize());
-            if (tries++ > MAX_TRIES) throw;
-          }
-        } while (replacementTree == null);
-        cutPoint.Parent.RemoveSubTree(cutPoint.ReplacedChildIndex);
-        cutPoint.Parent.InsertSubTree(cutPoint.ReplacedChildIndex, replacementTree);
-      }
       // remove references to deleted function
       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
-        if (subtree.DynamicSymbols.Contains(selectedDefunBranch.Name)) {
-          subtree.RemoveDynamicSymbol(selectedDefunBranch.Name);
+        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
+                                    where symb.FunctionName == selectedDefunBranch.FunctionName
+                                    select symb).SingleOrDefault();
+        if (matchingInvokeSymbol != null) {
+          subtree.Grammar.RemoveSymbol(matchingInvokeSymbol);
         }
       }
-      Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree));
+
+      DeletionByRandomRegeneration(random, symbolicExpressionTree, selectedDefunBranch);
       return true;
+    }
+
+    private static void DeletionByRandomRegeneration(IRandom random, SymbolicExpressionTree symbolicExpressionTree, DefunTreeNode selectedDefunBranch) {
+      // find first invocation and replace it with a randomly generated tree
+      // can't find all invocations in one step because once we replaced a top level invocation
+      // the invocations below it are removed already
+      var invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
+                                from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
+                                where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
+                                select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();
+      while (invocationCutPoint != null) {
+        // deletion by random regeneration
+        SymbolicExpressionTreeNode replacementTree = null;
+        // TODO: should weight symbols by tickets
+        var selectedSymbol = invocationCutPoint.Parent.GetAllowedSymbols(invocationCutPoint.ReplacedChildIndex).SelectRandom(random);
+
+        int minPossibleSize = invocationCutPoint.Parent.Grammar.GetMinExpressionLength(selectedSymbol);
+        int maxSize = Math.Max(minPossibleSize, invocationCutPoint.ReplacedChild.GetSize());
+        int minPossibleHeight = invocationCutPoint.Parent.Grammar.GetMinExpressionDepth(selectedSymbol);
+        int maxHeight = Math.Max(minPossibleHeight, invocationCutPoint.ReplacedChild.GetHeight());
+
+        replacementTree = ProbabilisticTreeCreator.PTC2(random, invocationCutPoint.Parent.Grammar, selectedSymbol, maxSize, maxHeight, 0, 0);
+        invocationCutPoint.Parent.RemoveSubTree(invocationCutPoint.ReplacedChildIndex);
+        invocationCutPoint.Parent.InsertSubTree(invocationCutPoint.ReplacedChildIndex, replacementTree);
+
+        invocationCutPoint = (from node in symbolicExpressionTree.IterateNodesPrefix()
+                              from subtree in node.SubTrees.OfType<InvokeFunctionTreeNode>()
+                              where subtree.Symbol.FunctionName == selectedDefunBranch.FunctionName
+                              select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(subtree), ReplacedChild = subtree }).FirstOrDefault();
+      }
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs	(revision 3360)
@@ -48,8 +48,8 @@
       IntValue maxFunctionDefiningBranches, IntValue maxFunctionArguments,
       out bool success) {
-      success = DuplicateRandomSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
+      success = DuplicateSubroutine(random, symbolicExpressionTree, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefiningBranches.Value, maxFunctionArguments.Value);
     }
 
-    public static bool DuplicateRandomSubroutine(
+    public static bool DuplicateSubroutine(
       IRandom random,
       SymbolicExpressionTree symbolicExpressionTree,
@@ -66,29 +66,32 @@
         return false;
       var selectedBranch = functionDefiningBranches.SelectRandom(random);
-      var clonedBranch = (DefunTreeNode)selectedBranch.Clone();
-      clonedBranch.Name = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First();
-      foreach (var node in symbolicExpressionTree.IterateNodesPrefix()) {
-        var invokeFunctionNode = node as InvokeFunctionTreeNode;
-        // find all invokations of the old function 
-        if (invokeFunctionNode != null && invokeFunctionNode.InvokedFunctionName == selectedBranch.Name) {
-          // add the new function name to the list of known functions in the branches that used the originating function
-          var branch = symbolicExpressionTree.GetTopLevelBranchOf(invokeFunctionNode);
-          branch.AddDynamicSymbol(clonedBranch.Name, clonedBranch.NumberOfArguments);
-          // flip coin wether to replace with newly defined function
-          if (random.NextDouble() < 0.5) {
-            invokeFunctionNode.InvokedFunctionName = clonedBranch.Name;
-          }
+      var duplicatedDefunBranch = (DefunTreeNode)selectedBranch.Clone();
+      string newFunctionName = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First();
+      duplicatedDefunBranch.FunctionName = newFunctionName;
+      symbolicExpressionTree.Root.SubTrees.Add(duplicatedDefunBranch);
+      duplicatedDefunBranch.Grammar = (ISymbolicExpressionGrammar)selectedBranch.Grammar.Clone();
+      // add an invoke symbol for each branch that is allowed to invoke the original function
+      foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
+        var matchingInvokeSymbol = (from symb in subtree.Grammar.Symbols.OfType<InvokeFunction>()
+                                    where symb.FunctionName == selectedBranch.FunctionName
+                                    select symb).SingleOrDefault();
+        if (matchingInvokeSymbol != null) {
+          GrammarModifier.AddDynamicSymbol(subtree.Grammar, subtree.Symbol, duplicatedDefunBranch.FunctionName, duplicatedDefunBranch.NumberOfArguments);
         }
       }
-      Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree));
+      // for all invoke nodes of the original function replace the invoke of the original function with an invoke of the new function randomly
+      var originalFunctionInvocations = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
+                                        where node.Symbol.FunctionName == selectedBranch.FunctionName
+                                        select node;
+      foreach (var originalFunctionInvokeNode in originalFunctionInvocations) {
+        var newInvokeSymbol = (from symb in originalFunctionInvokeNode.Grammar.Symbols.OfType<InvokeFunction>()
+                               where symb.FunctionName == duplicatedDefunBranch.FunctionName
+                               select symb).Single();
+        // flip coin wether to replace with newly defined function
+        if (random.NextDouble() < 0.5) {
+          originalFunctionInvokeNode.Symbol = newInvokeSymbol;
+        }
+      }
       return true;
-    }
-
-    private static bool ContainsNode(SymbolicExpressionTreeNode branch, SymbolicExpressionTreeNode node) {
-      if (branch == node) return true;
-      else foreach (var subtree in branch.SubTrees) {
-          if (ContainsNode(subtree, node)) return true;
-        }
-      return false;
     }
 
@@ -96,8 +99,6 @@
       return from node in symbolicExpressionTree.IterateNodesPrefix()
              where node.Symbol is Defun
-             select ((DefunTreeNode)node).Name;
+             select ((DefunTreeNode)node).FunctionName;
     }
-
-
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 3360)
@@ -29,4 +29,5 @@
 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
 using System.Text;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
 
 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
@@ -51,19 +52,6 @@
       int maxFunctionDefinitions, int maxFunctionArguments
       ) {
-      // tree size is limited by the grammar and by the explicit size constraints
-      int allowedMinSize = grammar.GetMinExpressionLength(grammar.StartSymbol);
-      int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(grammar.StartSymbol));
-      // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
-      int treeSize = random.Next(allowedMinSize, allowedMaxSize);
       SymbolicExpressionTree tree = new SymbolicExpressionTree();
-      do {
-        try {
-          tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1, maxFunctionDefinitions, maxFunctionArguments);
-        }
-        catch (ArgumentException) {
-          // try a different size
-          treeSize = random.Next(allowedMinSize, allowedMaxSize);
-        }
-      } while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
+      tree.Root = PTC2(random, grammar, grammar.StartSymbol, maxTreeSize, maxTreeHeight, maxFunctionDefinitions, maxFunctionArguments);
       return tree;
     }
@@ -74,10 +62,37 @@
       public int ExtensionPointDepth { get; set; }
     }
+
+    /// <summary>
+    /// Creates a random tree with <paramref name="maxTreeSize"/> and <paramref name="maxDepth"/>.
+    /// </summary>
+    /// <param name="random"></param>
+    /// <param name="grammar"></param>
+    /// <param name="rootSymbol"></param>
+    /// <param name="maxTreeSize"></param>
+    /// <param name="maxDepth"></param>
+    /// <param name="maxFunctionDefinitions"></param>
+    /// <param name="maxFunctionArguments"></param>
+    /// <returns></returns>
     public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol,
-      int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
-      SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode();
-      root.Grammar = grammar;
-      if (size <= 1 || maxDepth <= 1) return root;
-      CreateFullTreeFromSeed(random, root, size, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
+      int maxTreeSize, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
+      // tree size is limited by the grammar and by the explicit size constraints
+      int allowedMinSize = grammar.GetMinExpressionLength(rootSymbol);
+      int allowedMaxSize = Math.Min(maxTreeSize, grammar.GetMaxExpressionLength(rootSymbol));
+      // select a target tree size uniformly in the possible range (as determined by explicit limits and limits of the grammar)
+      int treeSize = random.Next(allowedMinSize, allowedMaxSize + 1);
+      SymbolicExpressionTreeNode root = null;
+      do {
+        try {
+          root = rootSymbol.CreateTreeNode();
+          root.Grammar = grammar;
+          if (treeSize <= 1 || maxDepth <= 1) return root;
+          CreateFullTreeFromSeed(random, root, treeSize, maxDepth, maxFunctionDefinitions, maxFunctionArguments);
+        }
+        catch (ArgumentException) {
+          // try a different size
+          root = null;
+          treeSize = random.Next(allowedMinSize, allowedMaxSize);
+        }
+      } while (root == null || root.GetSize() > maxTreeSize || root.GetHeight() > maxDepth);
       return root;
     }
@@ -105,12 +120,4 @@
         if (extensionDepth + parent.Grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) {
           ReplaceWithMinimalTree(random, root, parent, argumentIndex, maxFunctionDefinitions, maxFunctionArguments);
-          //parent.RemoveSubTree(argumentIndex);
-          //var allowedSymbols = from s in parent.Grammar.Symbols
-          //                     where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)
-          //                     select s;
-          //SymbolicExpressionTreeNode branch = CreateMinimalTree(random, parent, allowedSymbols);
-          //parent.InsertSubTree(argumentIndex, branch); // insert a smallest possible tree
-          //currentSize += branch.GetSize();
-          //totalListMinSize -= branch.GetSize();
         } else {
           var allowedSymbols = from s in parent.Grammar.Symbols
@@ -118,8 +125,4 @@
                                where parent.Grammar.GetMinExpressionDepth(s) + extensionDepth - 1 < maxDepth
                                where parent.Grammar.GetMaxExpressionLength(s) > size - totalListMinSize - currentSize
-                               /*||
-                                     totalListMinSize + currentSize >= size * 0.9 // if the necessary size is almost reached then also allow
-                               // terminals or terminal-branches*/
-                               // where !IsDynamicSymbol(s) || IsDynamicSymbolAllowed(grammar, root, parent, s)
                                select s;
           Symbol selectedSymbol = SelectRandomSymbol(random, allowedSymbols);
@@ -172,15 +175,25 @@
         var dummy = new SymbolicExpressionTreeNode();
         tree.AddSubTree(dummy);
-        dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone(); 
+        dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
         // replace the just inserted dummy by recursive application
         ReplaceWithMinimalTree(random, root, tree, i, maxFunctionDefinitions, maxFunctionArguments);
       }
     }
-   
+
     private static void InitializeNewTreeNode(IRandom random, SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode newTree, int maxFunctionDefinitions, int maxFunctionArguments) {
       // NB it is assumed that defuns are only allowed as children of root and nowhere else
       // also assumes that newTree is already attached to root somewhere
-      if (IsTopLevelBranch(root, newTree))
+      if (IsTopLevelBranch(root, newTree)) {
         newTree.Grammar = (ISymbolicExpressionGrammar)newTree.Grammar.Clone();
+
+        // allow invokes of existing ADFs with higher index
+        int argIndex = root.SubTrees.IndexOf(newTree);
+        for (int i = argIndex + 1; i < root.SubTrees.Count; i++) {
+          var otherDefunNode = root.SubTrees[i] as DefunTreeNode;
+          if (otherDefunNode != null) {
+            GrammarModifier.AddDynamicSymbol(newTree.Grammar, newTree.Symbol, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);
+          }
+        }
+      }
       if (newTree.Symbol is Defun) {
         var defunTree = newTree as DefunTreeNode;
@@ -197,62 +210,15 @@
         defunTree.NumberOfArguments = nArgs;
         if (nArgs > 0) {
-          AddDynamicArguments(defunTree.Grammar, nArgs);
-        }
+          GrammarModifier.AddDynamicArguments(defunTree.Grammar, defunTree.Symbol, Enumerable.Range(0, nArgs));
+        }
+        // in existing branches with smaller index allow invoke of current function
         int argIndex = root.SubTrees.IndexOf(newTree);
-        // allow invokes of ADFs with higher index
-        for (int i = argIndex + 1; i < root.SubTrees.Count; i++) {
-          var otherDefunNode = root.SubTrees[i] as DefunTreeNode;
-          if (otherDefunNode != null) {
-            var allowedParents = from sym in defunTree.Grammar.Symbols
-                                 where defunTree.Grammar.IsAllowedChild(defunTree.Symbol, sym, 0)
-                                 select sym;
-            var allowedChildren = allowedParents;
-            AddDynamicSymbol(defunTree.Grammar, allowedParents, allowedChildren, otherDefunNode.FunctionName, otherDefunNode.NumberOfArguments);
-          }
-        }
-        // make the ADF available as symbol for other branches (with smaller index to prevent recursions)
         for (int i = 0; i < argIndex; i++) {
           // if not dummy node
           if (root.SubTrees[i].Symbol != null) {
-            var topLevelGrammar = root.SubTrees[i].Grammar;
-            var allowedParents = from sym in root.SubTrees[i].Grammar.Symbols
-                                 where root.SubTrees[i].Grammar.IsAllowedChild(root.SubTrees[i].Symbol, sym, 0)
-                                 select sym;
-            var allowedChildren = allowedParents;
-
-            AddDynamicSymbol(topLevelGrammar, allowedParents, allowedChildren, functionName, nArgs);
+            var existingBranch = root.SubTrees[i];
+            GrammarModifier.AddDynamicSymbol(existingBranch.Grammar, existingBranch.Symbol, functionName, nArgs);
           }
         }
-      }
-    }
-
-    private static void AddDynamicSymbol(ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> allowedParents, IEnumerable<Symbol> allowedChildren, string symbolName, int nArgs) {
-      var invokeSym = new InvokeFunction(symbolName);
-      grammar.AddSymbol(invokeSym);
-      grammar.SetMinSubtreeCount(invokeSym, nArgs);
-      grammar.SetMaxSubtreeCount(invokeSym, nArgs);
-      foreach (var parent in allowedParents) {
-        for (int arg = 0; arg < grammar.GetMaxSubtreeCount(parent); arg++) {
-          grammar.SetAllowedChild(parent, invokeSym, arg);
-        }
-      }
-      foreach (var child in allowedChildren) {
-        for (int arg = 0; arg < grammar.GetMaxSubtreeCount(invokeSym); arg++) {
-          grammar.SetAllowedChild(invokeSym, child, arg);
-        }
-      }
-    }
-
-    private static void AddDynamicArguments(ISymbolicExpressionGrammar grammar, int nArgs) {
-      for (int argIndex = 0; argIndex < nArgs; argIndex++) {
-        var argNode = new Argument(argIndex);
-        grammar.AddSymbol(argNode);
-        grammar.SetMinSubtreeCount(argNode, 0);
-        grammar.SetMaxSubtreeCount(argNode, 0);
-        // allow the argument as child of any other symbol
-        foreach (var symb in grammar.Symbols)
-          for (int i = 0; i < grammar.GetMaxSubtreeCount(symb); i++) {
-            grammar.SetAllowedChild(symb, argNode, i);
-          }
       }
     }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs	(revision 3360)
@@ -81,4 +81,9 @@
 
     public void RemoveSymbol(Symbol symbol) {
+      foreach (var parent in Symbols) {
+        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
+          if (IsAllowedChild(parent, symbol, i))
+            allowedChildSymbols[parent.Name][i].Remove(symbol.Name);
+      }
       allSymbols.RemoveWhere(s => s.Name == symbol.Name);
       minSubTreeCount.Remove(symbol.Name);
@@ -208,5 +213,11 @@
       clone.minSubTreeCount = new Dictionary<string, int>(minSubTreeCount);
       clone.startSymbol = startSymbol;
-      clone.allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>(allowedChildSymbols);
+      clone.allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
+      foreach (var entry in allowedChildSymbols) {
+        clone.allowedChildSymbols[entry.Key] = new List<HashSet<string>>();
+        foreach (var set in entry.Value) {
+          clone.allowedChildSymbols[entry.Key].Add(new HashSet<string>(set));
+        }
+      }
       clone.allSymbols = new HashSet<Symbol>(allSymbols);
       return clone;
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs	(revision 3360)
@@ -82,5 +82,5 @@
     }
 
-    private void Reset() {
+    private new void Reset() {
       base.Reset();
       Initialize();
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj	(revision 3360)
@@ -85,4 +85,13 @@
     <Compile Include="ArchitectureAlteringOperators\ArgumentCreater.cs" />
     <Compile Include="ArchitectureAlteringOperators\ArgumentDeleter.cs" />
+    <Compile Include="ArchitectureAlteringOperators\ArgumentDuplicater.cs" />
+    <Compile Include="ArchitectureAlteringOperators\GrammarModifier.cs" />
+    <Compile Include="ArchitectureAlteringOperators\RandomArchitectureAlteringOperator.cs" />
+    <Compile Include="ArchitectureAlteringOperators\SubroutineCreater.cs">
+      <SubType>Code</SubType>
+    </Compile>
+    <Compile Include="ArchitectureAlteringOperators\SubroutineDeleter.cs" />
+    <Compile Include="ArchitectureAlteringOperators\SubroutineDuplicater.cs" />
+    <Compile Include="SymbolicExpressionTreeTopLevelNode.cs" />
     <Compile Include="Crossovers\SubtreeCrossover.cs">
       <SubType>Code</SubType>
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs	(revision 3360)
@@ -77,11 +77,4 @@
     }
 
-    public SymbolicExpressionTreeNode GetTopLevelBranchOf(SymbolicExpressionTreeNode node) {
-      foreach (var branch in root.SubTrees) {
-        if (branch.IterateNodesPrefix().Contains(node)) return branch;
-      }
-      throw new ArgumentException("Node was not found in tree.");
-    }
-
     public override IDeepCloneable Clone(Cloner cloner) {
       SymbolicExpressionTree clone = new SymbolicExpressionTree();
@@ -91,11 +84,4 @@
       return clone;
     }
-
-    public bool IsValidExpression() {
-      if (root.Symbol != root.Grammar.StartSymbol) return false;
-      foreach (var subtree in root.SubTrees)
-        if (subtree.Grammar == root.Grammar) return false;
-      return root.IsValidTree();
-    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs	(revision 3360)
@@ -128,13 +128,4 @@
       yield return this;
     }
-    //public int GetMinExpressionLength() {
-    //  return Grammar.GetMinExpressionLength(Symbol);
-    //}
-    //public int GetMaxExpressionLength() {
-    //  return Grammar.GetMaxExpressionLength(Symbol);
-    //}
-    //public int GetMinExpressionDepth() {
-    //  return Grammar.GetMinExpressionDepth(Symbol);
-    //}
     public IEnumerable<Symbol> GetAllowedSymbols(int argumentIndex) {
       return Grammar.Symbols.Where(s => Grammar.IsAllowedChild(Symbol, s, argumentIndex));
@@ -146,13 +137,4 @@
       return Grammar.GetMaxSubtreeCount(Symbol);
     }
-    //public int GetMaxExpressionLength(Symbol s) {
-    //  return Grammar.GetMaxExpressionLength(s);
-    //}
-    //public int GetMinExpressionLength(Symbol s) {
-    //  return Grammar.GetMinExpressionLength(s);
-    //}
-    //public int GetMinExpressionDepth(Symbol s) {
-    //  return Grammar.GetMinExpressionDepth(s);
-    //}
 
     #region ICloneable Members
@@ -165,17 +147,5 @@
 
 
-    public bool IsValidTree() {
-      var matchingSymbol = (from symb in Grammar.Symbols
-                            where symb.Name == Symbol.Name
-                            select symb).SingleOrDefault();
 
-      if (SubTrees.Count < Grammar.GetMinSubtreeCount(matchingSymbol)) return false;
-      else if (SubTrees.Count > Grammar.GetMaxSubtreeCount(matchingSymbol)) return false;
-      else for (int i = 0; i < SubTrees.Count; i++) {
-          if (!GetAllowedSymbols(i).Select(x => x.Name).Contains(SubTrees[i].Symbol.Name)) return false;
-          if (!SubTrees[i].IsValidTree()) return false;
-        }
-      return true;
-    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs	(revision 3360)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTopLevelNode.cs	(revision 3360)
@@ -0,0 +1,54 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
+using System.Linq;
+using System.Collections.Generic;
+using System.Text;
+using HeuristicLab.Core;
+using System.Xml;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Data;
+using System.Diagnostics;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
+  [StorableClass]
+  public class SymbolicExpressionTreeTopLevelNode : SymbolicExpressionTreeNode {
+
+    public SymbolicExpressionTreeTopLevelNode()
+      : base() {
+    }
+
+    public SymbolicExpressionTreeTopLevelNode(Symbol symbol)
+      : base(symbol) {
+    }
+
+    // copy constructor
+    protected SymbolicExpressionTreeTopLevelNode(SymbolicExpressionTreeTopLevelNode original)
+      : base(original) {
+      Grammar = (ISymbolicExpressionGrammar)original.Grammar.Clone();
+    }
+
+    public override object Clone() {
+      return new SymbolicExpressionTreeTopLevelNode(this);
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/DefunTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/DefunTreeNode.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/DefunTreeNode.cs	(revision 3360)
@@ -25,5 +25,5 @@
 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols {
   [StorableClass]
-  public sealed class DefunTreeNode : SymbolicExpressionTreeNode {
+  public sealed class DefunTreeNode : SymbolicExpressionTreeTopLevelNode {
     private int numberOfArguments;
     [Storable]
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunctionTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunctionTreeNode.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunctionTreeNode.cs	(revision 3360)
@@ -27,4 +27,9 @@
     public new InvokeFunction Symbol {
       get { return (InvokeFunction)base.Symbol; }
+      set {
+        if (value == null) throw new ArgumentNullException();
+        if (!(value is InvokeFunction)) throw new ArgumentNullException();
+        base.Symbol = value;
+      }
     }
 
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbolTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbolTreeNode.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbolTreeNode.cs	(revision 3360)
@@ -24,5 +24,5 @@
 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols {
   [StorableClass]
-  public sealed class StartSymbolTreeNode : SymbolicExpressionTreeNode {
+  public sealed class StartSymbolTreeNode : SymbolicExpressionTreeTopLevelNode {
     // copy constructor
     private StartSymbolTreeNode(StartSymbolTreeNode original)
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/AllArchitectureAlteringOperatorsTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/AllArchitectureAlteringOperatorsTest.cs	(revision 3360)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/AllArchitectureAlteringOperatorsTest.cs	(revision 3360)
@@ -0,0 +1,98 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+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;
+using HeuristicLab.Data;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
+  [TestClass]
+  public class AllArchitectureAlteringOperatorsTest {
+    private const int POPULATION_SIZE = 1000;
+    private const int N_ITERATIONS = 20;
+    private const int MAX_TREE_SIZE = 100;
+    private const int MAX_TREE_HEIGHT = 10;
+    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;
+      }
+    }
+
+    [TestMethod()]
+    public void AllArchitectureAlteringOperatorsDistributionTest() {
+      var trees = new List<SymbolicExpressionTree>();
+      var newTrees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister(31415);
+      int failedEvents = 0;
+      IntValue maxTreeSize = new IntValue(100);
+      IntValue maxTreeHeigth = new IntValue(10);
+      IntValue maxDefuns = new IntValue(3);
+      IntValue maxArgs = new IntValue(3);
+      for (int i = 0; i < POPULATION_SIZE; i++) {
+        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
+        Util.IsValid(tree);
+        trees.Add(tree);
+      }
+      Stopwatch stopwatch = new Stopwatch();
+      stopwatch.Start();
+      var combinedAAOperator = new RandomArchitectureAlteringOperator();
+      for (int g = 0; g < N_ITERATIONS; g++) {
+        for (int i = 0; i < POPULATION_SIZE; i++) {
+          var selectedTree = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
+          var op = combinedAAOperator.Operators.SelectRandom(random);
+          bool success;
+          op.ModifyArchitecture(random, selectedTree, grammar, maxTreeSize, maxTreeHeigth, maxDefuns, maxArgs, out success);
+          if (!success) failedEvents++;
+          Util.IsValid(selectedTree);
+          newTrees.Add(selectedTree);
+        }
+        trees = newTrees;
+      }
+      stopwatch.Stop();
+      var msPerOperation = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)N_ITERATIONS;
+      Assert.Inconclusive("AllArchitectureAlteringOperators: " + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE / N_ITERATIONS * 100 + " %" + Environment.NewLine +
+        "Operations / s: ~" + Math.Round(1000.0 / (msPerOperation)) + "operations / s)" + Environment.NewLine +
+        Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(trees) + Environment.NewLine
+        );
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs	(revision 3360)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Text;
 using System.Collections.Generic;
@@ -34,13 +55,16 @@
       var trees = new List<SymbolicExpressionTree>();
       var grammar = Grammars.CreateArithmeticAndAdfGrammar();
-      var random = new MersenneTwister();
+      var random = new MersenneTwister(31415);
+      int failedEvents = 0;
       for (int i = 0; i < POPULATION_SIZE; i++) {
         var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
-        ArgumentCreater.CreateNewArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
-        Assert.IsTrue(tree.IsValidExpression());
+        if (!ArgumentCreater.CreateNewArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
+          failedEvents++;
+        Util.IsValid(tree);
         trees.Add(tree);
       }
       Assert.Inconclusive("ArgumentCreator: " + Environment.NewLine +
-        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
+        Util.GetSizeDistributionString(trees, 200, 20) + Environment.NewLine +
         Util.GetFunctionDistributionString(trees) + Environment.NewLine +
         Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDeleterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDeleterTest.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDeleterTest.cs	(revision 3360)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Text;
 using System.Collections.Generic;
@@ -34,12 +55,15 @@
       var trees = new List<SymbolicExpressionTree>();
       var grammar = Grammars.CreateArithmeticAndAdfGrammar();
-      var random = new MersenneTwister();
+      var random = new MersenneTwister(31415);
+      int failedEvents = 0;
       for (int i = 0; i < POPULATION_SIZE; i++) {
         var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
-        ArgumentDeleter.DeleteArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
-        Assert.IsTrue(tree.IsValidExpression());
+        if (!ArgumentDeleter.DeleteArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
+          failedEvents++;
+        Util.IsValid(tree);
         trees.Add(tree);
       }
       Assert.Inconclusive("ArgumentDeleter: " + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
         Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
         Util.GetFunctionDistributionString(trees) + Environment.NewLine +
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDuplicaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDuplicaterTest.cs	(revision 3360)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDuplicaterTest.cs	(revision 3360)
@@ -0,0 +1,75 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+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 ArgumentDuplicaterTest {
+    private const int POPULATION_SIZE = 1000;
+    private const int MAX_TREE_SIZE = 100;
+    private const int MAX_TREE_HEIGHT = 10;
+    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;
+      }
+    }
+
+    [TestMethod()]
+    public void ArgumentDuplicaterDistributionsTest() {
+      var trees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister();
+      int failedEvents = 0;
+      for (int i = 0; i < POPULATION_SIZE; i++) {
+        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
+        if (!ArgumentDuplicater.DuplicateArgument(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
+          failedEvents++;
+        Util.IsValid(tree);
+        trees.Add(tree);
+      }
+      Assert.Inconclusive("ArgumentDuplicater: " + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
+        Util.GetSizeDistributionString(trees, 200, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(trees) + Environment.NewLine
+        );
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs	(revision 3360)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Text;
 using System.Collections.Generic;
@@ -74,4 +95,27 @@
       return g;
     }
+
+    public static void HasValidAdfGrammars(SymbolicExpressionTree tree) {
+      Assert.AreEqual(tree.Root.Grammar.Symbols.Count(), 8);
+      Assert.AreEqual(tree.Root.GetAllowedSymbols(0).Count(), 1); // only the start symbol is allowed
+      // we allow 3 ADF branches
+      Assert.AreEqual(tree.Root.GetAllowedSymbols(1).Count(), 1); // only the defun branch is allowed
+      Assert.AreEqual(tree.Root.GetAllowedSymbols(2).Count(), 1); // only the defun symbol is allowed
+      Assert.AreEqual(tree.Root.GetAllowedSymbols(3).Count(), 1); // only the defun symbol is allowed
+      foreach (var subtree in tree.Root.SubTrees) {
+        // check consistency of each sub-tree grammar independently
+        var allowedSymbols = subtree.GetAllowedSymbols(0);
+        int numberOfAllowedSymbols = allowedSymbols.Count();
+        foreach (var parent in allowedSymbols) {
+          for (int argIndex = 0; argIndex < subtree.Grammar.GetMaxSubtreeCount(parent); argIndex++) {
+            var allowedChildren = from child in subtree.Grammar.Symbols
+                                  where subtree.Grammar.IsAllowedChild(parent, child, argIndex)
+                                  select child;
+            Assert.AreEqual(numberOfAllowedSymbols, allowedChildren.Count());
+          }
+        }
+      }
+    }
+
   }
 }
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 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.Tests.csproj	(revision 3360)
@@ -41,5 +41,12 @@
     <Compile Include="ArgumentCreaterTest.cs" />
     <Compile Include="ArgumentDeleterTest.cs" />
+    <Compile Include="ArgumentDuplicaterTest.cs" />
+    <Compile Include="AllArchitectureAlteringOperatorsTest.cs" />
+    <Compile Include="SubroutineDeleterTest.cs" />
+    <Compile Include="SubroutineDuplicaterTest.cs" />
     <Compile Include="Grammars.cs" />
+    <Compile Include="SubroutineCreaterTest.cs">
+      <SubType>Code</SubType>
+    </Compile>
     <Compile Include="SubtreeCrossoverTest.cs">
       <SubType>Code</SubType>
@@ -50,7 +57,15 @@
   </ItemGroup>
   <ItemGroup>
+    <ProjectReference Include="..\..\..\HeuristicLab.Collections\3.3\HeuristicLab.Collections-3.3.csproj">
+      <Project>{958B43BC-CC5C-4FA2-8628-2B3B01D890B6}</Project>
+      <Name>HeuristicLab.Collections-3.3</Name>
+    </ProjectReference>
     <ProjectReference Include="..\..\..\HeuristicLab.Core\3.3\HeuristicLab.Core-3.3.csproj">
       <Project>{C36BD924-A541-4A00-AFA8-41701378DDC5}</Project>
       <Name>HeuristicLab.Core-3.3</Name>
+    </ProjectReference>
+    <ProjectReference Include="..\..\..\HeuristicLab.Data\3.3\HeuristicLab.Data-3.3.csproj">
+      <Project>{BBAB9DF5-5EF3-4BA8-ADE9-B36E82114937}</Project>
+      <Name>HeuristicLab.Data-3.3</Name>
     </ProjectReference>
     <ProjectReference Include="..\..\..\HeuristicLab.Operators\3.3\HeuristicLab.Operators-3.3.csproj">
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs	(revision 3360)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Text;
 using System.Collections.Generic;
@@ -38,6 +59,7 @@
       }
 
-      foreach (var tree in randomTrees)
-        Assert.IsTrue(tree.IsValidExpression());
+      foreach (var tree in randomTrees) {
+        Util.IsValid(tree);
+      }
       Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine +
         Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
@@ -55,8 +77,9 @@
       var random = new MersenneTwister();
       for (int i = 0; i < POPULATION_SIZE; i++) {
-        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3));
+        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
+        Grammars.HasValidAdfGrammars(tree);
+        Util.IsValid(tree);
+        randomTrees.Add(tree);
       }
-      foreach (var tree in randomTrees)
-        Assert.IsTrue(tree.IsValidExpression());
       Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine +
         Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs	(revision 3360)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Text;
 using System.Collections.Generic;
@@ -8,13 +29,11 @@
 using System.Diagnostics;
 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
-using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
 
 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
   [TestClass]
   public class SubroutineCreaterTest {
-    private static ISymbolicExpressionGrammar grammar;
-    private static List<SymbolicExpressionTree> subroutineTrees;
-    private static int failedEvents;
-
+    private const int POPULATION_SIZE = 1000;
+    private const int MAX_TREE_SIZE = 100;
+    private const int MAX_TREE_HEIGHT = 10;
     private TestContext testContextInstance;
 
@@ -32,52 +51,24 @@
     }
 
-    [ClassInitialize()]
-    public static void SubroutineCreaterTestInitialize(TestContext testContext) {
-      var randomTrees = new List<SymbolicExpressionTree>();
-      subroutineTrees = new List<SymbolicExpressionTree>();
-      int populationSize = 1000;
-      failedEvents = 0;
-      grammar = Grammars.CreateArithmeticAndAdfGrammar();
-      var random = new MersenneTwister();
-      for (int i = 0; i < populationSize; i++) {
-        var randTree = ProbabilisticTreeCreator.Create(random, grammar, 100, 10);
-        // PTC create is tested separately
-        randomTrees.Add(randTree);
+    [TestMethod()]
+    public void SubroutineCreaterDistributionsTest() {
+      var trees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister(31415);
+      int failedEvents = 0;
+      for (int i = 0; i < POPULATION_SIZE; i++) {
+        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
+        if (!SubroutineCreater.CreateSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
+          failedEvents++;
+        Util.IsValid(tree);
+        trees.Add(tree);
       }
-      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);
-        if (!success) failedEvents++;
-        subroutineTrees.Add(par0);
-      }
-    }
-
-
-    [TestMethod()]
-    public void SubroutineCreaterCreateTest() {
-      foreach (var tree in subroutineTrees)
-        Assert.IsTrue(grammar.IsValidExpression(tree));
-    }
-
-    [TestMethod()]
-    public void SubroutineCreaterSizeDistributionTest() {
-      Assert.Inconclusive("SubroutineCreater: " + Util.GetSizeDistributionString(subroutineTrees, 105, 5));
-    }
-
-    [TestMethod()]
-    public void SubroutineCreaterFunctionDistributionTest() {
-      Assert.Inconclusive("SubroutineCreater: " + Util.GetFunctionDistributionString(subroutineTrees));
-    }
-
-    [TestMethod()]
-    public void SubroutineCreaterNumberOfSubTreesDistributionTest() {
-      Assert.Inconclusive("SubroutineCreater: " + Util.GetNumberOfSubTreesDistributionString(subroutineTrees));
-    }
-
-
-    [TestMethod()]
-    public void SubroutineCreaterTerminalDistributionTest() {
-      Assert.Inconclusive("SubroutineCreater: " + Util.GetTerminalDistributionString(subroutineTrees));
+      Assert.Inconclusive("SubroutineCreator: " + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
+        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(trees) + Environment.NewLine
+        );
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineDeleterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineDeleterTest.cs	(revision 3360)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineDeleterTest.cs	(revision 3360)
@@ -0,0 +1,75 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+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 SubroutineDeleterTest {
+    private const int POPULATION_SIZE = 1000;
+    private const int MAX_TREE_SIZE = 100;
+    private const int MAX_TREE_HEIGHT = 10;
+    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;
+      }
+    }
+
+    [TestMethod()]
+    public void SubroutineDeleterDistributionsTest() {
+      var trees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister(31415);
+      int failedEvents = 0;
+      for (int i = 0; i < POPULATION_SIZE; i++) {
+        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
+        if (!SubroutineDeleter.DeleteSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
+          failedEvents++;
+        Util.IsValid(tree);
+        trees.Add(tree);
+      }
+      Assert.Inconclusive("SubroutineDeleter: " + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
+        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(trees) + Environment.NewLine
+        );
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineDuplicaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineDuplicaterTest.cs	(revision 3360)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineDuplicaterTest.cs	(revision 3360)
@@ -0,0 +1,75 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+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 SubroutineDuplicaterTest {
+    private const int POPULATION_SIZE = 1000;
+    private const int MAX_TREE_SIZE = 100;
+    private const int MAX_TREE_HEIGHT = 10;
+    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;
+      }
+    }
+
+    [TestMethod()]
+    public void SubroutineDuplicaterDistributionsTest() {
+      var trees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister();
+      int failedEvents = 0;
+      for (int i = 0; i < POPULATION_SIZE; i++) {
+        var tree = ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3);
+        if (!SubroutineDuplicater.DuplicateSubroutine(random, tree, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 3, 3))
+          failedEvents++;
+        Util.IsValid(tree);
+        trees.Add(tree);
+      }
+      Assert.Inconclusive("SubroutineDuplicater: " + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
+        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(trees) + Environment.NewLine
+        );
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs	(revision 3360)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Text;
 using System.Collections.Generic;
@@ -11,8 +32,5 @@
   [TestClass]
   public class SubtreeCrossoverTest {
-    private static ISymbolicExpressionGrammar grammar;
-    private static List<SymbolicExpressionTree> crossoverTrees;
-    private static double msPerCrossoverEvent;
-
+    private const int POPULATION_SIZE = 1000;
     private TestContext testContextInstance;
 
@@ -30,14 +48,16 @@
     }
 
-    [ClassInitialize()]
-    public static void SubtreeCrossoverTestInitialize(TestContext testContext) {
-      crossoverTrees = new List<SymbolicExpressionTree>();
-      int populationSize = 1000;
+    [TestMethod()]
+    public void SubtreeCrossoverDistributionsTest() {
       int generations = 5;
+      var trees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister();
       int failedEvents = 0;
-      grammar = Grammars.CreateArithmeticAndAdfGrammar();
-      var random = new MersenneTwister();
-      for (int i = 0; i < populationSize; i++) {
-        crossoverTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10, 3, 3));
+      List<SymbolicExpressionTree> crossoverTrees;
+      double msPerCrossoverEvent;
+
+      for (int i = 0; i < POPULATION_SIZE; i++) {
+        trees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10, 3, 3));
       }
       Stopwatch stopwatch = new Stopwatch();
@@ -45,7 +65,7 @@
       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();
+        for (int i = 0; i < POPULATION_SIZE; i++) {
+          var par0 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
+          var par1 = (SymbolicExpressionTree)trees.SelectRandom(random).Clone();
           bool success;
           newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success));
@@ -55,37 +75,17 @@
       }
       stopwatch.Stop();
-      foreach (var tree in crossoverTrees)
-        Assert.IsTrue(tree.IsValidExpression());
-      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;      
-    }
+      foreach (var tree in trees)
+        Util.IsValid(tree);
 
+      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)POPULATION_SIZE / (double)generations;
 
-
-    [TestMethod()]
-    public void SubtreeCrossoverSpeed() {
-
-      Assert.Inconclusive(msPerCrossoverEvent + " ms per crossover event (~" +
-        Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)");
-    }
-
-    [TestMethod()]
-    public void SubtreeCrossoverSizeDistributions() {
-      Assert.Inconclusive("SubtreeCrossover: " + Util.GetSizeDistributionString(crossoverTrees, 105, 5));
-    }
-
-    [TestMethod()]
-    public void SubtreeCrossoverFunctionDistributionTest() {
-      Assert.Inconclusive("SubtreeCrossover: " + Util.GetFunctionDistributionString(crossoverTrees));
-    }
-
-    [TestMethod()]
-    public void SubtreeCrossoverNumberOfSubTreesDistributionTest() {
-      Assert.Inconclusive("SubtreeCrossover: " + Util.GetNumberOfSubTreesDistributionString(crossoverTrees));
-    }
-
-
-    [TestMethod()]
-    public void SubtreeCrossoverTerminalDistributionTest() {
-      Assert.Inconclusive("SubtreeCrossover: " + Util.GetTerminalDistributionString(crossoverTrees));
+      Assert.Inconclusive("SubtreeCrossover: " + Environment.NewLine +
+        "Failed events: " + failedEvents / (double)POPULATION_SIZE * 100 + " %" + Environment.NewLine +
+        msPerCrossoverEvent + " ms per crossover event (~" + Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)" + Environment.NewLine +
+        Util.GetSizeDistributionString(trees, 105, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(trees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(trees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(trees) + Environment.NewLine
+        );
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs	(revision 3359)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs	(revision 3360)
@@ -1,3 +1,24 @@
-﻿using System;
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ *
+ * This file is part of HeuristicLab.
+ *
+ * HeuristicLab is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * HeuristicLab is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
+ */
+#endregion
+
+using System;
 using System.Text;
 using System.Collections.Generic;
@@ -13,12 +34,17 @@
       int[] histogram = new int[maxTreeSize / binSize];
       for (int i = 0; i < trees.Count; i++) {
-        histogram[trees[i].Size / binSize]++;
+        int binIndex = Math.Min(histogram.Length - 1, trees[i].Size / binSize);
+        histogram[binIndex]++;
       }
       StringBuilder strBuilder = new StringBuilder();
-      for (int i = 0; i < histogram.Length; i++) {
+      for (int i = 0; i < histogram.Length - 1; i++) {
         strBuilder.Append(Environment.NewLine);
         strBuilder.Append("< "); strBuilder.Append((i + 1) * binSize);
         strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)trees.Count);
       }
+      strBuilder.Append(Environment.NewLine);
+      strBuilder.Append(">= "); strBuilder.Append(histogram.Length * binSize);
+      strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[histogram.Length - 1] / (double)trees.Count);
+
       return "Size distribution: " + strBuilder;
     }
@@ -88,4 +114,24 @@
       return "Terminal distribution: " + strBuilder;
     }
+
+    public static void IsValid(SymbolicExpressionTree tree) {
+      Grammars.HasValidAdfGrammars(tree);
+      Assert.AreEqual(tree.Root.Symbol, tree.Root.Grammar.StartSymbol);
+      foreach (var subtree in tree.Root.SubTrees)
+        Assert.AreNotSame(subtree.Grammar, tree.Root.Grammar);
+      IsValid(tree.Root);
+    }
+
+    public static void IsValid(SymbolicExpressionTreeNode treeNode) {
+      var matchingSymbol = (from symb in treeNode.Grammar.Symbols
+                            where symb.Name == treeNode.Symbol.Name
+                            select symb).SingleOrDefault();
+      Assert.IsTrue(treeNode.SubTrees.Count >= treeNode.Grammar.GetMinSubtreeCount(matchingSymbol));
+      Assert.IsTrue(treeNode.SubTrees.Count <= treeNode.Grammar.GetMaxSubtreeCount(matchingSymbol));
+      for (int i = 0; i < treeNode.SubTrees.Count; i++) {
+        Assert.IsTrue(treeNode.GetAllowedSymbols(i).Select(x => x.Name).Contains(treeNode.SubTrees[i].Symbol.Name));
+        IsValid(treeNode.SubTrees[i]);
+      }
+    }
   }
 }
