Index: trunk/sources/HeuristicLab.GP/Recombination/GPCrossoverBase.cs
===================================================================
--- trunk/sources/HeuristicLab.GP/Recombination/GPCrossoverBase.cs	(revision 831)
+++ trunk/sources/HeuristicLab.GP/Recombination/GPCrossoverBase.cs	(revision 832)
@@ -64,5 +64,5 @@
         }
 
-        IFunctionTree child = Cross(gardener, random, parent0, parent1);
+        IFunctionTree child = Cross(scope, gardener, random, parent0, parent1);
         Debug.Assert(gardener.IsValidTree(child));
         IScope childScope = new Scope(i.ToString());
@@ -76,5 +76,5 @@
     }
 
-    internal abstract IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1);
+    internal abstract IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1);
 
     private IFunctionTree TakeNextParent(IScope scope) {
Index: trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs
===================================================================
--- trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs	(revision 831)
+++ trunk/sources/HeuristicLab.GP/Recombination/OnePointCrossOver.cs	(revision 832)
@@ -49,5 +49,5 @@
     }
 
-    internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
+    internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
       List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
       GetCrossOverPoints(gardener, tree0, tree1, allowedCrossOverPoints);
Index: trunk/sources/HeuristicLab.GP/Recombination/StandardCrossOver.cs
===================================================================
--- trunk/sources/HeuristicLab.GP/Recombination/StandardCrossOver.cs	(revision 831)
+++ trunk/sources/HeuristicLab.GP/Recombination/StandardCrossOver.cs	(revision 832)
@@ -32,6 +32,7 @@
 
 namespace HeuristicLab.GP {
-  public class StandardCrossOver : OperatorBase {
-    private const int MAX_RECOMBINATION_TRIES = 20;
+  public class StandardCrossOver : GPCrossoverBase {
+    private const int MAX_RECOMBINATION_TRIES = 100;
+
     public override string Description {
       get {
@@ -44,215 +45,63 @@
     public StandardCrossOver()
       : base() {
-      AddVariableInfo(new VariableInfo("Random", "Pseudo random number generator", typeof(MersenneTwister), VariableKind.In));
-      AddVariableInfo(new VariableInfo("OperatorLibrary", "The operator library containing all available operators", typeof(GPOperatorLibrary), VariableKind.In));
       AddVariableInfo(new VariableInfo("MaxTreeHeight", "The maximal allowed height of the tree", typeof(IntData), VariableKind.In));
       AddVariableInfo(new VariableInfo("MaxTreeSize", "The maximal allowed size (number of nodes) of the tree", typeof(IntData), VariableKind.In));
-      AddVariableInfo(new VariableInfo("FunctionTree", "The tree to mutate", typeof(IFunctionTree), VariableKind.In | VariableKind.New));
-      AddVariableInfo(new VariableInfo("TreeSize", "The size (number of nodes) of the tree", typeof(IntData), VariableKind.New));
-      AddVariableInfo(new VariableInfo("TreeHeight", "The height of the tree", typeof(IntData), VariableKind.New));
     }
 
-    public override IOperation Apply(IScope scope) {
-      MersenneTwister random = GetVariableValue<MersenneTwister>("Random", scope, true);
-      GPOperatorLibrary opLibrary = GetVariableValue<GPOperatorLibrary>("OperatorLibrary", scope, true);
+    internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
       int maxTreeHeight = GetVariableValue<IntData>("MaxTreeHeight", scope, true).Data;
       int maxTreeSize = GetVariableValue<IntData>("MaxTreeSize", scope, true).Data;
 
-      TreeGardener gardener = new TreeGardener(random, opLibrary);
-
-      if((scope.SubScopes.Count % 2) != 0)
-        throw new InvalidOperationException("Number of parents is not even");
-
-      CompositeOperation initOperations = new CompositeOperation();
-
-      int children = scope.SubScopes.Count / 2;
-      for(int i = 0; i < children; i++) {
-        IScope parent1 = scope.SubScopes[0];
-        scope.RemoveSubScope(parent1);
-        IScope parent2 = scope.SubScopes[0];
-        scope.RemoveSubScope(parent2);
-        IScope child = new Scope(i.ToString());
-        IOperation childInitOperation = Cross(gardener, maxTreeSize, maxTreeHeight, scope, random, parent1, parent2, child);
-        initOperations.AddOperation(childInitOperation);
-        scope.AddSubScope(child);
-      }
-
-      return initOperations;
-    }
-
-    private IOperation Cross(TreeGardener gardener, int maxTreeSize, int maxTreeHeight,
-      IScope scope, MersenneTwister random, IScope parent1, IScope parent2, IScope child) {
-      List<IFunctionTree> newBranches;
-      IFunctionTree newTree = Cross(gardener, parent1, parent2,
-        random, maxTreeSize, maxTreeHeight, out newBranches);
-
-
-      int newTreeSize = newTree.Size;
-      int newTreeHeight = newTree.Height;
-      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("FunctionTree"), newTree));
-      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeSize"), new IntData(newTreeSize)));
-      child.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("TreeHeight"), new IntData(newTreeHeight)));
+      // when tree0 is terminal then try to cross into tree1, when tree1 is also terminal just return tree0 unchanged.
+      IFunctionTree newTree;
+      if(tree0.SubTrees.Count > 0) {
+        newTree = Cross(gardener, tree0, tree1, random, maxTreeSize, maxTreeHeight);
+      } else if(tree1.SubTrees.Count > 0) {
+        newTree = Cross(gardener, tree1, tree0, random, maxTreeSize, maxTreeHeight);
+      } else newTree = tree0;
 
       // check if the new tree is valid and if the height of is still in the allowed bounds (we are not so strict for the max-size)
-      Debug.Assert(gardener.IsValidTree(newTree) && newTreeHeight <= maxTreeHeight && newTreeSize <= maxTreeSize);
-      return gardener.CreateInitializationOperation(newBranches, child);
+      Debug.Assert(gardener.IsValidTree(newTree) && newTree.Height <= maxTreeHeight && newTree.Size <= maxTreeSize);
+      return newTree;
     }
 
 
-    private IFunctionTree Cross(TreeGardener gardener, IScope f, IScope g, MersenneTwister random, int maxTreeSize, int maxTreeHeight, out List<IFunctionTree> newBranches) {
-      IFunctionTree tree0 = f.GetVariableValue<IFunctionTree>("FunctionTree", false);
-      int tree0Height = f.GetVariableValue<IntData>("TreeHeight", false).Data;
-      int tree0Size = f.GetVariableValue<IntData>("TreeSize", false).Data;
+    private IFunctionTree Cross(TreeGardener gardener, IFunctionTree tree0, IFunctionTree tree1, MersenneTwister random, int maxTreeSize, int maxTreeHeight) {
+      int tries = 0;
+      List<IFunctionTree> allowedCrossoverPoints = null;
+      IFunctionTree parent0;
+      int replacedChildIndex;
+      do {
+        // select a random crossover point in the first parent tree0
+        parent0 = null;
+        while(parent0 == null) parent0 = gardener.GetRandomParentNode(tree0);
+        // select a random branch to replace
+        replacedChildIndex = random.Next(parent0.SubTrees.Count);
 
-      IFunctionTree tree1 = g.GetVariableValue<IFunctionTree>("FunctionTree", false);
-      int tree1Height = g.GetVariableValue<IntData>("TreeHeight", false).Data;
-      int tree1Size = g.GetVariableValue<IntData>("TreeSize", false).Data;
+        // calculate the max size and height that the inserted branch can have 
+        int maxInsertedBranchSize = maxTreeSize - (tree0.Size - parent0.SubTrees[replacedChildIndex].Size);
+        int maxInsertedBranchHeight = maxTreeHeight - gardener.GetBranchLevel(tree0, parent0); // branchlevel is 1 if tree0==parent0
 
-      if(tree0Size == 1 && tree1Size == 1) {
-        return CombineTerminals(gardener, tree0, tree1, random, maxTreeHeight, out newBranches);
-      } else {
-        newBranches = new List<IFunctionTree>();
+        IList<IFunction> allowedFunctions = gardener.GetAllowedSubFunctions(parent0.Function, replacedChildIndex);
+        allowedCrossoverPoints = GetPossibleCrossoverPoints(gardener, tree1, maxInsertedBranchSize, maxInsertedBranchHeight, allowedFunctions);
+      } while(allowedCrossoverPoints.Count == 0 || tries++ > MAX_RECOMBINATION_TRIES);
 
-        // we are going to insert tree1 into tree0 at a random place so we have to make sure that tree0 is not a terminal
-        // in case both trees are higher than 1 we swap the trees with probability 50%
-        if(tree0Height == 1 || (tree1Height > 1 && random.Next(2) == 0)) {
-          IFunctionTree tmp = tree0; tree0 = tree1; tree1 = tmp;
-          int tmpHeight = tree0Height; tree0Height = tree1Height; tree1Height = tmpHeight;
-          int tmpSize = tree0Size; tree0Size = tree1Size; tree1Size = tmpSize;
-        }
+      if(allowedCrossoverPoints.Count > 0) {
+        IFunctionTree branch1 = allowedCrossoverPoints[random.Next(allowedCrossoverPoints.Count)];
 
-        // save the roots because later on we change tree0 and tree1 while searching a valid tree configuration
-        IFunctionTree root0 = tree0;
-        IFunctionTree root1 = tree1;
-        int root0Height = tree0Height;
-        int root1Height = tree1Height;
-        int rootSize = tree0Size;
-
-        // select a random suboperators of the two trees at a random level
-        int tree0Level = random.Next(root0Height - 1); // since we checked before that the height of tree0 is > 1 this is OK
-        int tree1Level = random.Next(root1Height);
-        tree0 = gardener.GetRandomBranch(tree0, tree0Level);
-        tree1 = gardener.GetRandomBranch(tree1, tree1Level);
-
-        // recalculate the size and height of tree1 (the one that we want to insert) because we need to check constraints later on
-        tree1Size = tree1.Size;
-        tree1Height = tree1.Height;
-
-        List<int> possibleChildIndices = new List<int>();
-
-        // Now tree0 is supposed to take tree1 as one if its children. If this is not possible,
-        // then go down in either of the two trees as far as possible. If even then it is not possible
-        // to merge the trees then throw an exception
-        // find the list of allowed indices (regarding allowed sub-trees, maxTreeSize and maxTreeHeight)
-        for(int i = 0; i < tree0.SubTrees.Count; i++) {
-          int subTreeSize = tree0.SubTrees[i].Size;
-
-          // the index is ok when the function is allowed as sub-tree and we don't violate the maxSize and maxHeight constraints
-          if(gardener.GetAllowedSubFunctions(tree0.Function, i).Contains(tree1.Function) &&
-            rootSize - subTreeSize + tree1Size < maxTreeSize &&
-            tree0Level + tree1Height < maxTreeHeight) {
-            possibleChildIndices.Add(i);
-          }
-        }
-        int tries = 0;
-        while(possibleChildIndices.Count == 0) {
-          if(tries++ > MAX_RECOMBINATION_TRIES) {
-            if(random.Next() > 0.5) return root1;
-            else return root0;
-          }
-          // we couln't find a possible configuration given the current tree0 and tree1
-          // possible reasons for this are: 
-          //  - tree1 is not allowed as sub-tree of tree0
-          //  - appending tree1 as child of tree0 would create a tree that exceedes the maxTreeHeight
-          //  - replacing any child of tree0 with tree1 woulde create a tree that exceedes the maxTeeSize
-          // thus we just try until we find a valid configuration
-
-          tree0Level = random.Next(root0Height - 1);
-          tree1Level = random.Next(root1Height);
-          tree0 = gardener.GetRandomBranch(root0, tree0Level);
-          tree1 = gardener.GetRandomBranch(root1, tree1Level);
-
-          // recalculate the size and height of tree1 (the one that we want to insert) because we need to check constraints later on
-          tree1Size = tree1.Size;
-          tree1Height = tree1.Height;
-          // recalculate the list of possible indices 
-          possibleChildIndices.Clear();
-          for(int i = 0; i < tree0.SubTrees.Count; i++) {
-            int subTreeSize = tree0.SubTrees[i].Size;
-
-            // when the function is allowed as sub-tree and we don't violate the maxSize and maxHeight constraints
-            // the index is ok
-            if(gardener.GetAllowedSubFunctions(tree0.Function, i).Contains(tree1.Function) &&
-              rootSize - subTreeSize + tree1Size < maxTreeSize &&
-              tree0Level + tree1Height < maxTreeHeight) {
-              possibleChildIndices.Add(i);
-            }
-          }
-        }
-        // replace the existing sub-tree at a random index in tree0 with tree1
-        int selectedIndex = possibleChildIndices[random.Next(possibleChildIndices.Count)];
-        tree0.RemoveSubTree(selectedIndex);
-        tree0.InsertSubTree(selectedIndex, tree1);
-        return root0;
+        // replace the branch in tree0 with the selected branch from tree1
+        parent0.RemoveSubTree(replacedChildIndex);
+        parent0.InsertSubTree(replacedChildIndex, branch1);
       }
+      return tree0;
     }
 
-
-    // take f and g and create a tree that has f and g as sub-trees 
-    // example
-    //       O
-    //      /|\
-    //     g 2 f 
-    //
-    private IFunctionTree CombineTerminals(TreeGardener gardener, IFunctionTree f, IFunctionTree g, MersenneTwister random, int maxTreeHeight, out List<IFunctionTree> newBranches) {
-      newBranches = new List<IFunctionTree>();
-      // determine the set of possible parent functions
-      ICollection<IFunction> possibleParents = gardener.GetPossibleParents(new List<IFunction>() { f.Function, g.Function });
-      if(possibleParents.Count == 0) throw new InvalidProgramException();
-      // and select a random one
-      IFunctionTree parent = possibleParents.ElementAt(random.Next(possibleParents.Count())).GetTreeNode();
-
-      int nSlots = Math.Max(2, parent.Function.MinArity);
-      // determine which slot can take which sub-trees
-      List<IFunctionTree>[] slots = new List<IFunctionTree>[nSlots];
-      for(int slot = 0; slot < nSlots; slot++) {
-        ICollection<IFunction> allowedSubFunctions = gardener.GetAllowedSubFunctions(parent.Function, slot);
-        List<IFunctionTree> allowedTrees = new List<IFunctionTree>();
-        if(allowedSubFunctions.Contains(f.Function)) allowedTrees.Add(f);
-        if(allowedSubFunctions.Contains(g.Function)) allowedTrees.Add(g);
-        slots[slot] = allowedTrees;
+    private List<IFunctionTree> GetPossibleCrossoverPoints(TreeGardener gardener, IFunctionTree tree, int maxInsertedBranchSize, int maxInsertedBranchHeight, IList<IFunction> allowedFunctions) {
+      List<IFunctionTree> crossoverPoints = new List<IFunctionTree>();
+      foreach(IFunctionTree possiblePoint in gardener.GetAllSubTrees(tree)) {
+        if(allowedFunctions.Contains(possiblePoint.Function) && possiblePoint.Size <= maxInsertedBranchSize && possiblePoint.Height <= maxInsertedBranchHeight)
+          crossoverPoints.Add(possiblePoint);
       }
-      // fill the slots in the order of degrees of freedom
-      int[] slotSequence = Enumerable.Range(0, slots.Count()).OrderBy(slot => slots[slot].Count()).ToArray();
-
-      // tmp arry to store the tree for each sub-tree slot of the parent
-      IFunctionTree[] selectedFunctionTrees = new IFunctionTree[nSlots];
-
-      // fill the sub-tree slots of the parent starting with the slots that can take potentially both functions (f and g)
-      for(int i = 0; i < slotSequence.Length; i++) {
-        int slot = slotSequence[i];
-        List<IFunctionTree> allowedTrees = slots[slot];
-        // when neither f nor g fit into the slot => create a new random tree
-        if(allowedTrees.Count() == 0) {
-          var allowedFunctions = gardener.GetAllowedSubFunctions(parent.Function, slot);
-          selectedFunctionTrees[slot] = gardener.CreateRandomTree(allowedFunctions, 1, 1);
-          newBranches.AddRange(gardener.GetAllSubTrees(selectedFunctionTrees[slot]));
-        } else {
-          // select randomly which tree to insert into this slot
-          IFunctionTree selectedTree = allowedTrees[random.Next(allowedTrees.Count())];
-          selectedFunctionTrees[slot] = selectedTree;
-          // remove the tree that we used in this slot from following function-sets
-          for(int j = i + 1; j < slotSequence.Length; j++) {
-            int otherSlot = slotSequence[j];
-            slots[otherSlot].Remove(selectedTree);
-          }
-        }
-      }
-      // actually append the sub-trees to the parent tree
-      for(int i = 0; i < selectedFunctionTrees.Length; i++) {
-        parent.InsertSubTree(i, selectedFunctionTrees[i]);
-      }
-
-      return parent;
+      return crossoverPoints;
     }
   }
Index: trunk/sources/HeuristicLab.GP/Recombination/UniformCrossover.cs
===================================================================
--- trunk/sources/HeuristicLab.GP/Recombination/UniformCrossover.cs	(revision 831)
+++ trunk/sources/HeuristicLab.GP/Recombination/UniformCrossover.cs	(revision 832)
@@ -52,5 +52,5 @@
     }
 
-    internal override IFunctionTree Cross(TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
+    internal override IFunctionTree Cross(IScope scope, TreeGardener gardener, MersenneTwister random, IFunctionTree tree0, IFunctionTree tree1) {
       List<CrossoverPoint> allowedCrossOverPoints = new List<CrossoverPoint>();
       GetCrossOverPoints(gardener, tree0, tree1, allowedCrossOverPoints);
