Index: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs
===================================================================
--- branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs	(revision 18065)
+++ branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs	(revision 18066)
@@ -24,5 +24,4 @@
     private const string GrammarParameterName = "Grammar";
     private const string StructureTemplateParameterName = "Structure Template";
-    private const string MainTreeResultParameterName = "Main Tree";
     #endregion
 
@@ -32,5 +31,4 @@
     public IFixedValueParameter<StructureTemplate> StructureTemplateParameter => (IFixedValueParameter<StructureTemplate>)Parameters[StructureTemplateParameterName];
     public IValueParameter<ISymbolicDataAnalysisGrammar> GrammarParameter => (IValueParameter<ISymbolicDataAnalysisGrammar>)Parameters[GrammarParameterName];
-    public IResultParameter<ISymbolicExpressionTree> MainTreeResultParameter => (IResultParameter<ISymbolicExpressionTree>)Parameters[MainTreeResultParameterName];
     #endregion
 
@@ -61,5 +59,5 @@
     IDataAnalysisProblemData IDataAnalysisProblem.ProblemData => ProblemData;
 
-    public override bool Maximization => false;
+    public override bool Maximization => true;
     #endregion
 
@@ -78,18 +76,11 @@
 
       var structureTemplate = new StructureTemplate();
-      structureTemplate.Changed += (sender, args) => {
-        foreach (var e in Encoding.Encodings.ToArray())
-          Encoding.Remove(e);
+      structureTemplate.Changed += OnTemplateChanged;
 
-        foreach(var sf in structureTemplate.SubFunctions.Values) {
-          Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth));
-        }
-      };
-
-      Parameters.Add(new ValueParameter<RegressionProblemData>(ProblemDataParameterName, problemData));
+      Parameters.Add(new ValueParameter<IRegressionProblemData>(ProblemDataParameterName, problemData));
       Parameters.Add(new FixedValueParameter<StructureTemplate>(StructureTemplateParameterName, structureTemplate));
       Parameters.Add(new ValueParameter<ISymbolicDataAnalysisGrammar>(GrammarParameterName, grammar));
-      Parameters.Add(new ResultParameter<ISymbolicExpressionTree>(MainTreeResultParameterName, ""));
-      MainTreeResultParameter.DefaultValue = new SymbolicExpressionTree();
+
+      structureTemplate.Template = "f(x)*f(y)+5";
     }
 
@@ -105,39 +96,73 @@
     #endregion
 
-    public override double Evaluate(Individual individual, IRandom random) {
-      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
-      var subFunctionDict = StructureTemplate.SubFunctions;
-          
-      // build main tree
-      foreach (var n in templateTree.IterateNodesPrefix()) {
-        if(n is SubFunctionTreeNode subFunctionTreeNode) {
-          if(subFunctionDict.TryGetValue(subFunctionTreeNode, out SubFunction subFunction)) {
-            var subFunctionTree = individual.SymbolicExpressionTree(subFunction.Name);
-            var parent = n.Parent;
-            // remove all subtrees
+    private void OnTemplateChanged(object sender, EventArgs args) {
+      foreach (var e in Encoding.Encodings.ToArray())
+        Encoding.Remove(e);
 
-            var subtreeCount = parent.SubtreeCount;
-            for (int idx = 0; idx < subtreeCount; ++idx)
-              parent.RemoveSubtree(idx);
+      foreach (var sf in StructureTemplate.SubFunctions.Values) {
+        Encoding.Add(new SymbolicExpressionTreeEncoding(sf.Name, sf.Grammar, sf.MaximumSymbolicExpressionTreeLength, sf.MaximumSymbolicExpressionTreeDepth));
+      }
+    }
 
-            // add new tree
-            parent.AddSubtree(subFunctionTree.Root);
-          }
+    public override void Analyze(Individual[] individuals, double[] qualities, ResultCollection results, IRandom random) {
+      base.Analyze(individuals, qualities, results, random);
+
+      int bestIdx = 0;
+      double bestQuality = Maximization ? double.MinValue : double.MaxValue;
+      for(int idx = 0; idx < qualities.Length; ++idx) {
+        if((Maximization && qualities[idx] > bestQuality) || 
+          (!Maximization && qualities[idx] < bestQuality)) {
+          bestQuality = qualities[idx];
+          bestIdx = idx;
         }
       }
 
-      MainTreeResultParameter.ActualValue = templateTree;
+      if (results.TryGetValue("Best Tree", out IResult result))
+        result.Value = BuildTree(individuals[bestIdx]);
+      else
+        results.Add(new Result("Best Tree", BuildTree(individuals[bestIdx])));
+
       /*
-      foreach (var kvp in individual.Values) {
-        if(kvp.Value is SymbolicExpressionTree tree) {
-          foreach(var n in tree.IterateNodesPrefix()) {
-            if(n.Symbol is Variable v) {
-              var t = v.VariableNames;
-            }
-          }
+      if (results.TryGetValue("Tree", out IResult result)) {
+        var list = result.Value as ItemList<ISymbolicExpressionTree>;
+        list.Clear();
+        list.AddRange(individuals.Select(x => (BuildTree(x))));
+      } else 
+        results.Add(new Result("Tree", new ItemList<ISymbolicExpressionTree>(individuals.Select(x => (BuildTree(x))))));
+      */
+    }
+
+    public override double Evaluate(Individual individual, IRandom random) {
+      var tree = BuildTree(individual);
+      var interpreter = new SymbolicDataAnalysisExpressionTreeInterpreter();
+      var estimationInterval = ProblemData.VariableRanges.GetInterval(ProblemData.TargetVariable);
+      var quality = SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(
+        interpreter, tree, 
+        estimationInterval.LowerBound, estimationInterval.UpperBound, 
+        ProblemData, ProblemData.TrainingIndices, false);
+     
+      return quality;
+    }
+
+    private ISymbolicExpressionTree BuildTree(Individual individual) {
+      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
+
+      // build main tree
+      foreach (var n in templateTree.IterateNodesPrefix()) {
+        if (n.Symbol is SubFunctionSymbol) {
+          var subFunctionTreeNode = n as SubFunctionTreeNode;
+          var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.SubFunction.Name);
+          var parent = n.Parent;
+
+          // remove SubFunctionTreeNode
+          parent.RemoveSubtree(parent.IndexOfSubtree(subFunctionTreeNode));
+
+          // add new tree
+          var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
+                                            .GetSubtree(0); // Offset
+          parent.AddSubtree(subTree);
         }
       }
-      */
-      return 0.0;
+      return templateTree;
     }
 
Index: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs
===================================================================
--- branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs	(revision 18065)
+++ branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/StructureTemplate.cs	(revision 18066)
@@ -1,7 +1,4 @@
 ﻿using System;
 using System.Collections.Generic;
-using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
 using HeuristicLab.Core;
 using HEAL.Attic;
@@ -33,9 +30,6 @@
     public ISymbolicExpressionTree Tree => tree;
 
-    //[Storable]
-    //private IDictionary<SubFunctionTreeNode, SubFunction> subFunctions;
     [Storable]
     public IDictionary<SubFunctionTreeNode, SubFunction> SubFunctions { get; private set; } = new Dictionary<SubFunctionTreeNode, SubFunction>();
-      //subFunctions == null ? new Dictionary<SubFunctionTreeNode, SubFunction>() : subFunctions;
 
     protected InfixExpressionParser Parser { get; set; } = new InfixExpressionParser();
@@ -49,7 +43,5 @@
 
     #region Constructors
-    public StructureTemplate() {
-      Template = "f(x)*f(y)+5";
-    }
+    public StructureTemplate() { }
 
     [StorableConstructor]
@@ -66,4 +58,5 @@
     private void GetSubFunctions(ISymbolicExpressionTree tree) {
       int count = 1;
+      SubFunctions.Clear();
       foreach (var node in tree.IterateNodesPrefix())
         if (node is SubFunctionTreeNode subFunctionTreeNode) { 
@@ -72,7 +65,7 @@
             FunctionArguments = subFunctionTreeNode.FunctionArguments 
           };
+          subFunctionTreeNode.SubFunction = subFunction;
           SubFunctions.Add(subFunctionTreeNode, subFunction);
         }
-
     }
   }
Index: branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionTreeNode.cs
===================================================================
--- branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionTreeNode.cs	(revision 18065)
+++ branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Symbols/SubFunctionTreeNode.cs	(revision 18066)
@@ -16,4 +16,6 @@
 
     public IEnumerable<string> FunctionArguments { get; set; } = Enumerable.Empty<string>();
+    
+    public SubFunction SubFunction { get; set; }
     #endregion
 
@@ -24,5 +26,7 @@
     protected SubFunctionTreeNode(StorableConstructorFlag _) : base(_) { }
 
-    protected SubFunctionTreeNode(SubFunctionTreeNode original, Cloner cloner) : base(original, cloner) { }
+    protected SubFunctionTreeNode(SubFunctionTreeNode original, Cloner cloner) : base(original, cloner) {
+      this.SubFunction = original.SubFunction;
+    }
     #endregion
 
