Changeset 13315
- Timestamp:
- 11/20/15 11:27:39 (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicDataAnalysisExpressionTreeInterpreterTest.cs
r13314 r13315 98 98 [TestCategory("Problems.DataAnalysis.Symbolic")] 99 99 [TestProperty("Time", "long")] 100 public void ILEmittingInterpreterTestFullGrammarPerformance() {101 TestFullGrammarPerformance(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6);102 }103 [TestMethod]104 [TestCategory("Problems.DataAnalysis.Symbolic")]105 [TestProperty("Time", "long")]106 100 public void ILEmittingInterpreterTestArithmeticGrammarPerformance() { 107 101 TestArithmeticGrammarPerformance(new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter(), 7.5e6); 108 102 } 109 110 103 111 104 [TestMethod] … … 184 177 EvaluateTerminals(interpreter, ds); 185 178 EvaluateOperations(interpreter, ds); 179 EvaluateLaggedOperations(interpreter, ds); 180 EvaluateSpecialFunctions(interpreter, ds); 186 181 EvaluateAdf(interpreter, ds); 187 }188 189 [TestMethod]190 [TestCategory("Problems.DataAnalysis.Symbolic")]191 [TestProperty("Time", "short")]192 public void ILEmittingInterpreterTestEvaluation() {193 var interpreter = new SymbolicDataAnalysisExpressionTreeILEmittingInterpreter();194 EvaluateTerminals(interpreter, ds);195 EvaluateOperations(interpreter, ds);196 182 } 197 183 … … 204 190 EvaluateTerminals(interpreter, ds); 205 191 EvaluateOperations(interpreter, ds); 192 EvaluateSpecialFunctions(interpreter, ds); 206 193 } 207 194 … … 214 201 EvaluateTerminals(interpreter, ds); 215 202 EvaluateOperations(interpreter, ds); 216 } 217 218 [TestMethod] 219 [TestCategory("Problems.DataAnalysis.Symbolic")] 220 [TestProperty("Time", "long")] 221 public void TestInterpreterEvaluationResults() { 203 EvaluateLaggedOperations(interpreter, ds); 204 EvaluateSpecialFunctions(interpreter, ds); 205 } 206 207 [TestMethod] 208 [TestCategory("Problems.DataAnalysis.Symbolic")] 209 [TestProperty("Time", "long")] 210 public void TestInterpretersEstimatedValuesConsistency() { 222 211 var twister = new MersenneTwister(); 223 212 int seed = twister.Next(0, int.MaxValue); … … 258 247 [TestCategory("Problems.DataAnalysis.Symbolic")] 259 248 [TestProperty("Time", "long")] 260 public void TestCompiledInterpreterE valuationResults() {261 const double delta = 1e-1 0;249 public void TestCompiledInterpreterEstimatedValuesConsistency() { 250 const double delta = 1e-12; 262 251 263 252 var twister = new MersenneTwister(); … … 271 260 272 261 var grammar = new TypeCoherentExpressionGrammar(); 273 var hash = new HashSet<Type>(new[] { typeof(LaggedSymbol), typeof(LaggedVariable), typeof(TimeLag), typeof(Derivative), typeof(Integral), typeof(AutoregressiveTargetVariable) }); 274 foreach (var symbol in grammar.Symbols.Where(x => hash.Contains(x.GetType()))) symbol.Enabled = false; 262 grammar.ConfigureAsDefaultRegressionGrammar(); 275 263 var randomTrees = Util.CreateRandomTrees(twister, dataset, grammar, N, 1, 10, 0, 0); 276 264 foreach (ISymbolicExpressionTree tree in randomTrees) { … … 281 269 new SymbolicDataAnalysisExpressionCompiledTreeInterpreter(), 282 270 new SymbolicDataAnalysisExpressionTreeInterpreter(), 271 new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), 283 272 }; 284 273 var rows = Enumerable.Range(0, numRows).ToList(); … … 287 276 for (int i = 0; i < randomTrees.Length; ++i) { 288 277 var tree = randomTrees[i]; 289 List<List<double>> valuesMatrix = new List<List<double>>(); 290 // the try-catch block below is useful for debugging lambda expressions 291 try { 292 valuesMatrix = interpreters.Select(x => x.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToList()).ToList(); 293 } 294 catch (Exception e) { 295 var lambda = SymbolicDataAnalysisExpressionCompiledTreeInterpreter.CreateDelegate(tree, dataset); 296 Console.WriteLine(e.Message); 297 Console.WriteLine(lambda.ToString()); 298 } 278 var valuesMatrix = interpreters.Select(x => x.GetSymbolicExpressionTreeValues(tree, dataset, rows).ToList()).ToList(); 299 279 for (int m = 0; m < interpreters.Length - 1; ++m) { 300 280 for (int n = m + 1; n < interpreters.Length; ++n) { … … 338 318 } 339 319 340 private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {320 private void EvaluateTerminals(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 341 321 // constants 342 322 Evaluate(interpreter, ds, "(+ 1.5 3.5)", 0, 5.0); … … 347 327 } 348 328 349 private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) {329 private void EvaluateAdf(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 350 330 351 331 // ADF … … 386 366 } 387 367 388 private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds) { 368 private void EvaluateSpecialFunctions(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 369 // special functions 370 Action<double> checkAiry = (x) => { 371 double ai, aip, bi, bip; 372 alglib.airy(x, out ai, out aip, out bi, out bip); 373 Evaluate(interpreter, ds, "(airya " + x + ")", 0, ai); 374 Evaluate(interpreter, ds, "(airyb " + x + ")", 0, bi); 375 }; 376 377 Action<double> checkBessel = (x) => { 378 Evaluate(interpreter, ds, "(bessel " + x + ")", 0, alglib.besseli0(x)); 379 }; 380 381 Action<double> checkSinCosIntegrals = (x) => { 382 double si, ci; 383 alglib.sinecosineintegrals(x, out si, out ci); 384 Evaluate(interpreter, ds, "(cosint " + x + ")", 0, ci); 385 Evaluate(interpreter, ds, "(sinint " + x + ")", 0, si); 386 }; 387 Action<double> checkHypSinCosIntegrals = (x) => { 388 double shi, chi; 389 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 390 Evaluate(interpreter, ds, "(hypcosint " + x + ")", 0, chi); 391 Evaluate(interpreter, ds, "(hypsinint " + x + ")", 0, shi); 392 }; 393 Action<double> checkFresnelSinCosIntegrals = (x) => { 394 double c = 0, s = 0; 395 alglib.fresnelintegral(x, ref c, ref s); 396 Evaluate(interpreter, ds, "(fresnelcosint " + x + ")", 0, c); 397 Evaluate(interpreter, ds, "(fresnelsinint " + x + ")", 0, s); 398 }; 399 Action<double> checkNormErf = (x) => { 400 Evaluate(interpreter, ds, "(norm " + x + ")", 0, alglib.normaldistribution(x)); 401 Evaluate(interpreter, ds, "(erf " + x + ")", 0, alglib.errorfunction(x)); 402 }; 403 404 Action<double> checkGamma = (x) => { 405 Evaluate(interpreter, ds, "(gamma " + x + ")", 0, alglib.gammafunction(x)); 406 }; 407 Action<double> checkPsi = (x) => { 408 try { 409 Evaluate(interpreter, ds, "(psi " + x + ")", 0, alglib.psi(x)); 410 } 411 catch (alglib.alglibexception) { // ignore cases where alglib throws an exception 412 } 413 }; 414 Action<double> checkDawson = (x) => { 415 Evaluate(interpreter, ds, "(dawson " + x + ")", 0, alglib.dawsonintegral(x)); 416 }; 417 Action<double> checkExpInt = (x) => { 418 Evaluate(interpreter, ds, "(expint " + x + ")", 0, alglib.exponentialintegralei(x)); 419 }; 420 421 foreach (var e in new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }) { 422 checkAiry(e); 423 checkBessel(e); 424 checkSinCosIntegrals(e); 425 checkGamma(e); 426 checkExpInt(e); 427 checkDawson(e); 428 checkPsi(e); 429 checkNormErf(e); 430 checkFresnelSinCosIntegrals(e); 431 checkHypSinCosIntegrals(e); 432 } 433 } 434 435 private void EvaluateLaggedOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 436 // lag 437 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0)); 438 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1)); 439 Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2)); 440 Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1)); 441 442 // integral 443 Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1)); 444 Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 445 Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 446 Evaluate(interpreter, ds, "(integral -1.0 (* (variable 1.0 a) (variable 1.0 b)))", 1, ds.GetDoubleValue("A", 0) * ds.GetDoubleValue("B", 0) + ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)); 447 Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0); 448 449 // derivative 450 // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1 451 Evaluate(interpreter, ds, "(diff (variable 1.0 a)) ", 5, (ds.GetDoubleValue("A", 5) + 2 * ds.GetDoubleValue("A", 4) - 2 * ds.GetDoubleValue("A", 2) - ds.GetDoubleValue("A", 1)) / 8.0); 452 Evaluate(interpreter, ds, "(diff (variable 1.0 b)) ", 5, (ds.GetDoubleValue("B", 5) + 2 * ds.GetDoubleValue("B", 4) - 2 * ds.GetDoubleValue("B", 2) - ds.GetDoubleValue("B", 1)) / 8.0); 453 Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, + 454 (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) + 455 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) - 456 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) - 457 ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0); 458 Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0); 459 460 // timelag 461 Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2)); 462 Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2)); 463 Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a 1) (lagVariable 1.0 b 2)))", 1, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 2)); 464 Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0); 465 } 466 467 private void EvaluateOperations(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds) { 389 468 // addition 390 469 Evaluate(interpreter, ds, "(+ (variable 2.0 a ))", 1, 4.0); … … 509 588 // mean 510 589 Evaluate(interpreter, ds, "(mean -1.0 1.0 -1.0)", 0, -1.0 / 3.0); 511 512 // lag 513 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 1, ds.GetDoubleValue("A", 0)); 514 Evaluate(interpreter, ds, "(lagVariable 1.0 a -1) ", 2, ds.GetDoubleValue("A", 1)); 515 Evaluate(interpreter, ds, "(lagVariable 1.0 a 0) ", 2, ds.GetDoubleValue("A", 2)); 516 Evaluate(interpreter, ds, "(lagVariable 1.0 a 1) ", 0, ds.GetDoubleValue("A", 1)); 517 518 // integral 519 Evaluate(interpreter, ds, "(integral -1.0 (variable 1.0 a)) ", 1, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1)); 520 Evaluate(interpreter, ds, "(integral -1.0 (lagVariable 1.0 a 1)) ", 1, ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 521 Evaluate(interpreter, ds, "(integral -2.0 (variable 1.0 a)) ", 2, ds.GetDoubleValue("A", 0) + ds.GetDoubleValue("A", 1) + ds.GetDoubleValue("A", 2)); 522 Evaluate(interpreter, ds, "(integral -1.0 (* (variable 1.0 a) (variable 1.0 b)))", 1, ds.GetDoubleValue("A", 0) * ds.GetDoubleValue("B", 0) + ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)); 523 Evaluate(interpreter, ds, "(integral -2.0 3.0)", 1, 9.0); 524 525 // derivative 526 // (f_0 + 2 * f_1 - 2 * f_3 - f_4) / 8; // h = 1 527 Evaluate(interpreter, ds, "(diff (variable 1.0 a)) ", 5, (ds.GetDoubleValue("A", 5) + 2 * ds.GetDoubleValue("A", 4) - 2 * ds.GetDoubleValue("A", 2) - ds.GetDoubleValue("A", 1)) / 8.0); 528 Evaluate(interpreter, ds, "(diff (variable 1.0 b)) ", 5, (ds.GetDoubleValue("B", 5) + 2 * ds.GetDoubleValue("B", 4) - 2 * ds.GetDoubleValue("B", 2) - ds.GetDoubleValue("B", 1)) / 8.0); 529 Evaluate(interpreter, ds, "(diff (* (variable 1.0 a) (variable 1.0 b)))", 5, + 530 (ds.GetDoubleValue("A", 5) * ds.GetDoubleValue("B", 5) + 531 2 * ds.GetDoubleValue("A", 4) * ds.GetDoubleValue("B", 4) - 532 2 * ds.GetDoubleValue("A", 2) * ds.GetDoubleValue("B", 2) - 533 ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 1)) / 8.0); 534 Evaluate(interpreter, ds, "(diff -2.0 3.0)", 5, 0.0); 535 536 // timelag 537 Evaluate(interpreter, ds, "(lag -1.0 (lagVariable 1.0 a 2)) ", 1, ds.GetDoubleValue("A", 2)); 538 Evaluate(interpreter, ds, "(lag -2.0 (lagVariable 1.0 a 2)) ", 2, ds.GetDoubleValue("A", 2)); 539 Evaluate(interpreter, ds, "(lag -1.0 (* (lagVariable 1.0 a 1) (lagVariable 1.0 b 2)))", 1, ds.GetDoubleValue("A", 1) * ds.GetDoubleValue("B", 2)); 540 Evaluate(interpreter, ds, "(lag -2.0 3.0)", 1, 3.0); 541 542 { 543 // special functions 544 Action<double> checkAiry = (x) => { 545 double ai, aip, bi, bip; 546 alglib.airy(x, out ai, out aip, out bi, out bip); 547 Evaluate(interpreter, ds, "(airya " + x + ")", 0, ai); 548 Evaluate(interpreter, ds, "(airyb " + x + ")", 0, bi); 549 }; 550 551 Action<double> checkBessel = (x) => { 552 Evaluate(interpreter, ds, "(bessel " + x + ")", 0, alglib.besseli0(x)); 553 }; 554 555 Action<double> checkSinCosIntegrals = (x) => { 556 double si, ci; 557 alglib.sinecosineintegrals(x, out si, out ci); 558 Evaluate(interpreter, ds, "(cosint " + x + ")", 0, ci); 559 Evaluate(interpreter, ds, "(sinint " + x + ")", 0, si); 560 }; 561 Action<double> checkHypSinCosIntegrals = (x) => { 562 double shi, chi; 563 alglib.hyperbolicsinecosineintegrals(x, out shi, out chi); 564 Evaluate(interpreter, ds, "(hypcosint " + x + ")", 0, chi); 565 Evaluate(interpreter, ds, "(hypsinint " + x + ")", 0, shi); 566 }; 567 Action<double> checkFresnelSinCosIntegrals = (x) => { 568 double c = 0, s = 0; 569 alglib.fresnelintegral(x, ref c, ref s); 570 Evaluate(interpreter, ds, "(fresnelcosint " + x + ")", 0, c); 571 Evaluate(interpreter, ds, "(fresnelsinint " + x + ")", 0, s); 572 }; 573 Action<double> checkNormErf = (x) => { 574 Evaluate(interpreter, ds, "(norm " + x + ")", 0, alglib.normaldistribution(x)); 575 Evaluate(interpreter, ds, "(erf " + x + ")", 0, alglib.errorfunction(x)); 576 }; 577 578 Action<double> checkGamma = (x) => { 579 Evaluate(interpreter, ds, "(gamma " + x + ")", 0, alglib.gammafunction(x)); 580 }; 581 Action<double> checkPsi = (x) => { 582 try { 583 Evaluate(interpreter, ds, "(psi " + x + ")", 0, alglib.psi(x)); 584 } 585 catch (alglib.alglibexception) { // ignore cases where alglib throws an exception 586 } 587 }; 588 Action<double> checkDawson = (x) => { 589 Evaluate(interpreter, ds, "(dawson " + x + ")", 0, alglib.dawsonintegral(x)); 590 }; 591 Action<double> checkExpInt = (x) => { 592 Evaluate(interpreter, ds, "(expint " + x + ")", 0, alglib.exponentialintegralei(x)); 593 }; 594 595 596 597 foreach (var e in new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }) { 598 checkAiry(e); 599 checkBessel(e); 600 checkSinCosIntegrals(e); 601 checkGamma(e); 602 checkExpInt(e); 603 checkDawson(e); 604 checkPsi(e); 605 checkNormErf(e); 606 checkFresnelSinCosIntegrals(e); 607 checkHypSinCosIntegrals(e); 608 } 609 } 610 } 611 612 private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, Dataset ds, string expr, int index, double expected) { 590 } 591 592 private void Evaluate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, IDataset ds, string expr, int index, double expected) { 613 593 var importer = new SymbolicExpressionImporter(); 614 594 ISymbolicExpressionTree tree = importer.Import(expr);
Note: See TracChangeset
for help on using the changeset viewer.