Index: branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs
===================================================================
--- branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs	(revision 11997)
+++ branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs	(revision 11997)
@@ -0,0 +1,41 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2014 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 HeuristicLab.Analysis;
+using HeuristicLab.Core;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
+  [Item("SymbolicDataAnalysisBottomUpDiversityAnalyzer", "A diversity analyzer based on the bottom-up similarity between trees.")]
+  [StorableClass]
+  public class SymbolicDataAnalysisBottomUpDiversityAnalyzer : SingleObjectivePopulationDiversityAnalyzer {
+    [StorableConstructor]
+    protected SymbolicDataAnalysisBottomUpDiversityAnalyzer(bool deserializing) : base(deserializing) { }
+
+    [StorableHook(HookType.AfterDeserialization)]
+    private void AfterDeserialization() {
+    }
+
+    public SymbolicDataAnalysisBottomUpDiversityAnalyzer() {
+      SimilarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { SolutionVariableName = "SymbolicExpressionTree" };
+    }
+  }
+}
Index: branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisPhenotypicDiversityAnalyzer.cs
===================================================================
--- branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisPhenotypicDiversityAnalyzer.cs	(revision 11997)
+++ branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisPhenotypicDiversityAnalyzer.cs	(revision 11997)
@@ -0,0 +1,82 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2014 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.Linq;
+using HeuristicLab.Analysis;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Parameters;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Analyzers {
+  [Item("SymbolicDataAnalysisPhenotypicDiversityAnalyzer", "An analyzer which calculates diversity based on the phenotypic similarity between trees")]
+  [StorableClass]
+  public class SymbolicDataAnalysisPhenotypicDiversityAnalyzer : SingleObjectivePopulationDiversityAnalyzer {
+    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
+    private const string EvaluatedValuesParameterName = "EstimatedValues";
+    private const string SymbolicDataAnalysisTreeInterpreterParameterName = "SymbolicExpressionTreeInterpreter";
+    private const string ProblemDataParameterName = "ProblemData";
+    private const string EstimationLimitsParameterName = "EstimationLimits";
+
+    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
+      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
+    }
+
+    private IScopeTreeLookupParameter<DoubleArray> EvaluatedValuesParameter {
+      get { return (IScopeTreeLookupParameter<DoubleArray>)Parameters[EvaluatedValuesParameterName]; }
+    }
+
+    public ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter> SymbolicDataAnalysisTreeInterpreterParameter {
+      get { return (ILookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>)Parameters[SymbolicDataAnalysisTreeInterpreterParameterName]; }
+    }
+    public IValueLookupParameter<IDataAnalysisProblemData> ProblemDataParameter {
+      get { return (IValueLookupParameter<IDataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }
+    }
+
+    public IValueLookupParameter<DoubleLimit> EstimationLimitsParameter {
+      get { return (IValueLookupParameter<DoubleLimit>)Parameters[EstimationLimitsParameterName]; }
+    }
+
+    public SymbolicDataAnalysisPhenotypicDiversityAnalyzer() {
+      SimilarityCalculator = new PhenotypicSimilarityCalculator { SolutionVariableName = "SymbolicExpressionTree" };
+
+      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees."));
+      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(EvaluatedValuesParameterName, "Intermediate estimated values to be saved in the scopes."));
+      Parameters.Add(new LookupParameter<ISymbolicDataAnalysisExpressionTreeInterpreter>(SymbolicDataAnalysisTreeInterpreterParameterName, "The interpreter that should be used to calculate the output values of the symbolic data analysis tree."));
+      Parameters.Add(new ValueLookupParameter<IDataAnalysisProblemData>(ProblemDataParameterName, "The problem data on which the symbolic data analysis solution should be evaluated."));
+      Parameters.Add(new ValueLookupParameter<DoubleLimit>(EstimationLimitsParameterName, "The upper and lower limit that should be used as cut off value for the output values of symbolic data analysis trees."));
+    }
+
+    public override IOperation Apply() {
+      var trees = SymbolicExpressionTreeParameter.ActualValue;
+      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
+      var ds = ProblemDataParameter.ActualValue.Dataset;
+      var rows = ProblemDataParameter.ActualValue.TrainingIndices;
+
+      var evaluatedValues = new ItemArray<DoubleArray>(trees.Select(t => new DoubleArray(interpreter.GetSymbolicExpressionTreeValues(t, ds, rows).ToArray())));
+
+      EvaluatedValuesParameter.ActualValue = evaluatedValues;
+
+      return base.Apply();
+    }
+  }
+}
Index: branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisPopulationDiversityAnalyzer.cs
===================================================================
--- branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisPopulationDiversityAnalyzer.cs	(revision 11981)
+++ 	(revision )
@@ -1,185 +1,0 @@
-﻿#region License Information
-/* HeuristicLab
- * Copyright (C) 2002-2014 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.Linq;
-using HeuristicLab.Analysis;
-using HeuristicLab.Common;
-using HeuristicLab.Core;
-using HeuristicLab.Data;
-using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
-using HeuristicLab.Operators;
-using HeuristicLab.Optimization;
-using HeuristicLab.Parameters;
-using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
-
-// type definitions for ease of use
-
-namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
-  /// <summary>
-  /// Population diversity analyzer
-  /// </summary>
-  [Item("SymbolicDataAnalysisPopulationDiversityAnalyzer", "An operator that tracks population diversity")]
-  [StorableClass]
-  public sealed class SymbolicDataAnalysisPopulationDiversityAnalyzer : SingleSuccessorOperator, IAnalyzer {
-    #region Parameter names
-    private const string MaximumSymbolicExpressionTreeDepthParameterName = "MaximumSymbolicExpressionTreeDepth";
-    private const string SymbolicExpressionTreeParameterName = "SymbolicExpressionTree";
-    private const string UpdateIntervalParameterName = "UpdateInterval";
-    private const string UpdateCounterParameterName = "UpdateCounter";
-    private const string ResultsParameterName = "Results";
-    private const string GenerationsParameterName = "Generations";
-    private const string StoreHistoryParameterName = "StoreHistory";
-    // comparer parameters
-    private const string SimilarityValuesParmeterName = "Similarity";
-
-    private const string DistanceCalculatorParameterName = "DistanceCalculator";
-
-    #endregion
-
-    #region Parameters
-    public IValueLookupParameter<IntValue> MaximumSymbolicExpressionTreeDepthParameter {
-      get { return (IValueLookupParameter<IntValue>)Parameters[MaximumSymbolicExpressionTreeDepthParameterName]; }
-    }
-    public ValueParameter<IntValue> UpdateIntervalParameter {
-      get { return (ValueParameter<IntValue>)Parameters[UpdateIntervalParameterName]; }
-    }
-    public ValueParameter<IntValue> UpdateCounterParameter {
-      get { return (ValueParameter<IntValue>)Parameters[UpdateCounterParameterName]; }
-    }
-    public LookupParameter<ResultCollection> ResultsParameter {
-      get { return (LookupParameter<ResultCollection>)Parameters[ResultsParameterName]; }
-    }
-    public LookupParameter<IntValue> GenerationsParameter {
-      get { return (LookupParameter<IntValue>)Parameters[GenerationsParameterName]; }
-    }
-    public ValueParameter<BoolValue> StoreHistoryParameter {
-      get { return (ValueParameter<BoolValue>)Parameters[StoreHistoryParameterName]; }
-    }
-    public IScopeTreeLookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
-      get { return (IScopeTreeLookupParameter<ISymbolicExpressionTree>)Parameters[SymbolicExpressionTreeParameterName]; }
-    }
-    public ILookupParameter<DoubleValue> SimilarityParameter {
-      get { return (ILookupParameter<DoubleValue>)Parameters[SimilarityValuesParmeterName]; }
-    }
-    public IValueParameter<ISymbolicExpressionTreeDistanceCalculator> DistanceCalculatorParameter {
-      get { return (IValueParameter<ISymbolicExpressionTreeDistanceCalculator>)Parameters[DistanceCalculatorParameterName]; }
-    }
-    #endregion
-
-    #region Parameter properties
-    public IntValue MaximumSymbolicExpressionTreeDepth { get { return MaximumSymbolicExpressionTreeDepthParameter.ActualValue; } }
-    public IntValue UpdateInterval { get { return UpdateIntervalParameter.Value; } }
-    public IntValue UpdateCounter { get { return UpdateCounterParameter.Value; } }
-    public ResultCollection Results { get { return ResultsParameter.ActualValue; } }
-    public IntValue Generations { get { return GenerationsParameter.ActualValue; } }
-    public BoolValue StoreHistory { get { return StoreHistoryParameter.Value; } }
-    #endregion
-
-    [StorableConstructor]
-    private SymbolicDataAnalysisPopulationDiversityAnalyzer(bool deserializing)
-      : base(deserializing) {
-    }
-
-    private SymbolicDataAnalysisPopulationDiversityAnalyzer(
-      SymbolicDataAnalysisPopulationDiversityAnalyzer original, Cloner cloner)
-      : base(original, cloner) {
-    }
-
-    public override IDeepCloneable Clone(Cloner cloner) {
-      return new SymbolicDataAnalysisPopulationDiversityAnalyzer(this, cloner);
-    }
-
-    public SymbolicDataAnalysisPopulationDiversityAnalyzer() {
-      // add parameters
-      Parameters.Add(new ScopeTreeLookupParameter<ISymbolicExpressionTree>(SymbolicExpressionTreeParameterName, "The symbolic expression trees to analyze."));
-      Parameters.Add(new ValueParameter<IntValue>(UpdateIntervalParameterName, "The interval in which the tree length analysis should be applied.", new IntValue(1)));
-      Parameters.Add(new ValueParameter<IntValue>(UpdateCounterParameterName, "The value which counts how many times the operator was called since the last update", new IntValue(0)));
-      Parameters.Add(new ValueLookupParameter<ResultCollection>(ResultsParameterName, "The results collection where the analysis values should be stored."));
-      Parameters.Add(new LookupParameter<IntValue>(GenerationsParameterName, "The number of generations so far."));
-      Parameters.Add(new ValueParameter<BoolValue>(StoreHistoryParameterName, "True if the tree lengths history of the population should be stored.", new BoolValue(false)));
-      Parameters.Add(new LookupParameter<DoubleValue>(SimilarityValuesParmeterName, ""));
-      Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
-      Parameters.Add(new ValueParameter<ISymbolicExpressionTreeDistanceCalculator>(DistanceCalculatorParameterName, "The distance calculator between trees.", new BottomUpTreeDistanceCalculator()));
-      UpdateCounterParameter.Hidden = true;
-      UpdateIntervalParameter.Hidden = true;
-    }
-
-    [StorableHook(HookType.AfterDeserialization)]
-    private void AfterDeserialization() {
-      if (!Parameters.ContainsKey(MaximumSymbolicExpressionTreeDepthParameterName)) {
-        Parameters.Add(new ValueLookupParameter<IntValue>(MaximumSymbolicExpressionTreeDepthParameterName, "The maximal depth of the symbolic expression tree (a tree with one node has depth = 0)."));
-      }
-    }
-
-    #region IStatefulItem members
-    public override void InitializeState() {
-      UpdateCounter.Value = 0;
-      base.InitializeState();
-    }
-    #endregion
-
-    public bool EnabledByDefault {
-      get { return true; }
-    }
-
-    public override IOperation Apply() {
-      if (SimilarityParameter.ActualValue == null || SimilarityParameter.ActualValue.Value.IsAlmost(-1.0)) {
-        UpdateCounter.Value++;
-        if (UpdateCounter.Value != UpdateInterval.Value) return base.Apply();
-        UpdateCounter.Value = 0;
-        var trees = SymbolicExpressionTreeParameter.ActualValue.ToList();
-
-        SimilarityParameter.ActualValue = new DoubleValue();
-
-        var operations = new OperationCollection { Parallel = true };
-        foreach (var tree in trees) {
-          var op = new SymbolicDataAnalysisExpressionTreeSimilarityCalculator(DistanceCalculatorParameter.Value) {
-            CurrentSymbolicExpressionTree = tree,
-            MaximumTreeDepth = MaximumSymbolicExpressionTreeDepth.Value
-          };
-          var operation = ExecutionContext.CreateChildOperation(op, ExecutionContext.Scope);
-          operations.Add(operation);
-        }
-        return new OperationCollection { operations, ExecutionContext.CreateOperation(this) };
-      }
-
-      var results = ResultsParameter.ActualValue;
-      // population diversity
-      DataTable populationDiversityTable;
-      if (!results.ContainsKey("PopulationDiversity")) {
-        populationDiversityTable = new DataTable("PopulationDiversity") { VisualProperties = { YAxisTitle = "Diversity" } };
-        results.Add(new Result("PopulationDiversity", populationDiversityTable));
-      }
-      populationDiversityTable = (DataTable)results["PopulationDiversity"].Value;
-      if (!populationDiversityTable.Rows.ContainsKey("Diversity"))
-        populationDiversityTable.Rows.Add(new DataRow("Diversity") { VisualProperties = { StartIndexZero = true } });
-
-      int length = SymbolicExpressionTreeParameter.ActualValue.Length;
-      var similarity = SimilarityParameter.ActualValue.Value / (length * (length - 1) / 2.0);
-      var diversity = 1 - similarity;
-      SimilarityParameter.ActualValue.Value = -1.0;
-
-      populationDiversityTable.Rows["Diversity"].Values.Add(diversity);
-
-      return base.Apply();
-    }
-  }
-}
Index: branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
===================================================================
--- branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj	(revision 11981)
+++ branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj	(revision 11997)
@@ -118,4 +118,6 @@
   </ItemGroup>
   <ItemGroup>
+    <Compile Include="Analyzers\SymbolicDataAnalysisBottomUpDiversityAnalyzer.cs" />
+    <Compile Include="Analyzers\SymbolicDataAnalysisPhenotypicDiversityAnalyzer.cs" />
     <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectivePruningAnalyzer.cs" />
     <Compile Include="Analyzers\SymbolicDataAnalysisSingleObjectiveValidationParetoBestSolutionAnalyzer.cs" />
@@ -240,4 +242,5 @@
     <Compile Include="TreeMatching\SymbolicExpressionTreeNodeComparer.cs" />
     <Compile Include="TreeMatching\SymbolicExpressionTreeNodeEqualityComparer.cs" />
+    <Compile Include="TreeMatching\SymbolicExpressionTreePhenotypicSimilarityCalculator.cs" />
     <None Include="HeuristicLab.snk" />
     <None Include="Plugin.cs.frame" />
Index: branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeBottomUpSimilarityCalculator.cs
===================================================================
--- branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeBottomUpSimilarityCalculator.cs	(revision 11981)
+++ branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreeBottomUpSimilarityCalculator.cs	(revision 11997)
@@ -1,5 +1,5 @@
 ﻿#region License Information
 /* HeuristicLab
- * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
+ * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
  *
  * This file is part of HeuristicLab.
@@ -81,5 +81,5 @@
 
       // visit nodes in order of decreasing height to ensure correct mapping
-      var nodes1 = n1.IterateNodesPrefix().ToList();
+      var nodes1 = n1.IterateNodesPrefix().OrderByDescending(x => x.GetDepth()).ToList();
       var nodes2 = n2.IterateNodesPrefix().ToList();
       for (int i = 0; i < nodes1.Count; ++i) {
@@ -98,6 +98,8 @@
         if (w == null) continue;
 
-        // at this point we know that v and w are isomorphic, however, the mapping cannot be done directly (as in the paper) because the trees are unordered (subtree order might differ)
-        // the solution is to sort subtrees by label using IterateBreadthOrdered (this will work because the subtrees are isomorphic!) and simultaneously iterate over the two subtrees
+        // at this point we know that v and w are isomorphic, however, the mapping cannot be done directly
+        // (as in the paper) because the trees are unordered (subtree order might differ). the solution is 
+        // to sort subtrees from under commutative labels (this will work because the subtrees are isomorphic!)
+        // while iterating over the two subtrees
         var vv = IterateBreadthOrdered(v, comparer).ToList();
         var ww = IterateBreadthOrdered(w, comparer).ToList();
@@ -133,5 +135,5 @@
       foreach (var n in nodes) {
         if (n.SubtreeCount == 0) {
-          var label = Label(n);
+          var label = GetLabel(n);
           if (!labelMap.ContainsKey(label)) {
             var z = new GraphNode { SymbolicExpressionTreeNode = n, Label = label };
@@ -209,5 +211,5 @@
     }
 
-    private string Label(ISymbolicExpressionTreeNode node) {
+    private static string GetLabel(ISymbolicExpressionTreeNode node) {
       if (node.SubtreeCount > 0)
         return node.Symbol.Name;
Index: branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreePhenotypicSimilarityCalculator.cs
===================================================================
--- branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreePhenotypicSimilarityCalculator.cs	(revision 11997)
+++ branches/ALPS/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/TreeMatching/SymbolicExpressionTreePhenotypicSimilarityCalculator.cs	(revision 11997)
@@ -0,0 +1,74 @@
+﻿#region License Information
+/* HeuristicLab
+ * Copyright (C) 2002-2014 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 HeuristicLab.Common;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
+using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Optimization.Operators;
+using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
+
+namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
+  [Item("PhenotypicSimilarityCalculator", "An operator that calculates the similarity betweeon two trees based on the correlation of their outputs.")]
+  [StorableClass]
+  public class PhenotypicSimilarityCalculator : SingleObjectiveSolutionSimilarityCalculator {
+    public IDataAnalysisProblemData ProblemData { get; set; }
+    public ISymbolicDataAnalysisExpressionTreeInterpreter Interpreter { get; set; }
+
+    public PhenotypicSimilarityCalculator(PhenotypicSimilarityCalculator original, Cloner cloner)
+      : base(original, cloner) {
+    }
+
+    public PhenotypicSimilarityCalculator() { }
+
+    [StorableConstructor]
+    protected PhenotypicSimilarityCalculator(bool deserializing) : base(deserializing) { }
+
+    public override IDeepCloneable Clone(Cloner cloner) {
+      return new PhenotypicSimilarityCalculator(this, cloner);
+    }
+
+    public double CalculateSimilarity(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2) {
+      var v1 = Interpreter.GetSymbolicExpressionTreeValues(t1, ProblemData.Dataset, ProblemData.TrainingIndices);
+      var v2 = Interpreter.GetSymbolicExpressionTreeValues(t2, ProblemData.Dataset, ProblemData.TrainingIndices);
+
+      OnlineCalculatorError error;
+      var r2 = OnlinePearsonsRSquaredCalculator.Calculate(v1, v2, out error);
+
+      if (r2 > 1.0)
+        r2 = 1.0;
+
+      return error == OnlineCalculatorError.None ? r2 : 0;
+    }
+
+    public override double CalculateSolutionSimilarity(IScope leftSolution, IScope rightSolution) {
+      var leftValues = (DoubleArray)leftSolution.Variables["EstimatedValues"].Value;
+      var rightValues = (DoubleArray)rightSolution.Variables["EstimatedValues"].Value;
+      OnlineCalculatorError error;
+      var r2 = OnlinePearsonsRSquaredCalculator.Calculate(leftValues, rightValues, out error);
+
+      if (r2 > 1.0)
+        r2 = 1.0; // sometimes due to fp errors it can happen that the r2 is over 1 (like 1.0000000009)
+
+      return error == OnlineCalculatorError.None ? r2 : 0;
+    }
+  }
+}
