Index: branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs
===================================================================
--- branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs	(revision 7110)
+++ branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionDeterministicBestCrossover.cs	(revision 7110)
@@ -0,0 +1,123 @@
+#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2011 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.Persistence.Default.CompositeSerializers.Storable;
+using HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Data;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
+
+  [Item("DeterministicBestCrossover", "An operator which performs subtree swapping in a deterministic way: it chooses the child from the second parent who would produce the best result if replaced in the first parent")]
+  public sealed class SymbolicDataAnalysisExpressionDeterministicBestCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData {
+    [StorableConstructor]
+    private SymbolicDataAnalysisExpressionDeterministicBestCrossover(bool deserializing) : base(deserializing) { }
+    private SymbolicDataAnalysisExpressionDeterministicBestCrossover(SymbolicDataAnalysisExpressionDeterministicBestCrossover<T> original, Cloner cloner)
+      : base(original, cloner) {
+    }
+    public SymbolicDataAnalysisExpressionDeterministicBestCrossover()
+      : base() {
+    }
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new SymbolicDataAnalysisExpressionDeterministicBestCrossover<T>(this, cloner);
+    }
+    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
+      if (this.ExecutionContext == null)
+        throw new InvalidOperationException("ExecutionContext not set.");
+      IEnumerable<int> rows = GenerateRowsToEvaluate();
+      T problemData = ProblemDataParameter.ActualValue;
+      ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator = EvaluatorParameter.ActualValue;
+
+      return Cross(random, parent0, parent1, this.ExecutionContext, evaluator, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
+    }
+    /// <summary>
+    /// Takes two parent individuals P0 and P1. 
+    /// Randomly choose a node i from the first parent, then test all nodes j from the second parent to determine the best child that would be obtained by swapping i for j.
+    /// </summary>
+    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1, IExecutionContext context,
+                                                ISymbolicDataAnalysisSingleObjectiveEvaluator<T> evaluator, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
+      var crossoverPoints0 = new List<CutPoint>();
+      parent0.Root.ForEachNodePostfix((n) => {
+        if (n.Subtrees.Any() && n != parent0.Root)
+          foreach (var child in n.Subtrees)
+            crossoverPoints0.Add(new CutPoint(n, child));
+      });
+      CutPoint crossoverPoint0 = crossoverPoints0[random.Next(crossoverPoints0.Count)];
+      int level = parent0.Root.GetBranchLevel(crossoverPoint0.Child);
+      int length = parent0.Root.GetLength() - crossoverPoint0.Child.GetLength();
+
+      var allowedBranches = new List<ISymbolicExpressionTreeNode>();
+      parent1.Root.ForEachNodePostfix((n) => {
+        if (n.Subtrees.Any() && n != parent1.Root)
+          foreach (var child in n.Subtrees)
+            if (crossoverPoint0.IsMatchingPointType(child) && (child.GetDepth() + level <= maxDepth) && (child.GetLength() + length <= maxLength))
+              allowedBranches.Add(child);
+      });
+
+      // check if empty branch is allowed
+      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
+
+      if (allowedBranches.Count == 0)
+        return parent0;
+
+      // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
+      ISymbolicExpressionTreeNode selectedBranch = null;
+
+      var nodeQualities = new List<Tuple<ISymbolicExpressionTreeNode, double>>();
+
+      foreach (var node in allowedBranches) {
+        var parent = node.Parent;
+        swap(crossoverPoint0, node); // the swap will set the nodes parent to crossoverPoint0.Parent
+        double quality = evaluator.Evaluate(context, parent0, problemData, rows);
+        nodeQualities.Add(new Tuple<ISymbolicExpressionTreeNode, double>(node, quality));
+        node.Parent = parent; // restore correct parent
+      }
+
+      nodeQualities.Sort((a, b) => a.Item2.CompareTo(b.Item2)); // assuming this sorts the list in ascending order
+      selectedBranch = evaluator.Maximization ? nodeQualities.Last().Item1 : nodeQualities.First().Item1;
+      // swap the node that would create the best offspring
+      swap(crossoverPoint0, selectedBranch);
+
+      return parent0;
+    }
+
+    private static void swap(CutPoint crossoverPoint, ISymbolicExpressionTreeNode selectedBranch) {
+      if (crossoverPoint.Child != null) {
+        // manipulate the tree of parent0 in place
+        // replace the branch in tree0 with the selected branch from tree1
+        crossoverPoint.Parent.RemoveSubtree(crossoverPoint.ChildIndex);
+        if (selectedBranch != null) {
+          crossoverPoint.Parent.InsertSubtree(crossoverPoint.ChildIndex, selectedBranch);
+        }
+      } else {
+        // child is null (additional child should be added under the parent)
+        if (selectedBranch != null) {
+          crossoverPoint.Parent.AddSubtree(selectedBranch);
+        }
+      }
+    }
+  }
+}
