Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentCreater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentCreater.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentCreater.cs	(revision 3338)
@@ -60,14 +60,18 @@
       var functionDefiningBranches = symbolicExpressionTree.IterateNodesPrefix().OfType<DefunTreeNode>();
 
-      var allowedArgumentIndexes = Enumerable.Range(0, maxFunctionArguments);
-
       if (functionDefiningBranches.Count() == 0)
         // no function defining branch found => abort
         return false;
+
       // select a random function defining branch
-      var selectedDefunBranch = SelectRandomBranch(random, functionDefiningBranches);
+      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
+      var definedArguments = (from symbol in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
+                              select symbol.ArgumentIndex).Distinct();
+      if (definedArguments.Count() >= maxFunctionArguments)
+        // max number of arguments reached => abort
+        return false;
       // select a random cut point in the function defining branch
       // the branch at the cut point is to be replaced by a new argument node
-      var cutPoints = (from node in IterateNodesPrefix(selectedDefunBranch)
+      var cutPoints = (from node in selectedDefunBranch.IterateNodesPrefix()
                        where node.SubTrees.Count > 0
                        from subtree in node.SubTrees
@@ -78,9 +82,6 @@
         return false;
       var selectedCutPoint = cutPoints[random.Next(cutPoints.Count)];
-      var existingArguments = from node in IterateNodesPrefix(selectedDefunBranch)
-                              let argNode = node as ArgumentTreeNode
-                              where argNode != null
-                              select argNode;
-      var newArgumentIndex = allowedArgumentIndexes.Except(existingArguments.Select(x => x.ArgumentIndex)).First();
+      var allowedArgumentIndexes = Enumerable.Range(0, maxFunctionArguments);
+      var newArgumentIndex = allowedArgumentIndexes.Except(definedArguments).First();
       // replace the branch at the cut point with an argument node
       var newArgNode = MakeArgumentNode(newArgumentIndex);
@@ -88,23 +89,34 @@
       selectedCutPoint.Parent.RemoveSubTree(selectedCutPoint.ReplacedChildIndex);
       selectedCutPoint.Parent.InsertSubTree(selectedCutPoint.ReplacedChildIndex, newArgNode);
-      // find all invokations of the selected ADF and attach a cloned version of the originally cut out branch
+      // find all invocations of the selected ADF and attach a cloned version of the replaced branch (with all argument-nodes expanded)
       var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
-                            where node.InvokedFunctionName == selectedDefunBranch.Name
+                            where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
                             select node;
-      // append a new argument branch after preprocessing
       foreach (var invocationNode in invocationNodes) {
+        // append a new argument branch after expanding all argument nodes
         var clonedBranch = (SymbolicExpressionTreeNode)replacedBranch.Clone();
         ReplaceArgumentsInBranch(clonedBranch, invocationNode.SubTrees);
         invocationNode.InsertSubTree(newArgumentIndex, clonedBranch);
       }
-      // adapt the known functions informations
+      // increase expected number of arguments of function defining branch
+      // it's possible that the number of actually referenced arguments was reduced (all references were replaced by a single new argument)
+      // but the number of expected arguments is increased anyway
       selectedDefunBranch.NumberOfArguments++;
-      selectedDefunBranch.AddDynamicSymbol("ARG" + newArgumentIndex, 0);
+      selectedDefunBranch.Grammar.AddSymbol(newArgNode.Symbol);
+      selectedDefunBranch.Grammar.SetMinSubtreeCount(newArgNode.Symbol, 0);
+      selectedDefunBranch.Grammar.SetMaxSubtreeCount(newArgNode.Symbol, 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, newArgNode.Symbol, i);
+        }
       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
-        if (subtree.DynamicSymbols.Contains(selectedDefunBranch.Name)) {
-          subtree.SetDynamicSymbolArgumentCount(selectedDefunBranch.Name, selectedDefunBranch.NumberOfArguments);
+        // 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();
+        if (matchingSymbol != null) {
+          subtree.Grammar.SetMinSubtreeCount(matchingSymbol, selectedDefunBranch.NumberOfArguments);
+          subtree.Grammar.SetMaxSubtreeCount(matchingSymbol, selectedDefunBranch.NumberOfArguments);
         }
       }
-      Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree));
       return true;
     }
@@ -117,5 +129,5 @@
         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.ArgumentIndex].Clone();
+          branch.SubTrees[subtreeIndex] = (SymbolicExpressionTreeNode)argumentTrees[argNode.Symbol.ArgumentIndex].Clone();
         } else {
           // recursively replace arguments in all branches
@@ -125,21 +137,6 @@
     }
 
-    private static IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix(SymbolicExpressionTreeNode tree) {
-      yield return tree;
-      foreach (var subTree in tree.SubTrees) {
-        foreach (var node in IterateNodesPrefix(subTree)) {
-          yield return node;
-        }
-      }
-    }
-
-    private static T SelectRandomBranch<T>(IRandom random, IEnumerable<T> branches) {
-      var list = branches.ToList();
-      return list[random.Next(list.Count)];
-    }
-
     private static SymbolicExpressionTreeNode MakeArgumentNode(int argIndex) {
-      var node = (ArgumentTreeNode)(new Argument()).CreateTreeNode();
-      node.ArgumentIndex = argIndex;
+      var node = (ArgumentTreeNode)(new Argument(argIndex)).CreateTreeNode();
       return node;
     }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDeleter.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDeleter.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDeleter.cs	(revision 3338)
@@ -61,35 +61,32 @@
         // no function defining branch => abort
         return false;
-      var selectedDefunBranch = (DefunTreeNode)SelectRandomBranch(random, functionDefiningBranches);
+      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
       if (selectedDefunBranch.NumberOfArguments <= 1)
         // argument deletion by consolidation is not possible => abort
         return false;
-      var cutPoints = (from node in IterateNodesPrefix(selectedDefunBranch)
-                       where node.SubTrees.Count > 0
-                       from argNode in node.SubTrees.OfType<ArgumentTreeNode>()
-                       select new { Parent = node, ReplacedChildIndex = node.SubTrees.IndexOf(argNode), ReplacedChild = argNode }).ToList();
-      if (cutPoints.Count() == 0)
-        // no cut points found => abort
-        return false;
-      var selectedCutPoint = cutPoints[random.Next(cutPoints.Count)];
-      var removedArgument = selectedCutPoint.ReplacedChild.ArgumentIndex;
+      var removedArgument = (from sym in selectedDefunBranch.Grammar.Symbols.OfType<Argument>()
+                             select sym.ArgumentIndex).Distinct().SelectRandom(random);
+      // find invocations of the manipulated funcion and remove the specified argument tree
       var invocationNodes = from node in symbolicExpressionTree.IterateNodesPrefix().OfType<InvokeFunctionTreeNode>()
-                            where node.InvokedFunctionName == selectedDefunBranch.Name
+                            where node.Symbol.FunctionName == selectedDefunBranch.FunctionName
                             select node;
       foreach (var invokeNode in invocationNodes) {
-        invokeNode.RemoveSubTree(selectedCutPoint.ReplacedChild.ArgumentIndex);
+        invokeNode.RemoveSubTree(removedArgument);
       }
 
       DeleteArgumentByConsolidation(random, selectedDefunBranch, removedArgument);
 
-      selectedDefunBranch.RemoveDynamicSymbol("ARG" + removedArgument);
+      // 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();
+      selectedDefunBranch.Grammar.RemoveSymbol(matchingSymbol);
       // reduce arity in known functions of all root branches
       foreach (var subtree in symbolicExpressionTree.Root.SubTrees) {
-        if (subtree.DynamicSymbols.Contains(selectedDefunBranch.Name)) {
-          subtree.SetDynamicSymbolArgumentCount(selectedDefunBranch.Name, selectedDefunBranch.NumberOfArguments - 1);
+        var matchingInvokeSymbol = subtree.Grammar.Symbols.OfType<InvokeFunction>().Where(s => s.FunctionName == selectedDefunBranch.FunctionName).FirstOrDefault();
+        if (matchingInvokeSymbol != null) {
+          subtree.Grammar.SetMinSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments - 1);
+          subtree.Grammar.SetMaxSubtreeCount(matchingInvokeSymbol, selectedDefunBranch.NumberOfArguments - 1);
         }
       }
       selectedDefunBranch.NumberOfArguments--;
-      Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree));
       return true;
     }
@@ -97,27 +94,14 @@
     private static void DeleteArgumentByConsolidation(IRandom random, DefunTreeNode branch, int removedArgumentIndex) {
       // replace references to the deleted argument with random references to existing arguments
-      var possibleArgumentIndexes = (from node in IterateNodesPrefix(branch).OfType<ArgumentTreeNode>()
-                                     where node.ArgumentIndex != removedArgumentIndex
-                                     select node.ArgumentIndex).Distinct().ToList();
-      var argNodes = from node in IterateNodesPrefix(branch).OfType<ArgumentTreeNode>()
-                     where node.ArgumentIndex == removedArgumentIndex
+      var possibleArgumentSymbols = (from sym in branch.Grammar.Symbols.OfType<Argument>()
+                                     where sym.ArgumentIndex != removedArgumentIndex
+                                     select sym).ToList();
+      var argNodes = from node in branch.IterateNodesPrefix().OfType<ArgumentTreeNode>()
+                     where node.Symbol.ArgumentIndex == removedArgumentIndex
                      select node;
       foreach (var argNode in argNodes) {
-        var replacementArgument = possibleArgumentIndexes[random.Next(possibleArgumentIndexes.Count)];
-        argNode.ArgumentIndex = replacementArgument;
+        var replacementSymbol = possibleArgumentSymbols.SelectRandom(random);
+        argNode.Symbol = replacementSymbol;
       }
-    }
-    private static IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix(SymbolicExpressionTreeNode tree) {
-      yield return tree;
-      foreach (var subTree in tree.SubTrees) {
-        foreach (var node in IterateNodesPrefix(subTree)) {
-          yield return node;
-        }
-      }
-    }
-
-    private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<DefunTreeNode> branches) {
-      var list = branches.ToList();
-      return list[random.Next(list.Count)];
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDuplicater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDuplicater.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/ArgumentDuplicater.cs	(revision 3338)
@@ -106,18 +106,4 @@
       return true;
     }
-
-    private static IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix(SymbolicExpressionTreeNode tree) {
-      yield return tree;
-      foreach (var subTree in tree.SubTrees) {
-        foreach (var node in IterateNodesPrefix(subTree)) {
-          yield return node;
-        }
-      }
-    }
-
-    private static T SelectRandomBranch<T>(IRandom random, IEnumerable<T> branches) {
-      var list = branches.ToList();
-      return list[random.Next(list.Count)];
-    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineCreater.cs	(revision 3338)
@@ -82,5 +82,5 @@
       if (selectedBody == null) throw new InvalidOperationException();
       // select a random node in the selected branch
-      var allCutPoints = (from parent in IterateNodesPrefix(selectedBody)
+      var allCutPoints = (from parent in selectedBody.IterateNodesPrefix()
                           from subtree in parent.SubTrees
                           select new { Parent = parent, ReplacedBranchIndex = parent.SubTrees.IndexOf(subtree), ReplacedBranch = subtree }).ToList();
@@ -160,14 +160,5 @@
       return argumentBranches;
     }
-
-    private static IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix(SymbolicExpressionTreeNode tree) {
-      yield return tree;
-      foreach (var subTree in tree.SubTrees) {
-        foreach (var node in IterateNodesPrefix(subTree)) {
-          yield return node;
-        }
-      }
-    }
-
+    
     private static IEnumerable<string> UsedFunctionNames(SymbolicExpressionTree symbolicExpressionTree) {
       return from node in symbolicExpressionTree.IterateNodesPrefix()
@@ -175,9 +166,4 @@
              select ((DefunTreeNode)node).Name;
     }
-
-    private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<DefunTreeNode> branches) {
-      var list = branches.ToList();
-      return list[random.Next(list.Count)];
-    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDeleter.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDeleter.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDeleter.cs	(revision 3338)
@@ -63,5 +63,5 @@
         // no ADF to delete => abort
         return false;
-      var selectedDefunBranch = (DefunTreeNode)SelectRandomBranch(random, functionDefiningBranches);
+      var selectedDefunBranch = functionDefiningBranches.SelectRandom(random);
       // remove the selected defun
       int defunSubtreeIndex = symbolicExpressionTree.Root.SubTrees.IndexOf(selectedDefunBranch);
@@ -104,9 +104,4 @@
       return true;
     }
-
-    private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<DefunTreeNode> branches) {
-      var list = branches.ToList();
-      return list[random.Next(list.Count)];
-    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/ArchitectureAlteringOperators/SubroutineDuplicater.cs	(revision 3338)
@@ -65,5 +65,5 @@
         // no function defining branches to duplicate or already reached the max number of ADFs
         return false;
-      var selectedBranch = (DefunTreeNode)SelectRandomBranch(random, functionDefiningBranches);
+      var selectedBranch = functionDefiningBranches.SelectRandom(random);
       var clonedBranch = (DefunTreeNode)selectedBranch.Clone();
       clonedBranch.Name = allowedFunctionNames.Except(UsedFunctionNames(symbolicExpressionTree)).First();
@@ -73,5 +73,5 @@
         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 = FindDefiningBranch(symbolicExpressionTree, invokeFunctionNode);
+          var branch = symbolicExpressionTree.GetTopLevelBranchOf(invokeFunctionNode);
           branch.AddDynamicSymbol(clonedBranch.Name, clonedBranch.NumberOfArguments);
           // flip coin wether to replace with newly defined function
@@ -83,11 +83,4 @@
       Debug.Assert(grammar.IsValidExpression(symbolicExpressionTree));
       return true;
-    }
-
-    private static SymbolicExpressionTreeNode FindDefiningBranch(SymbolicExpressionTree tree, SymbolicExpressionTreeNode node) {
-      foreach (var subtree in tree.Root.SubTrees) {
-        if (ContainsNode(subtree, node)) return subtree;
-      }
-      return null;
     }
 
@@ -106,8 +99,5 @@
     }
 
-    private static SymbolicExpressionTreeNode SelectRandomBranch(IRandom random, IEnumerable<DefunTreeNode> branches) {
-      var list = branches.ToList();
-      return list[random.Next(list.Count)];
-    }
+
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Creators/ProbabilisticTreeCreator.cs	(revision 3338)
@@ -27,4 +27,6 @@
 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
 using System.Collections.Generic;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
+using System.Text;
 
 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
@@ -32,13 +34,21 @@
   [Item("ProbabilisticTreeCreator", "An operator that creates new symbolic expression trees with uniformly distributed size")]
   public class ProbabilisticTreeCreator : SymbolicExpressionTreeCreator {
+
     public ProbabilisticTreeCreator()
       : base() {
     }
 
-    protected override SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight) {
-      return Create(random, grammar, maxTreeSize.Value, maxTreeHeight.Value);
-    }
-
-    public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, int maxTreeSize, int maxTreeHeight) {
+    protected override SymbolicExpressionTree Create(
+      IRandom random,
+      ISymbolicExpressionGrammar grammar,
+      IntValue maxTreeSize, IntValue maxTreeHeight,
+      IntValue maxFunctionDefinitions, IntValue maxFunctionArguments) {
+      return Create(random, grammar, maxTreeSize.Value, maxTreeHeight.Value, maxFunctionDefinitions.Value, maxFunctionArguments.Value);
+    }
+
+    public static SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar,
+      int maxTreeSize, int maxTreeHeight,
+      int maxFunctionDefinitions, int maxFunctionArguments
+      ) {
       // tree size is limited by the grammar and by the explicit size constraints
       int allowedMinSize = grammar.GetMinExpressionLength(grammar.StartSymbol);
@@ -49,6 +59,5 @@
       do {
         try {
-          tree.Root = grammar.ProgramRootSymbol.CreateTreeNode();
-          tree.Root.AddSubTree(PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1));
+          tree.Root = PTC2(random, grammar, grammar.StartSymbol, treeSize + 1, maxTreeHeight + 1, maxFunctionDefinitions, maxFunctionArguments);
         }
         catch (ArgumentException) {
@@ -56,6 +65,199 @@
           treeSize = random.Next(allowedMinSize, allowedMaxSize);
         }
-      } while (tree.Root.SubTrees.Count == 0 || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
+      } while (tree.Root == null || tree.Size > maxTreeSize || tree.Height > maxTreeHeight);
       return tree;
+    }
+
+    private class TreeExtensionPoint {
+      public SymbolicExpressionTreeNode Parent { get; set; }
+      public int ChildIndex { get; set; }
+      public int ExtensionPointDepth { get; set; }
+    }
+    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);
+      return root;
+    }
+
+    private static void CreateFullTreeFromSeed(IRandom random, SymbolicExpressionTreeNode root, int size, int maxDepth, int maxFunctionDefinitions, int maxFunctionArguments) {
+      List<TreeExtensionPoint> extensionPoints = new List<TreeExtensionPoint>();
+      int currentSize = 1;
+      int totalListMinSize = root.Grammar.GetMinExpressionLength(root.Symbol) - 1;
+      int actualArity = SampleArity(random, root, size);
+      for (int i = 0; i < actualArity; i++) {
+        // insert a dummy sub-tree and add the pending extension to the list
+        var dummy = new SymbolicExpressionTreeNode();
+        root.AddSubTree(dummy);
+        dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
+        extensionPoints.Add(new TreeExtensionPoint { Parent = root, ChildIndex = i, ExtensionPointDepth = 2 });
+      }
+      // while there are pending extension points and we have not reached the limit of adding new extension points
+      while (extensionPoints.Count > 0 && totalListMinSize + currentSize < size) {
+        int randomIndex = random.Next(extensionPoints.Count);
+        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
+        extensionPoints.RemoveAt(randomIndex);
+        SymbolicExpressionTreeNode parent = nextExtension.Parent;
+        int argumentIndex = nextExtension.ChildIndex;
+        int extensionDepth = nextExtension.ExtensionPointDepth;
+        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
+                               where parent.Grammar.IsAllowedChild(parent.Symbol, s, argumentIndex)
+                               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);
+          SymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
+          parent.RemoveSubTree(argumentIndex);
+          parent.InsertSubTree(argumentIndex, newTree);
+
+          InitializeNewTreeNode(random, root, newTree, maxFunctionDefinitions, maxFunctionArguments);
+
+          currentSize++;
+          totalListMinSize--;
+
+          actualArity = SampleArity(random, newTree, size - currentSize);
+          for (int i = 0; i < actualArity; i++) {
+            // insert a dummy sub-tree and add the pending extension to the list
+            var dummy = new SymbolicExpressionTreeNode();
+            newTree.AddSubTree(dummy);
+            if (IsTopLevelBranch(root, dummy))
+              dummy.Grammar = (ISymbolicExpressionGrammar)dummy.Grammar.Clone();
+            extensionPoints.Add(new TreeExtensionPoint { Parent = newTree, ChildIndex = i, ExtensionPointDepth = extensionDepth + 1 });
+          }
+          totalListMinSize += newTree.Grammar.GetMinExpressionLength(newTree.Symbol);
+        }
+      }
+      // fill all pending extension points
+      while (extensionPoints.Count > 0) {
+        int randomIndex = random.Next(extensionPoints.Count);
+        TreeExtensionPoint nextExtension = extensionPoints[randomIndex];
+        extensionPoints.RemoveAt(randomIndex);
+        SymbolicExpressionTreeNode parent = nextExtension.Parent;
+        int a = nextExtension.ChildIndex;
+        int d = nextExtension.ExtensionPointDepth;
+        ReplaceWithMinimalTree(random, root, parent, a, maxFunctionDefinitions, maxFunctionArguments);
+      }
+    }
+
+    private static void ReplaceWithMinimalTree(IRandom random, SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode parent, int argumentIndex, int maxFunctionDefinitions, int maxFunctionArguments) {
+      // determine possible symbols that will lead to the smallest possible tree
+      var possibleSymbols = (from s in parent.GetAllowedSymbols(argumentIndex)
+                             group s by parent.Grammar.GetMinExpressionLength(s) into g
+                             orderby g.Key
+                             select g).First();
+      var selectedSymbol = SelectRandomSymbol(random, possibleSymbols);
+      var tree = selectedSymbol.CreateTreeNode();
+      parent.RemoveSubTree(argumentIndex);
+      parent.InsertSubTree(argumentIndex, tree);
+      InitializeNewTreeNode(random, root, tree, maxFunctionDefinitions, maxFunctionArguments);
+      for (int i = 0; i < tree.GetMinSubtreeCount(); i++) {
+        // insert a dummy sub-tree and add the pending extension to the list
+        var dummy = new SymbolicExpressionTreeNode();
+        tree.AddSubTree(dummy);
+        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))
+        newTree.Grammar = (ISymbolicExpressionGrammar)newTree.Grammar.Clone();
+      if (newTree.Symbol is Defun) {
+        var defunTree = newTree as DefunTreeNode;
+        string formatString = new StringBuilder().Append('0', (int)Math.Log10(maxFunctionDefinitions * 10 - 1)).ToString(); // >= 100 functions => ###
+        var allowedNames = from index in Enumerable.Range(0, maxFunctionDefinitions)
+                           select "ADF" + index.ToString(formatString);
+        var takenNames = (from node in root.IterateNodesPrefix().OfType<DefunTreeNode>()
+                          select node.FunctionName).Distinct();
+        var remainingNames = allowedNames.Except(takenNames).ToList();
+        string functionName = remainingNames[random.Next(remainingNames.Count)];
+        // set name and number of arguments of the ADF
+        int nArgs = random.Next(maxFunctionArguments);
+        defunTree.FunctionName = functionName;
+        defunTree.NumberOfArguments = nArgs;
+        if (nArgs > 0) {
+          AddDynamicArguments(defunTree.Grammar, nArgs);
+        }
+        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);
+          }
+        }
+      }
+    }
+
+    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);
+          }
+      }
+    }
+
+    private static bool IsTopLevelBranch(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode branch) {
+      return root.SubTrees.IndexOf(branch) > -1;
     }
 
@@ -75,75 +277,8 @@
     }
 
-
-    public static SymbolicExpressionTreeNode PTC2(IRandom random, ISymbolicExpressionGrammar grammar, Symbol rootSymbol, int size, int maxDepth) {
-      SymbolicExpressionTreeNode root = rootSymbol.CreateTreeNode();
-      if (size <= 1 || maxDepth <= 1) return root;
-      List<object[]> extensionPoints = new List<object[]>();
-      int currentSize = 1;
-      int totalListMinSize = grammar.GetMinExpressionLength(rootSymbol) - 1;
-
-      int actualArity = SampleArity(random, grammar, rootSymbol, size);
-      for (int i = 0; i < actualArity; i++) {
-        // insert a dummy sub-tree and add the pending extension to the list
-        root.AddSubTree(null);
-        extensionPoints.Add(new object[] { root, i, 2 });
-      }
-
-      // while there are pending extension points and we have not reached the limit of adding new extension points
-      while (extensionPoints.Count > 0 && totalListMinSize + currentSize < size) {
-        int randomIndex = random.Next(extensionPoints.Count);
-        object[] nextExtension = extensionPoints[randomIndex];
-        extensionPoints.RemoveAt(randomIndex);
-        SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0];
-        int argumentIndex = (int)nextExtension[1];
-        int extensionDepth = (int)nextExtension[2];
-        if (extensionDepth + grammar.GetMinExpressionDepth(parent.Symbol) >= maxDepth) {
-          parent.RemoveSubTree(argumentIndex);
-          SymbolicExpressionTreeNode branch = CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, argumentIndex));
-          parent.InsertSubTree(argumentIndex, branch); // insert a smallest possible tree
-          currentSize += branch.GetSize();
-          totalListMinSize -= branch.GetSize();
-        } else {
-          var allowedSubFunctions = from s in grammar.GetAllowedSymbols(parent.Symbol, argumentIndex)
-                                    where grammar.GetMinExpressionDepth(parent.Symbol) + extensionDepth - 1 < maxDepth
-                                    where 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
-                                    select s;
-          Symbol selectedSymbol = SelectRandomSymbol(random, allowedSubFunctions);
-          SymbolicExpressionTreeNode newTree = selectedSymbol.CreateTreeNode();
-          parent.RemoveSubTree(argumentIndex);
-          parent.InsertSubTree(argumentIndex, newTree);
-          currentSize++;
-          totalListMinSize--;
-
-          actualArity = SampleArity(random, grammar, selectedSymbol, size - currentSize);
-          for (int i = 0; i < actualArity; i++) {
-            // insert a dummy sub-tree and add the pending extension to the list
-            newTree.AddSubTree(null);
-            extensionPoints.Add(new object[] { newTree, i, extensionDepth + 1 });
-          }
-          totalListMinSize += grammar.GetMinExpressionLength(selectedSymbol);
-        }
-      }
-      // fill all pending extension points
-      while (extensionPoints.Count > 0) {
-        int randomIndex = random.Next(extensionPoints.Count);
-        object[] nextExtension = extensionPoints[randomIndex];
-        extensionPoints.RemoveAt(randomIndex);
-        SymbolicExpressionTreeNode parent = (SymbolicExpressionTreeNode)nextExtension[0];
-        int a = (int)nextExtension[1];
-        int d = (int)nextExtension[2];
-        parent.RemoveSubTree(a);
-        parent.InsertSubTree(a,
-          CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(parent.Symbol, a))); // append a tree with minimal possible height
-      }
-      return root;
-    }
-
-    private static int SampleArity(IRandom random, ISymbolicExpressionGrammar grammar, Symbol symbol, int targetSize) {
+    private static int SampleArity(IRandom random, SymbolicExpressionTreeNode node, int targetSize) {
       // select actualArity randomly with the constraint that the sub-trees in the minimal arity can become large enough
-      int minArity = grammar.GetMinSubTreeCount(symbol);
-      int maxArity = grammar.GetMaxSubTreeCount(symbol);
+      int minArity = node.GetMinSubtreeCount();
+      int maxArity = node.GetMaxSubtreeCount();
       if (maxArity > targetSize) {
         maxArity = targetSize;
@@ -153,6 +288,6 @@
       long aggregatedLongestExpressionLength = 0;
       for (int i = 0; i < maxArity; i++) {
-        aggregatedLongestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i)
-                                              select grammar.GetMaxExpressionLength(s)).Max();
+        aggregatedLongestExpressionLength += (from s in node.GetAllowedSymbols(i)
+                                              select node.Grammar.GetMaxExpressionLength(s)).Max();
         if (aggregatedLongestExpressionLength < targetSize) minArity = i;
         else break;
@@ -163,6 +298,6 @@
       long aggregatedShortestExpressionLength = 0;
       for (int i = 0; i < maxArity; i++) {
-        aggregatedShortestExpressionLength += (from s in grammar.GetAllowedSymbols(symbol, i)
-                                               select grammar.GetMinExpressionLength(s)).Min();
+        aggregatedShortestExpressionLength += (from s in node.GetAllowedSymbols(i)
+                                               select node.Grammar.GetMinExpressionLength(s)).Min();
         if (aggregatedShortestExpressionLength > targetSize) {
           maxArity = i;
@@ -173,18 +308,4 @@
       return random.Next(minArity, maxArity + 1);
     }
-
-    private static SymbolicExpressionTreeNode CreateMinimalTree(IRandom random, ISymbolicExpressionGrammar grammar, IEnumerable<Symbol> symbols) {
-      // determine possible symbols that will lead to the smallest possible tree
-      var possibleSymbols = (from s in symbols
-                             group s by grammar.GetMinExpressionLength(s) into g
-                             orderby g.Key
-                             select g).First();
-      var selectedSymbol = SelectRandomSymbol(random, possibleSymbols);
-      // build minimal tree by recursive application
-      var tree = selectedSymbol.CreateTreeNode();
-      for (int i = 0; i < grammar.GetMinSubTreeCount(selectedSymbol); i++)
-        tree.AddSubTree(CreateMinimalTree(random, grammar, grammar.GetAllowedSymbols(selectedSymbol, i)));
-      return tree;
-    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Crossovers/SubtreeCrossover.cs	(revision 3338)
@@ -49,11 +49,11 @@
     }
 
-    protected override SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,
+    protected override SymbolicExpressionTree Cross(IRandom random,
       SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
       IntValue maxTreeSize, IntValue maxTreeHeight, out bool success) {
-      return Cross(random, grammar, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);
+      return Cross(random, parent0, parent1, InternalCrossoverPointProbabilityParameter.ActualValue.Value, maxTreeSize.Value, maxTreeHeight.Value, out success);
     }
 
-    public static SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,
+    public static SymbolicExpressionTree Cross(IRandom random,
       SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
       double internalCrossoverPointProbability, int maxTreeSize, int maxTreeHeight, out bool success) {
@@ -67,9 +67,9 @@
       int maxInsertedBranchHeight = maxTreeHeight - GetBranchLevel(parent0.Root, crossoverPoint0);
 
-      var allowedBranches = from branch in IterateNodes(parent1.Root)
+      var allowedBranches = from branch in parent1.Root.IterateNodesPrefix()
                             where branch.GetSize() < maxInsertedBranchSize
                             where branch.GetHeight() < maxInsertedBranchHeight
-                            where grammar.GetAllowedSymbols(crossoverPoint0.Symbol, replacedSubtreeIndex).Contains(branch.Symbol)
-                            where IsMatchingPointType(parent0, crossoverPoint0, branch)
+                            // where crossoverPoint0.GetAllowedSymbols(replacedSubtreeIndex).Contains(branch.Symbol)
+                            where IsMatchingPointType(crossoverPoint0, replacedSubtreeIndex, branch)
                             select branch;
 
@@ -89,33 +89,35 @@
     }
 
-    private static bool IsMatchingPointType(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent, SymbolicExpressionTreeNode newBranch) {
-      var functionCalls = (from node in IterateNodes(newBranch)
-                           let invokeNode = node as InvokeFunctionTreeNode
-                           let argNode = node as ArgumentTreeNode
-                           where invokeNode != null || argNode != null
-                           let name = invokeNode != null ? invokeNode.InvokedFunctionName : "ARG" + argNode.ArgumentIndex
-                           let argCount = invokeNode != null ? invokeNode.SubTrees.Count : 0
-                           select new { FunctionName = name, FunctionArgumentCount = argCount }).Distinct();
-      var definingBranch = GetDefiningBranch(tree, parent);
-      if (definingBranch == null) return false;
-      foreach (var functionCall in functionCalls) {
-        if (!definingBranch.DynamicSymbols.Contains(functionCall.FunctionName) ||
-          definingBranch.GetDynamicSymbolArgumentCount(functionCall.FunctionName) != functionCall.FunctionArgumentCount)
-          return false;
+    private static bool IsMatchingPointType(SymbolicExpressionTreeNode parent, int replacedSubtreeIndex, SymbolicExpressionTreeNode branch) {
+      // cannot compare dynamic symbols by reference because new instances of Invoke/Argument are created on demand
+      // instead we must check equality by names and number of arguments 
+      var branchSymbols = (from node in branch.IterateNodesPrefix()
+                           select new { FunctionName = node.Symbol.Name, SubtreeCount = node.SubTrees.Count }).Distinct();
+
+      // check syntax constraints of direct parent - child relation
+      var matchingSymbolInParentGrammar = (from sym in parent.GetAllowedSymbols(replacedSubtreeIndex)
+                                           where sym.Name == branch.Symbol.Name
+                                           select sym).SingleOrDefault();
+      if (matchingSymbolInParentGrammar == null ||
+        branch.SubTrees.Count < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) ||
+        branch.SubTrees.Count > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false;
+
+      // check point type for the whole branch
+      foreach (var branchSymbol in branchSymbols) {
+        matchingSymbolInParentGrammar = (from sym in parent.Grammar.Symbols
+                                         where sym.Name == branchSymbol.FunctionName
+                                         select sym).SingleOrDefault();
+        if (matchingSymbolInParentGrammar == null ||
+          branchSymbol.SubtreeCount < parent.Grammar.GetMinSubtreeCount(matchingSymbolInParentGrammar) ||
+          branchSymbol.SubtreeCount > parent.Grammar.GetMaxSubtreeCount(matchingSymbolInParentGrammar)) return false;
       }
+
       return true;
     }
 
-    private static SymbolicExpressionTreeNode GetDefiningBranch(SymbolicExpressionTree tree, SymbolicExpressionTreeNode parent) {
-      foreach (var treeNode in tree.Root.SubTrees) {
-        if (IterateNodes(treeNode).Contains(parent)) return treeNode;
-      }
-      return null;
-    }
-
     private static void SelectCrossoverPoint(IRandom random, SymbolicExpressionTree parent0, double internalNodeProbability, out SymbolicExpressionTreeNode crossoverPoint, out int subtreeIndex) {
-      var crossoverPoints = from branch in IterateNodes(parent0.Root)
+      var crossoverPoints = from branch in parent0.Root.IterateNodesPrefix()
                             where branch.SubTrees.Count > 0
-                            where !(branch.Symbol is ProgramRootSymbol)
+                            where branch != parent0.Root
                             from index in Enumerable.Range(0, branch.SubTrees.Count)
                             let p = new { CrossoverPoint = branch, SubtreeIndex = index, IsLeaf = branch.SubTrees[index].SubTrees.Count == 0 }
@@ -172,13 +174,4 @@
     }
 
-    private static IEnumerable<SymbolicExpressionTreeNode> IterateNodes(SymbolicExpressionTreeNode root) {
-      foreach (var subTree in root.SubTrees) {
-        foreach (var branch in IterateNodes(subTree)) {
-          yield return branch;
-        }
-      }
-      yield return root;
-    }
-
     private static int GetBranchLevel(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode point) {
       if (root == point) return 0;
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/DefaultSymbolicExpressionGrammar.cs	(revision 3338)
@@ -34,77 +34,152 @@
   public class DefaultSymbolicExpressionGrammar : Item, ISymbolicExpressionGrammar {
     [Storable]
-    private int minFunctionDefinitions;
-    [Storable]
-    private int maxFunctionDefinitions;
-    [Storable]
-    private int minFunctionArguments;
-    [Storable]
-    private int maxFunctionArguments;
-
-    [Storable]
     private Dictionary<string, int> minSubTreeCount;
     [Storable]
     private Dictionary<string, int> maxSubTreeCount;
     [Storable]
-    private Dictionary<string, List<HashSet<string>>> allowedFunctions;
+    private Dictionary<string, List<HashSet<string>>> allowedChildSymbols;
     [Storable]
     private HashSet<Symbol> allSymbols;
 
-    public DefaultSymbolicExpressionGrammar(int minFunctionDefinitions, int maxFunctionDefinitions, int minFunctionArguments, int maxFunctionArguments)
+    public DefaultSymbolicExpressionGrammar()
       : base() {
-      this.minFunctionDefinitions = minFunctionDefinitions;
-      this.maxFunctionDefinitions = maxFunctionDefinitions;
-      this.minFunctionArguments = minFunctionArguments;
-      this.maxFunctionArguments = maxFunctionArguments;
+      Reset();
+    }
+
+    private void Initialize() {
+      startSymbol = new StartSymbol();
+      AddSymbol(startSymbol);
+      SetMinSubtreeCount(startSymbol, 1);
+      SetMaxSubtreeCount(startSymbol, 1);
+    }
+
+    #region ISymbolicExpressionGrammar Members
+
+    private Symbol startSymbol;
+    public Symbol StartSymbol {
+      get { return startSymbol; }
+      set { startSymbol = value; }
+    }
+
+    protected void Reset() {
       minSubTreeCount = new Dictionary<string, int>();
       maxSubTreeCount = new Dictionary<string, int>();
-      allowedFunctions = new Dictionary<string, List<HashSet<string>>>();
+      allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>();
       allSymbols = new HashSet<Symbol>();
-      cachedMinExpressionLength = new Dictionary<Symbol, int>();
-      cachedMaxExpressionLength = new Dictionary<Symbol, int>();
-      cachedMinExpressionDepth = new Dictionary<Symbol, int>();
+      cachedMinExpressionLength = new Dictionary<string, int>();
+      cachedMaxExpressionLength = new Dictionary<string, int>();
+      cachedMinExpressionDepth = new Dictionary<string, int>();
       Initialize();
     }
 
-    private void Initialize() {
-      programRootSymbol = new ProgramRootSymbol();
-      var defunSymbol = new Defun();
-      startSymbol = new StartSymbol();
-      var invokeFunctionSymbol = new InvokeFunction();
-
-      SetMinSubTreeCount(programRootSymbol, minFunctionDefinitions + 1);
-      SetMaxSubTreeCount(programRootSymbol, maxFunctionDefinitions + 1);
-      SetMinSubTreeCount(startSymbol, 1);
-      SetMaxSubTreeCount(startSymbol, 1);
-      SetMinSubTreeCount(defunSymbol, 1);
-      SetMaxSubTreeCount(defunSymbol, 1);
-      SetMinSubTreeCount(invokeFunctionSymbol, minFunctionArguments);
-      SetMaxSubTreeCount(invokeFunctionSymbol, maxFunctionArguments);
-      AddAllowedSymbols(programRootSymbol, 0, startSymbol);
-      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
-        AddAllowedSymbols(programRootSymbol, argumentIndex, defunSymbol);
-      }
-    }
-
-    public void AddAllowedSymbols(Symbol parent, int argumentIndex, Symbol allowedChild) {
-      allSymbols.Add(parent); allSymbols.Add(allowedChild);
-      if (!allowedFunctions.ContainsKey(parent.Name)) {
-        allowedFunctions[parent.Name] = new List<HashSet<string>>();
-      }
-      while (allowedFunctions[parent.Name].Count <= argumentIndex)
-        allowedFunctions[parent.Name].Add(new HashSet<string>());
-      allowedFunctions[parent.Name][argumentIndex].Add(allowedChild.Name);
-      ClearCaches();
-    }
-
-    public void SetMaxSubTreeCount(Symbol parent, int nSubTrees) {
-      maxSubTreeCount[parent.Name] = nSubTrees;
-      ClearCaches();
-    }
-
-    public void SetMinSubTreeCount(Symbol parent, int nSubTrees) {
-      minSubTreeCount[parent.Name] = nSubTrees;
-      ClearCaches();
-    }
+    public void AddSymbol(Symbol symbol) {
+      if (allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Symbol " + symbol + " is already defined.");
+      allSymbols.Add(symbol);
+      allowedChildSymbols[symbol.Name] = new List<HashSet<string>>();
+      ClearCaches();
+    }
+
+    public void RemoveSymbol(Symbol symbol) {
+      allSymbols.RemoveWhere(s => s.Name == symbol.Name);
+      minSubTreeCount.Remove(symbol.Name);
+      maxSubTreeCount.Remove(symbol.Name);
+      allowedChildSymbols.Remove(symbol.Name);
+      ClearCaches();
+    }
+
+    public IEnumerable<Symbol> Symbols {
+      get { return allSymbols.AsEnumerable(); }
+    }
+
+    public void SetAllowedChild(Symbol parent, Symbol child, int argumentIndex) {
+      if (!allSymbols.Any(s => s.Name == parent.Name)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
+      if (!allSymbols.Any(s => s.Name == child.Name)) throw new ArgumentException("Unknown symbol: " + child, "child");
+      if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
+      allowedChildSymbols[parent.Name][argumentIndex].Add(child.Name);
+      ClearCaches();
+    }
+
+    public bool IsAllowedChild(Symbol parent, Symbol child, int argumentIndex) {
+      if (!allSymbols.Any(s => s.Name == parent.Name)) throw new ArgumentException("Unknown symbol: " + parent, "parent");
+      if (!allSymbols.Any(s => s.Name == child.Name)) throw new ArgumentException("Unknown symbol: " + child, "child");
+      if (argumentIndex >= GetMaxSubtreeCount(parent)) throw new ArgumentException("Symbol " + parent + " can have only " + GetMaxSubtreeCount(parent) + " subtrees.");
+      if (allowedChildSymbols.ContainsKey(parent.Name)) return allowedChildSymbols[parent.Name][argumentIndex].Contains(child.Name);
+      return false;
+    }
+
+    private Dictionary<string, int> cachedMinExpressionLength;
+    public int GetMinExpressionLength(Symbol symbol) {
+      if (!allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Unknown symbol: " + symbol);
+      if (!cachedMinExpressionLength.ContainsKey(symbol.Name)) {
+        cachedMinExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
+        long sumOfMinExpressionLengths = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
+                                              let minForSlot = (long)(from s in allSymbols
+                                                                      where IsAllowedChild(symbol, s, argIndex)
+                                                                      select GetMinExpressionLength(s)).DefaultIfEmpty(0).Min()
+                                              select minForSlot).DefaultIfEmpty(0).Sum();
+
+        cachedMinExpressionLength[symbol.Name] = (int)Math.Min(sumOfMinExpressionLengths, int.MaxValue);
+      }
+      return cachedMinExpressionLength[symbol.Name];
+    }
+
+    private Dictionary<string, int> cachedMaxExpressionLength;
+    public int GetMaxExpressionLength(Symbol symbol) {
+      if (!allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Unknown symbol: " + symbol);
+      if (!cachedMaxExpressionLength.ContainsKey(symbol.Name)) {
+        cachedMaxExpressionLength[symbol.Name] = int.MaxValue; // prevent infinite recursion
+        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaxSubtreeCount(symbol))
+                                  let maxForSlot = (long)(from s in allSymbols
+                                                          where IsAllowedChild(symbol, s, argIndex)
+                                                          select GetMaxExpressionLength(s)).DefaultIfEmpty(0).Max()
+                                  select maxForSlot).DefaultIfEmpty(0).Sum();
+        long limit = int.MaxValue;
+        cachedMaxExpressionLength[symbol.Name] = (int)Math.Min(sumOfMaxTrees, limit);
+      }
+      return cachedMaxExpressionLength[symbol.Name];
+    }
+
+    private Dictionary<string, int> cachedMinExpressionDepth;
+    public int GetMinExpressionDepth(Symbol symbol) {
+      if (!allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Unknown symbol: " + symbol);
+      if (!cachedMinExpressionDepth.ContainsKey(symbol.Name)) {
+        cachedMinExpressionDepth[symbol.Name] = int.MaxValue; // prevent infinite recursion
+        cachedMinExpressionDepth[symbol.Name] = 1 + (from argIndex in Enumerable.Range(0, GetMinSubtreeCount(symbol))
+                                                     let minForSlot = (from s in allSymbols
+                                                                       where IsAllowedChild(symbol, s, argIndex)
+                                                                       select GetMinExpressionDepth(s)).DefaultIfEmpty(0).Min()
+                                                     select minForSlot).DefaultIfEmpty(0).Max();
+      }
+      return cachedMinExpressionDepth[symbol.Name];
+    }
+
+    public void SetMaxSubtreeCount(Symbol symbol, int nSubTrees) {
+      if (!allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Unknown symbol: " + symbol);
+      maxSubTreeCount[symbol.Name] = nSubTrees;
+      while (allowedChildSymbols[symbol.Name].Count <= nSubTrees)
+        allowedChildSymbols[symbol.Name].Add(new HashSet<string>());
+      while (allowedChildSymbols[symbol.Name].Count > nSubTrees) {
+        allowedChildSymbols[symbol.Name].RemoveAt(allowedChildSymbols[symbol.Name].Count - 1);
+      }
+      ClearCaches();
+    }
+
+    public void SetMinSubtreeCount(Symbol symbol, int nSubTrees) {
+      if (!allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Unknown symbol: " + symbol);
+      minSubTreeCount[symbol.Name] = nSubTrees;
+      ClearCaches();
+    }
+
+    public int GetMinSubtreeCount(Symbol symbol) {
+      if (!allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Unknown symbol: " + symbol);
+      return minSubTreeCount[symbol.Name];
+    }
+
+    public int GetMaxSubtreeCount(Symbol symbol) {
+      if (!allSymbols.Any(s => s.Name == symbol.Name)) throw new ArgumentException("Unknown symbol: " + symbol);
+      return maxSubTreeCount[symbol.Name];
+    }
+
+    #endregion
 
     private void ClearCaches() {
@@ -114,123 +189,27 @@
     }
 
-    private void symbol_ToStringChanged(object sender, EventArgs e) {
-      OnToStringChanged();
-    }
-
-    #region ISymbolicExpressionGrammar Members
-
-    private Symbol programRootSymbol;
-    public Symbol ProgramRootSymbol {
-      get { return programRootSymbol; }
-    }
-
-    private Symbol startSymbol;
-    public Symbol StartSymbol {
-      get { return startSymbol; }
-    }
-
-    public IEnumerable<Symbol> GetAllowedSymbols(Symbol parent, int argumentIndex) {
-      return from name in allowedFunctions[parent.Name][argumentIndex]
-             from sym in allSymbols
-             where name == sym.Name
-             select sym;
-    }
-
-
-    private Dictionary<Symbol, int> cachedMinExpressionLength;
-    public int GetMinExpressionLength(Symbol start) {
-      if (!cachedMinExpressionLength.ContainsKey(start)) {
-        cachedMinExpressionLength[start] = int.MaxValue; // prevent infinite recursion
-        cachedMinExpressionLength[start] = 1 + (from argIndex in Enumerable.Range(0, GetMinSubTreeCount(start))
-                                                let minForSlot = (from symbol in GetAllowedSymbols(start, argIndex)
-                                                                  select GetMinExpressionLength(symbol)).DefaultIfEmpty(0).Min()
-                                                select minForSlot).DefaultIfEmpty(0).Sum();
-      }
-      return cachedMinExpressionLength[start];
-    }
-
-    private Dictionary<Symbol, int> cachedMaxExpressionLength;
-    public int GetMaxExpressionLength(Symbol start) {
-      if (!cachedMaxExpressionLength.ContainsKey(start)) {
-        cachedMaxExpressionLength[start] = int.MaxValue; // prevent infinite recursion
-        long sumOfMaxTrees = 1 + (from argIndex in Enumerable.Range(0, GetMaxSubTreeCount(start))
-                                  let maxForSlot = (long)(from symbol in GetAllowedSymbols(start, argIndex)
-                                                          select GetMaxExpressionLength(symbol)).DefaultIfEmpty(0).Max()
-                                  select maxForSlot).DefaultIfEmpty(0).Sum();
-        long limit = int.MaxValue;
-        cachedMaxExpressionLength[start] = (int)Math.Min(sumOfMaxTrees, limit);
-      }
-      return cachedMaxExpressionLength[start];
-    }
-
-    private Dictionary<Symbol, int> cachedMinExpressionDepth;
-    public int GetMinExpressionDepth(Symbol start) {
-      if (!cachedMinExpressionDepth.ContainsKey(start)) {
-        cachedMinExpressionDepth[start] = int.MaxValue; // prevent infinite recursion
-        cachedMinExpressionDepth[start] = 1 + (from argIndex in Enumerable.Range(0, GetMinSubTreeCount(start))
-                                               let minForSlot = (from symbol in GetAllowedSymbols(start, argIndex)
-                                                                 select GetMinExpressionDepth(symbol)).DefaultIfEmpty(0).Min()
-                                               select minForSlot).DefaultIfEmpty(0).Max();
-      }
-      return cachedMinExpressionDepth[start];
-    }
-
-    public int GetMinSubTreeCount(Symbol start) {
-      return minSubTreeCount[start.Name];
-    }
-
-    public int GetMaxSubTreeCount(Symbol start) {
-      return maxSubTreeCount[start.Name];
-    }
-
-    public bool IsValidExpression(SymbolicExpressionTree expression) {
-      if (expression.Root.Symbol != ProgramRootSymbol) return false;
-      // check dynamic symbols
-      foreach (var branch in expression.Root.SubTrees) {
-        foreach (var dynamicNode in branch.DynamicSymbols) {
-          if (!dynamicNode.StartsWith("ARG")) {
-            if (FindDefinitionOfDynamicFunction(expression.Root, dynamicNode) == null) return false;
-          }
-        }
-      }
-      return IsValidExpression(expression.Root);
-    }
-
-    #endregion
-    private bool IsValidExpression(SymbolicExpressionTreeNode root) {
-      if (root.SubTrees.Count < GetMinSubTreeCount(root.Symbol)) return false;
-      if (root.SubTrees.Count > GetMaxSubTreeCount(root.Symbol)) return false;
-      if (root.Symbol is Defun || root.Symbol is StartSymbol) {
-        // check references to dynamic symbols
-        if (!CheckDynamicSymbolsInBranch(root, root.SubTrees[0])) return false;
-      }
-      for (int i = 0; i < root.SubTrees.Count; i++) {
-        if (!GetAllowedSymbols(root.Symbol, i).Contains(root.SubTrees[i].Symbol)) return false;
-        if (!IsValidExpression(root.SubTrees[i])) return false;
-      }
-      return true;
-    }
-
-    private SymbolicExpressionTreeNode FindDefinitionOfDynamicFunction(SymbolicExpressionTreeNode root, string dynamicNode) {
-      return (from node in root.SubTrees.OfType<DefunTreeNode>()
-              where node.Name == dynamicNode
-              select node).FirstOrDefault();
-    }
-
-    private bool CheckDynamicSymbolsInBranch(SymbolicExpressionTreeNode root, SymbolicExpressionTreeNode node) {
-      var argNode = node as ArgumentTreeNode;
-      var invokeNode = node as InvokeFunctionTreeNode;
-      if (argNode != null) {
-        if (!root.DynamicSymbols.Contains("ARG" + argNode.ArgumentIndex)) return false;
-      } else if (invokeNode != null) {
-        if (!root.DynamicSymbols.Contains(invokeNode.InvokedFunctionName)) return false;
-        if (root.GetDynamicSymbolArgumentCount(invokeNode.InvokedFunctionName) != invokeNode.SubTrees.Count()) return false;
-      }
-      foreach (var subtree in node.SubTrees) {
-        if (!CheckDynamicSymbolsInBranch(root, subtree)) return false;
-      }
-      return true;
-    }
-
+    //private void symbol_ToStringChanged(object sender, EventArgs e) {
+    //  OnToStringChanged();
+    //}
+
+    //private bool IsValidExpression(SymbolicExpressionTreeNode root) {
+    //  if (root.SubTrees.Count < root.GetMinSubtreeCount()) return false;
+    //  else if (root.SubTrees.Count > root.GetMaxSubtreeCount()) return false;
+    //  else for (int i = 0; i < root.SubTrees.Count; i++) {
+    //      if (!root.GetAllowedSymbols(i).Select(x => x.Name).Contains(root.SubTrees[i].Symbol.Name)) return false;
+    //      if (!IsValidExpression(root.SubTrees[i])) return false;
+    //    }
+    //  return true;
+    //}
+
+    public override IDeepCloneable Clone(Cloner cloner) {
+      DefaultSymbolicExpressionGrammar clone = (DefaultSymbolicExpressionGrammar)base.Clone(cloner);
+      clone.maxSubTreeCount = new Dictionary<string, int>(maxSubTreeCount);
+      clone.minSubTreeCount = new Dictionary<string, int>(minSubTreeCount);
+      clone.startSymbol = startSymbol;
+      clone.allowedChildSymbols = new Dictionary<string, List<HashSet<string>>>(allowedChildSymbols);
+      clone.allSymbols = new HashSet<Symbol>(allSymbols);
+      return clone;
+    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/EnumerableExtensions.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/EnumerableExtensions.cs	(revision 3338)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/EnumerableExtensions.cs	(revision 3338)
@@ -0,0 +1,43 @@
+#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.Collections.Generic;
+using System.Text;
+using HeuristicLab.Core;
+using System.Linq;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Data;
+using HeuristicLab.Parameters;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
+  public static class EnumerableExtensions {
+    public static T SelectRandom<T>(this IEnumerable<T> xs, IRandom random) {
+      var list = xs as IList<T>;
+      if (list != null) {
+        return list[random.Next(list.Count)];
+      } else {
+        list = xs.ToList();
+        return list[random.Next(list.Count)];
+      }
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs	(revision 3338)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/GlobalSymbolicExpressionGrammar.cs	(revision 3338)
@@ -0,0 +1,145 @@
+#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.Collections.Generic;
+using System.Linq;
+using System.Text;
+using HeuristicLab.Core;
+using System.Xml;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
+  [StorableClass]
+  [Item("GlobalSymbolicExpressionGrammar", "Represents a grammar that defines the syntax of symbolic expression trees.")]
+  public class GlobalSymbolicExpressionGrammar : DefaultSymbolicExpressionGrammar {
+    [Storable]
+    private int minFunctionDefinitions;
+    public int MinFunctionDefinitions {
+      get { return minFunctionDefinitions; }
+      set { 
+        minFunctionDefinitions = value;
+        Reset();
+      }
+    }
+    [Storable]
+    private int maxFunctionDefinitions;
+    public int MaxFunctionDefinitions {
+      get { return maxFunctionDefinitions; }
+      set { 
+        maxFunctionDefinitions = value;
+        Reset();
+      }
+    }
+    [Storable]
+    private int minFunctionArguments;
+    public int MinFunctionArguments {
+      get { return minFunctionArguments; }
+      set { 
+        minFunctionArguments = value;
+        Reset();
+      }
+    }
+    [Storable]
+    private int maxFunctionArguments;
+    public int MaxFunctionArguments {
+      get { return maxFunctionArguments; }
+      set { 
+        maxFunctionArguments = value;
+        Reset();
+      }
+    }
+    [Storable]
+    private ISymbolicExpressionGrammar mainBranchGrammar;
+
+    public GlobalSymbolicExpressionGrammar() : base() { } // empty constructor for cloning
+
+    public GlobalSymbolicExpressionGrammar(ISymbolicExpressionGrammar mainBranchGrammar )
+      : base() {
+      maxFunctionArguments = 3;
+      maxFunctionDefinitions = 3;
+      this.mainBranchGrammar = mainBranchGrammar;
+      Initialize();
+    }
+
+    private void Reset() {
+      base.Reset();
+      Initialize();
+    }
+
+    private void Initialize() {
+      // remove the start symbol of the default grammar
+      RemoveSymbol(StartSymbol);
+
+      StartSymbol = new ProgramRootSymbol();
+      var defunSymbol = new Defun();
+      AddSymbol(StartSymbol);
+      AddSymbol(defunSymbol);
+
+      SetMinSubtreeCount(StartSymbol, minFunctionDefinitions + 1);
+      SetMaxSubtreeCount(StartSymbol, maxFunctionDefinitions + 1);
+      SetMinSubtreeCount(defunSymbol, 1);
+      SetMaxSubtreeCount(defunSymbol, 1);
+
+      // copy symbols from mainBranchGrammar
+      foreach (var symb in mainBranchGrammar.Symbols) {
+        AddSymbol(symb);
+        SetMinSubtreeCount(symb, mainBranchGrammar.GetMinSubtreeCount(symb));
+        SetMaxSubtreeCount(symb, mainBranchGrammar.GetMaxSubtreeCount(symb));
+      }
+
+      // the start symbol of the mainBranchGrammar is allowed as the result producing branch
+      SetAllowedChild(StartSymbol, mainBranchGrammar.StartSymbol, 0);
+
+      // ADF branches maxFunctionDefinitions 
+      for (int argumentIndex = 1; argumentIndex < maxFunctionDefinitions + 1; argumentIndex++) {
+        SetAllowedChild(StartSymbol, defunSymbol, argumentIndex);
+      }
+
+      // copy syntax constraints from mainBranchGrammar
+      foreach (var parent in mainBranchGrammar.Symbols) {
+        for (int i = 0; i < mainBranchGrammar.GetMaxSubtreeCount(parent); i++) {
+          foreach (var child in mainBranchGrammar.Symbols) {
+            if (mainBranchGrammar.IsAllowedChild(parent, child, i)) {
+              SetAllowedChild(parent, child, i);
+            }
+          }
+        }
+      }
+
+      // every symbol of the mainBranchGrammar that is allowed as child of the start symbol is also allowed as direct child of defun
+      foreach (var symb in mainBranchGrammar.Symbols) {
+        if (mainBranchGrammar.IsAllowedChild(mainBranchGrammar.StartSymbol, symb, 0))
+          SetAllowedChild(defunSymbol, symb, 0);
+      }
+    }
+    public override IDeepCloneable Clone(Cloner cloner) {
+      GlobalSymbolicExpressionGrammar clone = (GlobalSymbolicExpressionGrammar)base.Clone(cloner);
+      clone.maxFunctionArguments = maxFunctionArguments;
+      clone.maxFunctionDefinitions = maxFunctionDefinitions;
+      clone.minFunctionArguments = minFunctionArguments;
+      clone.minFunctionDefinitions = minFunctionDefinitions;
+      clone.mainBranchGrammar = mainBranchGrammar;
+      return clone;
+    }
+  }
+}
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 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.csproj	(revision 3338)
@@ -83,15 +83,12 @@
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="ArchitectureAlteringOperators\ArgumentDuplicater.cs" />
     <Compile Include="ArchitectureAlteringOperators\ArgumentCreater.cs" />
     <Compile Include="ArchitectureAlteringOperators\ArgumentDeleter.cs" />
-    <Compile Include="ArchitectureAlteringOperators\RandomArchitectureAlteringOperator.cs" />
-    <Compile Include="ArchitectureAlteringOperators\SubroutineDeleter.cs" />
-    <Compile Include="ArchitectureAlteringOperators\SubroutineCreater.cs">
+    <Compile Include="Crossovers\SubtreeCrossover.cs">
       <SubType>Code</SubType>
     </Compile>
-    <Compile Include="ArchitectureAlteringOperators\SubroutineDuplicater.cs">
-      <SubType>Code</SubType>
-    </Compile>
+    <Compile Include="GlobalSymbolicExpressionGrammar.cs" />
+    <Compile Include="EnumerableExtensions.cs" />
+    <Compile Include="Symbols\StartSymbolTreeNode.cs" />
     <Compile Include="DefaultSymbolicExpressionGrammar.cs">
       <SubType>Code</SubType>
@@ -103,5 +100,4 @@
     <Compile Include="SymbolicExpressionTreeManipulator.cs" />
     <Compile Include="SymbolicExpressionTreeTerminalNode.cs" />
-    <Compile Include="Crossovers\SubtreeCrossover.cs" />
     <Compile Include="Interfaces\ISymbolicExpressionGrammar.cs" />
     <Compile Include="Creators\ProbabilisticTreeCreator.cs" />
@@ -126,10 +122,4 @@
     <Compile Include="Symbols\Multiplication.cs" />
     <Compile Include="Symbols\Subtraction.cs" />
-    <Compile Include="Symbols\Values.cs">
-      <SubType>Code</SubType>
-    </Compile>
-    <Compile Include="Symbols\ValuesTreeNode.cs">
-      <SubType>Code</SubType>
-    </Compile>
   </ItemGroup>
   <ItemGroup>
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Interfaces/ISymbolicExpressionGrammar.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Interfaces/ISymbolicExpressionGrammar.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Interfaces/ISymbolicExpressionGrammar.cs	(revision 3338)
@@ -31,13 +31,16 @@
   public interface ISymbolicExpressionGrammar : IItem {
     Symbol StartSymbol { get; }
-    Symbol ProgramRootSymbol { get; }
-    IEnumerable<Symbol> GetAllowedSymbols(Symbol parent, int argumentIndex);
+    void AddSymbol(Symbol symbol);
+    void RemoveSymbol(Symbol symbol);
+    IEnumerable<Symbol> Symbols { get; }
+    void SetAllowedChild(Symbol parent, Symbol child, int argumentIndex);
+    bool IsAllowedChild(Symbol parent, Symbol child, int argumentIndex);
     int GetMinExpressionLength(Symbol start);
     int GetMaxExpressionLength(Symbol start);
     int GetMinExpressionDepth(Symbol start);
-    int GetMinSubTreeCount(Symbol start);
-    int GetMaxSubTreeCount(Symbol start);
-
-    bool IsValidExpression(SymbolicExpressionTree expression);
+    int GetMinSubtreeCount(Symbol symbol);
+    void SetMinSubtreeCount(Symbol symbol, int value);
+    int GetMaxSubtreeCount(Symbol symbol);
+    void SetMaxSubtreeCount(Symbol symbol, int value);
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTree.cs	(revision 3338)
@@ -50,7 +50,4 @@
     }
 
-    [Storable]
-    private Dictionary<int, IEnumerable<string>> allowedFunctionsInBranch;
-
     public int Size {
       get {
@@ -67,31 +64,22 @@
     public SymbolicExpressionTree()
       : base() {
-      allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>();
     }
 
     public SymbolicExpressionTree(SymbolicExpressionTreeNode root)
       : base() {
-      allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>();
     }
 
     public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
-      return IterateNodesPrefix(root);
-    }
-    private IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix(SymbolicExpressionTreeNode node) {
-      yield return node;
-      foreach (var subtree in node.SubTrees) {
-        foreach (var n in IterateNodesPrefix(subtree))
-          yield return n;
-      }
+      return root.IterateNodesPrefix();
     }
     public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
-      return IterateNodesPostfix(root);
+      return root.IterateNodesPostfix();
     }
-    private IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix(SymbolicExpressionTreeNode node) {
-      foreach (var subtree in node.SubTrees) {
-        foreach (var n in IterateNodesPrefix(subtree))
-          yield return n;
+
+    public SymbolicExpressionTreeNode GetTopLevelBranchOf(SymbolicExpressionTreeNode node) {
+      foreach (var branch in root.SubTrees) {
+        if (branch.IterateNodesPrefix().Contains(node)) return branch;
       }
-      yield return node;
+      throw new ArgumentException("Node was not found in tree.");
     }
 
@@ -101,6 +89,12 @@
       clone.ReadOnlyView = ReadOnlyView;
       clone.root = (SymbolicExpressionTreeNode)this.root.Clone();
-      clone.allowedFunctionsInBranch = new Dictionary<int, IEnumerable<string>>(allowedFunctionsInBranch);
       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/SymbolicExpressionTreeCompiler.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCompiler.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCompiler.cs	(revision 3338)
@@ -36,5 +36,5 @@
       {typeof(Division), CodeSymbol.Div},
       {typeof(InvokeFunction), CodeSymbol.Call},
-      {typeof(Values), CodeSymbol.Values}
+      //{typeof(Values), CodeSymbol.Values}
     };
     private Dictionary<string, short> entryPoint = new Dictionary<string, short>();
@@ -50,5 +50,5 @@
                              select node;
       foreach (DefunTreeNode branch in functionBranches) {
-        entryPoint[branch.Name] = (short)code.Count;
+        entryPoint[branch.FunctionName] = (short)code.Count;
         code.AddRange(Compile(branch));
       }
@@ -65,5 +65,5 @@
           if (instr.symbol == CodeSymbol.Call) {
             var invokeNode = (InvokeFunctionTreeNode)node;
-            instr.iArg0 = entryPoint[invokeNode.InvokedFunctionName];
+            instr.iArg0 = entryPoint[invokeNode.Symbol.FunctionName];
           }
         } else {
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCreator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCreator.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCreator.cs	(revision 3338)
@@ -35,11 +35,33 @@
   [StorableClass]
   public abstract class SymbolicExpressionTreeCreator : SymbolicExpressionTreeOperator, ISolutionCreator {
+    private const string MaxFunctionDefinitionsParameterName = "MaxFunctionDefinitions";
+    private const string MaxFunctionArgumentsParameterName = "MaxFunctionArguments";
+    #region Parameter Properties
+    public IValueLookupParameter<IntValue> MaxFunctionDefinitionsParameter {
+      get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionDefinitionsParameterName]; }
+    }
+    public IValueLookupParameter<IntValue> MaxFunctionArgumentsParameter {
+      get { return (IValueLookupParameter<IntValue>)Parameters[MaxFunctionArgumentsParameterName]; }
+    }
+    #endregion
+
+    #region Propeties
+    public IntValue MaxFunctionDefinitions {
+      get { return MaxFunctionDefinitionsParameter.ActualValue; }
+    }
+    public IntValue MaxFunctionArguments {
+      get { return MaxFunctionArgumentsParameter.ActualValue; }
+    }
+
+    #endregion
     protected SymbolicExpressionTreeCreator()
       : base() {
+      Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionDefinitionsParameterName, "Maximal number of function definitions in the symbolic expression tree."));
+      Parameters.Add(new ValueLookupParameter<IntValue>(MaxFunctionArgumentsParameterName, "Maximal number of arguments of automatically defined functions in the symbolic expression tree."));
     }
 
     public sealed override IOperation Apply() {
-      SymbolicExpressionTreeParameter.ActualValue = Create(RandomParameter.ActualValue, SymbolicExpressionGrammarParameter.ActualValue,
-        MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue);
+      SymbolicExpressionTreeParameter.ActualValue = Create(Random, SymbolicExpressionGrammar,
+        MaxTreeSize, MaxTreeHeight, MaxFunctionDefinitions, MaxFunctionArguments);
 
       foreach (var node in SymbolicExpressionTreeParameter.ActualValue.IterateNodesPostfix()) {
@@ -49,5 +71,10 @@
     }
 
-    protected abstract SymbolicExpressionTree Create(IRandom random, ISymbolicExpressionGrammar grammar, IntValue maxTreeSize, IntValue maxTreeHeight);
+    protected abstract SymbolicExpressionTree Create(
+      IRandom random,
+      ISymbolicExpressionGrammar grammar,
+      IntValue maxTreeSize, IntValue maxTreeHeight,
+      IntValue maxFunctionDefinitions, IntValue maxFunctionArguments
+      );
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCrossover.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCrossover.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeCrossover.cs	(revision 3338)
@@ -67,5 +67,4 @@
 
       IRandom random = RandomParameter.ActualValue;
-      ISymbolicExpressionGrammar grammar = SymbolicExpressionGrammarParameter.ActualValue;
 
       // randomly swap parents to remove a possible bias from selection (e.g. when using gender-specific selection)
@@ -77,5 +76,5 @@
 
       bool success;
-      SymbolicExpressionTree result = Cross(random, grammar, parent0, parent1,
+      SymbolicExpressionTree result = Cross(random, parent0, parent1,
         MaxTreeSizeParameter.ActualValue, MaxTreeHeightParameter.ActualValue, out success);
       
@@ -89,5 +88,5 @@
     }
 
-    protected abstract SymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionGrammar grammar,
+    protected abstract SymbolicExpressionTree Cross(IRandom random, 
       SymbolicExpressionTree parent0, SymbolicExpressionTree parent1,
       IntValue maxTreeSize, IntValue maxTreeHeight, out bool success);
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeNode.cs	(revision 3338)
@@ -38,8 +38,10 @@
     private Symbol symbol;
 
-    public SymbolicExpressionTreeNode() { }
+    public SymbolicExpressionTreeNode() {
+      subTrees = new List<SymbolicExpressionTreeNode>();
+    }
 
-    public SymbolicExpressionTreeNode(Symbol symbol) {
-      subTrees = new List<SymbolicExpressionTreeNode>();
+    public SymbolicExpressionTreeNode(Symbol symbol)
+      : this() {
       this.symbol = symbol;
     }
@@ -49,8 +51,8 @@
       symbol = original.symbol;
       subTrees = new List<SymbolicExpressionTreeNode>();
+      grammar = original.grammar;
       foreach (var subtree in original.SubTrees) {
-        AddSubTree((SymbolicExpressionTreeNode)subtree.Clone());
+        SubTrees.Add((SymbolicExpressionTreeNode)subtree.Clone());
       }
-      dynamicSymbols = new Dictionary<string, int>(original.dynamicSymbols);
     }
 
@@ -68,4 +70,14 @@
     }
 
+    private ISymbolicExpressionGrammar grammar;
+    public virtual ISymbolicExpressionGrammar Grammar {
+      get { return grammar; }
+      set {
+        grammar = value;
+        foreach (var subtree in subTrees)
+          subtree.Grammar = value;
+      }
+    }
+
     public int GetSize() {
       int size = 1;
@@ -80,46 +92,67 @@
     }
 
-    [Storable]
-    private Dictionary<string, int> dynamicSymbols = new Dictionary<string, int>();
-    public void AddDynamicSymbol(string symbolName) {
-      Debug.Assert(!dynamicSymbols.ContainsKey(symbolName));
-      dynamicSymbols[symbolName] = 0;
-    }
-
-    public void AddDynamicSymbol(string symbolName, int nArguments) {
-      AddDynamicSymbol(symbolName);
-      SetDynamicSymbolArgumentCount(symbolName, nArguments);
-    }
-
-    public void RemoveDynamicSymbol(string symbolName) {
-      dynamicSymbols.Remove(symbolName);
-    }
-
-    public IEnumerable<string> DynamicSymbols {
-      get { return dynamicSymbols.Keys; }
-    }
-
-    public int GetDynamicSymbolArgumentCount(string symbolName) {
-      return dynamicSymbols[symbolName];
-    }
-    public void SetDynamicSymbolArgumentCount(string symbolName, int nArguments) {
-      Debug.Assert(dynamicSymbols.ContainsKey(symbolName));
-      dynamicSymbols[symbolName] = nArguments;
-    }
-
     public virtual void ResetLocalParameters(IRandom random) { }
     public virtual void ShakeLocalParameters(IRandom random, double shakingFactor) { }
 
-    protected internal virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
+    public virtual void AddSubTree(SymbolicExpressionTreeNode tree) {
       SubTrees.Add(tree);
+      //if (tree != null) 
+      tree.Grammar = Grammar;
     }
 
-    protected internal virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
+    public virtual void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
       SubTrees.Insert(index, tree);
+      //if (tree != null) 
+      tree.Grammar = Grammar;
     }
 
-    protected internal virtual void RemoveSubTree(int index) {
+    public virtual void RemoveSubTree(int index) {
+      //if (SubTrees[index] != null)
+      SubTrees[index].Grammar = null;
       SubTrees.RemoveAt(index);
     }
+
+    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPrefix() {
+      yield return this;
+      foreach (var subtree in subTrees) {
+        foreach (var n in subtree.IterateNodesPrefix())
+          yield return n;
+      }
+    }
+
+    public IEnumerable<SymbolicExpressionTreeNode> IterateNodesPostfix() {
+      foreach (var subtree in subTrees) {
+        foreach (var n in subtree.IterateNodesPrefix())
+          yield return n;
+      }
+      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));
+    }
+    public int GetMinSubtreeCount() {
+      return Grammar.GetMinSubtreeCount(Symbol);
+    }
+    public int GetMaxSubtreeCount() {
+      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
@@ -130,4 +163,19 @@
 
     #endregion
+
+
+    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/SymbolicExpressionTreeOperator.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeOperator.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeOperator.cs	(revision 3338)
@@ -45,4 +45,5 @@
     }
 
+    #region Parameter Properties
     public ILookupParameter<IRandom> RandomParameter {
       get { return (LookupParameter<IRandom>)Parameters[RandomParameterName]; }
@@ -60,4 +61,23 @@
       get { return (ILookupParameter<ISymbolicExpressionGrammar>)Parameters[SymbolicExpressionGrammarParameterName]; }
     }
+    #endregion
+
+    #region Properties
+    public IRandom Random {
+      get { return RandomParameter.ActualValue; }
+    }
+    public SymbolicExpressionTree SymbolicExpressionTree {
+      get { return SymbolicExpressionTreeParameter.ActualValue; }
+    }
+    public IntValue MaxTreeSize {
+      get { return MaxTreeSizeParameter.ActualValue; }
+    }
+    public IntValue MaxTreeHeight {
+      get { return MaxTreeHeightParameter.ActualValue; }
+    }
+    public ISymbolicExpressionGrammar SymbolicExpressionGrammar {
+      get { return SymbolicExpressionGrammarParameter.ActualValue; }
+    }
+    #endregion
 
     protected SymbolicExpressionTreeOperator()
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTerminalNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTerminalNode.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/SymbolicExpressionTreeTerminalNode.cs	(revision 3338)
@@ -37,11 +37,11 @@
     }
 
-    protected internal override void AddSubTree(SymbolicExpressionTreeNode tree) {
+    public override void AddSubTree(SymbolicExpressionTreeNode tree) {
       throw new NotSupportedException();
     }
-    protected internal override void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
+    public override void InsertSubTree(int index, SymbolicExpressionTreeNode tree) {
       throw new NotSupportedException();
     }
-    protected internal override void RemoveSubTree(int index) {
+    public override void RemoveSubTree(int index) {
       throw new NotSupportedException();
     }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Argument.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Argument.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Argument.cs	(revision 3338)
@@ -34,4 +34,16 @@
       }
     }
+
+    private int argumentIndex;
+    public int ArgumentIndex {
+      get { return argumentIndex; }
+    }
+
+    public Argument(int argumentIndex)
+      : base() {
+      this.argumentIndex = argumentIndex;
+      this.name = "ARG" + argumentIndex;
+    }
+
     public override SymbolicExpressionTreeNode CreateTreeNode() {
       return new ArgumentTreeNode(this);
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/ArgumentTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/ArgumentTreeNode.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/ArgumentTreeNode.cs	(revision 3338)
@@ -25,15 +25,16 @@
   [StorableClass]
   public sealed class ArgumentTreeNode : SymbolicExpressionTreeNode {
-    private int argumentIndex;
-    [Storable]
-    public int ArgumentIndex {
-      get { return argumentIndex; }
-      set { argumentIndex = value; }
+    public new Argument Symbol {
+      get { return (Argument)base.Symbol; }
+      set {
+        if (value == null) throw new ArgumentNullException();
+        if(!(value is Argument)) throw new ArgumentException();
+        base.Symbol = value; 
+      }
     }
-
+    
     // copy constructor
     private ArgumentTreeNode(ArgumentTreeNode original)
       : base(original) {
-      argumentIndex = original.argumentIndex;
     }
 
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Defun.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Defun.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/Defun.cs	(revision 3338)
@@ -34,4 +34,5 @@
       }
     }
+
     public override SymbolicExpressionTreeNode CreateTreeNode() {
       return new DefunTreeNode(this);
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/DefunTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/DefunTreeNode.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/DefunTreeNode.cs	(revision 3338)
@@ -22,4 +22,5 @@
 using System;
 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Core;
 namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols {
   [StorableClass]
@@ -31,9 +32,13 @@
       set { numberOfArguments = value; }
     }
-    private string name;
+    private string functionName;
     [Storable]
-    public string Name {
-      get { return name; }
-      set { this.name = value; }
+    public string FunctionName {
+      get { return functionName; }
+      set { this.functionName = value; }
+    }
+
+    private new Defun Symbol {
+      get { return (Defun)base.Symbol; }
     }
 
@@ -41,9 +46,10 @@
     private DefunTreeNode(DefunTreeNode original)
       : base(original) {
-      name = original.Name;
+      functionName = original.functionName;
       numberOfArguments = original.numberOfArguments;
     }
 
     public DefunTreeNode(Defun defunSymbol) : base(defunSymbol) { }
+
 
     public override object Clone() {
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunction.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunction.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunction.cs	(revision 3338)
@@ -34,4 +34,12 @@
       }
     }
+    public string FunctionName { get; set; }
+
+    public InvokeFunction(string functionName)
+      : base() {
+      this.FunctionName = functionName;
+      this.name = "Invoke: " + functionName;
+    }
+
     public override SymbolicExpressionTreeNode CreateTreeNode() {
       return new InvokeFunctionTreeNode(this);
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunctionTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunctionTreeNode.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/InvokeFunctionTreeNode.cs	(revision 3338)
@@ -25,9 +25,6 @@
   [StorableClass]
   public sealed class InvokeFunctionTreeNode : SymbolicExpressionTreeNode {
-    private string invokedFunctionName;
-    [Storable]
-    public string InvokedFunctionName {
-      get { return invokedFunctionName; }
-      set { this.invokedFunctionName = value; }
+    public new InvokeFunction Symbol {
+      get { return (InvokeFunction)base.Symbol; }
     }
 
@@ -35,5 +32,4 @@
     private InvokeFunctionTreeNode(InvokeFunctionTreeNode original)
       : base(original) {
-      invokedFunctionName = original.invokedFunctionName;
     }
 
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbol.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbol.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbol.cs	(revision 3338)
@@ -31,4 +31,8 @@
       }
     }
+
+    public override SymbolicExpressionTreeNode CreateTreeNode() {
+      return new StartSymbolTreeNode(this);
+    }
   }
 }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbolTreeNode.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbolTreeNode.cs	(revision 3338)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Symbols/StartSymbolTreeNode.cs	(revision 3338)
@@ -0,0 +1,38 @@
+#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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.GeneralSymbols {
+  [StorableClass]
+  public sealed class StartSymbolTreeNode : SymbolicExpressionTreeNode {
+    // copy constructor
+    private StartSymbolTreeNode(StartSymbolTreeNode original)
+      : base(original) {
+    }
+
+    public StartSymbolTreeNode(StartSymbol startSymbol) : base(startSymbol) { }
+
+    public override object Clone() {
+      return new StartSymbolTreeNode(this);
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs	(revision 3338)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentCreaterTest.cs	(revision 3338)
@@ -0,0 +1,51 @@
+﻿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 ArgumentCreaterTest {
+    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 ArgumentCreaterDistributionsTest() {
+      var trees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister();
+      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());
+        trees.Add(tree);
+      }
+      Assert.Inconclusive("ArgumentCreator: " + 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/ArgumentDeleterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDeleterTest.cs	(revision 3338)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ArgumentDeleterTest.cs	(revision 3338)
@@ -0,0 +1,51 @@
+﻿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 ArgumentDeleterTest {
+    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 ArgumentDeleterDistributionsTest() {
+      var trees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      var random = new MersenneTwister();
+      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());
+        trees.Add(tree);
+      }
+      Assert.Inconclusive("ArgumentDeleter: " + 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/Grammars.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs	(revision 3338)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Grammars.cs	(revision 3338)
@@ -0,0 +1,77 @@
+﻿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.GeneralSymbols;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
+  public static class Grammars {
+    private class Addition : Symbol { }
+    private class Subtraction : Symbol { }
+    private class Multiplication : Symbol { }
+    private class Division : Symbol { }
+    private class Terminal : Symbol { }
+
+    private class SimpleArithmeticGrammar : DefaultSymbolicExpressionGrammar {
+      public SimpleArithmeticGrammar()
+        : base() {
+        Initialize();
+      }
+
+      private void Initialize() {
+        var add = new Addition();
+        var sub = new Subtraction();
+        var mul = new Multiplication();
+        var div = new Division();
+        var terminal = new Terminal();
+
+        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
+        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
+        foreach (var symb in allSymbols)
+          AddSymbol(symb);
+
+        foreach (var funSymb in functionSymbols) {
+          SetMinSubtreeCount(funSymb, 1);
+          SetMaxSubtreeCount(funSymb, 3);
+        }
+        SetMinSubtreeCount(terminal, 0);
+        SetMaxSubtreeCount(terminal, 0);
+
+        // allow each symbol as child of the start symbol
+        foreach (var symb in allSymbols) {
+          SetAllowedChild(StartSymbol, symb, 0);
+        }
+
+        // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
+        foreach (var parent in allSymbols) {
+          for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
+            foreach (var child in allSymbols) {
+              SetAllowedChild(parent, child, i);
+            }
+        }
+      }
+    }
+
+    public static ISymbolicExpressionGrammar CreateSimpleArithmeticGrammar() {
+      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
+      g.MaxFunctionArguments = 0;
+      g.MinFunctionArguments = 0;
+      g.MaxFunctionDefinitions = 0;
+      g.MinFunctionDefinitions = 0;
+      return g;
+    }
+
+    public static ISymbolicExpressionGrammar CreateArithmeticAndAdfGrammar() {
+      var g = new GlobalSymbolicExpressionGrammar(new SimpleArithmeticGrammar());
+      g.MaxFunctionArguments = 3;
+      g.MinFunctionArguments = 0;
+      g.MaxFunctionDefinitions = 3;
+      g.MinFunctionDefinitions = 0;
+      return g;
+    }
+  }
+}
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 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding-3.3.Tests.csproj	(revision 3338)
@@ -39,6 +39,11 @@
   </ItemGroup>
   <ItemGroup>
-    <Compile Include="SubroutineCreaterTest.cs" />
-    <Compile Include="SubtreeCrossoverTest.cs" />
+    <Compile Include="ArgumentCreaterTest.cs" />
+    <Compile Include="ArgumentDeleterTest.cs" />
+    <Compile Include="Grammars.cs" />
+    <Compile Include="SubtreeCrossoverTest.cs">
+      <SubType>Code</SubType>
+    </Compile>
+    <Compile Include="Util.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
     <Compile Include="ProbabilisticTreeCreaterTest.cs" />
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/ProbabilisticTreeCreaterTest.cs	(revision 3338)
@@ -11,16 +11,7 @@
   [TestClass]
   public class ProbabilisticTreeCreaterTest {
-    public ProbabilisticTreeCreaterTest() {
-      int populationSize = 1000;
-      randomTrees = new List<SymbolicExpressionTree>();
-      var grammar = new TestGrammar();
-      var random = new MersenneTwister();
-      for (int i = 0; i < populationSize; i++) {
-        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10));
-      }
-      foreach (var tree in randomTrees)
-        Assert.IsTrue(grammar.IsValidExpression(tree));
-    }
-
+    private const int POPULATION_SIZE = 1000;
+    private const int MAX_TREE_SIZE = 100;
+    private const int MAX_TREE_HEIGHT = 10;
     private TestContext testContextInstance;
 
@@ -38,126 +29,40 @@
     }
 
-    private List<SymbolicExpressionTree> randomTrees;
-
-
-    private class Addition : Symbol { }
-    private class Subtraction : Symbol { }
-    private class Multiplication : Symbol { }
-    private class Division : Symbol { }
-    private class Terminal : Symbol { }
-
-    private class TestGrammar : DefaultSymbolicExpressionGrammar {
-      public TestGrammar()
-        : base(0, 0, 0, 0) {
-        Initialize();
+    [TestMethod()]
+    public void ProbabilisticTreeCreaterDistributionsTest() {
+      var randomTrees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateSimpleArithmeticGrammar();
+      var random = new MersenneTwister();
+      for (int i = 0; i < POPULATION_SIZE; i++) {
+        randomTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, MAX_TREE_SIZE, MAX_TREE_HEIGHT, 0, 0));
       }
 
-      private void Initialize() {
-        var add = new Addition();
-        var sub = new Subtraction();
-        var mul = new Multiplication();
-        var div = new Division();
-        var terminal = new Terminal();
-
-        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
-        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
-        allSymbols.ForEach(s => AddAllowedSymbols(StartSymbol, 0, s));
-
-        SetMinSubTreeCount(terminal, 0);
-        SetMaxSubTreeCount(terminal, 0);
-        int maxSubTrees = 3;
-        foreach (var functionSymbol in functionSymbols) {
-          SetMinSubTreeCount(functionSymbol, 1);
-          SetMaxSubTreeCount(functionSymbol, maxSubTrees);
-          foreach (var childSymbol in allSymbols) {
-            for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
-              AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
-            }
-          }
-        }
-      }
-    }
-
-    [TestMethod()]
-    public void ProbabilisticTreeCreaterSizeDistributionTest() {
-      int[] histogram = new int[105 / 5];
-      for (int i = 0; i < randomTrees.Count; i++) {
-        histogram[randomTrees[i].Size / 5]++;
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      for (int i = 0; i < histogram.Length; i++) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
-        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)randomTrees.Count);
-      }
-      Assert.Inconclusive("Size distribution of ProbabilisticTreeCreator: " + strBuilder);
-    }
-
-    [TestMethod()]
-    public void ProbabilisticTreeCreaterFunctionDistributionTest() {
-      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
-      double n = 0.0;
-      for (int i = 0; i < randomTrees.Count; i++) {
-        foreach (var node in randomTrees[i].IterateNodesPrefix()) {
-          if (node.SubTrees.Count > 0) {
-            if (!occurances.ContainsKey(node.Symbol))
-              occurances[node.Symbol] = 0;
-            occurances[node.Symbol]++;
-            n++;
-          }
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var function in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(function.Name); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
-      }
-      Assert.Inconclusive("Function distribution of ProbabilisticTreeCreator: " + strBuilder);
-    }
-
-    [TestMethod()]
-    public void ProbabilisticTreeCreaterNumberOfSubTreesDistributionTest() {
-      Dictionary<int, int> occurances = new Dictionary<int, int>();
-      double n = 0.0;
-      for (int i = 0; i < randomTrees.Count; i++) {
-        foreach (var node in randomTrees[i].IterateNodesPrefix()) {
-          if (!occurances.ContainsKey(node.SubTrees.Count))
-            occurances[node.SubTrees.Count] = 0;
-          occurances[node.SubTrees.Count]++;
-          n++;
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var arity in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(arity); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
-      }
-      Assert.Inconclusive("Distribution of function arities of ProbabilisticTreeCreator: " + strBuilder);
+      foreach (var tree in randomTrees)
+        Assert.IsTrue(tree.IsValidExpression());
+      Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine +
+        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(randomTrees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
+        );
     }
 
 
     [TestMethod()]
-    public void ProbabilisticTreeCreaterTerminalDistributionTest() {
-      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
-      double n = 0.0;
-      for (int i = 0; i < randomTrees.Count; i++) {
-        foreach (var node in randomTrees[i].IterateNodesPrefix()) {
-          if (node.SubTrees.Count == 0) {
-            if (!occurances.ContainsKey(node.Symbol))
-              occurances[node.Symbol] = 0;
-            occurances[node.Symbol]++;
-            n++;
-          }
-        }
+    public void ProbabilisticTreeCreaterWithAdfDistributionsTest() {
+      var randomTrees = new List<SymbolicExpressionTree>();
+      var grammar = Grammars.CreateArithmeticAndAdfGrammar();
+      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));
       }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var function in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(function.Name); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
-      }
-      Assert.Inconclusive("Terminal distribution of ProbabilisticTreeCreator: " + strBuilder);
+      foreach (var tree in randomTrees)
+        Assert.IsTrue(tree.IsValidExpression());
+      Assert.Inconclusive("ProbabilisticTreeCreator: " + Environment.NewLine +
+        Util.GetSizeDistributionString(randomTrees, 105, 5) + Environment.NewLine +
+        Util.GetFunctionDistributionString(randomTrees) + Environment.NewLine +
+        Util.GetNumberOfSubTreesDistributionString(randomTrees) + Environment.NewLine +
+        Util.GetTerminalDistributionString(randomTrees) + Environment.NewLine
+        );
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubroutineCreaterTest.cs	(revision 3338)
@@ -13,6 +13,7 @@
   [TestClass]
   public class SubroutineCreaterTest {
-    public SubroutineCreaterTest() {
-    }
+    private static ISymbolicExpressionGrammar grammar;
+    private static List<SymbolicExpressionTree> subroutineTrees;
+    private static int failedEvents;
 
     private TestContext testContextInstance;
@@ -36,9 +37,10 @@
       subroutineTrees = new List<SymbolicExpressionTree>();
       int populationSize = 1000;
-      var grammar = new TestGrammar();
+      failedEvents = 0;
+      grammar = Grammars.CreateArithmeticAndAdfGrammar();
       var random = new MersenneTwister();
       for (int i = 0; i < populationSize; i++) {
         var randTree = ProbabilisticTreeCreator.Create(random, grammar, 100, 10);
-        Assert.IsTrue(grammar.IsValidExpression(randTree));
+        // PTC create is tested separately
         randomTrees.Add(randTree);
       }
@@ -47,5 +49,5 @@
         var par0 = (SymbolicExpressionTree)randomTrees[random.Next(populationSize)].Clone();
         bool success = SubroutineCreater.CreateSubroutine(random, par0, grammar, 100, 10, 3, 3);
-        Assert.IsTrue(grammar.IsValidExpression(par0));
+        if (!success) failedEvents++;
         subroutineTrees.Add(par0);
       }
@@ -53,113 +55,23 @@
 
 
-
-    private static List<SymbolicExpressionTree> subroutineTrees;
-    private static int failedEvents;
-
-    private class Addition : Symbol { }
-    private class Subtraction : Symbol { }
-    private class Multiplication : Symbol { }
-    private class Division : Symbol { }
-    private class Terminal : Symbol { }
-
-    private class TestGrammar : DefaultSymbolicExpressionGrammar {
-      public TestGrammar()
-        : base(0, 3, 0, 3) {
-        Initialize();
-      }
-
-      private void Initialize() {
-        var add = new Addition();
-        var sub = new Subtraction();
-        var mul = new Multiplication();
-        var div = new Division();
-        var terminal = new Terminal();
-
-        var defun = new Defun();
-        var invoke = new InvokeFunction();
-
-        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal, invoke };
-        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
-        foreach (var symb in allSymbols) {
-          AddAllowedSymbols(StartSymbol, 0, symb);
-          AddAllowedSymbols(defun, 0, symb);
-        }
-        SetMinSubTreeCount(invoke, 0);
-        SetMaxSubTreeCount(invoke, 3);
-        for (int i = 0; i < 3; i++) {
-          allSymbols.ForEach(s => AddAllowedSymbols(invoke, i, s));
-        }
-        SetMinSubTreeCount(terminal, 0);
-        SetMaxSubTreeCount(terminal, 0);
-        int maxSubTrees = 3;
-        foreach (var functionSymbol in functionSymbols) {
-          SetMinSubTreeCount(functionSymbol, 1);
-          SetMaxSubTreeCount(functionSymbol, maxSubTrees);
-          foreach (var childSymbol in allSymbols) {
-            for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
-              AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
-            }
-          }
-        }
-      }
+    [TestMethod()]
+    public void SubroutineCreaterCreateTest() {
+      foreach (var tree in subroutineTrees)
+        Assert.IsTrue(grammar.IsValidExpression(tree));
     }
 
     [TestMethod()]
     public void SubroutineCreaterSizeDistributionTest() {
-      int[] histogram = new int[105 / 5];
-      for (int i = 0; i < subroutineTrees.Count; i++) {
-        histogram[subroutineTrees[i].Size / 5]++;
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      for (int i = 0; i < histogram.Length; i++) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
-        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)subroutineTrees.Count);
-      }
-      Assert.Inconclusive("Size distribution of SubroutineCreater: " + strBuilder);
+      Assert.Inconclusive("SubroutineCreater: " + Util.GetSizeDistributionString(subroutineTrees, 105, 5));
     }
 
     [TestMethod()]
     public void SubroutineCreaterFunctionDistributionTest() {
-      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
-      double n = 0.0;
-      for (int i = 0; i < subroutineTrees.Count; i++) {
-        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
-          if (node.SubTrees.Count > 0) {
-            if (!occurances.ContainsKey(node.Symbol))
-              occurances[node.Symbol] = 0;
-            occurances[node.Symbol]++;
-            n++;
-          }
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var function in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(function.Name); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
-      }
-      Assert.Inconclusive("Function distribution of SubroutineCreater: " + strBuilder);
+      Assert.Inconclusive("SubroutineCreater: " + Util.GetFunctionDistributionString(subroutineTrees));
     }
 
     [TestMethod()]
     public void SubroutineCreaterNumberOfSubTreesDistributionTest() {
-      Dictionary<int, int> occurances = new Dictionary<int, int>();
-      double n = 0.0;
-      for (int i = 0; i < subroutineTrees.Count; i++) {
-        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
-          if (!occurances.ContainsKey(node.SubTrees.Count))
-            occurances[node.SubTrees.Count] = 0;
-          occurances[node.SubTrees.Count]++;
-          n++;
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var arity in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(arity); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
-      }
-      Assert.Inconclusive("Distribution of function arities of SubroutineCreater: " + strBuilder);
+      Assert.Inconclusive("SubroutineCreater: " + Util.GetNumberOfSubTreesDistributionString(subroutineTrees));
     }
 
@@ -167,23 +79,5 @@
     [TestMethod()]
     public void SubroutineCreaterTerminalDistributionTest() {
-      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
-      double n = 0.0;
-      for (int i = 0; i < subroutineTrees.Count; i++) {
-        foreach (var node in subroutineTrees[i].IterateNodesPrefix()) {
-          if (node.SubTrees.Count == 0) {
-            if (!occurances.ContainsKey(node.Symbol))
-              occurances[node.Symbol] = 0;
-            occurances[node.Symbol]++;
-            n++;
-          }
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var function in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(function.Name); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
-      }
-      Assert.Inconclusive("Terminal distribution of SubroutineCreater: " + strBuilder);
+      Assert.Inconclusive("SubroutineCreater: " + Util.GetTerminalDistributionString(subroutineTrees));
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/SubtreeCrossoverTest.cs	(revision 3338)
@@ -11,6 +11,7 @@
   [TestClass]
   public class SubtreeCrossoverTest {
-    public SubtreeCrossoverTest() {
-    }
+    private static ISymbolicExpressionGrammar grammar;
+    private static List<SymbolicExpressionTree> crossoverTrees;
+    private static double msPerCrossoverEvent;
 
     private TestContext testContextInstance;
@@ -34,8 +35,9 @@
       int populationSize = 1000;
       int generations = 5;
-      var grammar = new TestGrammar();
+      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));
+        crossoverTrees.Add(ProbabilisticTreeCreator.Create(random, grammar, 100, 10, 3, 3));
       }
       Stopwatch stopwatch = new Stopwatch();
@@ -47,5 +49,6 @@
           var par1 = (SymbolicExpressionTree)crossoverTrees[random.Next(populationSize)].Clone();
           bool success;
-          newPopulation.Add(SubtreeCrossover.Cross(random, grammar, par0, par1, 0.9, 100, 10, out success));
+          newPopulation.Add(SubtreeCrossover.Cross(random, par0, par1, 0.9, 100, 10, out success));
+          if (!success) failedEvents++;
         }
         crossoverTrees = newPopulation;
@@ -53,53 +56,13 @@
       stopwatch.Stop();
       foreach (var tree in crossoverTrees)
-        Assert.IsTrue(grammar.IsValidExpression(tree));
-      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;
+        Assert.IsTrue(tree.IsValidExpression());
+      msPerCrossoverEvent = stopwatch.ElapsedMilliseconds / (double)populationSize / (double)generations;      
     }
 
 
 
-    private static List<SymbolicExpressionTree> crossoverTrees;
-    private static double msPerCrossoverEvent;
-
-    private class Addition : Symbol { }
-    private class Subtraction : Symbol { }
-    private class Multiplication : Symbol { }
-    private class Division : Symbol { }
-    private class Terminal : Symbol { }
-
-    private class TestGrammar : DefaultSymbolicExpressionGrammar {
-      public TestGrammar()
-        : base(0, 0, 0, 0) {
-        Initialize();
-      }
-
-      private void Initialize() {
-        var add = new Addition();
-        var sub = new Subtraction();
-        var mul = new Multiplication();
-        var div = new Division();
-        var terminal = new Terminal();
-
-        var allSymbols = new List<Symbol>() { add, sub, mul, div, terminal };
-        var functionSymbols = new List<Symbol>() { add, sub, mul, div };
-        allSymbols.ForEach(s => AddAllowedSymbols(StartSymbol, 0, s));
-
-        SetMinSubTreeCount(terminal, 0);
-        SetMaxSubTreeCount(terminal, 0);
-        int maxSubTrees = 3;
-        foreach (var functionSymbol in functionSymbols) {
-          SetMinSubTreeCount(functionSymbol, 1);
-          SetMaxSubTreeCount(functionSymbol, maxSubTrees);
-          foreach (var childSymbol in allSymbols) {
-            for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
-              AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
-            }
-          }
-        }
-      }
-    }
-
     [TestMethod()]
     public void SubtreeCrossoverSpeed() {
+
       Assert.Inconclusive(msPerCrossoverEvent + " ms per crossover event (~" +
         Math.Round(1000.0 / (msPerCrossoverEvent)) + "crossovers / s)");
@@ -107,60 +70,16 @@
 
     [TestMethod()]
-    public void SubtreeCrossoverSizeDistributionTest() {
-      int[] histogram = new int[105 / 5];
-      for (int i = 0; i < crossoverTrees.Count; i++) {
-        histogram[crossoverTrees[i].Size / 5]++;
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      for (int i = 0; i < histogram.Length; i++) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append("< "); strBuilder.Append((i + 1) * 5);
-        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)crossoverTrees.Count);
-      }
-      Assert.Inconclusive("Size distribution of SubtreeCrossover: " + strBuilder);
+    public void SubtreeCrossoverSizeDistributions() {
+      Assert.Inconclusive("SubtreeCrossover: " + Util.GetSizeDistributionString(crossoverTrees, 105, 5));
     }
 
     [TestMethod()]
     public void SubtreeCrossoverFunctionDistributionTest() {
-      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
-      double n = 0.0;
-      for (int i = 0; i < crossoverTrees.Count; i++) {
-        foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
-          if (node.SubTrees.Count > 0) {
-            if (!occurances.ContainsKey(node.Symbol))
-              occurances[node.Symbol] = 0;
-            occurances[node.Symbol]++;
-            n++;
-          }
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var function in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(function.Name); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
-      }
-      Assert.Inconclusive("Function distribution of SubtreeCrossover: " + strBuilder);
+      Assert.Inconclusive("SubtreeCrossover: " + Util.GetFunctionDistributionString(crossoverTrees));
     }
 
     [TestMethod()]
     public void SubtreeCrossoverNumberOfSubTreesDistributionTest() {
-      Dictionary<int, int> occurances = new Dictionary<int, int>();
-      double n = 0.0;
-      for (int i = 0; i < crossoverTrees.Count; i++) {
-        foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
-          if (!occurances.ContainsKey(node.SubTrees.Count))
-            occurances[node.SubTrees.Count] = 0;
-          occurances[node.SubTrees.Count]++;
-          n++;
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var arity in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(arity); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
-      }
-      Assert.Inconclusive("Distribution of function arities of SubtreeCrossover: " + strBuilder);
+      Assert.Inconclusive("SubtreeCrossover: " + Util.GetNumberOfSubTreesDistributionString(crossoverTrees));
     }
 
@@ -168,23 +87,5 @@
     [TestMethod()]
     public void SubtreeCrossoverTerminalDistributionTest() {
-      Dictionary<Symbol, int> occurances = new Dictionary<Symbol, int>();
-      double n = 0.0;
-      for (int i = 0; i < crossoverTrees.Count; i++) {
-        foreach (var node in crossoverTrees[i].IterateNodesPrefix()) {
-          if (node.SubTrees.Count == 0) {
-            if (!occurances.ContainsKey(node.Symbol))
-              occurances[node.Symbol] = 0;
-            occurances[node.Symbol]++;
-            n++;
-          }
-        }
-      }
-      StringBuilder strBuilder = new StringBuilder();
-      foreach (var function in occurances.Keys) {
-        strBuilder.Append(Environment.NewLine);
-        strBuilder.Append(function.Name); strBuilder.Append(": ");
-        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
-      }
-      Assert.Inconclusive("Terminal distribution of SubtreeCrossover: " + strBuilder);
+      Assert.Inconclusive("SubtreeCrossover: " + Util.GetTerminalDistributionString(crossoverTrees));
     }
   }
Index: /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs
===================================================================
--- /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs	(revision 3338)
+++ /trunk/sources/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.3/Tests/Util.cs	(revision 3338)
@@ -0,0 +1,91 @@
+﻿using System;
+using System.Text;
+using System.Collections.Generic;
+using System.Linq;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Random;
+using System.Diagnostics;
+
+namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding_3._3.Tests {
+  public static class Util {
+    public static string GetSizeDistributionString(IList<SymbolicExpressionTree> trees, int maxTreeSize, int binSize) {
+      int[] histogram = new int[maxTreeSize / binSize];
+      for (int i = 0; i < trees.Count; i++) {
+        histogram[trees[i].Size / binSize]++;
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      for (int i = 0; i < histogram.Length; i++) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append("< "); strBuilder.Append((i + 1) * binSize);
+        strBuilder.Append(": "); strBuilder.AppendFormat("{0:#0.00%}", histogram[i] / (double)trees.Count);
+      }
+      return "Size distribution: " + strBuilder;
+    }
+
+    public static string GetFunctionDistributionString(IList<SymbolicExpressionTree> trees) {
+      Dictionary<string, int> occurances = new Dictionary<string, int>();
+      double n = 0.0;
+      for (int i = 0; i < trees.Count; i++) {
+        foreach (var node in trees[i].IterateNodesPrefix()) {
+          if (node.SubTrees.Count > 0) {
+            if (!occurances.ContainsKey(node.Symbol.Name))
+              occurances[node.Symbol.Name] = 0;
+            occurances[node.Symbol.Name]++;
+            n++;
+          }
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var function in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(function); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
+      }
+      return "Function distribution: " + strBuilder;
+    }
+
+    public static string GetNumberOfSubTreesDistributionString(IList<SymbolicExpressionTree> trees) {
+      Dictionary<int, int> occurances = new Dictionary<int, int>();
+      double n = 0.0;
+      for (int i = 0; i < trees.Count; i++) {
+        foreach (var node in trees[i].IterateNodesPrefix()) {
+          if (!occurances.ContainsKey(node.SubTrees.Count))
+            occurances[node.SubTrees.Count] = 0;
+          occurances[node.SubTrees.Count]++;
+          n++;
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var arity in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(arity); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[arity] / n);
+      }
+      return "Distribution of function arities: " + strBuilder;
+    }
+
+
+    public static string GetTerminalDistributionString(IList<SymbolicExpressionTree> trees) {
+      Dictionary<string, int> occurances = new Dictionary<string, int>();
+      double n = 0.0;
+      for (int i = 0; i < trees.Count; i++) {
+        foreach (var node in trees[i].IterateNodesPrefix()) {
+          if (node.SubTrees.Count == 0) {
+            if (!occurances.ContainsKey(node.Symbol.Name))
+              occurances[node.Symbol.Name] = 0;
+            occurances[node.Symbol.Name]++;
+            n++;
+          }
+        }
+      }
+      StringBuilder strBuilder = new StringBuilder();
+      foreach (var function in occurances.Keys) {
+        strBuilder.Append(Environment.NewLine);
+        strBuilder.Append(function); strBuilder.Append(": ");
+        strBuilder.AppendFormat("{0:#0.00%}", occurances[function] / n);
+      }
+      return "Terminal distribution: " + strBuilder;
+    }
+  }
+}
Index: /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/AntInterpreter.cs	(revision 3338)
@@ -29,4 +29,5 @@
 namespace HeuristicLab.Problems.ArtificialAnt {
   public class AntInterpreter {
+
     public int MaxTimeSteps { get; set; }
     public int FoodEaten { get; set; }
@@ -48,4 +49,14 @@
       }
     }
+
+    private SymbolicExpressionTreeNode FindMatchingFunction(string name) {
+      foreach (var defunBranch in expression.Root.SubTrees.OfType<DefunTreeNode>()) {
+        if (defunBranch.FunctionName == name) return defunBranch;
+      }
+      throw new ArgumentException("Function definition for " + name + " not found.");
+    }
+
+
+
     public int ElapsedTime { get; set; }
     private int currentDirection;
@@ -81,6 +92,8 @@
     public void Step() {
       // expression evaluated completly => start at root again
-      if (nodeStack.Count == 0)
+      if (nodeStack.Count == 0) {
         nodeStack.Push(Expression.ResultProducingExpression);
+      }
+
       var currentNode = nodeStack.Pop();
       if (currentNode.Symbol is Left) {
@@ -116,15 +129,15 @@
       } else if (currentNode.Symbol is InvokeFunction) {
         var invokeNode = currentNode as InvokeFunctionTreeNode;
-        var funBranch = (from node in expression.Root.SubTrees
-                         let funNode = node as DefunTreeNode
-                         where funNode != null
-                         where funNode.Name == invokeNode.InvokedFunctionName
-                         select funNode).FirstOrDefault();
-        if (funBranch == null) throw new InvalidOperationException("Can't find definition of function " + invokeNode.InvokedFunctionName);
-        nodeStack.Push(funBranch.SubTrees[0]);
-        foreach (var subTree in invokeNode.SubTrees)
-          nodeStack.Push(subTree);
-      } else if(currentNode.Symbol is Argument) {
-        // do nothing
+        var functionDefinition = (SymbolicExpressionTreeNode)FindMatchingFunction(invokeNode.InvokedFunctionName).Clone();
+        var argumentCutPoints = (from node in functionDefinition.IterateNodesPrefix()
+                                 where node.SubTrees.Count > 0
+                                 from subtree in node.SubTrees
+                                 where subtree is ArgumentTreeNode
+                                 select new { Parent = node, Argument = subtree.Symbol as Argument, ChildIndex = node.SubTrees.IndexOf(subtree) }).ToList();
+        foreach (var cutPoint in argumentCutPoints) {
+          cutPoint.Parent.RemoveSubTree(cutPoint.ChildIndex);
+          cutPoint.Parent.InsertSubTree(cutPoint.ChildIndex, (SymbolicExpressionTreeNode)invokeNode.SubTrees[cutPoint.Argument.ArgumentIndex].Clone());
+        }
+        nodeStack.Push(functionDefinition.SubTrees[0]);
       } else {
         throw new InvalidOperationException(currentNode.Symbol.ToString());
Index: /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntExpressionGrammar.cs	(revision 3338)
@@ -33,5 +33,5 @@
 
     public ArtificialAntExpressionGrammar()
-      : base(0, 3, 0, 3) {
+      : base() {
       Initialize();
     }
@@ -45,40 +45,32 @@
       var right = new Right();
       var defun = new Defun();
-      var invoke = new InvokeFunction();
       var allSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3, move, left, right };
       var nonTerminalSymbols = new List<Symbol>() { ifFoodAhead, prog2, prog3 };
-      SetMinSubTreeCount(ifFoodAhead, 2);
-      SetMaxSubTreeCount(ifFoodAhead, 2);
-      SetMinSubTreeCount(prog2, 2);
-      SetMaxSubTreeCount(prog2, 2);
-      SetMinSubTreeCount(prog3, 3);
-      SetMaxSubTreeCount(prog3, 3);
-      SetMinSubTreeCount(move, 0);
-      SetMaxSubTreeCount(move, 0);
-      SetMinSubTreeCount(left, 0);
-      SetMaxSubTreeCount(left, 0);
-      SetMinSubTreeCount(right, 0);
-      SetMaxSubTreeCount(right, 0);
-      foreach (var sym in allSymbols) {
-        AddAllowedSymbols(StartSymbol, 0, sym);
-        AddAllowedSymbols(defun, 0, sym);
 
-        for (int i = 0; i < GetMaxSubTreeCount(invoke); i++) {
-          AddAllowedSymbols(invoke, i, sym);
-        }
-      }
-      foreach (var sym in nonTerminalSymbols) {
-        for (int argIndex = 0; argIndex < GetMaxSubTreeCount(sym); argIndex++) {
-          AddAllowedSymbols(sym, argIndex, invoke);
-        }
-      }
-      foreach (var nonTerminal in nonTerminalSymbols) {
-        foreach (var child in allSymbols) {
-          for (int argIndex = 0; argIndex < GetMaxSubTreeCount(nonTerminal); argIndex++) {
-            AddAllowedSymbols(nonTerminal, argIndex, child);
+      allSymbols.ForEach(s => AddSymbol(s));
+      SetMinSubtreeCount(ifFoodAhead, 2);
+      SetMaxSubtreeCount(ifFoodAhead, 2);
+      SetMinSubtreeCount(prog2, 2);
+      SetMaxSubtreeCount(prog2, 2);
+      SetMinSubtreeCount(prog3, 3);
+      SetMaxSubtreeCount(prog3, 3);
+      SetMinSubtreeCount(move, 0);
+      SetMaxSubtreeCount(move, 0);
+      SetMinSubtreeCount(left, 0);
+      SetMaxSubtreeCount(left, 0);
+      SetMinSubtreeCount(right, 0);
+      SetMaxSubtreeCount(right, 0);
+
+      // each symbols is allowed as child of the start symbol
+      allSymbols.ForEach(s => SetAllowedChild(StartSymbol, s, 0));
+
+      // each symbol is allowed as child of all other symbols (except for terminals that have MaxSubtreeCount == 0
+      foreach (var parent in allSymbols) {
+        for (int argIndex = 0; argIndex < GetMaxSubtreeCount(parent); argIndex++) {
+          foreach (var child in allSymbols) {
+            SetAllowedChild(parent, child, argIndex);
           }
         }
       }
-
     }
   }
Index: /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Problems.ArtificialAnt/3.3/ArtificialAntProblem.cs	(revision 3338)
@@ -151,4 +151,12 @@
       set { MaxExpressionDepthParameter.Value = value; }
     }
+    public IntValue MaxFunctionDefinitions {
+      get { return MaxFunctionDefinitionsParameter.Value; }
+      set { MaxFunctionDefinitionsParameter.Value = value; }
+    }
+    public IntValue MaxFunctionArguments {
+      get { return MaxFunctionArgumentsParameter.Value; }
+      set { MaxFunctionArgumentsParameter.Value = value; }
+    }
     public SymbolicExpressionTreeCreator SolutionCreator {
       get { return SolutionCreatorParameter.Value; }
@@ -168,6 +176,6 @@
       get { return EvaluatorParameter.Value; }
     }
-    public ISymbolicExpressionGrammar ArtificialAntExpressionGrammar {
-      get { return ArtificialAntExpressionGrammarParameter.Value; }
+    public GlobalSymbolicExpressionGrammar ArtificialAntExpressionGrammar {
+      get { return (GlobalSymbolicExpressionGrammar)ArtificialAntExpressionGrammarParameter.Value; }
     }
     public ISingleObjectiveSolutionsVisualizer Visualizer {
@@ -191,16 +199,16 @@
       SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
       Evaluator evaluator = new Evaluator();
-      ArtificialAntExpressionGrammar grammar = new ArtificialAntExpressionGrammar();
       BestAntTrailVisualizer visualizer = new BestAntTrailVisualizer();
       BoolMatrix world = new BoolMatrix(santaFeAntTrail);
+      ISymbolicExpressionGrammar grammar = new GlobalSymbolicExpressionGrammar(new ArtificialAntExpressionGrammar());
       Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to true as the Artificial Ant Problem is a maximization problem.", new BoolValue(true)));
       Parameters.Add(new ValueParameter<SymbolicExpressionTreeCreator>("SolutionCreator", "The operator which should be used to create new artificial ant solutions.", creator));
       Parameters.Add(new ValueParameter<Evaluator>("Evaluator", "The operator which should be used to evaluate artificial ant solutions.", evaluator));
       Parameters.Add(new ValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this artificial ant instance.", new DoubleValue(89)));
-      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
       Parameters.Add(new ValueParameter<IntValue>("MaxExpressionLength", "Maximal length of the expression to control the artificial ant.", new IntValue(100)));
       Parameters.Add(new ValueParameter<IntValue>("MaxExpressionDepth", "Maximal depth of the expression to control the artificial ant.", new IntValue(10)));
       Parameters.Add(new ValueParameter<IntValue>("MaxFunctionDefinitions", "Maximal number of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
       Parameters.Add(new ValueParameter<IntValue>("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions in the expression to control the artificial ant.", new IntValue(3)));
+      Parameters.Add(new ValueParameter<ISymbolicExpressionGrammar>("ArtificialAntExpressionGrammar", "The grammar that should be used for artificial ant expressions.", grammar));
       Parameters.Add(new ValueParameter<BoolMatrix>("World", "The world for the artificial ant with scattered food items.", world));
       Parameters.Add(new ValueParameter<IntValue>("MaxTimeSteps", "The number of time steps the artificial ant has available to collect all food items.", new IntValue(600)));
@@ -292,4 +300,16 @@
       Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
       VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
+      MaxFunctionArgumentsParameter.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
+      MaxFunctionArguments.ValueChanged += new EventHandler(MaxFunctionArgumentsParameter_ValueChanged);
+      MaxFunctionDefinitionsParameter.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
+      MaxFunctionDefinitions.ValueChanged += new EventHandler(MaxFunctionDefinitionsParameter_ValueChanged);
+    }
+
+    void MaxFunctionDefinitionsParameter_ValueChanged(object sender, EventArgs e) {
+      ArtificialAntExpressionGrammar.MaxFunctionDefinitions = MaxFunctionDefinitions.Value;
+    }
+
+    void MaxFunctionArgumentsParameter_ValueChanged(object sender, EventArgs e) {
+      ArtificialAntExpressionGrammar.MaxFunctionArguments = MaxFunctionArguments.Value;
     }
 
@@ -348,4 +368,8 @@
         op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
       }
+      foreach (SymbolicExpressionTreeCreator op in Operators.OfType<SymbolicExpressionTreeCreator>()) {
+        op.MaxFunctionArgumentsParameter.ActualName = MaxFunctionArgumentsParameter.Name;
+        op.MaxFunctionDefinitionsParameter.ActualName = MaxFunctionDefinitionsParameter.Name;
+      }
     }
 
Index: /trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/ArithmeticExpressionGrammar.cs
===================================================================
--- /trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/ArithmeticExpressionGrammar.cs	(revision 3337)
+++ /trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/Symbolic/ArithmeticExpressionGrammar.cs	(revision 3338)
@@ -48,5 +48,5 @@
 
     public ArithmeticExpressionGrammar()
-      : base(0, 0, 0, 0) {
+      : base() {
       Initialize();
     }
@@ -62,20 +62,27 @@
       var allSymbols = new List<Symbol>() { add, sub, mul, div, constant, variableSymbol };
       var functionSymbols = new List<Symbol>() { add, sub, mul, div };
-      allSymbols.ForEach(s => AddAllowedSymbols(StartSymbol, 0, s));
+      foreach (var symb in allSymbols)
+        AddSymbol(symb);
 
+      foreach (var funSymb in functionSymbols) {
+        SetMinSubtreeCount(funSymb, 1);
+        SetMaxSubtreeCount(funSymb, 3);
+      }
+      SetMinSubtreeCount(constant, 0);
+      SetMaxSubtreeCount(constant, 0);
+      SetMinSubtreeCount(variableSymbol, 0);
+      SetMaxSubtreeCount(variableSymbol, 0);
 
-      SetMinSubTreeCount(constant, 0);
-      SetMaxSubTreeCount(constant, 0);
-      SetMinSubTreeCount(variableSymbol, 0);
-      SetMaxSubTreeCount(variableSymbol, 0);
-      int maxSubTrees = 3;
-      foreach (var functionSymbol in functionSymbols) {
-        SetMinSubTreeCount(functionSymbol, 1);
-        SetMaxSubTreeCount(functionSymbol, maxSubTrees);
-        foreach (var childSymbol in allSymbols) {
-          for (int argumentIndex = 0; argumentIndex < maxSubTrees; argumentIndex++) {
-            AddAllowedSymbols(functionSymbol, argumentIndex, childSymbol);
+      // allow each symbol as child of the start symbol
+      foreach (var symb in allSymbols) {
+        SetAllowedChild(StartSymbol, symb, 0);
+      }
+
+      // allow each symbol as child of every other symbol (except for terminals that have maxSubtreeCount == 0)
+      foreach (var parent in allSymbols) {
+        for (int i = 0; i < GetMaxSubtreeCount(parent); i++)
+          foreach (var child in allSymbols) {
+            SetAllowedChild(parent, child, i);
           }
-        }
       }
     }
