- Timestamp:
- 05/09/17 19:54:31 (8 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Converters/TreeSimplifier.cs
r14938 r14949 33 33 /// </summary> 34 34 public class TreeSimplifier { 35 private readonly Addition addSymbol = new Addition(); 36 private readonly Multiplication mulSymbol = new Multiplication(); 37 private readonly Division divSymbol = new Division(); 38 private readonly Constant constSymbol = new Constant(); 39 private readonly Logarithm logSymbol = new Logarithm(); 40 private readonly Exponential expSymbol = new Exponential(); 41 private readonly Root rootSymbol = new Root(); 42 private readonly Square sqrSymbol = new Square(); 43 private readonly SquareRoot sqrtSymbol = new SquareRoot(); 44 private readonly Power powSymbol = new Power(); 45 private readonly Sine sineSymbol = new Sine(); 46 private readonly Cosine cosineSymbol = new Cosine(); 47 private readonly Tangent tanSymbol = new Tangent(); 48 private readonly IfThenElse ifThenElseSymbol = new IfThenElse(); 49 private readonly And andSymbol = new And(); 50 private readonly Or orSymbol = new Or(); 51 private readonly Not notSymbol = new Not(); 52 private readonly GreaterThan gtSymbol = new GreaterThan(); 53 private readonly LessThan ltSymbol = new LessThan(); 54 private readonly Integral integralSymbol = new Integral(); 55 private readonly LaggedVariable laggedVariableSymbol = new LaggedVariable(); 56 private readonly TimeLag timeLagSymbol = new TimeLag(); 57 58 public ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) { 35 private static readonly Addition addSymbol = new Addition(); 36 private static readonly Multiplication mulSymbol = new Multiplication(); 37 private static readonly Division divSymbol = new Division(); 38 private static readonly Constant constSymbol = new Constant(); 39 private static readonly Logarithm logSymbol = new Logarithm(); 40 private static readonly Exponential expSymbol = new Exponential(); 41 private static readonly Root rootSymbol = new Root(); 42 private static readonly Square sqrSymbol = new Square(); 43 private static readonly SquareRoot sqrtSymbol = new SquareRoot(); 44 private static readonly Power powSymbol = new Power(); 45 private static readonly Sine sineSymbol = new Sine(); 46 private static readonly Cosine cosineSymbol = new Cosine(); 47 private static readonly Tangent tanSymbol = new Tangent(); 48 private static readonly IfThenElse ifThenElseSymbol = new IfThenElse(); 49 private static readonly And andSymbol = new And(); 50 private static readonly Or orSymbol = new Or(); 51 private static readonly Not notSymbol = new Not(); 52 private static readonly GreaterThan gtSymbol = new GreaterThan(); 53 private static readonly LessThan ltSymbol = new LessThan(); 54 private static readonly Integral integralSymbol = new Integral(); 55 private static readonly LaggedVariable laggedVariableSymbol = new LaggedVariable(); 56 private static readonly TimeLag timeLagSymbol = new TimeLag(); 57 58 [Obsolete("Use static method TreeSimplifier.Simplify instead")] 59 public TreeSimplifier() { } 60 61 public static ISymbolicExpressionTree Simplify(ISymbolicExpressionTree originalTree) { 59 62 var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone(); 60 63 // macro expand (initially no argument trees) … … 66 69 // check that each node is only referenced once 67 70 var nodes = rootNode.IterateNodesPrefix().ToArray(); 68 foreach (var n in nodes) if(nodes.Count(ni => ni == n) > 1) throw new InvalidOperationException();71 foreach (var n in nodes) if (nodes.Count(ni => ni == n) > 1) throw new InvalidOperationException(); 69 72 #endif 70 73 return new SymbolicExpressionTree(rootNode); … … 72 75 73 76 // the argumentTrees list contains already expanded trees used as arguments for invocations 74 private ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node,77 private static ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node, 75 78 IList<ISymbolicExpressionTreeNode> argumentTrees) { 76 79 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees); … … 97 100 } 98 101 99 private ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) {102 private static ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) { 100 103 foreach (var subtree in root.Subtrees.OfType<DefunTreeNode>()) { 101 104 if (subtree.FunctionName == functionName) return subtree.GetSubtree(0); … … 108 111 109 112 // arithmetic 110 private bool IsDivision(ISymbolicExpressionTreeNode node) {113 private static bool IsDivision(ISymbolicExpressionTreeNode node) { 111 114 return node.Symbol is Division; 112 115 } 113 116 114 private bool IsMultiplication(ISymbolicExpressionTreeNode node) {117 private static bool IsMultiplication(ISymbolicExpressionTreeNode node) { 115 118 return node.Symbol is Multiplication; 116 119 } 117 120 118 private bool IsSubtraction(ISymbolicExpressionTreeNode node) {121 private static bool IsSubtraction(ISymbolicExpressionTreeNode node) { 119 122 return node.Symbol is Subtraction; 120 123 } 121 124 122 private bool IsAddition(ISymbolicExpressionTreeNode node) {125 private static bool IsAddition(ISymbolicExpressionTreeNode node) { 123 126 return node.Symbol is Addition; 124 127 } 125 128 126 private bool IsAverage(ISymbolicExpressionTreeNode node) {129 private static bool IsAverage(ISymbolicExpressionTreeNode node) { 127 130 return node.Symbol is Average; 128 131 } 129 132 130 133 // exponential 131 private bool IsLog(ISymbolicExpressionTreeNode node) {134 private static bool IsLog(ISymbolicExpressionTreeNode node) { 132 135 return node.Symbol is Logarithm; 133 136 } 134 137 135 private bool IsExp(ISymbolicExpressionTreeNode node) {138 private static bool IsExp(ISymbolicExpressionTreeNode node) { 136 139 return node.Symbol is Exponential; 137 140 } 138 141 139 private bool IsRoot(ISymbolicExpressionTreeNode node) {142 private static bool IsRoot(ISymbolicExpressionTreeNode node) { 140 143 return node.Symbol is Root; 141 144 } 142 145 143 private bool IsSquare(ISymbolicExpressionTreeNode node) {146 private static bool IsSquare(ISymbolicExpressionTreeNode node) { 144 147 return node.Symbol is Square; 145 148 } 146 149 147 private bool IsSquareRoot(ISymbolicExpressionTreeNode node) {150 private static bool IsSquareRoot(ISymbolicExpressionTreeNode node) { 148 151 return node.Symbol is SquareRoot; 149 152 } 150 153 151 private bool IsPower(ISymbolicExpressionTreeNode node) {154 private static bool IsPower(ISymbolicExpressionTreeNode node) { 152 155 return node.Symbol is Power; 153 156 } 154 157 155 158 // trigonometric 156 private bool IsSine(ISymbolicExpressionTreeNode node) {159 private static bool IsSine(ISymbolicExpressionTreeNode node) { 157 160 return node.Symbol is Sine; 158 161 } 159 162 160 private bool IsCosine(ISymbolicExpressionTreeNode node) {163 private static bool IsCosine(ISymbolicExpressionTreeNode node) { 161 164 return node.Symbol is Cosine; 162 165 } 163 166 164 private bool IsTangent(ISymbolicExpressionTreeNode node) {167 private static bool IsTangent(ISymbolicExpressionTreeNode node) { 165 168 return node.Symbol is Tangent; 166 169 } 167 170 168 171 // boolean 169 private bool IsIfThenElse(ISymbolicExpressionTreeNode node) {172 private static bool IsIfThenElse(ISymbolicExpressionTreeNode node) { 170 173 return node.Symbol is IfThenElse; 171 174 } 172 175 173 private bool IsAnd(ISymbolicExpressionTreeNode node) {176 private static bool IsAnd(ISymbolicExpressionTreeNode node) { 174 177 return node.Symbol is And; 175 178 } 176 179 177 private bool IsOr(ISymbolicExpressionTreeNode node) {180 private static bool IsOr(ISymbolicExpressionTreeNode node) { 178 181 return node.Symbol is Or; 179 182 } 180 183 181 private bool IsNot(ISymbolicExpressionTreeNode node) {184 private static bool IsNot(ISymbolicExpressionTreeNode node) { 182 185 return node.Symbol is Not; 183 186 } 184 187 185 188 // comparison 186 private bool IsGreaterThan(ISymbolicExpressionTreeNode node) {189 private static bool IsGreaterThan(ISymbolicExpressionTreeNode node) { 187 190 return node.Symbol is GreaterThan; 188 191 } 189 192 190 private bool IsLessThan(ISymbolicExpressionTreeNode node) {193 private static bool IsLessThan(ISymbolicExpressionTreeNode node) { 191 194 return node.Symbol is LessThan; 192 195 } 193 196 194 private bool IsBoolean(ISymbolicExpressionTreeNode node) {197 private static bool IsBoolean(ISymbolicExpressionTreeNode node) { 195 198 return 196 199 node.Symbol is GreaterThan || … … 201 204 202 205 // terminals 203 private bool IsVariable(ISymbolicExpressionTreeNode node) {206 private static bool IsVariable(ISymbolicExpressionTreeNode node) { 204 207 return node.Symbol is Variable; 205 208 } 206 209 207 private bool IsVariableBase(ISymbolicExpressionTreeNode node) {210 private static bool IsVariableBase(ISymbolicExpressionTreeNode node) { 208 211 return node is VariableTreeNodeBase; 209 212 } 210 213 211 private bool IsFactor(ISymbolicExpressionTreeNode node) {214 private static bool IsFactor(ISymbolicExpressionTreeNode node) { 212 215 return node is FactorVariableTreeNode; 213 216 } 214 217 215 private bool IsBinFactor(ISymbolicExpressionTreeNode node) {218 private static bool IsBinFactor(ISymbolicExpressionTreeNode node) { 216 219 return node is BinaryFactorVariableTreeNode; 217 220 } 218 221 219 private bool IsConstant(ISymbolicExpressionTreeNode node) {222 private static bool IsConstant(ISymbolicExpressionTreeNode node) { 220 223 return node.Symbol is Constant; 221 224 } 222 225 223 226 // dynamic 224 private bool IsTimeLag(ISymbolicExpressionTreeNode node) {227 private static bool IsTimeLag(ISymbolicExpressionTreeNode node) { 225 228 return node.Symbol is TimeLag; 226 229 } 227 230 228 private bool IsIntegral(ISymbolicExpressionTreeNode node) {231 private static bool IsIntegral(ISymbolicExpressionTreeNode node) { 229 232 return node.Symbol is Integral; 230 233 } … … 237 240 /// <param name="original"></param> 238 241 /// <returns></returns> 239 public ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) {242 public static ISymbolicExpressionTreeNode GetSimplifiedTree(ISymbolicExpressionTreeNode original) { 240 243 if (IsConstant(original) || IsVariableBase(original)) { 241 244 return (ISymbolicExpressionTreeNode)original.Clone(); … … 291 294 #region specific simplification routines 292 295 293 private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) {296 private static ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) { 294 297 // can't simplify this function but simplify all subtrees 295 298 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees); … … 310 313 } 311 314 312 private ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) {315 private static ISymbolicExpressionTreeNode SimplifyConstantExpression(ISymbolicExpressionTreeNode original) { 313 316 // not yet implemented 314 317 return original; 315 318 } 316 319 317 private ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) {320 private static ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) { 318 321 if (original.Subtrees.Count() == 1) { 319 322 return GetSimplifiedTree(original.GetSubtree(0)); … … 328 331 } 329 332 330 private ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) {333 private static ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) { 331 334 if (original.Subtrees.Count() == 1) { 332 335 return Invert(GetSimplifiedTree(original.GetSubtree(0))); … … 343 346 } 344 347 345 private ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) {348 private static ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) { 346 349 if (original.Subtrees.Count() == 1) { 347 350 return GetSimplifiedTree(original.GetSubtree(0)); … … 353 356 } 354 357 355 private ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) {358 private static ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) { 356 359 if (original.Subtrees.Count() == 1) { 357 360 return Negate(GetSimplifiedTree(original.GetSubtree(0))); … … 365 368 } 366 369 367 private ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) {370 private static ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) { 368 371 if (original.Subtrees.Count() == 1) { 369 372 return GetSimplifiedTree(original.GetSubtree(0)); … … 377 380 } 378 381 379 private ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) {382 private static ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) { 380 383 return MakeNot(GetSimplifiedTree(original.GetSubtree(0))); 381 384 } 382 385 383 private ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) {386 private static ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) { 384 387 return original.Subtrees 385 388 .Select(GetSimplifiedTree) … … 387 390 } 388 391 389 private ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) {392 private static ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) { 390 393 return original.Subtrees 391 394 .Select(GetSimplifiedTree) … … 393 396 } 394 397 395 private ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) {398 private static ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) { 396 399 return MakeLessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 397 400 } 398 401 399 private ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) {402 private static ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) { 400 403 return MakeGreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 401 404 } 402 405 403 private ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) {406 private static ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) { 404 407 return MakeIfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)), 405 408 GetSimplifiedTree(original.GetSubtree(2))); 406 409 } 407 410 408 private ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) {411 private static ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) { 409 412 return MakeTangent(GetSimplifiedTree(original.GetSubtree(0))); 410 413 } 411 414 412 private ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) {415 private static ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) { 413 416 return MakeCosine(GetSimplifiedTree(original.GetSubtree(0))); 414 417 } 415 418 416 private ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) {419 private static ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) { 417 420 return MakeSine(GetSimplifiedTree(original.GetSubtree(0))); 418 421 } 419 422 420 private ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) {423 private static ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) { 421 424 return MakeExp(GetSimplifiedTree(original.GetSubtree(0))); 422 425 } 423 426 424 private ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) {427 private static ISymbolicExpressionTreeNode SimplifySquare(ISymbolicExpressionTreeNode original) { 425 428 return MakeSquare(GetSimplifiedTree(original.GetSubtree(0))); 426 429 } 427 430 428 private ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) {431 private static ISymbolicExpressionTreeNode SimplifySquareRoot(ISymbolicExpressionTreeNode original) { 429 432 return MakeSquareRoot(GetSimplifiedTree(original.GetSubtree(0))); 430 433 } 431 434 432 private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) {435 private static ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) { 433 436 return MakeLog(GetSimplifiedTree(original.GetSubtree(0))); 434 437 } 435 438 436 private ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) {439 private static ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) { 437 440 return MakeRoot(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 438 441 } 439 442 440 private ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) {443 private static ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) { 441 444 return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 442 445 } 443 446 444 private ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) {447 private static ISymbolicExpressionTreeNode SimplifyTimeLag(ISymbolicExpressionTreeNode original) { 445 448 var laggedTreeNode = original as ILaggedTreeNode; 446 449 var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0)); … … 452 455 } 453 456 454 private ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) {457 private static ISymbolicExpressionTreeNode SimplifyIntegral(ISymbolicExpressionTreeNode original) { 455 458 var laggedTreeNode = original as ILaggedTreeNode; 456 459 var simplifiedSubtree = GetSimplifiedTree(original.GetSubtree(0)); … … 466 469 #region low level tree restructuring 467 470 468 private ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) {471 private static ISymbolicExpressionTreeNode MakeTimeLag(ISymbolicExpressionTreeNode subtree, int lag) { 469 472 if (lag == 0) return subtree; 470 473 if (IsConstant(subtree)) return subtree; … … 475 478 } 476 479 477 private ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) {480 private static ISymbolicExpressionTreeNode MakeIntegral(ISymbolicExpressionTreeNode subtree, int lag) { 478 481 if (lag == 0) return subtree; 479 482 else if (lag == -1 || lag == 1) { … … 487 490 } 488 491 489 private ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) {492 private static ISymbolicExpressionTreeNode MakeNot(ISymbolicExpressionTreeNode t) { 490 493 if (IsConstant(t)) { 491 494 var constNode = t as ConstantTreeNode; … … 508 511 } 509 512 510 private ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {513 private static ISymbolicExpressionTreeNode MakeOr(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 511 514 if (IsConstant(a) && IsConstant(b)) { 512 515 var constA = a as ConstantTreeNode; … … 538 541 } 539 542 540 private ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {543 private static ISymbolicExpressionTreeNode MakeAnd(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 541 544 if (IsConstant(a) && IsConstant(b)) { 542 545 var constA = a as ConstantTreeNode; … … 568 571 } 569 572 570 private ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide,573 private static ISymbolicExpressionTreeNode MakeLessThan(ISymbolicExpressionTreeNode leftSide, 571 574 ISymbolicExpressionTreeNode rightSide) { 572 575 if (IsConstant(leftSide) && IsConstant(rightSide)) { … … 583 586 } 584 587 585 private ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide,588 private static ISymbolicExpressionTreeNode MakeGreaterThan(ISymbolicExpressionTreeNode leftSide, 586 589 ISymbolicExpressionTreeNode rightSide) { 587 590 if (IsConstant(leftSide) && IsConstant(rightSide)) { … … 598 601 } 599 602 600 private ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition,603 private static ISymbolicExpressionTreeNode MakeIfThenElse(ISymbolicExpressionTreeNode condition, 601 604 ISymbolicExpressionTreeNode trueBranch, ISymbolicExpressionTreeNode falseBranch) { 602 605 if (IsConstant(condition)) { … … 620 623 } 621 624 622 private ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) {625 private static ISymbolicExpressionTreeNode MakeSine(ISymbolicExpressionTreeNode node) { 623 626 if (IsConstant(node)) { 624 627 var constT = node as ConstantTreeNode; … … 637 640 } 638 641 639 private ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) {642 private static ISymbolicExpressionTreeNode MakeTangent(ISymbolicExpressionTreeNode node) { 640 643 if (IsConstant(node)) { 641 644 var constT = node as ConstantTreeNode; … … 654 657 } 655 658 656 private ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) {659 private static ISymbolicExpressionTreeNode MakeCosine(ISymbolicExpressionTreeNode node) { 657 660 if (IsConstant(node)) { 658 661 var constT = node as ConstantTreeNode; … … 673 676 } 674 677 675 private ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) {678 private static ISymbolicExpressionTreeNode MakeExp(ISymbolicExpressionTreeNode node) { 676 679 if (IsConstant(node)) { 677 680 var constT = node as ConstantTreeNode; … … 697 700 } 698 701 } 699 private ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) {702 private static ISymbolicExpressionTreeNode MakeLog(ISymbolicExpressionTreeNode node) { 700 703 if (IsConstant(node)) { 701 704 var constT = node as ConstantTreeNode; … … 715 718 } 716 719 717 private ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) {720 private static ISymbolicExpressionTreeNode MakeSquare(ISymbolicExpressionTreeNode node) { 718 721 if (IsConstant(node)) { 719 722 var constT = node as ConstantTreeNode; … … 734 737 } 735 738 736 private ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) {739 private static ISymbolicExpressionTreeNode MakeSquareRoot(ISymbolicExpressionTreeNode node) { 737 740 if (IsConstant(node)) { 738 741 var constT = node as ConstantTreeNode; … … 753 756 } 754 757 755 private ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {758 private static ISymbolicExpressionTreeNode MakeRoot(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 756 759 if (IsConstant(a) && IsConstant(b)) { 757 760 var constA = a as ConstantTreeNode; … … 808 811 809 812 810 private ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {813 private static ISymbolicExpressionTreeNode MakePower(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 811 814 if (IsConstant(a) && IsConstant(b)) { 812 815 var constA = a as ConstantTreeNode; … … 863 866 864 867 // MakeFraction, MakeProduct and MakeSum take two already simplified trees and create a new simplified tree 865 private ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {868 private static ISymbolicExpressionTreeNode MakeFraction(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 866 869 if (IsConstant(a) && IsConstant(b)) { 867 870 // fold constants … … 932 935 } 933 936 934 private ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {937 private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 935 938 if (IsConstant(a) && IsConstant(b)) { 936 939 // fold constants … … 1027 1030 1028 1031 // makes sure variable symbols in sums are combined 1029 private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) {1032 private static void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) { 1030 1033 var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees); 1031 1034 while (sum.Subtrees.Any()) sum.RemoveSubtree(0); … … 1066 1069 1067 1070 // nodes referencing variables can be grouped if they have 1068 private st ring GroupId(IVariableTreeNode node) {1071 private static string GroupId(IVariableTreeNode node) { 1069 1072 var binaryFactorNode = node as BinaryFactorVariableTreeNode; 1070 1073 var factorNode = node as FactorVariableTreeNode; … … 1085 1088 1086 1089 1087 private ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {1090 private static ISymbolicExpressionTreeNode MakeProduct(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 1088 1091 if (IsConstant(a) && IsConstant(b)) { 1089 1092 // fold constants … … 1166 1169 #region helper functions 1167 1170 1168 private bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) {1171 private static bool ContainsVariableCondition(ISymbolicExpressionTreeNode node) { 1169 1172 if (node.Symbol is VariableCondition) return true; 1170 1173 foreach (var subtree in node.Subtrees) … … 1173 1176 } 1174 1177 1175 private ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) {1178 private static ISymbolicExpressionTreeNode AddLagToDynamicNodes(ISymbolicExpressionTreeNode node, int lag) { 1176 1179 var laggedTreeNode = node as ILaggedTreeNode; 1177 1180 var variableNode = node as VariableTreeNode; … … 1195 1198 } 1196 1199 1197 private bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) {1200 private static bool AreSameTypeAndVariable(ISymbolicExpressionTreeNode a, ISymbolicExpressionTreeNode b) { 1198 1201 return GroupId((IVariableTreeNode)a) == GroupId((IVariableTreeNode)b); 1199 1202 } 1200 1203 1201 1204 // helper to combine the constant factors in products and to combine variables (powers of 2, 3...) 1202 private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) {1205 private static void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) { 1203 1206 var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees); 1204 1207 while (prod.Subtrees.Any()) prod.RemoveSubtree(0); … … 1264 1267 /// <param name="x"></param> 1265 1268 /// <returns>-x</returns> 1266 private ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) {1269 private static ISymbolicExpressionTreeNode Negate(ISymbolicExpressionTreeNode x) { 1267 1270 if (IsConstant(x)) { 1268 1271 ((ConstantTreeNode)x).Value *= -1; … … 1301 1304 /// <param name="x"></param> 1302 1305 /// <returns></returns> 1303 private ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) {1306 private static ISymbolicExpressionTreeNode Invert(ISymbolicExpressionTreeNode x) { 1304 1307 if (IsConstant(x)) { 1305 1308 return MakeConstant(1.0 / ((ConstantTreeNode)x).Value); … … 1315 1318 } 1316 1319 1317 private ISymbolicExpressionTreeNode MakeConstant(double value) {1320 private static ISymbolicExpressionTreeNode MakeConstant(double value) { 1318 1321 ConstantTreeNode constantTreeNode = (ConstantTreeNode)(constSymbol.CreateTreeNode()); 1319 1322 constantTreeNode.Value = value; … … 1321 1324 } 1322 1325 1323 private ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) {1326 private static ISymbolicExpressionTreeNode MakeFactor(FactorVariable sy, string variableName, IEnumerable<double> weights) { 1324 1327 var tree = (FactorVariableTreeNode)sy.CreateTreeNode(); 1325 1328 tree.VariableName = variableName; … … 1327 1330 return tree; 1328 1331 } 1329 private ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) {1332 private static ISymbolicExpressionTreeNode MakeBinFactor(BinaryFactorVariable sy, string variableName, string variableValue, double weight) { 1330 1333 var tree = (BinaryFactorVariableTreeNode)sy.CreateTreeNode(); 1331 1334 tree.VariableName = variableName; -
trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplificationOperator.cs
r14843 r14949 37 37 } 38 38 39 private readonly TreeSimplifier simplifier = new TreeSimplifier();40 41 39 [StorableConstructor] 42 40 protected SymbolicDataAnalysisExpressionTreeSimplificationOperator(bool deserializing) : base(deserializing) { } … … 53 51 public override IOperation Apply() { 54 52 var tree = SymbolicExpressionTreeParameter.ActualValue; 55 var simplifiedTree = simplifier.Simplify(tree);56 simplifiedTree = simplifier.Simplify(simplifiedTree);53 var simplifiedTree = TreeSimplifier.Simplify(tree); 54 simplifiedTree = TreeSimplifier.Simplify(simplifiedTree); 57 55 SymbolicExpressionTreeParameter.ActualValue = simplifiedTree; 58 56
Note: See TracChangeset
for help on using the changeset viewer.