Index: branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs
===================================================================
--- branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs	(revision 7036)
+++ branches/gp-crossover/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Crossovers/SymbolicDataAnalysisExpressionSemanticSimilarityCrossover.cs	(revision 7036)
@@ -0,0 +1,127 @@
+#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("ProbabilisticFunctionalCrossover", "An operator which performs subtree swapping based on behavioral similarity")]
+  public sealed class SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> : SymbolicDataAnalysisExpressionCrossover<T> where T : class, IDataAnalysisProblemData 
+  {
+    [StorableConstructor]
+    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(bool deserializing) : base(deserializing) { }
+    private SymbolicDataAnalysisExpressionSemanticSimilarityCrossover(SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T> original, Cloner cloner)
+      : base(original, cloner) {
+    }
+    public SymbolicDataAnalysisExpressionSemanticSimilarityCrossover()
+      : base() {
+    }
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new SymbolicDataAnalysisExpressionSemanticSimilarityCrossover<T>(this, cloner);
+    } 
+    protected override ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1) {
+      ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
+      IEnumerable<int> rows = GenerateRowsToEvaluate();
+      T problemData = ProblemDataParameter.ActualValue;
+      return Cross(random, parent0, parent1, interpreter, problemData, rows, MaximumSymbolicExpressionTreeDepth.Value, MaximumSymbolicExpressionTreeLength.Value);
+    }
+    /// <summary>
+    /// Takes two parent individuals P0 and P1. 
+    /// Randomly choose a node i from the first parent, then get a node j from the second parent that matches the semantic similarity criteria.
+    /// </summary>
+    public static ISymbolicExpressionTree Cross(IRandom random, ISymbolicExpressionTree parent0, ISymbolicExpressionTree parent1,
+                                                ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, T problemData, IEnumerable<int> rows, int maxDepth, int maxLength) {
+      List<CutPoint> 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();
+
+      List<ISymbolicExpressionTreeNode> 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(n);
+      });
+
+      // check if empty branch is allowed
+      if (crossoverPoint0.IsMatchingPointType(null)) allowedBranches.Add(null);
+
+      if (allowedBranches.Count == 0) 
+        return parent0;
+
+     var dataset = problemData.Dataset;
+
+      // create symbols in order to improvize an ad-hoc tree so that the child can be evaluated
+      var rootSymbol = new ProgramRootSymbol();
+      var startSymbol = new StartSymbol();
+      var tree0 = CreateTreeFromNode(random, crossoverPoint0.Child, rootSymbol, startSymbol);
+      IEnumerable<double> estimatedValues0 = interpreter.GetSymbolicExpressionTreeValues(tree0, dataset, rows);
+
+      ISymbolicExpressionTreeNode selectedBranch = null;
+
+      // pick the first node that fulfills the semantic similarity conditions
+      foreach (var node in allowedBranches) {
+        var tree1 = CreateTreeFromNode(random, node, rootSymbol, startSymbol);
+        IEnumerable<double> estimatedValues1 = interpreter.GetSymbolicExpressionTreeValues(tree1, dataset, rows);
+
+        //double ssd = SSD(estimatedValues0, estimatedValues1);
+        OnlineCalculatorError errorState;
+        double ssd = OnlineMeanAbsoluteErrorCalculator.Calculate(estimatedValues0, estimatedValues1, out errorState);
+
+        // TODO: introduce parameters alpha and beta for establishing the interval boundaries. the best values need to be determined experimentally
+        if (0.0001 < ssd && ssd < 10) {
+          selectedBranch = node;
+          break;
+        }
+      }
+
+      // perform the actual swap
+      if (crossoverPoint0.Child != null) {
+        // manipulate the tree of parent0 in place
+        // replace the branch in tree0 with the selected branch from tree1
+        crossoverPoint0.Parent.RemoveSubtree(crossoverPoint0.ChildIndex);
+        if (selectedBranch != null) {
+          crossoverPoint0.Parent.InsertSubtree(crossoverPoint0.ChildIndex, selectedBranch);
+        }
+      } else {
+        // child is null (additional child should be added under the parent)
+        if (selectedBranch != null) {
+          crossoverPoint0.Parent.AddSubtree(selectedBranch);
+        }
+      }
+      return parent0;
+    }
+  }
+}
