Index: branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs
===================================================================
--- branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs	(revision 17596)
+++ branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs	(revision 17597)
@@ -267,4 +267,8 @@
     private static bool IsMean(ISymbolicExpressionTreeNode node) {
       return node.Symbol is Mean;
+    }
+
+    private static bool IsLength(ISymbolicExpressionTreeNode node) {
+      return node.Symbol is Length;
     }
     #endregion
@@ -345,4 +349,6 @@
       } else if (IsMean(original)) {
         return SimplifyMeanAggregation(original);
+      } else if (IsLength(original)) {
+        return SimplifyLengthAggregation(original);
       } else {
         return SimplifyAny(original);
@@ -546,4 +552,7 @@
     }
 
+    private ISymbolicExpressionTreeNode SimplifyLengthAggregation(ISymbolicExpressionTreeNode original) {
+      return MakeLengthAggregation(GetSimplifiedTree(original.GetSubtree(0)));
+    }
     #endregion
 
@@ -1367,18 +1376,10 @@
     }
 
-    ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) {
+    private ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) {
       if (IsConstant(node)) { // assumes scalar constant
         return node;
       } else if (IsAddition(node) || IsSubtraction(node)) {
         var terms = node.Subtrees;
-        if (IsSubtraction(node)) {
-          if (terms.Count() == 1) {
-            terms = terms.Select(Negate);
-          } else {
-            var first = terms.First().ToEnumerable();
-            var remaining = terms.Skip(1).Select(Negate);
-            terms = first.Concat(remaining).ToList();
-          }
-        }
+        if (IsSubtraction(node)) terms = InvertNodes(terms, Negate);
 
         var scalarTerms = terms.Where(IsScalarNode).ToList();
@@ -1389,12 +1390,8 @@
           var vectorNode = remainingTerms.Aggregate(MakeSum);
 
-          var lengthNode = lengthSymbol.CreateTreeNode();
-          lengthNode.AddSubtree((ISymbolicExpressionTreeNode)vectorNode.Clone());
-          var scalarMulNode = mulSymbol.CreateTreeNode();
-          scalarMulNode.AddSubtree(scalarNode);
-          scalarMulNode.AddSubtree(lengthNode);
-
-          var sumNode = sumSymbol.CreateTreeNode();
-          sumNode.AddSubtree(vectorNode);
+          var lengthNode = MakeLengthAggregation((ISymbolicExpressionTreeNode)vectorNode.Clone());
+          var scalarMulNode = MakeProduct(scalarNode, lengthNode);
+
+          var sumNode = MakeSumAggregation(vectorNode);
 
           return MakeSum(scalarMulNode, sumNode);
@@ -1411,13 +1408,5 @@
       } else if (IsMultiplication(node) || IsDivision(node)) {
         var factors = node.Subtrees;
-        if (IsDivision(node)) {
-          if (factors.Count() == 1) {
-            factors = factors.Select(Invert);
-          } else {
-            var first = factors.First().ToEnumerable();
-            var remaining = factors.Skip(1).Select(Invert);
-            factors = first.Concat(remaining).ToList();
-          }
-        }
+        if (IsDivision(node)) factors = InvertNodes(factors, Invert);
 
         var scalarFactors = factors.Where(IsScalarNode).ToList();
@@ -1428,6 +1417,5 @@
           var vectorNode = remainingFactors.Aggregate(MakeProduct);
 
-          var sumNode = sumSymbol.CreateTreeNode();
-          sumNode.AddSubtree(vectorNode);
+          var sumNode = MakeSumAggregation(vectorNode);
 
           return MakeProduct(scalarNode, sumNode);
@@ -1442,4 +1430,11 @@
         } else
           throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
+      } else if (IsVariableBase(node)) { // weight is like multiplication
+        var variableNode = (VariableTreeNodeBase)node;
+        var weight = variableNode.Weight;
+        variableNode.Weight = 1.0;
+        var sumNode = sumSymbol.CreateTreeNode();
+        sumNode.AddSubtree(node);
+        return MakeProduct(MakeConstant(weight), sumNode);
       } else {
         var sumNode = sumSymbol.CreateTreeNode();
@@ -1449,18 +1444,10 @@
     }
 
-    ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) {
+    private ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) {
       if (IsConstant(node)) { // assumes scalar constant
         return node;
       } else if (IsAddition(node) || IsSubtraction(node)) {
         var terms = node.Subtrees;
-        if (IsSubtraction(node)) {
-          if (terms.Count() == 1) {
-            terms = terms.Select(Negate);
-          } else {
-            var first = terms.First().ToEnumerable();
-            var remaining = terms.Skip(1).Select(Negate);
-            terms = first.Concat(remaining).ToList();
-          }
-        }
+        if (IsSubtraction(node)) terms = InvertNodes(terms, Negate);
 
         var scalarTerms = terms.Where(IsScalarNode).ToList();
@@ -1471,6 +1458,5 @@
           var vectorNode = remainingTerms.Aggregate(MakeSum);
 
-          var meanNode = meanSymbol.CreateTreeNode();
-          meanNode.AddSubtree(vectorNode);
+          var meanNode = MakeMeanAggregation(vectorNode);
 
           return MakeSum(scalarNode, meanNode);
@@ -1487,13 +1473,5 @@
       } else if (IsMultiplication(node) || IsDivision(node)) {
         var factors = node.Subtrees;
-        if (IsDivision(node)) {
-          if (factors.Count() == 1) {
-            factors = factors.Select(Invert);
-          } else {
-            var first = factors.First().ToEnumerable();
-            var remaining = factors.Skip(1).Select(Invert);
-            factors = first.Concat(remaining).ToList();
-          }
-        }
+        if (IsDivision(node)) factors = InvertNodes(factors, Invert);
 
         var scalarFactors = factors.Where(IsScalarNode).ToList();
@@ -1504,6 +1482,5 @@
           var vectorNode = remainingFactors.Aggregate(MakeProduct);
 
-          var meanNode = meanSymbol.CreateTreeNode();
-          meanNode.AddSubtree(vectorNode);
+          var meanNode = MakeMeanAggregation(vectorNode);
 
           return MakeProduct(scalarNode, meanNode);
@@ -1518,8 +1495,67 @@
         } else
           throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
+      } else if (IsVariableBase(node)) { // weight is like multiplication
+        var variableNode = (VariableTreeNodeBase)node;
+        var weight = variableNode.Weight;
+        variableNode.Weight = 1.0;
+        var meanNode = meanSymbol.CreateTreeNode();
+        meanNode.AddSubtree(node);
+        return MakeProduct(MakeConstant(weight), meanNode);
       } else {
         var meanNode = meanSymbol.CreateTreeNode();
         meanNode.AddSubtree(node);
         return meanNode;
+      }
+    }
+
+    private ISymbolicExpressionTreeNode MakeLengthAggregation(ISymbolicExpressionTreeNode node) {
+      if (IsConstant(node)) { // assumes scalar constant
+        return MakeConstant(1.0);
+      } else if (IsAddition(node) || IsSubtraction(node)) {
+        var terms = node.Subtrees;
+        if (IsSubtraction(node)) terms = InvertNodes(terms, Negate);
+
+        var scalarTerms = terms.Where(IsScalarNode).ToList();
+        var remainingTerms = terms.Except(scalarTerms).ToList();
+
+        if (remainingTerms.Any()) {
+          var vectorNode = remainingTerms.Aggregate(MakeSum);
+
+          var lengthNode = lengthSymbol.CreateTreeNode();
+          lengthNode.AddSubtree(vectorNode);
+
+          return lengthNode;
+        } else if (scalarTerms.Any()) {
+          return MakeConstant(1.0);
+        } else
+          throw new InvalidOperationException("Addition does not contain any terms to simplify.");
+      } else if (IsMultiplication(node) || IsDivision(node)) {
+        var factors = node.Subtrees;
+        if (IsDivision(node)) factors = InvertNodes(factors, Invert);
+
+        var scalarFactors = factors.Where(IsScalarNode).ToList();
+        var remainingFactors = factors.Except(scalarFactors).ToList();
+
+        if (remainingFactors.Any()) {
+          var vectorNode = remainingFactors.Aggregate(MakeProduct);
+
+          var lengthNode = lengthSymbol.CreateTreeNode();
+          lengthNode.AddSubtree(vectorNode);
+
+          return lengthNode;
+        } else if (scalarFactors.Any()) {
+          return MakeConstant(1.0);
+        } else
+          throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
+      } else if (IsVariableBase(node)) { // weight is like multiplication
+        var variableNode = (VariableTreeNodeBase)node;
+        variableNode.Weight = 1.0;
+        var lengthNode = lengthSymbol.CreateTreeNode();
+        lengthNode.AddSubtree(node);
+        return lengthNode;
+      } else {
+        var lengthNode = lengthSymbol.CreateTreeNode();
+        lengthNode.AddSubtree(node);
+        return lengthNode;
       }
     }
@@ -1678,5 +1714,5 @@
     }
 
-    private ISymbolicExpressionTreeNode MakeConstant(double value) {
+    private static ISymbolicExpressionTreeNode MakeConstant(double value) {
       ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode());
       constantTreeNode.Value = value;
@@ -1684,5 +1720,5 @@
     }
 
-    private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
+    private static ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
       var tree = (FactorVariableTreeNode)sy.CreateTreeNode();
       tree.VariableName = variableName;
@@ -1690,5 +1726,5 @@
       return tree;
     }
-    private ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
+    private static ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
       var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode();
       tree.VariableName = variableName;
@@ -1697,5 +1733,11 @@
       return tree;
     }
-
+    private static IEnumerable<ISymbolicExpressionTreeNode> InvertNodes(IEnumerable<ISymbolicExpressionTreeNode> nodes, Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> invertFunc) {
+      if (nodes.Count() == 1)
+        return nodes.Select(invertFunc);
+      var first = nodes.First().ToEnumerable();
+      var remaining = nodes.Skip(1).Select(invertFunc);
+      return first.Concat(remaining);
+    }
 
     #endregion
