Changeset 17597
- Timestamp:
- 06/10/20 17:17:43 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs
r17596 r17597 267 267 private static bool IsMean(ISymbolicExpressionTreeNode node) { 268 268 return node.Symbol is Mean; 269 } 270 271 private static bool IsLength(ISymbolicExpressionTreeNode node) { 272 return node.Symbol is Length; 269 273 } 270 274 #endregion … … 345 349 } else if (IsMean(original)) { 346 350 return SimplifyMeanAggregation(original); 351 } else if (IsLength(original)) { 352 return SimplifyLengthAggregation(original); 347 353 } else { 348 354 return SimplifyAny(original); … … 546 552 } 547 553 554 private ISymbolicExpressionTreeNode SimplifyLengthAggregation(ISymbolicExpressionTreeNode original) { 555 return MakeLengthAggregation(GetSimplifiedTree(original.GetSubtree(0))); 556 } 548 557 #endregion 549 558 … … 1367 1376 } 1368 1377 1369 ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) {1378 private ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) { 1370 1379 if (IsConstant(node)) { // assumes scalar constant 1371 1380 return node; 1372 1381 } else if (IsAddition(node) || IsSubtraction(node)) { 1373 1382 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); 1383 1384 1384 1385 var scalarTerms = terms.Where(IsScalarNode).ToList(); … … 1389 1390 var vectorNode = remainingTerms.Aggregate(MakeSum); 1390 1391 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); 1399 1396 1400 1397 return MakeSum(scalarMulNode, sumNode); … … 1411 1408 } else if (IsMultiplication(node) || IsDivision(node)) { 1412 1409 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); 1422 1411 1423 1412 var scalarFactors = factors.Where(IsScalarNode).ToList(); … … 1428 1417 var vectorNode = remainingFactors.Aggregate(MakeProduct); 1429 1418 1430 var sumNode = sumSymbol.CreateTreeNode(); 1431 sumNode.AddSubtree(vectorNode); 1419 var sumNode = MakeSumAggregation(vectorNode); 1432 1420 1433 1421 return MakeProduct(scalarNode, sumNode); … … 1442 1430 } else 1443 1431 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); 1444 1439 } else { 1445 1440 var sumNode = sumSymbol.CreateTreeNode(); … … 1449 1444 } 1450 1445 1451 ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) {1446 private ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) { 1452 1447 if (IsConstant(node)) { // assumes scalar constant 1453 1448 return node; 1454 1449 } else if (IsAddition(node) || IsSubtraction(node)) { 1455 1450 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); 1465 1452 1466 1453 var scalarTerms = terms.Where(IsScalarNode).ToList(); … … 1471 1458 var vectorNode = remainingTerms.Aggregate(MakeSum); 1472 1459 1473 var meanNode = meanSymbol.CreateTreeNode(); 1474 meanNode.AddSubtree(vectorNode); 1460 var meanNode = MakeMeanAggregation(vectorNode); 1475 1461 1476 1462 return MakeSum(scalarNode, meanNode); … … 1487 1473 } else if (IsMultiplication(node) || IsDivision(node)) { 1488 1474 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); 1498 1476 1499 1477 var scalarFactors = factors.Where(IsScalarNode).ToList(); … … 1504 1482 var vectorNode = remainingFactors.Aggregate(MakeProduct); 1505 1483 1506 var meanNode = meanSymbol.CreateTreeNode(); 1507 meanNode.AddSubtree(vectorNode); 1484 var meanNode = MakeMeanAggregation(vectorNode); 1508 1485 1509 1486 return MakeProduct(scalarNode, meanNode); … … 1518 1495 } else 1519 1496 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); 1520 1504 } else { 1521 1505 var meanNode = meanSymbol.CreateTreeNode(); 1522 1506 meanNode.AddSubtree(node); 1523 1507 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; 1524 1560 } 1525 1561 } … … 1678 1714 } 1679 1715 1680 private ISymbolicExpressionTreeNode MakeConstant(double value) {1716 private static ISymbolicExpressionTreeNode MakeConstant(double value) { 1681 1717 ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode()); 1682 1718 constantTreeNode.Value = value; … … 1684 1720 } 1685 1721 1686 private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {1722 private static ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) { 1687 1723 var tree = (FactorVariableTreeNode)sy.CreateTreeNode(); 1688 1724 tree.VariableName = variableName; … … 1690 1726 return tree; 1691 1727 } 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) { 1693 1729 var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode(); 1694 1730 tree.VariableName = variableName; … … 1697 1733 return tree; 1698 1734 } 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 } 1700 1742 1701 1743 #endregion
Note: See TracChangeset
for help on using the changeset viewer.