- Timestamp:
- 03/17/11 14:07:47 (14 years ago)
- Location:
- branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4
- Files:
-
- 1 deleted
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Analyzers/SymbolicDataAnalysisVariableFrequencyAnalyzer.cs
r5732 r5733 153 153 var varCondNode = node as VariableConditionTreeNode; 154 154 IncReferenceCount(references, varCondNode.VariableName, currentLag); 155 GetVariableReferences(references, node.GetSub Tree(0), currentLag);156 GetVariableReferences(references, node.GetSub Tree(1), currentLag);155 GetVariableReferences(references, node.GetSubtree(0), currentLag); 156 GetVariableReferences(references, node.GetSubtree(1), currentLag); 157 157 } else if (node.Symbol is Integral) { 158 158 var laggedNode = node as LaggedTreeNode; 159 159 for (int l = laggedNode.Lag; l <= 0; l++) { 160 GetVariableReferences(references, node.GetSub Tree(0), currentLag + l);160 GetVariableReferences(references, node.GetSubtree(0), currentLag + l); 161 161 } 162 162 } else if (node.Symbol is Derivative) { 163 163 var laggedNode = node as LaggedTreeNode; 164 164 for (int l = laggedNode.Lag; l <= 0; l++) { 165 GetVariableReferences(references, node.GetSub Tree(0), currentLag + l);165 GetVariableReferences(references, node.GetSubtree(0), currentLag + l); 166 166 } 167 167 } else if (node.Symbol is TimeLag) { 168 168 var laggedNode = node as LaggedTreeNode; 169 GetVariableReferences(references, node.GetSub Tree(0), currentLag + laggedNode.Lag);169 GetVariableReferences(references, node.GetSubtree(0), currentLag + laggedNode.Lag); 170 170 } 171 171 } -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4.csproj
r5720 r5733 185 185 <Compile Include="Interfaces\ISymbolicDataAnalysisSolution.cs" /> 186 186 <Compile Include="Interfaces\ISymbolicDataAnalysisSolutionCreator.cs" /> 187 <Compile Include="Interfaces\ISymbolicDataAnalysisProblem.cs" />188 187 <Compile Include="Properties\AssemblyInfo.cs" /> 189 188 </ItemGroup> -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisExpressionTreeSimplifier.cs
r5574 r5733 55 55 var clone = (ISymbolicExpressionTreeNode)originalTree.Root.Clone(); 56 56 // macro expand (initially no argument trees) 57 var macroExpandedTree = MacroExpand(clone, clone.GetSub Tree(0), new List<ISymbolicExpressionTreeNode>());57 var macroExpandedTree = MacroExpand(clone, clone.GetSubtree(0), new List<ISymbolicExpressionTreeNode>()); 58 58 ISymbolicExpressionTreeNode rootNode = (new ProgramRootSymbol()).CreateTreeNode(); 59 rootNode.AddSub Tree(GetSimplifiedTree(macroExpandedTree));59 rootNode.AddSubtree(GetSimplifiedTree(macroExpandedTree)); 60 60 return new SymbolicExpressionTree(rootNode); 61 61 } … … 63 63 // the argumentTrees list contains already expanded trees used as arguments for invocations 64 64 private ISymbolicExpressionTreeNode MacroExpand(ISymbolicExpressionTreeNode root, ISymbolicExpressionTreeNode node, IList<ISymbolicExpressionTreeNode> argumentTrees) { 65 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Sub Trees);66 while (node.Sub Trees.Count() > 0) node.RemoveSubTree(0);65 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(node.Subtrees); 66 while (node.SubtreesCount > 0) node.RemoveSubtree(0); 67 67 if (node.Symbol is InvokeFunction) { 68 68 var invokeSym = node.Symbol as InvokeFunction; … … 80 80 // recursive application 81 81 foreach (var subtree in subtrees) { 82 node.AddSub Tree(MacroExpand(root, subtree, argumentTrees));82 node.AddSubtree(MacroExpand(root, subtree, argumentTrees)); 83 83 } 84 84 return node; … … 87 87 88 88 private ISymbolicExpressionTreeNode FindFunctionDefinition(ISymbolicExpressionTreeNode root, string functionName) { 89 foreach (var subtree in root.Sub Trees.OfType<DefunTreeNode>()) {90 if (subtree.FunctionName == functionName) return subtree.GetSub Tree(0);89 foreach (var subtree in root.Subtrees.OfType<DefunTreeNode>()) { 90 if (subtree.FunctionName == functionName) return subtree.GetSubtree(0); 91 91 } 92 92 … … 232 232 private ISymbolicExpressionTreeNode SimplifyAny(ISymbolicExpressionTreeNode original) { 233 233 // can't simplify this function but simplify all subtrees 234 List<ISymbolicExpressionTreeNode> sub Trees = new List<ISymbolicExpressionTreeNode>(original.SubTrees);235 while (original.Sub Trees.Count() > 0) original.RemoveSubTree(0);234 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(original.Subtrees); 235 while (original.Subtrees.Count() > 0) original.RemoveSubtree(0); 236 236 var clone = (SymbolicExpressionTreeNode)original.Clone(); 237 List<ISymbolicExpressionTreeNode> simplifiedSub Trees = new List<ISymbolicExpressionTreeNode>();238 foreach (var sub Tree in subTrees) {239 simplifiedSub Trees.Add(GetSimplifiedTree(subTree));240 original.AddSub Tree(subTree);241 } 242 foreach (var simplifiedSubtree in simplifiedSub Trees) {243 clone.AddSub Tree(simplifiedSubtree);244 } 245 if (simplifiedSub Trees.TrueForAll(t => IsConstant(t))) {237 List<ISymbolicExpressionTreeNode> simplifiedSubtrees = new List<ISymbolicExpressionTreeNode>(); 238 foreach (var subtree in subtrees) { 239 simplifiedSubtrees.Add(GetSimplifiedTree(subtree)); 240 original.AddSubtree(subtree); 241 } 242 foreach (var simplifiedSubtree in simplifiedSubtrees) { 243 clone.AddSubtree(simplifiedSubtree); 244 } 245 if (simplifiedSubtrees.TrueForAll(t => IsConstant(t))) { 246 246 SimplifyConstantExpression(clone); 247 247 } … … 255 255 256 256 private ISymbolicExpressionTreeNode SimplifyAverage(ISymbolicExpressionTreeNode original) { 257 if (original.Sub Trees.Count() == 1) {258 return GetSimplifiedTree(original.GetSub Tree(0));257 if (original.Subtrees.Count() == 1) { 258 return GetSimplifiedTree(original.GetSubtree(0)); 259 259 } else { 260 260 // simplify expressions x0..xn 261 261 // make sum(x0..xn) / n 262 Trace.Assert(original.Sub Trees.Count() > 1);263 var sum = original.Sub Trees262 Trace.Assert(original.Subtrees.Count() > 1); 263 var sum = original.Subtrees 264 264 .Select(x => GetSimplifiedTree(x)) 265 265 .Aggregate((a, b) => MakeSum(a, b)); 266 return MakeFraction(sum, MakeConstant(original.Sub Trees.Count()));266 return MakeFraction(sum, MakeConstant(original.Subtrees.Count())); 267 267 } 268 268 } 269 269 270 270 private ISymbolicExpressionTreeNode SimplifyDivision(ISymbolicExpressionTreeNode original) { 271 if (original.Sub Trees.Count() == 1) {272 return Invert(GetSimplifiedTree(original.GetSub Tree(0)));271 if (original.Subtrees.Count() == 1) { 272 return Invert(GetSimplifiedTree(original.GetSubtree(0))); 273 273 } else { 274 274 // simplify expressions x0..xn 275 275 // make multiplication (x0 * 1/(x1 * x1 * .. * xn)) 276 Trace.Assert(original.Sub Trees.Count() > 1);277 var simplifiedTrees = original.Sub Trees.Select(x => GetSimplifiedTree(x));276 Trace.Assert(original.Subtrees.Count() > 1); 277 var simplifiedTrees = original.Subtrees.Select(x => GetSimplifiedTree(x)); 278 278 return 279 279 MakeProduct(simplifiedTrees.First(), Invert(simplifiedTrees.Skip(1).Aggregate((a, b) => MakeProduct(a, b)))); … … 282 282 283 283 private ISymbolicExpressionTreeNode SimplifyMultiplication(ISymbolicExpressionTreeNode original) { 284 if (original.Sub Trees.Count() == 1) {285 return GetSimplifiedTree(original.GetSub Tree(0));286 } else { 287 Trace.Assert(original.Sub Trees.Count() > 1);288 return original.Sub Trees284 if (original.Subtrees.Count() == 1) { 285 return GetSimplifiedTree(original.GetSubtree(0)); 286 } else { 287 Trace.Assert(original.Subtrees.Count() > 1); 288 return original.Subtrees 289 289 .Select(x => GetSimplifiedTree(x)) 290 290 .Aggregate((a, b) => MakeProduct(a, b)); … … 293 293 294 294 private ISymbolicExpressionTreeNode SimplifySubtraction(ISymbolicExpressionTreeNode original) { 295 if (original.Sub Trees.Count() == 1) {296 return Negate(GetSimplifiedTree(original.GetSub Tree(0)));295 if (original.Subtrees.Count() == 1) { 296 return Negate(GetSimplifiedTree(original.GetSubtree(0))); 297 297 } else { 298 298 // simplify expressions x0..xn 299 299 // make addition (x0,-x1..-xn) 300 Trace.Assert(original.Sub Trees.Count() > 1);301 var simplifiedTrees = original.Sub Trees.Select(x => GetSimplifiedTree(x));300 Trace.Assert(original.Subtrees.Count() > 1); 301 var simplifiedTrees = original.Subtrees.Select(x => GetSimplifiedTree(x)); 302 302 return simplifiedTrees.Take(1) 303 303 .Concat(simplifiedTrees.Skip(1).Select(x => Negate(x))) … … 307 307 308 308 private ISymbolicExpressionTreeNode SimplifyAddition(ISymbolicExpressionTreeNode original) { 309 if (original.Sub Trees.Count() == 1) {310 return GetSimplifiedTree(original.GetSub Tree(0));309 if (original.Subtrees.Count() == 1) { 310 return GetSimplifiedTree(original.GetSubtree(0)); 311 311 } else { 312 312 // simplify expression x0..xn 313 313 // make addition (x0..xn) 314 Trace.Assert(original.Sub Trees.Count() > 1);315 return original.Sub Trees314 Trace.Assert(original.Subtrees.Count() > 1); 315 return original.Subtrees 316 316 .Select(x => GetSimplifiedTree(x)) 317 317 .Aggregate((a, b) => MakeSum(a, b)); … … 320 320 321 321 private ISymbolicExpressionTreeNode SimplifyNot(ISymbolicExpressionTreeNode original) { 322 return MakeNot(GetSimplifiedTree(original.GetSub Tree(0)));322 return MakeNot(GetSimplifiedTree(original.GetSubtree(0))); 323 323 } 324 324 private ISymbolicExpressionTreeNode SimplifyOr(ISymbolicExpressionTreeNode original) { 325 return original.Sub Trees325 return original.Subtrees 326 326 .Select(x => GetSimplifiedTree(x)) 327 327 .Aggregate((a, b) => MakeOr(a, b)); 328 328 } 329 329 private ISymbolicExpressionTreeNode SimplifyAnd(ISymbolicExpressionTreeNode original) { 330 return original.Sub Trees330 return original.Subtrees 331 331 .Select(x => GetSimplifiedTree(x)) 332 332 .Aggregate((a, b) => MakeAnd(a, b)); 333 333 } 334 334 private ISymbolicExpressionTreeNode SimplifyLessThan(ISymbolicExpressionTreeNode original) { 335 return MakeLessThan(GetSimplifiedTree(original.GetSub Tree(0)), GetSimplifiedTree(original.GetSubTree(1)));335 return MakeLessThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 336 336 } 337 337 private ISymbolicExpressionTreeNode SimplifyGreaterThan(ISymbolicExpressionTreeNode original) { 338 return MakeGreaterThan(GetSimplifiedTree(original.GetSub Tree(0)), GetSimplifiedTree(original.GetSubTree(1)));338 return MakeGreaterThan(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 339 339 } 340 340 private ISymbolicExpressionTreeNode SimplifyIfThenElse(ISymbolicExpressionTreeNode original) { 341 return MakeIfThenElse(GetSimplifiedTree(original.GetSub Tree(0)), GetSimplifiedTree(original.GetSubTree(1)), GetSimplifiedTree(original.GetSubTree(2)));341 return MakeIfThenElse(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1)), GetSimplifiedTree(original.GetSubtree(2))); 342 342 } 343 343 private ISymbolicExpressionTreeNode SimplifyTangent(ISymbolicExpressionTreeNode original) { 344 return MakeTangent(GetSimplifiedTree(original.GetSub Tree(0)));344 return MakeTangent(GetSimplifiedTree(original.GetSubtree(0))); 345 345 } 346 346 private ISymbolicExpressionTreeNode SimplifyCosine(ISymbolicExpressionTreeNode original) { 347 return MakeCosine(GetSimplifiedTree(original.GetSub Tree(0)));347 return MakeCosine(GetSimplifiedTree(original.GetSubtree(0))); 348 348 } 349 349 private ISymbolicExpressionTreeNode SimplifySine(ISymbolicExpressionTreeNode original) { 350 return MakeSine(GetSimplifiedTree(original.GetSub Tree(0)));350 return MakeSine(GetSimplifiedTree(original.GetSubtree(0))); 351 351 } 352 352 private ISymbolicExpressionTreeNode SimplifyExp(ISymbolicExpressionTreeNode original) { 353 return MakeExp(GetSimplifiedTree(original.GetSub Tree(0)));353 return MakeExp(GetSimplifiedTree(original.GetSubtree(0))); 354 354 } 355 355 356 356 private ISymbolicExpressionTreeNode SimplifyLog(ISymbolicExpressionTreeNode original) { 357 return MakeLog(GetSimplifiedTree(original.GetSub Tree(0)));357 return MakeLog(GetSimplifiedTree(original.GetSubtree(0))); 358 358 } 359 359 private ISymbolicExpressionTreeNode SimplifyRoot(ISymbolicExpressionTreeNode original) { 360 return MakeRoot(GetSimplifiedTree(original.GetSub Tree(0)), GetSimplifiedTree(original.GetSubTree(1)));360 return MakeRoot(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 361 361 } 362 362 363 363 private ISymbolicExpressionTreeNode SimplifyPower(ISymbolicExpressionTreeNode original) { 364 return MakePower(GetSimplifiedTree(original.GetSub Tree(0)), GetSimplifiedTree(original.GetSubTree(1)));364 return MakePower(GetSimplifiedTree(original.GetSubtree(0)), GetSimplifiedTree(original.GetSubtree(1))); 365 365 } 366 366 #endregion … … 373 373 else return MakeConstant(1.0); 374 374 } else if (IsNot(t)) { 375 return t.GetSub Tree(0);375 return t.GetSubtree(0); 376 376 } else if (!IsBoolean(t)) { 377 377 var gtNode = gtSymbol.CreateTreeNode(); 378 gtNode.AddSub Tree(t); gtNode.AddSubTree(MakeConstant(0.0));378 gtNode.AddSubtree(t); gtNode.AddSubtree(MakeConstant(0.0)); 379 379 var notNode = notSymbol.CreateTreeNode(); 380 notNode.AddSub Tree(gtNode);380 notNode.AddSubtree(gtNode); 381 381 return notNode; 382 382 } else { 383 383 var notNode = notSymbol.CreateTreeNode(); 384 notNode.AddSub Tree(t);384 notNode.AddSubtree(t); 385 385 return notNode; 386 386 } … … 406 406 // the constant value has no effect on the result of the boolean condition so we can drop the constant term 407 407 var orNode = orSymbol.CreateTreeNode(); 408 orNode.AddSub Tree(a);408 orNode.AddSubtree(a); 409 409 return orNode; 410 410 } 411 411 } else { 412 412 var orNode = orSymbol.CreateTreeNode(); 413 orNode.AddSub Tree(a);414 orNode.AddSub Tree(b);413 orNode.AddSubtree(a); 414 orNode.AddSubtree(b); 415 415 return orNode; 416 416 } … … 432 432 // the constant value has no effect on the result of the boolean condition so we can drop the constant term 433 433 var andNode = andSymbol.CreateTreeNode(); 434 andNode.AddSub Tree(a);434 andNode.AddSubtree(a); 435 435 return andNode; 436 436 } else { … … 440 440 } else { 441 441 var andNode = andSymbol.CreateTreeNode(); 442 andNode.AddSub Tree(a);443 andNode.AddSub Tree(b);442 andNode.AddSubtree(a); 443 andNode.AddSubtree(b); 444 444 return andNode; 445 445 } … … 453 453 } else { 454 454 var ltNode = ltSymbol.CreateTreeNode(); 455 ltNode.AddSub Tree(leftSide);456 ltNode.AddSub Tree(rightSide);455 ltNode.AddSubtree(leftSide); 456 ltNode.AddSubtree(rightSide); 457 457 return ltNode; 458 458 } … … 466 466 } else { 467 467 var gtNode = gtSymbol.CreateTreeNode(); 468 gtNode.AddSub Tree(leftSide);469 gtNode.AddSub Tree(rightSide);468 gtNode.AddSubtree(leftSide); 469 gtNode.AddSubtree(rightSide); 470 470 return gtNode; 471 471 } … … 479 479 var ifNode = ifThenElseSymbol.CreateTreeNode(); 480 480 if (IsBoolean(condition)) { 481 ifNode.AddSub Tree(condition);481 ifNode.AddSubtree(condition); 482 482 } else { 483 483 var gtNode = gtSymbol.CreateTreeNode(); 484 gtNode.AddSub Tree(condition); gtNode.AddSubTree(MakeConstant(0.0));485 ifNode.AddSub Tree(gtNode);486 } 487 ifNode.AddSub Tree(trueBranch);488 ifNode.AddSub Tree(falseBranch);484 gtNode.AddSubtree(condition); gtNode.AddSubtree(MakeConstant(0.0)); 485 ifNode.AddSubtree(gtNode); 486 } 487 ifNode.AddSubtree(trueBranch); 488 ifNode.AddSubtree(falseBranch); 489 489 return ifNode; 490 490 } … … 497 497 } else { 498 498 var sineNode = sineSymbol.CreateTreeNode(); 499 sineNode.AddSub Tree(node);499 sineNode.AddSubtree(node); 500 500 return sineNode; 501 501 } … … 507 507 } else { 508 508 var tanNode = tanSymbol.CreateTreeNode(); 509 tanNode.AddSub Tree(node);509 tanNode.AddSubtree(node); 510 510 return tanNode; 511 511 } … … 517 517 } else { 518 518 var cosNode = cosineSymbol.CreateTreeNode(); 519 cosNode.AddSub Tree(node);519 cosNode.AddSubtree(node); 520 520 return cosNode; 521 521 } … … 526 526 return MakeConstant(Math.Exp(constT.Value)); 527 527 } else if (IsLog(node)) { 528 return node.GetSub Tree(0);528 return node.GetSubtree(0); 529 529 } else if (IsAddition(node)) { 530 return node.Sub Trees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, t));530 return node.Subtrees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, t)); 531 531 } else if (IsSubtraction(node)) { 532 return node.Sub Trees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, Negate(t)));532 return node.Subtrees.Select(s => MakeExp(s)).Aggregate((s, t) => MakeProduct(s, Negate(t))); 533 533 } else { 534 534 var expNode = expSymbol.CreateTreeNode(); 535 expNode.AddSub Tree(node);535 expNode.AddSubtree(node); 536 536 return expNode; 537 537 } … … 542 542 return MakeConstant(Math.Log(constT.Value)); 543 543 } else if (IsExp(node)) { 544 return node.GetSub Tree(0);544 return node.GetSubtree(0); 545 545 } else if (IsMultiplication(node)) { 546 return node.Sub Trees.Select(s => MakeLog(s)).Aggregate((x, y) => MakeSum(x, y));546 return node.Subtrees.Select(s => MakeLog(s)).Aggregate((x, y) => MakeSum(x, y)); 547 547 } else if (IsDivision(node)) { 548 548 var subtractionNode = subSymbol.CreateTreeNode(); 549 foreach (var sub Tree in node.SubTrees) {550 subtractionNode.AddSub Tree(MakeLog(subTree));549 foreach (var subtree in node.Subtrees) { 550 subtractionNode.AddSubtree(MakeLog(subtree)); 551 551 } 552 552 return subtractionNode; 553 553 } else { 554 554 var logNode = logSymbol.CreateTreeNode(); 555 logNode.AddSub Tree(node);555 logNode.AddSubtree(node); 556 556 return logNode; 557 557 } … … 573 573 } else if (constBValue < 0) { 574 574 var rootNode = rootSymbol.CreateTreeNode(); 575 rootNode.AddSub Tree(a);576 rootNode.AddSub Tree(MakeConstant(-1.0 * constBValue));575 rootNode.AddSubtree(a); 576 rootNode.AddSubtree(MakeConstant(-1.0 * constBValue)); 577 577 return MakeFraction(MakeConstant(1.0), rootNode); 578 578 } else { 579 579 var rootNode = rootSymbol.CreateTreeNode(); 580 rootNode.AddSub Tree(a);581 rootNode.AddSub Tree(MakeConstant(constBValue));580 rootNode.AddSubtree(a); 581 rootNode.AddSubtree(MakeConstant(constBValue)); 582 582 return rootNode; 583 583 } 584 584 } else { 585 585 var rootNode = rootSymbol.CreateTreeNode(); 586 rootNode.AddSub Tree(a);587 rootNode.AddSub Tree(b);586 rootNode.AddSubtree(a); 587 rootNode.AddSubtree(b); 588 588 return rootNode; 589 589 } … … 605 605 } else if (exponent < 0) { 606 606 var powNode = powSymbol.CreateTreeNode(); 607 powNode.AddSub Tree(a);608 powNode.AddSub Tree(MakeConstant(-1.0 * exponent));607 powNode.AddSubtree(a); 608 powNode.AddSubtree(MakeConstant(-1.0 * exponent)); 609 609 return MakeFraction(MakeConstant(1.0), powNode); 610 610 } else { 611 611 var powNode = powSymbol.CreateTreeNode(); 612 powNode.AddSub Tree(a);613 powNode.AddSub Tree(MakeConstant(exponent));612 powNode.AddSubtree(a); 613 powNode.AddSubtree(MakeConstant(exponent)); 614 614 return powNode; 615 615 } 616 616 } else { 617 617 var powNode = powSymbol.CreateTreeNode(); 618 powNode.AddSub Tree(a);619 powNode.AddSub Tree(b);618 powNode.AddSubtree(a); 619 powNode.AddSubtree(b); 620 620 return powNode; 621 621 } … … 641 641 return MakeConstant(aVar.Weight / bVar.Weight); 642 642 } else if (IsAddition(a) && IsConstant(b)) { 643 return a.Sub Trees643 return a.Subtrees 644 644 .Select(x => GetSimplifiedTree(x)) 645 645 .Select(x => MakeFraction(x, b)) … … 649 649 } else if (IsDivision(a) && IsConstant(b)) { 650 650 // (a1 / a2) / c => (a1 / (a2 * c)) 651 Trace.Assert(a.Sub Trees.Count() == 2);652 return MakeFraction(a.GetSub Tree(0), MakeProduct(a.GetSubTree(1), b));651 Trace.Assert(a.Subtrees.Count() == 2); 652 return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b)); 653 653 } else if (IsDivision(a) && IsDivision(b)) { 654 654 // (a1 / a2) / (b1 / b2) => 655 Trace.Assert(a.Sub Trees.Count() == 2);656 Trace.Assert(b.Sub Trees.Count() == 2);657 return MakeFraction(MakeProduct(a.GetSub Tree(0), b.GetSubTree(1)), MakeProduct(a.GetSubTree(1), b.GetSubTree(0)));655 Trace.Assert(a.Subtrees.Count() == 2); 656 Trace.Assert(b.Subtrees.Count() == 2); 657 return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(1)), MakeProduct(a.GetSubtree(1), b.GetSubtree(0))); 658 658 } else if (IsDivision(a)) { 659 659 // (a1 / a2) / b => (a1 / (a2 * b)) 660 Trace.Assert(a.Sub Trees.Count() == 2);661 return MakeFraction(a.GetSub Tree(0), MakeProduct(a.GetSubTree(1), b));660 Trace.Assert(a.Subtrees.Count() == 2); 661 return MakeFraction(a.GetSubtree(0), MakeProduct(a.GetSubtree(1), b)); 662 662 } else if (IsDivision(b)) { 663 663 // a / (b1 / b2) => (a * b2) / b1 664 Trace.Assert(b.Sub Trees.Count() == 2);665 return MakeFraction(MakeProduct(a, b.GetSub Tree(1)), b.GetSubTree(0));664 Trace.Assert(b.Subtrees.Count() == 2); 665 return MakeFraction(MakeProduct(a, b.GetSubtree(1)), b.GetSubtree(0)); 666 666 } else { 667 667 var div = divSymbol.CreateTreeNode(); 668 div.AddSub Tree(a);669 div.AddSub Tree(b);668 div.AddSubtree(a); 669 div.AddSubtree(b); 670 670 return div; 671 671 } … … 688 688 var add = addSymbol.CreateTreeNode(); 689 689 // add all sub trees except for the last 690 for (int i = 0; i < a.Sub Trees.Count() - 1; i++) add.AddSubTree(a.GetSubTree(i));691 for (int i = 0; i < b.Sub Trees.Count() - 1; i++) add.AddSubTree(b.GetSubTree(i));692 if (IsConstant(a.Sub Trees.Last()) && IsConstant(b.SubTrees.Last())) {693 add.AddSub Tree(MakeSum(a.SubTrees.Last(), b.SubTrees.Last()));694 } else if (IsConstant(a.Sub Trees.Last())) {695 add.AddSub Tree(b.SubTrees.Last());696 add.AddSub Tree(a.SubTrees.Last());697 } else { 698 add.AddSub Tree(a.SubTrees.Last());699 add.AddSub Tree(b.SubTrees.Last());690 for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i)); 691 for (int i = 0; i < b.Subtrees.Count() - 1; i++) add.AddSubtree(b.GetSubtree(i)); 692 if (IsConstant(a.Subtrees.Last()) && IsConstant(b.Subtrees.Last())) { 693 add.AddSubtree(MakeSum(a.Subtrees.Last(), b.Subtrees.Last())); 694 } else if (IsConstant(a.Subtrees.Last())) { 695 add.AddSubtree(b.Subtrees.Last()); 696 add.AddSubtree(a.Subtrees.Last()); 697 } else { 698 add.AddSubtree(a.Subtrees.Last()); 699 add.AddSubtree(b.Subtrees.Last()); 700 700 } 701 701 MergeVariablesInSum(add); 702 if (add.Sub Trees.Count() == 1) {703 return add.GetSub Tree(0);702 if (add.Subtrees.Count() == 1) { 703 return add.GetSubtree(0); 704 704 } else { 705 705 return add; … … 711 711 var add = addSymbol.CreateTreeNode(); 712 712 // add all sub trees except for the last 713 for (int i = 0; i < a.Sub Trees.Count() - 1; i++) add.AddSubTree(a.GetSubTree(i));714 if (IsConstant(a.Sub Trees.Last()))715 add.AddSub Tree(MakeSum(a.SubTrees.Last(), b));713 for (int i = 0; i < a.Subtrees.Count() - 1; i++) add.AddSubtree(a.GetSubtree(i)); 714 if (IsConstant(a.Subtrees.Last())) 715 add.AddSubtree(MakeSum(a.Subtrees.Last(), b)); 716 716 else { 717 add.AddSub Tree(a.SubTrees.Last());718 add.AddSub Tree(b);717 add.AddSubtree(a.Subtrees.Last()); 718 add.AddSubtree(b); 719 719 } 720 720 return add; … … 722 722 // a is already an addition => append b 723 723 var add = addSymbol.CreateTreeNode(); 724 add.AddSub Tree(b);725 foreach (var sub Tree in a.SubTrees) {726 add.AddSub Tree(subTree);724 add.AddSubtree(b); 725 foreach (var subtree in a.Subtrees) { 726 add.AddSubtree(subtree); 727 727 } 728 728 MergeVariablesInSum(add); 729 if (add.Sub Trees.Count() == 1) {730 return add.GetSub Tree(0);729 if (add.Subtrees.Count() == 1) { 730 return add.GetSubtree(0); 731 731 } else { 732 732 return add; … … 734 734 } else { 735 735 var add = addSymbol.CreateTreeNode(); 736 add.AddSub Tree(a);737 add.AddSub Tree(b);736 add.AddSubtree(a); 737 add.AddSubtree(b); 738 738 MergeVariablesInSum(add); 739 if (add.Sub Trees.Count() == 1) {740 return add.GetSub Tree(0);739 if (add.Subtrees.Count() == 1) { 740 return add.GetSubtree(0); 741 741 } else { 742 742 return add; … … 748 748 // possible improvement: combine sums of products where the products only reference the same variable 749 749 private void MergeVariablesInSum(ISymbolicExpressionTreeNode sum) { 750 var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Sub Trees);751 while (sum.Sub Trees.Count() > 0) sum.RemoveSubTree(0);750 var subtrees = new List<ISymbolicExpressionTreeNode>(sum.Subtrees); 751 while (sum.Subtrees.Count() > 0) sum.RemoveSubtree(0); 752 752 var groupedVarNodes = from node in subtrees.OfType<VariableTreeNode>() 753 753 let lag = (node is LaggedVariableTreeNode) ? ((LaggedVariableTreeNode)node).Lag : 0 754 754 group node by node.VariableName + lag into g 755 755 select g; 756 var unchangedSub Trees = subtrees.Where(t => !(t is VariableTreeNode));756 var unchangedSubtrees = subtrees.Where(t => !(t is VariableTreeNode)); 757 757 758 758 foreach (var variableNodeGroup in groupedVarNodes) { … … 760 760 var representative = variableNodeGroup.First(); 761 761 representative.Weight = weightSum; 762 sum.AddSub Tree(representative);763 } 764 foreach (var unchangedSubtree in unchangedSub Trees)765 sum.AddSub Tree(unchangedSubtree);762 sum.AddSubtree(representative); 763 } 764 foreach (var unchangedSubtree in unchangedSubtrees) 765 sum.AddSubtree(unchangedSubtree); 766 766 } 767 767 … … 784 784 } else if (IsConstant(b) && IsAddition(a)) { 785 785 // multiply constants into additions 786 return a.Sub Trees.Select(x => MakeProduct(x, b)).Aggregate((c, d) => MakeSum(c, d));786 return a.Subtrees.Select(x => MakeProduct(x, b)).Aggregate((c, d) => MakeSum(c, d)); 787 787 } else if (IsDivision(a) && IsDivision(b)) { 788 788 // (a1 / a2) * (b1 / b2) => (a1 * b1) / (a2 * b2) 789 Trace.Assert(a.Sub Trees.Count() == 2);790 Trace.Assert(b.Sub Trees.Count() == 2);791 return MakeFraction(MakeProduct(a.GetSub Tree(0), b.GetSubTree(0)), MakeProduct(a.GetSubTree(1), b.GetSubTree(1)));789 Trace.Assert(a.Subtrees.Count() == 2); 790 Trace.Assert(b.Subtrees.Count() == 2); 791 return MakeFraction(MakeProduct(a.GetSubtree(0), b.GetSubtree(0)), MakeProduct(a.GetSubtree(1), b.GetSubtree(1))); 792 792 } else if (IsDivision(a)) { 793 793 // (a1 / a2) * b => (a1 * b) / a2 794 Trace.Assert(a.Sub Trees.Count() == 2);795 return MakeFraction(MakeProduct(a.GetSub Tree(0), b), a.GetSubTree(1));794 Trace.Assert(a.Subtrees.Count() == 2); 795 return MakeFraction(MakeProduct(a.GetSubtree(0), b), a.GetSubtree(1)); 796 796 } else if (IsDivision(b)) { 797 797 // a * (b1 / b2) => (b1 * a) / b2 798 Trace.Assert(b.Sub Trees.Count() == 2);799 return MakeFraction(MakeProduct(b.GetSub Tree(0), a), b.GetSubTree(1));798 Trace.Assert(b.Subtrees.Count() == 2); 799 return MakeFraction(MakeProduct(b.GetSubtree(0), a), b.GetSubtree(1)); 800 800 } else if (IsMultiplication(a) && IsMultiplication(b)) { 801 801 // merge multiplications (make sure constants are merged) 802 802 var mul = mulSymbol.CreateTreeNode(); 803 for (int i = 0; i < a.Sub Trees.Count(); i++) mul.AddSubTree(a.GetSubTree(i));804 for (int i = 0; i < b.Sub Trees.Count(); i++) mul.AddSubTree(b.GetSubTree(i));803 for (int i = 0; i < a.Subtrees.Count(); i++) mul.AddSubtree(a.GetSubtree(i)); 804 for (int i = 0; i < b.Subtrees.Count(); i++) mul.AddSubtree(b.GetSubtree(i)); 805 805 MergeVariablesAndConstantsInProduct(mul); 806 806 return mul; … … 809 809 } else if (IsMultiplication(a)) { 810 810 // a is already an multiplication => append b 811 a.AddSub Tree(b);811 a.AddSubtree(b); 812 812 MergeVariablesAndConstantsInProduct(a); 813 813 return a; 814 814 } else { 815 815 var mul = mulSymbol.CreateTreeNode(); 816 mul.AddSub Tree(a);817 mul.AddSub Tree(b);816 mul.AddSubtree(a); 817 mul.AddSubtree(b); 818 818 MergeVariablesAndConstantsInProduct(mul); 819 819 return mul; … … 842 842 // helper to combine the constant factors in products and to combine variables (powers of 2, 3...) 843 843 private void MergeVariablesAndConstantsInProduct(ISymbolicExpressionTreeNode prod) { 844 var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Sub Trees);845 while (prod.Sub Trees.Count() > 0) prod.RemoveSubTree(0);844 var subtrees = new List<ISymbolicExpressionTreeNode>(prod.Subtrees); 845 while (prod.Subtrees.Count() > 0) prod.RemoveSubtree(0); 846 846 var groupedVarNodes = from node in subtrees.OfType<VariableTreeNode>() 847 847 let lag = (node is LaggedVariableTreeNode) ? ((LaggedVariableTreeNode)node).Lag : 0 … … 856 856 .Aggregate((c1, c2) => c1 * c2); 857 857 858 var unchangedSub Trees = from tree in subtrees858 var unchangedSubtrees = from tree in subtrees 859 859 where !(tree is VariableTreeNode) 860 860 where !(tree is ConstantTreeNode) … … 867 867 var poly = mulSymbol.CreateTreeNode(); 868 868 for (int p = 0; p < variableNodeGroup.Count(); p++) { 869 poly.AddSub Tree((ISymbolicExpressionTreeNode)representative.Clone());869 poly.AddSubtree((ISymbolicExpressionTreeNode)representative.Clone()); 870 870 } 871 prod.AddSub Tree(poly);872 } else { 873 prod.AddSub Tree(representative);874 } 875 } 876 877 foreach (var unchangedSubtree in unchangedSub Trees)878 prod.AddSub Tree(unchangedSubtree);871 prod.AddSubtree(poly); 872 } else { 873 prod.AddSubtree(representative); 874 } 875 } 876 877 foreach (var unchangedSubtree in unchangedSubtrees) 878 prod.AddSubtree(unchangedSubtree); 879 879 880 880 if (!constantProduct.IsAlmost(1.0)) { 881 prod.AddSub Tree(MakeConstant(constantProduct));881 prod.AddSubtree(MakeConstant(constantProduct)); 882 882 } 883 883 } … … 898 898 } else if (IsAddition(x)) { 899 899 // (x0 + x1 + .. + xn) * -1 => (-x0 + -x1 + .. + -xn) 900 List<ISymbolicExpressionTreeNode> sub Trees = new List<ISymbolicExpressionTreeNode>(x.SubTrees);901 while (x.Sub Trees.Count() > 0) x.RemoveSubTree(0);902 foreach (var sub Tree in subTrees) {903 x.AddSub Tree(Negate(subTree));900 List<ISymbolicExpressionTreeNode> subtrees = new List<ISymbolicExpressionTreeNode>(x.Subtrees); 901 while (x.Subtrees.Count() > 0) x.RemoveSubtree(0); 902 foreach (var subtree in subtrees) { 903 x.AddSubtree(Negate(subtree)); 904 904 } 905 905 } else if (IsMultiplication(x) || IsDivision(x)) { 906 906 // x0 * x1 * .. * xn * -1 => x0 * x1 * .. * -xn 907 x.RemoveSub Tree(x.SubTrees.Count() - 1);908 x.AddSub Tree(Negate(x.SubTrees.Last())); // last is maybe a constant, prefer to negate the constant907 x.RemoveSubtree(x.Subtrees.Count() - 1); 908 x.AddSubtree(Negate(x.Subtrees.Last())); // last is maybe a constant, prefer to negate the constant 909 909 } else { 910 910 // any other function … … 924 924 return MakeConstant(1.0 / ((ConstantTreeNode)x).Value); 925 925 } else if (IsDivision(x)) { 926 Trace.Assert(x.Sub Trees.Count() == 2);927 return MakeFraction(x.GetSub Tree(1), x.GetSubTree(0));926 Trace.Assert(x.Subtrees.Count() == 2); 927 return MakeFraction(x.GetSubtree(1), x.GetSubtree(0)); 928 928 } else { 929 929 // any other function -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/SymbolicDataAnalysisProblem.cs
r5726 r5733 38 38 namespace HeuristicLab.Problems.DataAnalysis.Symbolic { 39 39 [StorableClass] 40 public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, I SymbolicDataAnalysisProblem, IStorableContent40 public abstract class SymbolicDataAnalysisProblem<T, U, V> : HeuristicOptimizationProblem<U, V>, IDataAnalysisProblem, IStorableContent 41 41 where T : class,IDataAnalysisProblemData 42 42 where U : class, ISymbolicDataAnalysisEvaluator<T> … … 50 50 private const string MaximumFunctionDefinitionsParameterName = "MaximumFunctionDefinitions"; 51 51 private const string MaximumFunctionArgumentsParameterName = "MaximumFunctionArguments"; 52 private const string RelativeNumberOfEvaluatedSamples = "RelativeNumberOfEvaluatedSamples"; 53 private const string FitnessCalculationPartitionParameterName = "FitnessCalculationPartition"; 54 private const string ModelSelectionPartitionParamterName = "ModelSelectionPartition"; 52 55 53 56 private const string ProblemDataParameterDescription = ""; -
branches/DataAnalysis Refactoring/HeuristicLab.Problems.DataAnalysis.Symbolic/3.4/Tests/SymbolicExpressionImporter.cs
r5574 r5733 76 76 } else { 77 77 // only a main branch was given => insert the main branch into the default tree template 78 root.AddSub Tree(start);79 start.AddSub Tree(mainBranch);78 root.AddSubtree(start); 79 start.AddSubtree(mainBranch); 80 80 } 81 81 return new SymbolicExpressionTree(root); … … 99 99 tree = ParseDefun(tokens); 100 100 while (!tokens.Peek().Equals(Token.RPAR)) { 101 tree.AddSub Tree(ParseSexp(tokens));101 tree.AddSubtree(ParseSexp(tokens)); 102 102 } 103 103 } else if (tokens.Peek().StringValue.StartsWith(ARGSTART)) { … … 106 106 tree = ParseInvoke(tokens); 107 107 while (!tokens.Peek().Equals(Token.RPAR)) { 108 tree.AddSub Tree(ParseSexp(tokens));108 tree.AddSubtree(ParseSexp(tokens)); 109 109 } 110 110 } else { … … 112 112 tree = CreateTree(curToken); 113 113 while (!tokens.Peek().Equals(Token.RPAR)) { 114 tree.AddSub Tree(ParseSexp(tokens));114 tree.AddSubtree(ParseSexp(tokens)); 115 115 } 116 116 }
Note: See TracChangeset
for help on using the changeset viewer.