[15714] | 1 | using System.Linq;
|
---|
| 2 | using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
|
---|
| 3 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 4 |
|
---|
| 5 | namespace Test {
|
---|
| 6 | [TestClass]
|
---|
| 7 | public class TreeHashingTest {
|
---|
| 8 |
|
---|
| 9 | private Grammar grammar;
|
---|
| 10 | private TerminalSymbol varA;
|
---|
| 11 | private TerminalSymbol varB;
|
---|
| 12 | private TerminalSymbol varC;
|
---|
[15849] | 13 | private TerminalSymbol c;
|
---|
[15714] | 14 |
|
---|
| 15 | [TestInitialize]
|
---|
| 16 | public void InitTest() {
|
---|
| 17 | grammar = new Grammar(new[] { "a", "b", "c" });
|
---|
| 18 |
|
---|
[15834] | 19 | varA = grammar.VarTerminals.First(s => s.StringRepresentation == "a");
|
---|
| 20 | varB = grammar.VarTerminals.First(s => s.StringRepresentation == "b");
|
---|
| 21 | varC = grammar.VarTerminals.First(s => s.StringRepresentation == "c");
|
---|
[15849] | 22 | c = grammar.Const;
|
---|
[15714] | 23 | }
|
---|
| 24 |
|
---|
| 25 | [TestMethod]
|
---|
| 26 | [TestCategory("TreeHashing")]
|
---|
| 27 | public void SimpleEqualityAddition() {
|
---|
[16026] | 28 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
|
---|
| 29 | SymbolList s2 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
|
---|
[15714] | 30 |
|
---|
[15832] | 31 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 32 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15714] | 33 |
|
---|
| 34 | Assert.AreEqual(hash1, hash2);
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | [TestMethod]
|
---|
| 38 | [TestCategory("TreeHashing")]
|
---|
| 39 | public void SimpleInequalityAddition() {
|
---|
[16026] | 40 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varC, grammar.Addition });
|
---|
| 41 | SymbolList s2 = new SymbolList(new[] { varB, varB, grammar.Addition, varB, grammar.Addition });
|
---|
[15714] | 42 |
|
---|
[15832] | 43 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 44 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15714] | 45 |
|
---|
| 46 | Assert.AreNotEqual(hash1, hash2);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | [TestMethod]
|
---|
| 50 | [TestCategory("TreeHashing")]
|
---|
| 51 | public void CommutativityAddition() {
|
---|
[16026] | 52 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition });
|
---|
| 53 | SymbolList s2 = new SymbolList(new[] { varB, varA, grammar.Addition });
|
---|
[15714] | 54 |
|
---|
[15832] | 55 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 56 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15714] | 57 |
|
---|
| 58 | Assert.AreEqual(hash1, hash2);
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | [TestMethod]
|
---|
| 62 | [TestCategory("TreeHashing")]
|
---|
| 63 | public void AssociativityAddition() {
|
---|
[16026] | 64 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition, varA, grammar.Addition });
|
---|
| 65 | SymbolList s2 = new SymbolList(new[] { varA, varB, varA, grammar.Addition, grammar.Addition });
|
---|
[15714] | 66 |
|
---|
[15832] | 67 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 68 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15714] | 69 |
|
---|
| 70 | Assert.AreEqual(hash1, hash2);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | [TestMethod]
|
---|
| 74 | [TestCategory("TreeHashing")]
|
---|
| 75 | public void RepeatedAddition() {
|
---|
[16026] | 76 | SymbolList s1 = new SymbolList(new[] { varA, varA, grammar.Addition, varA, grammar.Addition });
|
---|
| 77 | SymbolList s2 = new SymbolList(new[] { varA });
|
---|
[15714] | 78 |
|
---|
[15832] | 79 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 80 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15714] | 81 |
|
---|
| 82 | Assert.AreEqual(hash1, hash2);
|
---|
| 83 | }
|
---|
[15725] | 84 |
|
---|
| 85 | [TestMethod]
|
---|
| 86 | [TestCategory("TreeHashing")]
|
---|
| 87 | public void ComplexInequality() {
|
---|
[16026] | 88 | SymbolList s1 = new SymbolList(new[] { varA, varA, varA, grammar.Multiplication, grammar.Multiplication });
|
---|
| 89 | SymbolList s2 = new SymbolList(new[] { varA, varA, varA, grammar.Multiplication, grammar.Addition });
|
---|
[15725] | 90 |
|
---|
[15832] | 91 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 92 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15725] | 93 |
|
---|
| 94 | Assert.AreNotEqual(hash1, hash2);
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[15746] | 97 | [TestMethod]
|
---|
| 98 | [TestCategory("TreeHashing")]
|
---|
| 99 | public void NonterminalHashing() {
|
---|
[16026] | 100 | SymbolList s1 = new SymbolList(new Symbol[] { varA, varA, grammar.Expr, grammar.Addition, grammar.Addition });
|
---|
| 101 | SymbolList s2 = new SymbolList(new Symbol[] { varA, grammar.Expr, grammar.Addition });
|
---|
[15725] | 102 |
|
---|
[15832] | 103 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 104 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15746] | 105 |
|
---|
| 106 | Assert.AreEqual(hash1, hash2);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[15784] | 109 | [TestMethod]
|
---|
| 110 | [TestCategory("TreeHashing")]
|
---|
| 111 | public void InverseFactorCancelationSimple() {
|
---|
[15791] | 112 | // 1/a * b * a * a
|
---|
[16026] | 113 | SymbolList s1 = new SymbolList(new Symbol[] { varA, grammar.Inv, varB, grammar.Multiplication, varA, grammar.Multiplication, varA, grammar.Multiplication });
|
---|
[15791] | 114 | // a * b
|
---|
[16026] | 115 | SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Multiplication });
|
---|
[15746] | 116 |
|
---|
[15832] | 117 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 118 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15784] | 119 |
|
---|
| 120 | Assert.AreEqual(hash1, hash2);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | [TestMethod]
|
---|
| 124 | [TestCategory("TreeHashing")]
|
---|
| 125 | public void InverseFactorCancelationComplex() {
|
---|
[16026] | 126 | SymbolList s1 = new SymbolList(new Symbol[] { varA, grammar.Sin, varA, varA, grammar.Multiplication, varA, grammar.Addition, grammar.Sin, grammar.Addition });
|
---|
| 127 | SymbolList s2 = new SymbolList(new Symbol[] { varA, varA, varA, grammar.Multiplication, grammar.Addition, grammar.Sin, varA, grammar.Inv, varA, grammar.Sin, varA, grammar.Multiplication, grammar.Multiplication, grammar.Addition });
|
---|
[15784] | 128 |
|
---|
[15832] | 129 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 130 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
[15784] | 131 |
|
---|
| 132 | Assert.AreEqual(hash1, hash2);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[15849] | 135 | // Constants
|
---|
| 136 | [TestMethod]
|
---|
| 137 | [TestCategory("TreeHashing")]
|
---|
| 138 | public void SimpleConst() {
|
---|
[16056] | 139 | SymbolList s1 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, grammar.Addition });
|
---|
[16026] | 140 | SymbolList s2 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, varA, grammar.Multiplication, grammar.Addition, c, grammar.Addition });
|
---|
[15849] | 141 |
|
---|
| 142 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
| 143 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
| 144 |
|
---|
| 145 | Assert.AreEqual(hash1, hash2);
|
---|
| 146 | }
|
---|
| 147 |
|
---|
[15795] | 148 | /* DEPRECATED; SINCE WE DO NOT ALLOW COMPOUND DIVISIONS
|
---|
[15791] | 149 | [TestMethod]
|
---|
| 150 | [TestCategory("TreeHashing")]
|
---|
| 151 | public void CompoundInverseCancellationToSingleInverse() {
|
---|
[16026] | 152 | SymbolList s1 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv, grammar.Inv });
|
---|
| 153 | SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv });
|
---|
[15791] | 154 |
|
---|
| 155 | int hash1 = grammar.CalcHashCode(s1);
|
---|
| 156 | int hash2 = grammar.CalcHashCode(s2);
|
---|
| 157 |
|
---|
| 158 | Assert.AreEqual(hash1, hash2);
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | [TestMethod]
|
---|
| 162 | [TestCategory("TreeHashing")]
|
---|
| 163 | public void CompoundInverseCancellationToDivisor() {
|
---|
[16026] | 164 | SymbolList s1 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition, grammar.Inv, grammar.Inv });
|
---|
| 165 | SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Addition });
|
---|
[15791] | 166 |
|
---|
| 167 | int hash1 = grammar.CalcHashCode(s1);
|
---|
| 168 | int hash2 = grammar.CalcHashCode(s2);
|
---|
| 169 |
|
---|
| 170 | Assert.AreEqual(hash1, hash2);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | [TestMethod]
|
---|
| 174 | [TestCategory("TreeHashing")]
|
---|
[15795] | 175 | public void UncancelableCompoundInverse() {
|
---|
[15791] | 176 | // 1 / ( 1/b + sin(a*c) )
|
---|
[16026] | 177 | SymbolList s1 = new SymbolList(new Symbol[] { varB, grammar.Inv, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition, grammar.Inv });
|
---|
[15791] | 178 | // b + sin(a*c)
|
---|
[16026] | 179 | SymbolList s2 = new SymbolList(new Symbol[] { varB, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition });
|
---|
[15791] | 180 |
|
---|
| 181 | int hash1 = grammar.CalcHashCode(s1);
|
---|
| 182 | int hash2 = grammar.CalcHashCode(s2);
|
---|
| 183 |
|
---|
| 184 | Assert.AreNotEqual(hash1, hash2);
|
---|
[15795] | 185 | }*/
|
---|
[15714] | 186 | }
|
---|
| 187 | }
|
---|