Free cookie consent management tool by TermsFeed Policy Generator

Changeset 17597


Ignore:
Timestamp:
06/10/20 17:17:43 (4 years ago)
Author:
pfleck
Message:

#3040: Added simplification rules for length-aggregation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs

    r17596 r17597  
    267267    private static bool IsMean(ISymbolicExpressionTreeNode node) {
    268268      return node.Symbol is Mean;
     269    }
     270
     271    private static bool IsLength(ISymbolicExpressionTreeNode node) {
     272      return node.Symbol is Length;
    269273    }
    270274    #endregion
     
    345349      } else if (IsMean(original)) {
    346350        return SimplifyMeanAggregation(original);
     351      } else if (IsLength(original)) {
     352        return SimplifyLengthAggregation(original);
    347353      } else {
    348354        return SimplifyAny(original);
     
    546552    }
    547553
     554    private ISymbolicExpressionTreeNode SimplifyLengthAggregation(ISymbolicExpressionTreeNode original) {
     555      return MakeLengthAggregation(GetSimplifiedTree(original.GetSubtree(0)));
     556    }
    548557    #endregion
    549558
     
    13671376    }
    13681377
    1369     ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) {
     1378    private ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) {
    13701379      if (IsConstant(node)) { // assumes scalar constant
    13711380        return node;
    13721381      } else if (IsAddition(node) || IsSubtraction(node)) {
    13731382        var terms = node.Subtrees;
    1374         if (IsSubtraction(node)) {
    1375           if (terms.Count() == 1) {
    1376             terms = terms.Select(Negate);
    1377           } else {
    1378             var first = terms.First().ToEnumerable();
    1379             var remaining = terms.Skip(1).Select(Negate);
    1380             terms = first.Concat(remaining).ToList();
    1381           }
    1382         }
     1383        if (IsSubtraction(node)) terms = InvertNodes(terms, Negate);
    13831384
    13841385        var scalarTerms = terms.Where(IsScalarNode).ToList();
     
    13891390          var vectorNode = remainingTerms.Aggregate(MakeSum);
    13901391
    1391           var lengthNode = lengthSymbol.CreateTreeNode();
    1392           lengthNode.AddSubtree((ISymbolicExpressionTreeNode)vectorNode.Clone());
    1393           var scalarMulNode = mulSymbol.CreateTreeNode();
    1394           scalarMulNode.AddSubtree(scalarNode);
    1395           scalarMulNode.AddSubtree(lengthNode);
    1396 
    1397           var sumNode = sumSymbol.CreateTreeNode();
    1398           sumNode.AddSubtree(vectorNode);
     1392          var lengthNode = MakeLengthAggregation((ISymbolicExpressionTreeNode)vectorNode.Clone());
     1393          var scalarMulNode = MakeProduct(scalarNode, lengthNode);
     1394
     1395          var sumNode = MakeSumAggregation(vectorNode);
    13991396
    14001397          return MakeSum(scalarMulNode, sumNode);
     
    14111408      } else if (IsMultiplication(node) || IsDivision(node)) {
    14121409        var factors = node.Subtrees;
    1413         if (IsDivision(node)) {
    1414           if (factors.Count() == 1) {
    1415             factors = factors.Select(Invert);
    1416           } else {
    1417             var first = factors.First().ToEnumerable();
    1418             var remaining = factors.Skip(1).Select(Invert);
    1419             factors = first.Concat(remaining).ToList();
    1420           }
    1421         }
     1410        if (IsDivision(node)) factors = InvertNodes(factors, Invert);
    14221411
    14231412        var scalarFactors = factors.Where(IsScalarNode).ToList();
     
    14281417          var vectorNode = remainingFactors.Aggregate(MakeProduct);
    14291418
    1430           var sumNode = sumSymbol.CreateTreeNode();
    1431           sumNode.AddSubtree(vectorNode);
     1419          var sumNode = MakeSumAggregation(vectorNode);
    14321420
    14331421          return MakeProduct(scalarNode, sumNode);
     
    14421430        } else
    14431431          throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
     1432      } else if (IsVariableBase(node)) { // weight is like multiplication
     1433        var variableNode = (VariableTreeNodeBase)node;
     1434        var weight = variableNode.Weight;
     1435        variableNode.Weight = 1.0;
     1436        var sumNode = sumSymbol.CreateTreeNode();
     1437        sumNode.AddSubtree(node);
     1438        return MakeProduct(MakeConstant(weight), sumNode);
    14441439      } else {
    14451440        var sumNode = sumSymbol.CreateTreeNode();
     
    14491444    }
    14501445
    1451     ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) {
     1446    private ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) {
    14521447      if (IsConstant(node)) { // assumes scalar constant
    14531448        return node;
    14541449      } else if (IsAddition(node) || IsSubtraction(node)) {
    14551450        var terms = node.Subtrees;
    1456         if (IsSubtraction(node)) {
    1457           if (terms.Count() == 1) {
    1458             terms = terms.Select(Negate);
    1459           } else {
    1460             var first = terms.First().ToEnumerable();
    1461             var remaining = terms.Skip(1).Select(Negate);
    1462             terms = first.Concat(remaining).ToList();
    1463           }
    1464         }
     1451        if (IsSubtraction(node)) terms = InvertNodes(terms, Negate);
    14651452
    14661453        var scalarTerms = terms.Where(IsScalarNode).ToList();
     
    14711458          var vectorNode = remainingTerms.Aggregate(MakeSum);
    14721459
    1473           var meanNode = meanSymbol.CreateTreeNode();
    1474           meanNode.AddSubtree(vectorNode);
     1460          var meanNode = MakeMeanAggregation(vectorNode);
    14751461
    14761462          return MakeSum(scalarNode, meanNode);
     
    14871473      } else if (IsMultiplication(node) || IsDivision(node)) {
    14881474        var factors = node.Subtrees;
    1489         if (IsDivision(node)) {
    1490           if (factors.Count() == 1) {
    1491             factors = factors.Select(Invert);
    1492           } else {
    1493             var first = factors.First().ToEnumerable();
    1494             var remaining = factors.Skip(1).Select(Invert);
    1495             factors = first.Concat(remaining).ToList();
    1496           }
    1497         }
     1475        if (IsDivision(node)) factors = InvertNodes(factors, Invert);
    14981476
    14991477        var scalarFactors = factors.Where(IsScalarNode).ToList();
     
    15041482          var vectorNode = remainingFactors.Aggregate(MakeProduct);
    15051483
    1506           var meanNode = meanSymbol.CreateTreeNode();
    1507           meanNode.AddSubtree(vectorNode);
     1484          var meanNode = MakeMeanAggregation(vectorNode);
    15081485
    15091486          return MakeProduct(scalarNode, meanNode);
     
    15181495        } else
    15191496          throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
     1497      } else if (IsVariableBase(node)) { // weight is like multiplication
     1498        var variableNode = (VariableTreeNodeBase)node;
     1499        var weight = variableNode.Weight;
     1500        variableNode.Weight = 1.0;
     1501        var meanNode = meanSymbol.CreateTreeNode();
     1502        meanNode.AddSubtree(node);
     1503        return MakeProduct(MakeConstant(weight), meanNode);
    15201504      } else {
    15211505        var meanNode = meanSymbol.CreateTreeNode();
    15221506        meanNode.AddSubtree(node);
    15231507        return meanNode;
     1508      }
     1509    }
     1510
     1511    private ISymbolicExpressionTreeNode MakeLengthAggregation(ISymbolicExpressionTreeNode node) {
     1512      if (IsConstant(node)) { // assumes scalar constant
     1513        return MakeConstant(1.0);
     1514      } else if (IsAddition(node) || IsSubtraction(node)) {
     1515        var terms = node.Subtrees;
     1516        if (IsSubtraction(node)) terms = InvertNodes(terms, Negate);
     1517
     1518        var scalarTerms = terms.Where(IsScalarNode).ToList();
     1519        var remainingTerms = terms.Except(scalarTerms).ToList();
     1520
     1521        if (remainingTerms.Any()) {
     1522          var vectorNode = remainingTerms.Aggregate(MakeSum);
     1523
     1524          var lengthNode = lengthSymbol.CreateTreeNode();
     1525          lengthNode.AddSubtree(vectorNode);
     1526
     1527          return lengthNode;
     1528        } else if (scalarTerms.Any()) {
     1529          return MakeConstant(1.0);
     1530        } else
     1531          throw new InvalidOperationException("Addition does not contain any terms to simplify.");
     1532      } else if (IsMultiplication(node) || IsDivision(node)) {
     1533        var factors = node.Subtrees;
     1534        if (IsDivision(node)) factors = InvertNodes(factors, Invert);
     1535
     1536        var scalarFactors = factors.Where(IsScalarNode).ToList();
     1537        var remainingFactors = factors.Except(scalarFactors).ToList();
     1538
     1539        if (remainingFactors.Any()) {
     1540          var vectorNode = remainingFactors.Aggregate(MakeProduct);
     1541
     1542          var lengthNode = lengthSymbol.CreateTreeNode();
     1543          lengthNode.AddSubtree(vectorNode);
     1544
     1545          return lengthNode;
     1546        } else if (scalarFactors.Any()) {
     1547          return MakeConstant(1.0);
     1548        } else
     1549          throw new InvalidOperationException("Multiplication does not contain any terms to simplify.");
     1550      } else if (IsVariableBase(node)) { // weight is like multiplication
     1551        var variableNode = (VariableTreeNodeBase)node;
     1552        variableNode.Weight = 1.0;
     1553        var lengthNode = lengthSymbol.CreateTreeNode();
     1554        lengthNode.AddSubtree(node);
     1555        return lengthNode;
     1556      } else {
     1557        var lengthNode = lengthSymbol.CreateTreeNode();
     1558        lengthNode.AddSubtree(node);
     1559        return lengthNode;
    15241560      }
    15251561    }
     
    16781714    }
    16791715
    1680     private ISymbolicExpressionTreeNode MakeConstant(double value) {
     1716    private static ISymbolicExpressionTreeNode MakeConstant(double value) {
    16811717      ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode());
    16821718      constantTreeNode.Value = value;
     
    16841720    }
    16851721
    1686     private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
     1722    private static ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {
    16871723      var tree = (FactorVariableTreeNode)sy.CreateTreeNode();
    16881724      tree.VariableName = variableName;
     
    16901726      return tree;
    16911727    }
    1692     private ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
     1728    private static ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {
    16931729      var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode();
    16941730      tree.VariableName = variableName;
     
    16971733      return tree;
    16981734    }
    1699 
     1735    private static IEnumerable<ISymbolicExpressionTreeNode> InvertNodes(IEnumerable<ISymbolicExpressionTreeNode> nodes, Func<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> invertFunc) {
     1736      if (nodes.Count() == 1)
     1737        return nodes.Select(invertFunc);
     1738      var first = nodes.First().ToEnumerable();
     1739      var remaining = nodes.Skip(1).Select(invertFunc);
     1740      return first.Concat(remaining);
     1741    }
    17001742
    17011743    #endregion
Note: See TracChangeset for help on using the changeset viewer.