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 18183)
+++ /branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic.Regression/3.4/SingleObjective/StructuredSymbolicRegressionSingleObjectiveProblem.cs	(revision 18184)
@@ -1,9 +1,7 @@
 ﻿using System;
-using System.Collections.Generic;
 using System.Linq;
 using HEAL.Attic;
 using HeuristicLab.Common;
 using HeuristicLab.Core;
-using HeuristicLab.Data;
 using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
 using HeuristicLab.Optimization;
@@ -28,5 +26,4 @@
 
     private const string SymbolicExpressionTreeName = "SymbolicExpressionTree";
-    private const string VariableName = "Variable";
 
     private const string StructureTemplateDescriptionText =
@@ -127,8 +124,10 @@
     }
 
-    public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original,
-      Cloner cloner) : base(original, cloner) {
+    public StructuredSymbolicRegressionSingleObjectiveProblem(StructuredSymbolicRegressionSingleObjectiveProblem original, Cloner cloner) : base(original, cloner) {
       RegisterEventHandlers();
     }
+
+    public override IDeepCloneable Clone(Cloner cloner) =>
+      new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
 
     [StorableConstructor]
@@ -143,8 +142,11 @@
     #endregion
 
-    #region Cloning
-    public override IDeepCloneable Clone(Cloner cloner) =>
-      new StructuredSymbolicRegressionSingleObjectiveProblem(this, cloner);
-    #endregion
+    private void RegisterEventHandlers() {
+      if (StructureTemplate != null) {
+        StructureTemplate.Changed += OnTemplateChanged;
+      }
+
+      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
+    }
 
     private void ProblemDataParameterValueChanged(object sender, EventArgs e) {
@@ -153,28 +155,33 @@
     }
 
-    private void RegisterEventHandlers() {
-      if (StructureTemplate != null) {
-        StructureTemplate.Changed += OnTemplateChanged;
-      }
-
-      ProblemDataParameter.ValueChanged += ProblemDataParameterValueChanged;
-    }
-
     private void OnTemplateChanged(object sender, EventArgs args) {
-      SetupStructureTemplate();
-    }
-
-    private void SetupStructureTemplate() {
+      SetupEncoding();
+    }
+
+    private void SetupEncoding() {
       foreach (var e in Encoding.Encodings.ToArray())
         Encoding.Remove(e);
 
-      foreach (var f in StructureTemplate.SubFunctions) {
-        SetupVariables(f);
+      foreach (var subFunction in StructureTemplate.SubFunctions) {
+        subFunction.SetupVariables(ProblemData.AllowedInputVariables);
         // to prevent the same encoding twice
-        if (!Encoding.Encodings.Any(x => x.Name == f.Name)) 
-          Encoding.Add(CreateEncoding(f));
-      }
-
-      ParameterizeEncoding();
+        if (Encoding.Encodings.Any(x => x.Name == subFunction.Name)) continue; // duplicate subfunction
+
+        var encoding = new SymbolicExpressionTreeEncoding(
+          subFunction.Name,
+          subFunction.Grammar,
+          subFunction.MaximumSymbolicExpressionTreeLength,
+          subFunction.MaximumSymbolicExpressionTreeDepth);
+        Encoding.Add(encoding);
+      }
+
+      //set multi manipulator as default manipulator for all encoding parts
+      var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().FirstOrDefault();
+      if (manipulator != null) {
+        foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) {
+          var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault();
+          param.Value = m == null ? param.ValidValues.First() : m;
+        }
+      }
     }
 
@@ -198,5 +205,9 @@
 
     public override double Evaluate(Individual individual, IRandom random) {
-      var tree = BuildTree(individual);
+      var templateTree = StructureTemplate.Tree;
+      if (templateTree == null)
+        throw new ArgumentException("No structure template defined!");
+
+      var tree = BuildTree(templateTree, individual);
 
       // NMSEConstraintsEvaluator sets linear scaling terms itself
@@ -233,74 +244,20 @@
     }
 
-    private ISymbolicExpressionTree BuildTree(Individual individual) {
-      if (StructureTemplate.Tree == null)
-        throw new ArgumentException("No structure template defined!");
-
-      var templateTree = (ISymbolicExpressionTree)StructureTemplate.Tree.Clone();
+    private static ISymbolicExpressionTree BuildTree(ISymbolicExpressionTree template, Individual individual) {
+      var resolvedTree = (ISymbolicExpressionTree)template.Clone();
 
       // build main tree
-      foreach (var subFunctionTreeNode in templateTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
+      foreach (var subFunctionTreeNode in resolvedTree.IterateNodesPrefix().OfType<SubFunctionTreeNode>()) {
         var subFunctionTree = individual.SymbolicExpressionTree(subFunctionTreeNode.Name);
 
-        // add new tree
-        var subTree = subFunctionTree.Root.GetSubtree(0)  // Start
-                                          .GetSubtree(0); // Offset
+        // extract function tree
+        var subTree = subFunctionTree.Root.GetSubtree(0)  // StartSymbol
+                                          .GetSubtree(0); // First Symbol
         subTree = (ISymbolicExpressionTreeNode)subTree.Clone();
         subFunctionTreeNode.AddSubtree(subTree);
-
-      }
-      return templateTree;
-    }
-
-    private SymbolicExpressionTreeEncoding CreateEncoding(SubFunction subFunction) {
-      var encoding = new SymbolicExpressionTreeEncoding(
-            subFunction.Name,
-            subFunction.Grammar,
-            subFunction.MaximumSymbolicExpressionTreeLength,
-            subFunction.MaximumSymbolicExpressionTreeDepth);
-      return encoding;
-    }
-
-    private void ParameterizeEncoding() {
-      var manipulator = (IParameterizedItem)Encoding.Operators.OfType<MultiEncodingManipulator>().FirstOrDefault();
-      if (manipulator != null) {
-        foreach (var param in manipulator.Parameters.OfType<ConstrainedValueParameter<IManipulator>>()) {
-          var m = param.ValidValues.OfType<MultiSymbolicExpressionTreeManipulator>().FirstOrDefault();
-          param.Value = m == null ? param.ValidValues.First() : m;
-        }
-      }
-    }
-    private void SetupVariables(SubFunction subFunction) {
-      var varSym = (Variable)subFunction.Grammar.GetSymbol(VariableName);
-      if (varSym == null) {
-        varSym = new Variable();
-        subFunction.Grammar.AddSymbol(varSym);
-      }
-
-      var allVariables = ProblemData.InputVariables.Select(x => x.Value);
-      var allInputs = allVariables.Where(x => x != ProblemData.TargetVariable);
-
-      // set all variables
-      varSym.AllVariableNames = allVariables;
-
-      // set all allowed variables
-      if (subFunction.Arguments.Contains("_")) {
-        varSym.VariableNames = allInputs;
-      } else {
-        var vars = new List<string>();
-        var exceptions = new List<Exception>();
-        foreach (var arg in subFunction.Arguments) {
-          if (allInputs.Contains(arg))
-            vars.Add(arg);
-          else
-            exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{subFunction.Name}' is not a valid variable."));
-        }
-        if (exceptions.Any())
-          throw new AggregateException(exceptions);
-        varSym.VariableNames = vars;
-      }
-
-      varSym.Enabled = true;
-    }
+      }
+      return resolvedTree;
+    }
+
 
     public void Load(IRegressionProblemData data) {
Index: /branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs
===================================================================
--- /branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs	(revision 18183)
+++ /branches/3136_Structural_GP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/StructureTemplate/SubFunction.cs	(revision 18184)
@@ -2,12 +2,9 @@
 using System.Collections.Generic;
 using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
-using HeuristicLab.Core;
 using HEAL.Attic;
 using HeuristicLab.Common;
-using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
+using HeuristicLab.Core;
+using HeuristicLab.Data;
 using HeuristicLab.Parameters;
-using HeuristicLab.Data;
 
 namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
@@ -65,4 +62,6 @@
     [StorableConstructor]
     protected SubFunction(StorableConstructorFlag _) : base(_) { }
+    public override IDeepCloneable Clone(Cloner cloner) =>
+      new SubFunction(this, cloner);
 
 
@@ -73,8 +72,4 @@
     #endregion
 
-    #region Cloning
-    public override IDeepCloneable Clone(Cloner cloner) =>
-      new SubFunction(this, cloner);
-    #endregion
 
     #region Event Handling
@@ -95,4 +90,32 @@
 
     public override bool Equals(object obj) => (obj is SubFunction other) && other.ToString() == ToString();
+
+    public void SetupVariables(IEnumerable<string> inputVariables) {
+      var allowedInputVariables = new List<string>(inputVariables);
+
+      foreach (var varSym in Grammar.Symbols.OfType<Variable>()) {
+        // set all variables
+        varSym.AllVariableNames = allowedInputVariables;
+
+        // set all allowed variables
+        if (Arguments.Contains("_")) {
+          varSym.VariableNames = allowedInputVariables;
+        } else {
+          var vars = new List<string>();
+          var exceptions = new List<Exception>();
+          foreach (var arg in Arguments) {
+            if (allowedInputVariables.Contains(arg))
+              vars.Add(arg);
+            else
+              exceptions.Add(new ArgumentException($"The argument '{arg}' for sub-function '{Name}' is not a valid variable."));
+          }
+          if (exceptions.Any())
+            throw new AggregateException(exceptions);
+          varSym.VariableNames = vars;
+        }
+
+        varSym.Enabled = true;
+      }
+    }
   }
 }
