Changeset 15440 for branches/MCTS-SymbReg-2796
- Timestamp:
- 10/29/17 21:22:21 (7 years ago)
- Location:
- branches/MCTS-SymbReg-2796
- Files:
-
- 1 added
- 1 deleted
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/HeuristicLab.Algorithms.DataAnalysis.MCTSSymbReg.csproj
r15438 r15440 104 104 <Compile Include="MctsSymbolicRegression\Disassembler.cs" /> 105 105 <Compile Include="MctsSymbolicRegression\ExpressionEvaluator.cs" /> 106 <Compile Include="MctsSymbolicRegression\ExprHashSymbolic.cs" /> 106 107 <Compile Include="MctsSymbolicRegression\MctsSymbolicRegressionAlgorithm.cs" /> 107 108 <Compile Include="MctsSymbolicRegression\MctsSymbolicRegressionStatic.cs" /> 108 109 <Compile Include="MctsSymbolicRegression\OpCodes.cs" /> 109 <Compile Include="MctsSymbolicRegression\ExprHash.cs" />110 110 <Compile Include="MctsSymbolicRegression\SymbolicExpressionGenerator.cs" /> 111 111 <Compile Include="MctsSymbolicRegression\Tree.cs" /> -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Automaton.cs
r15438 r15440 424 424 v == StateLogTFEnd || 425 425 v == StateInvTFEnd || 426 v == StateExpFEnd 426 v == StateExpFEnd || 427 v == StateLogTEnd || 428 v == StateInvTEnd || 429 v == StateTermEnd 427 430 ; 428 431 } -
branches/MCTS-SymbReg-2796/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/MctsSymbolicRegressionStatic.cs
r15439 r15440 587 587 // for selected states (EvalStates) we introduce state unification (detection of equivalent states) 588 588 if (automaton.IsEvalState(possibleFollowStates[i])) { 589 var hc = Hashcode(automaton) * (ulong)tree.state; // TODO fix unit test for structure enumeration 589 var hc = Hashcode(automaton); 590 hc = ((hc << 5) + hc) ^ (ulong)tree.state; // TODO fix unit test for structure enumeration 590 591 if (!state.nodes.TryGetValue(hc, out child)) { 592 // Console.WriteLine("New expression (hash: {0}, state: {1})", Hashcode(automaton), automaton.stateNames[possibleFollowStates[i]]); 591 593 child = new Tree() { 592 594 state = possibleFollowStates[i], … … 598 600 // only allow forward edges (don't add the child if we would go back in the graph) 599 601 else if (child.level > tree.level) { 602 // Console.WriteLine("Existing expression (hash: {0}, state: {1})", Hashcode(automaton), automaton.stateNames[possibleFollowStates[i]]); 600 603 // whenever we join paths we need to propagate back the statistics of the existing node through the newly created link 601 604 // to all parents 602 605 BackpropagateStatistics(tree, state, child.visits); 603 606 } else { 607 // Console.WriteLine("Cycle (hash: {0}, state: {1})", Hashcode(automaton), automaton.stateNames[possibleFollowStates[i]]); 604 608 // prevent cycles 605 609 Debug.Assert(child.level <= tree.level); … … 688 692 int nParams; 689 693 automaton.GetCode(out code, out nParams); 690 return ExprHash.GetHash(code, nParams);694 return (ulong)ExprHashSymbolic.GetHash(code, nParams); 691 695 } 692 696 -
branches/MCTS-SymbReg-2796/Tests/HeuristicLab.Algorithms.DataAnalysis-3.4/MctsSymbolicRegressionTest.cs
r15439 r15440 374 374 [TestCategory("Algorithms.DataAnalysis")] 375 375 [TestProperty("Time", "short")] 376 public void ExprHash Test() {376 public void ExprHashSymbolicTest() { 377 377 int nParams; 378 378 byte[] code; … … 386 386 codeGen.Emit1(OpCodes.Exit); 387 387 codeGen.GetCode(out code, out nParams); 388 var h1 = ExprHash .GetHash(code, nParams);388 var h1 = ExprHashSymbolic.GetHash(code, nParams); 389 389 390 390 codeGen = new CodeGenerator(); … … 394 394 codeGen.Emit1(OpCodes.Exit); 395 395 codeGen.GetCode(out code, out nParams); 396 var h2 = ExprHash .GetHash(code, nParams);396 var h2 = ExprHashSymbolic.GetHash(code, nParams); 397 397 398 398 Assert.AreEqual(h1, h2); … … 407 407 codeGen.Emit1(OpCodes.Exit); 408 408 codeGen.GetCode(out code, out nParams); 409 var h1 = ExprHash .GetHash(code, nParams);409 var h1 = ExprHashSymbolic.GetHash(code, nParams); 410 410 411 411 codeGen = new CodeGenerator(); … … 415 415 codeGen.Emit1(OpCodes.Exit); 416 416 codeGen.GetCode(out code, out nParams); 417 var h2 = ExprHash .GetHash(code, nParams);417 var h2 = ExprHashSymbolic.GetHash(code, nParams); 418 418 419 419 Assert.AreEqual(h1, h2); … … 430 430 codeGen.Emit1(OpCodes.Exit); 431 431 codeGen.GetCode(out code, out nParams); 432 var h1 = ExprHash .GetHash(code, nParams);432 var h1 = ExprHashSymbolic.GetHash(code, nParams); 433 433 434 434 codeGen = new CodeGenerator(); … … 442 442 codeGen.Emit1(OpCodes.Exit); 443 443 codeGen.GetCode(out code, out nParams); 444 var h2 = ExprHash .GetHash(code, nParams);444 var h2 = ExprHashSymbolic.GetHash(code, nParams); 445 445 446 446 Assert.AreEqual(h1, h2); … … 448 448 449 449 450 { 451 // div 450 { // 1/(x1x2) = 1/x1 * 1/x2 452 451 var codeGen = new CodeGenerator(); 453 452 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); … … 457 456 codeGen.Emit1(OpCodes.Exit); 458 457 codeGen.GetCode(out code, out nParams); 459 var h1 = ExprHash .GetHash(code, nParams);458 var h1 = ExprHashSymbolic.GetHash(code, nParams); 460 459 461 460 codeGen = new CodeGenerator(); … … 467 466 codeGen.Emit1(OpCodes.Exit); 468 467 codeGen.GetCode(out code, out nParams); 469 var h2 = ExprHash .GetHash(code, nParams);468 var h2 = ExprHashSymbolic.GetHash(code, nParams); 470 469 471 470 Assert.AreEqual(h1, h2); … … 480 479 codeGen.Emit1(OpCodes.Exit); 481 480 codeGen.GetCode(out code, out nParams); 482 var h1 = ExprHash .GetHash(code, nParams);481 var h1 = ExprHashSymbolic.GetHash(code, nParams); 483 482 484 483 codeGen = new CodeGenerator(); … … 490 489 codeGen.GetCode(out code, out nParams); 491 490 codeGen.Emit1(OpCodes.Exit); 492 var h2 = ExprHash .GetHash(code, nParams);491 var h2 = ExprHashSymbolic.GetHash(code, nParams); 493 492 494 493 Assert.AreEqual(h1, h2); … … 503 502 codeGen.Emit1(OpCodes.Exit); 504 503 codeGen.GetCode(out code, out nParams); 505 var h1 = ExprHash .GetHash(code, nParams);504 var h1 = ExprHashSymbolic.GetHash(code, nParams); 506 505 507 506 codeGen = new CodeGenerator(); … … 513 512 codeGen.Emit1(OpCodes.Exit); 514 513 codeGen.GetCode(out code, out nParams); 515 var h2 = ExprHash .GetHash(code, nParams);514 var h2 = ExprHashSymbolic.GetHash(code, nParams); 516 515 517 516 Assert.AreEqual(h1, h2); … … 526 525 codeGen.Emit1(OpCodes.Exit); 527 526 codeGen.GetCode(out code, out nParams); 528 var h1 = ExprHash .GetHash(code, nParams);527 var h1 = ExprHashSymbolic.GetHash(code, nParams); 529 528 530 529 codeGen = new CodeGenerator(); … … 532 531 codeGen.Emit1(OpCodes.Exit); 533 532 codeGen.GetCode(out code, out nParams); 534 var h2 = ExprHash .GetHash(code, nParams);533 var h2 = ExprHashSymbolic.GetHash(code, nParams); 535 534 536 535 Assert.AreEqual(h1, h2); … … 550 549 codeGen.Emit1(OpCodes.Exit); 551 550 codeGen.GetCode(out code, out nParams); 552 var h1 = ExprHash .GetHash(code, nParams);551 var h1 = ExprHashSymbolic.GetHash(code, nParams); 553 552 554 553 codeGen = new CodeGenerator(); … … 558 557 codeGen.Emit1(OpCodes.Exit); 559 558 codeGen.GetCode(out code, out nParams); 560 var h2 = ExprHash .GetHash(code, nParams);559 var h2 = ExprHashSymbolic.GetHash(code, nParams); 561 560 562 561 Assert.AreEqual(h1, h2); 563 562 } 564 563 565 { 564 { // c1 x1 + c2 x1 = c3 x1 (extended version) 566 565 var codeGen = new CodeGenerator(); 567 566 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); … … 582 581 codeGen.Emit1(OpCodes.Exit); 583 582 codeGen.GetCode(out code, out nParams); 584 var h1 = ExprHash .GetHash(code, nParams);583 var h1 = ExprHashSymbolic.GetHash(code, nParams); 585 584 586 585 codeGen = new CodeGenerator(); … … 594 593 codeGen.Emit1(OpCodes.Exit); 595 594 codeGen.GetCode(out code, out nParams); 596 var h2 = ExprHash .GetHash(code, nParams);595 var h2 = ExprHashSymbolic.GetHash(code, nParams); 597 596 598 597 Assert.AreEqual(h1, h2); 599 598 } 599 { 600 // exp(x1) * exp(x1) = exp(x1) 601 var codeGen = new CodeGenerator(); 602 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 603 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 604 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 605 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 606 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 607 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Exp); 608 609 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 610 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 611 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 612 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Exp); 613 614 codeGen.Emit1(OpCodes.Mul); 615 616 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 617 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 618 619 codeGen.Emit1(OpCodes.Exit); 620 codeGen.GetCode(out code, out nParams); 621 var h1 = ExprHashSymbolic.GetHash(code, nParams); 622 623 codeGen = new CodeGenerator(); 624 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 625 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 626 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 627 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 628 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 629 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Exp); 630 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 631 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 632 codeGen.Emit1(OpCodes.Exit); 633 codeGen.GetCode(out code, out nParams); 634 var h2 = ExprHashSymbolic.GetHash(code, nParams); 635 636 Assert.AreEqual(h1, h2); 637 } 638 { 639 // inv(x1) + inv(x1) != inv(x1) 640 var codeGen = new CodeGenerator(); 641 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 642 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 643 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 644 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 645 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 646 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Inv); 647 648 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 649 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 650 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 651 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Inv); 652 653 codeGen.Emit1(OpCodes.Add); 654 655 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 656 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 657 658 codeGen.Emit1(OpCodes.Exit); 659 codeGen.GetCode(out code, out nParams); 660 var h1 = ExprHashSymbolic.GetHash(code, nParams); 661 662 codeGen = new CodeGenerator(); 663 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 664 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 665 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 666 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 667 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 668 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Inv); 669 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 670 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 671 codeGen.Emit1(OpCodes.Exit); 672 codeGen.GetCode(out code, out nParams); 673 var h2 = ExprHashSymbolic.GetHash(code, nParams); 674 675 Assert.AreNotEqual(h1, h2); 676 } 677 678 { 679 // exp(x1) + exp(x1) != exp(x1) 680 var codeGen = new CodeGenerator(); 681 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 682 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 683 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 684 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 685 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 686 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Exp); 687 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 688 689 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 690 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 691 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 692 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 693 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Exp); 694 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 695 696 codeGen.Emit1(OpCodes.Add); 697 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 698 699 codeGen.Emit1(OpCodes.Exit); 700 codeGen.GetCode(out code, out nParams); 701 var h1 = ExprHashSymbolic.GetHash(code, nParams); 702 703 codeGen = new CodeGenerator(); 704 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 705 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 706 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 707 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 708 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 709 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Exp); 710 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 711 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 712 codeGen.Emit1(OpCodes.Exit); 713 codeGen.GetCode(out code, out nParams); 714 var h2 = ExprHashSymbolic.GetHash(code, nParams); 715 716 Assert.AreNotEqual(h1, h2); 717 } 718 { 719 // log(x1) + log(x1) != log(x1) 720 var codeGen = new CodeGenerator(); 721 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 722 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 723 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 724 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 725 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 726 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Log); 727 728 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 729 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 730 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 731 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Log); 732 733 codeGen.Emit1(OpCodes.Add); 734 735 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 736 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 737 738 codeGen.Emit1(OpCodes.Exit); 739 codeGen.GetCode(out code, out nParams); 740 var h1 = ExprHashSymbolic.GetHash(code, nParams); 741 742 codeGen = new CodeGenerator(); 743 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst0); 744 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadConst1); 745 codeGen.Emit1(MctsSymbolicRegression.OpCodes.LoadParamN); 746 codeGen.Emit2(MctsSymbolicRegression.OpCodes.LoadVar, 1); 747 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 748 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Log); 749 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Mul); 750 codeGen.Emit1(MctsSymbolicRegression.OpCodes.Add); 751 codeGen.Emit1(OpCodes.Exit); 752 codeGen.GetCode(out code, out nParams); 753 var h2 = ExprHashSymbolic.GetHash(code, nParams); 754 755 Assert.AreNotEqual(h1, h2); 756 } 757 600 758 } 601 759 #endregion … … 662 820 // log(x + c) * exp(x) 663 821 // log(x + c) * 1/(x + c) 664 // log(x + c) + log(x + c) // TODO822 // log(x + c) + log(x + c) 665 823 // log(x + c) + exp(x) 666 824 // log(x + c) + 1/(x+c) 667 825 // -- 6 668 // exp( x) * 1/(x+c)669 // exp(cx) + exp(cx) // TODO826 // exp(cx) * 1/(x+c) 827 // exp(cx) + exp(cx) 670 828 // exp(cx) + 1/(x+c) 671 829 // -- 3 672 // 1/(x+c) * 1/(x+c) // TODO673 // 1/(x+c) + 1/(x+c) // TODO830 // 1/(x+c) * 1/(x+c) 831 // 1/(x+c) + 1/(x+c) 674 832 // -- 2 675 833 // log(x*x) … … 679 837 680 838 681 TestMctsNumberOfSolutions(regProblem, 2, 2 7);839 TestMctsNumberOfSolutions(regProblem, 2, 25); 682 840 } 683 841 { … … 688 846 // -- 2 689 847 // x * x 690 // x + x ?691 848 // x * exp(x) 692 849 // x + exp(x) 693 // exp(x) * exp(x) 694 // exp(x) + exp(x) ? 850 // exp(x) + exp(x) 695 851 // exp(x*x) 696 // -- 7852 // -- 5 697 853 // x * x * x 698 854 // x + x * x 699 // x + x + x ?700 855 // x * x * exp(x) 701 856 // x + x * exp(x) 702 // x + x + exp(x) ?703 857 // exp(x) + x*x 704 858 // exp(x) + x*exp(x) 705 // x + exp(x) * exp(x) 706 // x + exp(x) + exp(x) ? 707 // x * exp(x) * exp(x) 859 // x + exp(x) + exp(x) 708 860 // x * exp(x*x) 709 861 // x + exp(x*x) 710 // -- 13 711 712 // exp(x) * exp(x) * exp(x) 713 // exp(x) + exp(x) * exp(x) 714 // exp(x) + exp(x) + exp(x) ? 715 // -- 3 862 // -- 9 863 864 // exp(x) + exp(x) + exp(x) 865 // -- 1 716 866 717 867 // exp(x) * exp(x*x) … … 720 870 // exp(x*x*x) 721 871 // -- 1 722 TestMctsNumberOfSolutions(regProblem, 3, 2 + 7 + 13 + 3 + 2 +1, allowLog: false, allowInv: false);872 TestMctsNumberOfSolutions(regProblem, 3, 2+5+9+1+2+1, allowLog: false, allowInv: false); 723 873 } 724 874 } … … 762 912 // exp(y) 763 913 // -- 4 764 // x (* |+) x914 // x (*) x 765 915 // x (*|+) exp(x) 766 916 // x (*|+) y 767 917 // x (*|+) exp(y) 768 // -- 8769 // exp(x) ( *|+) exp(x)918 // -- 7 919 // exp(x) (+) exp(x) 770 920 // exp(x) (*|+) exp(y) 771 // -- 4772 // y (* |+) y921 // -- 3 922 // y (*) y 773 923 // y (*|+) exp(x) 774 924 // y (*|+) exp(y) 775 // -- 6776 // exp(y) ( *|+) exp(y)777 // -- 2925 // -- 5 926 // exp(y) (+) exp(y) 927 // -- 1 778 928 // 779 929 // exp(x*x) … … 782 932 // -- 3 783 933 784 TestMctsNumberOfSolutions(regProblem, 2, 4 + 8 + 4 + 6 + 2+ 3, allowLog: false, allowInv: false);934 TestMctsNumberOfSolutions(regProblem, 2, 4 + 7 + 3 + 5 + 1 + 3, allowLog: false, allowInv: false); 785 935 } 786 936 … … 830 980 // inv(y*y) 831 981 // -- 6 832 // log(x+x)833 982 // log(x+y) 834 // log(y+y)835 836 // inv(x+x)837 983 // inv(x+y) 838 // inv(y+y) 839 // -- 6 840 TestMctsNumberOfSolutions(regProblem, 2, 6 + 6 + 4 + 2 + 5 + 3 + 1 + 6 + 6, allowExp: false, allowSum: false); 984 // -- 2 985 TestMctsNumberOfSolutions(regProblem, 2, 6 + 6 + 4 + 2 + 5 + 3 + 1 + 6 + 2, allowExp: false, allowSum: false); 841 986 } 842 987 }
Note: See TracChangeset
for help on using the changeset viewer.