Changeset 17593 for branches/3040_VectorBasedGP
- Timestamp:
- 06/10/20 11:31:32 (4 years ago)
- Location:
- branches/3040_VectorBasedGP
- Files:
-
- 3 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic.Views/3.4/InteractiveSymbolicDataAnalysisSolutionSimplifierView.cs
r17472 r17593 127 127 if (valid) { 128 128 // check if all variables are contained in the dataset 129 var variables = new HashSet<string>(Content.ProblemData.Dataset. DoubleVariables);129 var variables = new HashSet<string>(Content.ProblemData.Dataset.VariableNames); 130 130 valid = nodes.OfType<VariableTreeNode>().All(x => variables.Contains(x.VariableName)); 131 131 } … … 314 314 315 315 private void btnSimplify_Click(object sender, EventArgs e) { 316 var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree); 317 UpdateModel(simplifiedExpressionTree); 316 if (Content.Model.Interpreter is SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter) { // TODO: Own interface for data-dependent node-types-interpreter? 317 var simplifier = new VectorTreeSimplifier(interpreter, Content.ProblemData); 318 var simplifiedExpressionTree = simplifier.Simplify(Content.Model.SymbolicExpressionTree); 319 UpdateModel(simplifiedExpressionTree); 320 } else { 321 var simplifiedExpressionTree = TreeSimplifier.Simplify(Content.Model.SymbolicExpressionTree); 322 UpdateModel(simplifiedExpressionTree); 323 } 318 324 } 319 325 -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/VectorTreeSimplifier.cs
r17592 r17593 32 32 /// Simplifier for symbolic expressions 33 33 /// </summary> 34 public class TreeSimplifier {34 public class VectorTreeSimplifier { 35 35 private static readonly Addition addSymbol = new Addition(); 36 36 private static readonly Multiplication mulSymbol = new Multiplication(); … … 59 59 private static readonly LaggedVariable laggedVariableSymbol = new LaggedVariable(); 60 60 private static readonly TimeLag timeLagSymbol = new TimeLag(); 61 62 [Obsolete("Use static method TreeSimplifier.Simplify instead")] 63 public TreeSimplifier() { } 64 65 public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) { 61 private static readonly Sum sumSymbol = new Sum(); 62 private static readonly Mean meanSymbol = new Mean(); 63 private static readonly Length lengthSymbol = new Length(); 64 65 private readonly SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter; 66 private readonly IDataAnalysisProblemData problemData; 67 68 public VectorTreeSimplifier(SymbolicDataAnalysisExpressionTreeVectorInterpreter interpreter, IDataAnalysisProblemData problemData) { 69 this.interpreter = interpreter; 70 this.problemData = problemData; 71 } 72 73 public ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) { 66 74 var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone(); 67 75 // macro expand (initially no argument trees) … … 253 261 } 254 262 263 private static bool IsSum(ISymbolicExpressionTreeNode node) { 264 return node.Symbol is Sum; 265 } 266 267 private static bool IsMean(ISymbolicExpressionTreeNode node) { 268 return node.Symbol is Mean; 269 } 270 #endregion 271 272 #region type predicates 273 public bool IsScalarNode(ISymbolicExpressionTreeNode node) { 274 var results = interpreter.EvaluateNode(node, problemData.Dataset, problemData.TrainingIndices); 275 return results.All(r => r.IsScalar); 276 } 277 public bool IsVectorNode(ISymbolicExpressionTreeNode node) { 278 var results = interpreter.EvaluateNode(node, problemData.Dataset, problemData.TrainingIndices); 279 return results.All(r => r.IsVector); 280 } 255 281 #endregion 256 282 … … 260 286 /// <param name="original"></param> 261 287 /// <returns></returns> 262 public staticISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {288 public ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) { 263 289 if (IsConstant(original) || IsVariableBase(original)) { 264 290 return (ISymbolicExpressionTreeNode)original.Clone(); … … 315 341 } else if (IsIntegral(original)) { 316 342 return SimplifyIntegral(original); 343 } else if (IsSum(original)) { 344 return SimplifySumAggregation(original); 345 } else if (IsMean(original)) { 346 return SimplifyMeanAggregation(original); 317 347 } else { 318 348 return SimplifyAny(original); … … 322 352 #region specific simplification routines 323 353 324 private staticISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {354 private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) { 325 355 // can't simplify this function but simplify all subtrees 326 356 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees); … … 341 371 } 342 372 343 private staticISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {373 private ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) { 344 374 // not yet implemented 345 375 return original; 346 376 } 347 377 348 private staticISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {378 private ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) { 349 379 if (original.Subtrees.Count() == 1) { 350 380 return GetSimplifiedTree(original.GetSubtree(0)); … … 359 389 } 360 390 361 private staticISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {391 private ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) { 362 392 if (original.Subtrees.Count() == 1) { 363 393 return Invert(GetSimplifiedTree(original.GetSubtree(0))); … … 374 404 } 375 405 376 private staticISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {406 private ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) { 377 407 if (original.Subtrees.Count() == 1) { 378 408 return GetSimplifiedTree(original.GetSubtree(0)); … … 384 414 } 385 415 386 private staticISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {416 private ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) { 387 417 if (original.Subtrees.Count() == 1) { 388 418 return Negate(GetSimplifiedTree(original.GetSubtree(0))); … … 396 426 } 397 427 398 private staticISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {428 private ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) { 399 429 if (original.Subtrees.Count() == 1) { 400 430 return GetSimplifiedTree(original.GetSubtree(0)); … … 408 438 } 409 439 410 private staticISymbolicExpressionTreeNode SimplifyAbsolute(ISymbolicExpressionTreeNode original) {440 private ISymbolicExpressionTreeNode SimplifyAbsolute(ISymbolicExpressionTreeNode original) { 411 441 return MakeAbs(GetSimplifiedTree(original.GetSubtree(0))); 412 442 } 413 443 414 private staticISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {444 private ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) { 415 445 return MakeNot(GetSimplifiedTree(original.GetSubtree(0))); 416 446 } 417 447 418 private staticISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {448 private ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) { 419 449 return original.Subtrees 420 450 .Select(GetSimplifiedTree) … … 422 452 } 423 453 424 private staticISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {454 private ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) { 425 455 return original.Subtrees 426 456 .Select(GetSimplifiedTree) … … 428 458 } 429 459 430 private staticISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {460 private ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) { 431 461 return MakeLessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 432 462 } 433 463 434 private staticISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {464 private ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) { 435 465 return MakeGreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 436 466 } 437 467 438 private staticISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {468 private ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) { 439 469 return MakeIfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)), 440 470 GetSimplifiedTree(original.GetSubtree(2))); 441 471 } 442 472 443 private staticISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {473 private ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) { 444 474 return MakeTangent(GetSimplifiedTree(original.GetSubtree(0))); 445 475 } 446 476 447 private staticISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {477 private ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) { 448 478 return MakeCosine(GetSimplifiedTree(original.GetSubtree(0))); 449 479 } 450 480 451 private staticISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {481 private ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) { 452 482 return MakeSine(GetSimplifiedTree(original.GetSubtree(0))); 453 483 } 454 484 455 private staticISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {485 private ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) { 456 486 return MakeExp(GetSimplifiedTree(original.GetSubtree(0))); 457 487 } 458 488 459 private staticISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {489 private ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) { 460 490 return MakeSquare(GetSimplifiedTree(original.GetSubtree(0))); 461 491 } 462 492 463 private staticISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {493 private ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) { 464 494 return MakeSquareRoot(GetSimplifiedTree(original.GetSubtree(0))); 465 495 } 466 private staticISymbolicExpressionTreeNode SimplifyCube(ISymbolicExpressionTreeNode original) {496 private ISymbolicExpressionTreeNode SimplifyCube(ISymbolicExpressionTreeNode original) { 467 497 return MakeCube(GetSimplifiedTree(original.GetSubtree(0))); 468 498 } 469 499 470 private staticISymbolicExpressionTreeNode SimplifyCubeRoot(ISymbolicExpressionTreeNode original) {500 private ISymbolicExpressionTreeNode SimplifyCubeRoot(ISymbolicExpressionTreeNode original) { 471 501 return MakeCubeRoot(GetSimplifiedTree(original.GetSubtree(0))); 472 502 } 473 503 474 private staticISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {504 private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) { 475 505 return MakeLog(GetSimplifiedTree(original.GetSubtree(0))); 476 506 } 477 507 478 private staticISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {508 private ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) { 479 509 return MakeRoot(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 480 510 } 481 511 482 private staticISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {512 private ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) { 483 513 return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 484 514 } 485 515 486 private staticISymbolicExpressionTreeNode SimplifyAnalyticalQuotient(ISymbolicExpressionTreeNode original) {516 private ISymbolicExpressionTreeNode SimplifyAnalyticalQuotient(ISymbolicExpressionTreeNode original) { 487 517 return MakeAnalyticalQuotient(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 488 518 } 489 519 490 private staticISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {520 private ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) { 491 521 var laggedTreeNode = original as ILaggedTreeNode; 492 522 var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0)); … … 498 528 } 499 529 500 private staticISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {530 private ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) { 501 531 var laggedTreeNode = original as ILaggedTreeNode; 502 532 var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0)); … … 508 538 } 509 539 540 private ISymbolicExpressionTreeNode SimplifySumAggregation(ISymbolicExpressionTreeNode original) { 541 return MakeSumAggregation(GetSimplifiedTree(original.GetSubtree(0))); 542 } 543 544 private ISymbolicExpressionTreeNode SimplifyMeanAggregation(ISymbolicExpressionTreeNode original) { 545 return MakeMeanAggregation(GetSimplifiedTree(original.GetSubtree(0))); 546 } 547 510 548 #endregion 511 549 512 550 #region low level tree restructuring 513 551 514 private staticISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {552 private ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) { 515 553 if (lag == 0) return subtree; 516 554 if (IsConstant(subtree)) return subtree; … … 521 559 } 522 560 523 private staticISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {561 private ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) { 524 562 if (lag == 0) return subtree; 525 563 else if (lag == -1 || lag == 1) { … … 533 571 } 534 572 535 private staticISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {573 private ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) { 536 574 if (IsConstant(t)) { 537 575 var constNode = t as ConstantTreeNode; … … 554 592 } 555 593 556 private staticISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {594 private ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 557 595 if (IsConstant(a) && IsConstant(b)) { 558 596 var constA = a as ConstantTreeNode; … … 584 622 } 585 623 586 private staticISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {624 private ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 587 625 if (IsConstant(a) && IsConstant(b)) { 588 626 var constA = a as ConstantTreeNode; … … 614 652 } 615 653 616 private staticISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,654 private ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide, 617 655 ISymbolicExpressionTreeNode rightSide) { 618 656 if (IsConstant(leftSide) && IsConstant(rightSide)) { … … 629 667 } 630 668 631 private staticISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,669 private ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide, 632 670 ISymbolicExpressionTreeNode rightSide) { 633 671 if (IsConstant(leftSide) && IsConstant(rightSide)) { … … 644 682 } 645 683 646 private staticISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,684 private ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition, 647 685 ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) { 648 686 if (IsConstant(condition)) { … … 666 704 } 667 705 668 private staticISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {706 private ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) { 669 707 if (IsConstant(node)) { 670 708 var constT = node as ConstantTreeNode; … … 683 721 } 684 722 685 private staticISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {723 private ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) { 686 724 if (IsConstant(node)) { 687 725 var constT = node as ConstantTreeNode; … … 700 738 } 701 739 702 private staticISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {740 private ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) { 703 741 if (IsConstant(node)) { 704 742 var constT = node as ConstantTreeNode; … … 719 757 } 720 758 721 private staticISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {759 private ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) { 722 760 if (IsConstant(node)) { 723 761 var constT = node as ConstantTreeNode; … … 743 781 } 744 782 } 745 private staticISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {783 private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) { 746 784 if (IsConstant(node)) { 747 785 var constT = node as ConstantTreeNode; … … 761 799 } 762 800 763 private staticISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {801 private ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) { 764 802 if (IsConstant(node)) { 765 803 var constT = node as ConstantTreeNode; … … 793 831 } 794 832 795 private staticISymbolicExpressionTreeNode MakeCube(ISymbolicExpressionTreeNode node) {833 private ISymbolicExpressionTreeNode MakeCube(ISymbolicExpressionTreeNode node) { 796 834 if (IsConstant(node)) { 797 835 var constT = node as ConstantTreeNode; … … 816 854 } 817 855 818 private staticISymbolicExpressionTreeNode MakeAbs(ISymbolicExpressionTreeNode node) {856 private ISymbolicExpressionTreeNode MakeAbs(ISymbolicExpressionTreeNode node) { 819 857 if (IsConstant(node)) { 820 858 var constT = node as ConstantTreeNode; … … 848 886 849 887 // constant folding only 850 private staticISymbolicExpressionTreeNode MakeAnalyticalQuotient(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {888 private ISymbolicExpressionTreeNode MakeAnalyticalQuotient(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 851 889 if (IsConstant(b)) { 852 890 var c = b as ConstantTreeNode; … … 866 904 } 867 905 868 private staticISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {906 private ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) { 869 907 if (IsConstant(node)) { 870 908 var constT = node as ConstantTreeNode; … … 885 923 } 886 924 887 private staticISymbolicExpressionTreeNode MakeCubeRoot(ISymbolicExpressionTreeNode node) {925 private ISymbolicExpressionTreeNode MakeCubeRoot(ISymbolicExpressionTreeNode node) { 888 926 if (IsConstant(node)) { 889 927 var constT = node as ConstantTreeNode; … … 904 942 } 905 943 906 private staticISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {944 private ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 907 945 if (IsConstant(a) && IsConstant(b)) { 908 946 var constA = a as ConstantTreeNode; … … 959 997 960 998 961 private staticISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {999 private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 962 1000 if (IsConstant(a) && IsConstant(b)) { 963 1001 var constA = a as ConstantTreeNode; … … 1014 1052 1015 1053 // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree 1016 private staticISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {1054 private ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 1017 1055 if (IsConstant(a) && IsConstant(b)) { 1018 1056 // fold constants … … 1085 1123 } 1086 1124 1087 private staticISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {1125 private ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 1088 1126 if (IsConstant(a) && IsConstant(b)) { 1089 1127 // fold constants … … 1180 1218 1181 1219 // makes sure variable symbols in sums are combined 1182 private staticvoid MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {1220 private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) { 1183 1221 var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees); 1184 1222 while (sum.Subtrees.Any()) sum.RemoveSubtree(0); … … 1216 1254 1217 1255 // nodes referencing variables can be grouped if they have 1218 private st atic string GroupId(IVariableTreeNode node) {1256 private string GroupId(IVariableTreeNode node) { 1219 1257 var binaryFactorNode = node as BinaryFactorVariableTreeNode; 1220 1258 var factorNode = node as FactorVariableTreeNode; … … 1235 1273 1236 1274 1237 private staticISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {1275 private ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 1238 1276 if (IsConstant(a) && IsConstant(b)) { 1239 1277 // fold constants … … 1329 1367 } 1330 1368 1369 ISymbolicExpressionTreeNode MakeSumAggregation(ISymbolicExpressionTreeNode node) { 1370 if (IsConstant(node)) { // assumes scalar constant 1371 return node; 1372 } else if (IsAddition(node)) { 1373 var terms = node.Subtrees; 1374 var scalarTerms = terms.Where(IsScalarNode).ToList(); 1375 var remainingTerms = terms.Except(scalarTerms).ToList(); 1376 1377 if (scalarTerms.Any() && remainingTerms.Any()) { 1378 var scalarNode = scalarTerms.Aggregate(MakeSum); 1379 var vectorNode = remainingTerms.Aggregate(MakeSum); 1380 1381 var lengthNode = lengthSymbol.CreateTreeNode(); 1382 lengthNode.AddSubtree((ISymbolicExpressionTreeNode)vectorNode.Clone()); 1383 var scalarMulNode = mulSymbol.CreateTreeNode(); 1384 scalarMulNode.AddSubtree(scalarNode); 1385 scalarMulNode.AddSubtree(lengthNode); 1386 1387 var sumNode = sumSymbol.CreateTreeNode(); 1388 sumNode.AddSubtree(vectorNode); 1389 1390 return MakeSum(scalarMulNode, sumNode); 1391 } else if (scalarTerms.Any()) { 1392 var scalarNode = scalarTerms.Aggregate(MakeSum); 1393 return scalarNode; 1394 } else if (remainingTerms.Any()) { 1395 var vectorNode = remainingTerms.Aggregate(MakeSum); 1396 var sumNode = sumSymbol.CreateTreeNode(); 1397 sumNode.AddSubtree(vectorNode); 1398 return sumNode; 1399 } else 1400 throw new InvalidOperationException("Addition does not contain any terms to simplify."); 1401 } else if (IsMultiplication(node)) { 1402 var factors = node.Subtrees; 1403 var scalarFactors = factors.Where(IsScalarNode).ToList(); 1404 var remainingFactors = factors.Except(scalarFactors).ToList(); 1405 1406 if (scalarFactors.Any() && remainingFactors.Any()) { 1407 var scalarNode = scalarFactors.Aggregate(MakeProduct); 1408 var vectorNode = remainingFactors.Aggregate(MakeProduct); 1409 1410 var sumNode = sumSymbol.CreateTreeNode(); 1411 sumNode.AddSubtree(vectorNode); 1412 1413 return MakeProduct(scalarNode, sumNode); 1414 } else if (scalarFactors.Any()) { 1415 var scalarNode = scalarFactors.Aggregate(MakeProduct); 1416 return scalarNode; 1417 } else if (remainingFactors.Any()) { 1418 var vectorNode = remainingFactors.Aggregate(MakeProduct); 1419 var sumNode = sumSymbol.CreateTreeNode(); 1420 sumNode.AddSubtree(vectorNode); 1421 return sumNode; 1422 } else 1423 throw new InvalidOperationException("Multiplication does not contain any terms to simplify."); 1424 } else { 1425 var sumNode = sumSymbol.CreateTreeNode(); 1426 sumNode.AddSubtree(node); 1427 return sumNode; 1428 } 1429 } 1430 1431 ISymbolicExpressionTreeNode MakeMeanAggregation(ISymbolicExpressionTreeNode node) { 1432 if (IsConstant(node)) { // assumes scalar constant 1433 return node; 1434 } else if (IsAddition(node)) { 1435 var terms = node.Subtrees; 1436 var scalarTerms = terms.Where(IsScalarNode).ToList(); 1437 var remainingTerms = terms.Except(scalarTerms).ToList(); 1438 1439 if (scalarTerms.Any() && remainingTerms.Any()) { 1440 var scalarNode = scalarTerms.Aggregate(MakeSum); 1441 var vectorNode = remainingTerms.Aggregate(MakeSum); 1442 1443 var meanNode = meanSymbol.CreateTreeNode(); 1444 meanNode.AddSubtree(vectorNode); 1445 1446 return MakeSum(scalarNode, meanNode); 1447 } else if (scalarTerms.Any()) { 1448 var scalarNode = scalarTerms.Aggregate(MakeSum); 1449 return scalarNode; 1450 } else if (remainingTerms.Any()) { 1451 var vectorNode = remainingTerms.Aggregate(MakeSum); 1452 var meanNode = meanSymbol.CreateTreeNode(); 1453 meanNode.AddSubtree(vectorNode); 1454 return meanNode; 1455 } else 1456 throw new InvalidOperationException("Addition does not contain any terms to simplify."); 1457 } else if (IsMultiplication(node)) { 1458 var factors = node.Subtrees; 1459 var scalarFactors = factors.Where(IsScalarNode).ToList(); 1460 var remainingFactors = factors.Except(scalarFactors).ToList(); 1461 1462 if (scalarFactors.Any() && remainingFactors.Any()) { 1463 var scalarNode = scalarFactors.Aggregate(MakeProduct); 1464 var vectorNode = remainingFactors.Aggregate(MakeProduct); 1465 1466 var meanNode = meanSymbol.CreateTreeNode(); 1467 meanNode.AddSubtree(vectorNode); 1468 1469 return MakeProduct(scalarNode, meanNode); 1470 } else if (scalarFactors.Any()) { 1471 var scalarNode = scalarFactors.Aggregate(MakeProduct); 1472 return scalarNode; 1473 } else if (remainingFactors.Any()) { 1474 var vectorNode = remainingFactors.Aggregate(MakeProduct); 1475 var meanNode = meanSymbol.CreateTreeNode(); 1476 meanNode.AddSubtree(vectorNode); 1477 return meanNode; 1478 } else 1479 throw new InvalidOperationException("Multiplication does not contain any terms to simplify."); 1480 } else { 1481 var meanNode = meanSymbol.CreateTreeNode(); 1482 meanNode.AddSubtree(node); 1483 return meanNode; 1484 } 1485 } 1486 1331 1487 #endregion 1332 1488 1333 1489 #region helper functions 1334 1490 1335 private staticbool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {1491 private bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) { 1336 1492 if (node.Symbol is VariableCondition) return true; 1337 1493 foreach (var subtree in node.Subtrees) … … 1340 1496 } 1341 1497 1342 private staticISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {1498 private ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) { 1343 1499 var laggedTreeNode = node as ILaggedTreeNode; 1344 1500 var variableNode = node as VariableTreeNode; … … 1362 1518 } 1363 1519 1364 private staticbool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {1520 private bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 1365 1521 return GroupId((IVariableTreeNode)a) == GroupId((IVariableTreeNode)b); 1366 1522 } 1367 1523 1368 1524 // helper to combine the constant factors in products and to combine variables (powers of 2, 3...) 1369 private staticvoid MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {1525 private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) { 1370 1526 var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees); 1371 1527 while (prod.Subtrees.Any()) prod.RemoveSubtree(0); … … 1431 1587 /// <param name="x"></param> 1432 1588 /// <returns>-x</returns> 1433 private staticISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {1589 private ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) { 1434 1590 if (IsConstant(x)) { 1435 1591 ((ConstantTreeNode)x).Value *= -1; … … 1468 1624 /// <param name="x"></param> 1469 1625 /// <returns></returns> 1470 private staticISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {1626 private ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) { 1471 1627 if (IsConstant(x)) { 1472 1628 return MakeConstant(1.0 / ((ConstantTreeNode)x).Value); … … 1482 1638 } 1483 1639 1484 private staticISymbolicExpressionTreeNode MakeConstant(double value) {1640 private ISymbolicExpressionTreeNode MakeConstant(double value) { 1485 1641 ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode()); 1486 1642 constantTreeNode.Value = value; … … 1488 1644 } 1489 1645 1490 private staticISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {1646 private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) { 1491 1647 var tree = (FactorVariableTreeNode)sy.CreateTreeNode(); 1492 1648 tree.VariableName = variableName; … … 1494 1650 return tree; 1495 1651 } 1496 private staticISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {1652 private ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) { 1497 1653 var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode(); 1498 1654 tree.VariableName = variableName; -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r17573 r17593 161 161 </Compile> 162 162 <Compile Include="Converters\LinearModelToTreeConverter.cs" /> 163 <Compile Include="Converters\VectorTreeSimplifier.cs" /> 163 164 <Compile Include="Converters\TreeSimplifier.cs" /> 164 165 <Compile Include="Converters\DerivativeCalculator.cs" /> -
branches/3040_VectorBasedGP/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Interpreter/SymbolicDataAnalysisExpressionTreeVectorInterpreter.cs
r17573 r17593 254 254 } 255 255 256 public virtual IEnumerable<EvaluationResult> EvaluateNode(ISymbolicExpressionTreeNode node, IDataset dataset, IEnumerable<int> rows) { 257 //lock (syncRoot) { 258 // EvaluatedSolutions++; // increment the evaluated solutions counter 259 //} 260 261 var startNode = new StartSymbol().CreateTreeNode(); 262 startNode.AddSubtree(node); 263 var programNode = new ProgramRootSymbol().CreateTreeNode(); 264 programNode.AddSubtree(startNode); 265 var tree = new SymbolicExpressionTree(programNode); 266 267 var state = PrepareInterpreterState(tree, dataset); 268 269 foreach (var rowEnum in rows) { 270 int row = rowEnum; 271 var result = Evaluate(dataset, ref row, state); 272 yield return result; 273 state.Reset(); 274 } 275 } 276 256 277 public virtual EvaluationResult Evaluate(IDataset dataset, ref int row, InterpreterState state) { 257 278 Instruction currentInstr = state.NextInstruction(); … … 398 419 } 399 420 case OpCodes.Length: { 400 var cur = Evaluate(dataset, ref row, state);401 return AggregateApply(cur,402 s => 1,403 v => v.Count);404 }421 var cur = Evaluate(dataset, ref row, state); 422 return AggregateApply(cur, 423 s => 1, 424 v => v.Count); 425 } 405 426 case OpCodes.Min: { 406 var cur = Evaluate(dataset, ref row, state);407 return AggregateApply(cur,408 s => s,409 v => Statistics.Minimum(v));410 }427 var cur = Evaluate(dataset, ref row, state); 428 return AggregateApply(cur, 429 s => s, 430 v => Statistics.Minimum(v)); 431 } 411 432 case OpCodes.Max: { 412 var cur = Evaluate(dataset, ref row, state);413 return AggregateApply(cur,414 s => s,415 v => Statistics.Maximum(v));416 }433 var cur = Evaluate(dataset, ref row, state); 434 return AggregateApply(cur, 435 s => s, 436 v => Statistics.Maximum(v)); 437 } 417 438 case OpCodes.Variance: { 418 var cur = Evaluate(dataset, ref row, state);419 return AggregateApply(cur,420 s => double.NaN,421 v => Statistics.Variance(v));422 }439 var cur = Evaluate(dataset, ref row, state); 440 return AggregateApply(cur, 441 s => double.NaN, 442 v => Statistics.Variance(v)); 443 } 423 444 case OpCodes.Skewness: { 424 var cur = Evaluate(dataset, ref row, state);425 return AggregateApply(cur,426 s => double.NaN,427 v => Statistics.Skewness(v));428 }445 var cur = Evaluate(dataset, ref row, state); 446 return AggregateApply(cur, 447 s => double.NaN, 448 v => Statistics.Skewness(v)); 449 } 429 450 case OpCodes.Kurtosis: { 430 var cur = Evaluate(dataset, ref row, state);431 return AggregateApply(cur,432 s => double.NaN,433 v => Statistics.Kurtosis(v));434 }451 var cur = Evaluate(dataset, ref row, state); 452 return AggregateApply(cur, 453 s => double.NaN, 454 v => Statistics.Kurtosis(v)); 455 } 435 456 case OpCodes.EuclideanDistance: { 436 var x1 = Evaluate(dataset, ref row, state);437 var x2 = Evaluate(dataset, ref row, state);438 return AggregateMultipleApply(x1, x2,439 //(s1, s2) => s1 - s2,440 //(s1, v2) => Math.Sqrt((s1 - v2).PointwisePower(2).Sum()),441 //(v1, s2) => Math.Sqrt((v1 - s2).PointwisePower(2).Sum()),442 vvFunc: (v1, v2) => v1.Count == v2.Count ? Math.Sqrt((v1 - v2).PointwisePower(2).Sum()) : double.NaN);443 }457 var x1 = Evaluate(dataset, ref row, state); 458 var x2 = Evaluate(dataset, ref row, state); 459 return AggregateMultipleApply(x1, x2, 460 //(s1, s2) => s1 - s2, 461 //(s1, v2) => Math.Sqrt((s1 - v2).PointwisePower(2).Sum()), 462 //(v1, s2) => Math.Sqrt((v1 - s2).PointwisePower(2).Sum()), 463 vvFunc: (v1, v2) => v1.Count == v2.Count ? Math.Sqrt((v1 - v2).PointwisePower(2).Sum()) : double.NaN); 464 } 444 465 case OpCodes.Covariance: { 445 var x1 = Evaluate(dataset, ref row, state);446 var x2 = Evaluate(dataset, ref row, state);447 return AggregateMultipleApply(x1, x2,448 //(s1, s2) => 0,449 //(s1, v2) => 0,450 //(v1, s2) => 0,451 vvFunc: (v1, v2) => v1.Count == v2.Count ? Statistics.Covariance(v1, v2) : double.NaN);452 }466 var x1 = Evaluate(dataset, ref row, state); 467 var x2 = Evaluate(dataset, ref row, state); 468 return AggregateMultipleApply(x1, x2, 469 //(s1, s2) => 0, 470 //(s1, v2) => 0, 471 //(v1, s2) => 0, 472 vvFunc: (v1, v2) => v1.Count == v2.Count ? Statistics.Covariance(v1, v2) : double.NaN); 473 } 453 474 case OpCodes.Variable: { 454 475 if (row < 0 || row >= dataset.Rows) return EvaluationResult.NaN;
Note: See TracChangeset
for help on using the changeset viewer.