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;
|
---|
13 | private TerminalSymbol c;
|
---|
14 |
|
---|
15 | [TestInitialize]
|
---|
16 | public void InitTest() {
|
---|
17 | grammar = new Grammar(new[] { "a", "b", "c" });
|
---|
18 |
|
---|
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");
|
---|
22 | c = grammar.Const;
|
---|
23 | }
|
---|
24 |
|
---|
25 | [TestMethod]
|
---|
26 | [TestCategory("TreeHashing")]
|
---|
27 | public void SimpleEqualityAddition() {
|
---|
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 });
|
---|
30 |
|
---|
31 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
32 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
33 |
|
---|
34 | Assert.AreEqual(hash1, hash2);
|
---|
35 | }
|
---|
36 |
|
---|
37 | [TestMethod]
|
---|
38 | [TestCategory("TreeHashing")]
|
---|
39 | public void SimpleInequalityAddition() {
|
---|
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 });
|
---|
42 |
|
---|
43 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
44 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
45 |
|
---|
46 | Assert.AreNotEqual(hash1, hash2);
|
---|
47 | }
|
---|
48 |
|
---|
49 | [TestMethod]
|
---|
50 | [TestCategory("TreeHashing")]
|
---|
51 | public void CommutativityAddition() {
|
---|
52 | SymbolList s1 = new SymbolList(new[] { varA, varB, grammar.Addition });
|
---|
53 | SymbolList s2 = new SymbolList(new[] { varB, varA, grammar.Addition });
|
---|
54 |
|
---|
55 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
56 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
57 |
|
---|
58 | Assert.AreEqual(hash1, hash2);
|
---|
59 | }
|
---|
60 |
|
---|
61 | [TestMethod]
|
---|
62 | [TestCategory("TreeHashing")]
|
---|
63 | public void AssociativityAddition() {
|
---|
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 });
|
---|
66 |
|
---|
67 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
68 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
69 |
|
---|
70 | Assert.AreEqual(hash1, hash2);
|
---|
71 | }
|
---|
72 |
|
---|
73 | [TestMethod]
|
---|
74 | [TestCategory("TreeHashing")]
|
---|
75 | public void RepeatedAddition() {
|
---|
76 | SymbolList s1 = new SymbolList(new[] { varA, varA, grammar.Addition, varA, grammar.Addition });
|
---|
77 | SymbolList s2 = new SymbolList(new[] { varA });
|
---|
78 |
|
---|
79 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
80 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
81 |
|
---|
82 | Assert.AreEqual(hash1, hash2);
|
---|
83 | }
|
---|
84 |
|
---|
85 | [TestMethod]
|
---|
86 | [TestCategory("TreeHashing")]
|
---|
87 | public void ComplexInequality() {
|
---|
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 });
|
---|
90 |
|
---|
91 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
92 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
93 |
|
---|
94 | Assert.AreNotEqual(hash1, hash2);
|
---|
95 | }
|
---|
96 |
|
---|
97 | [TestMethod]
|
---|
98 | [TestCategory("TreeHashing")]
|
---|
99 | public void NonterminalHashing() {
|
---|
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 });
|
---|
102 |
|
---|
103 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
104 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
105 |
|
---|
106 | Assert.AreEqual(hash1, hash2);
|
---|
107 | }
|
---|
108 |
|
---|
109 | [TestMethod]
|
---|
110 | [TestCategory("TreeHashing")]
|
---|
111 | public void InverseFactorCancelationSimple() {
|
---|
112 | // 1/a * b * a * a
|
---|
113 | SymbolList s1 = new SymbolList(new Symbol[] { varA, grammar.Inv, varB, grammar.Multiplication, varA, grammar.Multiplication, varA, grammar.Multiplication });
|
---|
114 | // a * b
|
---|
115 | SymbolList s2 = new SymbolList(new Symbol[] { varA, varB, grammar.Multiplication });
|
---|
116 |
|
---|
117 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
118 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
119 |
|
---|
120 | Assert.AreEqual(hash1, hash2);
|
---|
121 | }
|
---|
122 |
|
---|
123 | [TestMethod]
|
---|
124 | [TestCategory("TreeHashing")]
|
---|
125 | public void InverseFactorCancelationComplex() {
|
---|
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 });
|
---|
128 |
|
---|
129 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
130 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
131 |
|
---|
132 | Assert.AreEqual(hash1, hash2);
|
---|
133 | }
|
---|
134 |
|
---|
135 | // Constants
|
---|
136 | [TestMethod]
|
---|
137 | [TestCategory("TreeHashing")]
|
---|
138 | public void SimpleConst() {
|
---|
139 | SymbolList s1 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, grammar.Addition });
|
---|
140 | SymbolList s2 = new SymbolList(new Symbol[] { c, varA, grammar.Multiplication, c, varA, grammar.Multiplication, grammar.Addition, c, grammar.Addition });
|
---|
141 |
|
---|
142 | int hash1 = grammar.Hasher.CalcHashCode(s1);
|
---|
143 | int hash2 = grammar.Hasher.CalcHashCode(s2);
|
---|
144 |
|
---|
145 | Assert.AreEqual(hash1, hash2);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /* DEPRECATED; SINCE WE DO NOT ALLOW COMPOUND DIVISIONS
|
---|
149 | [TestMethod]
|
---|
150 | [TestCategory("TreeHashing")]
|
---|
151 | public void CompoundInverseCancellationToSingleInverse() {
|
---|
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 });
|
---|
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() {
|
---|
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 });
|
---|
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")]
|
---|
175 | public void UncancelableCompoundInverse() {
|
---|
176 | // 1 / ( 1/b + sin(a*c) )
|
---|
177 | SymbolList s1 = new SymbolList(new Symbol[] { varB, grammar.Inv, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition, grammar.Inv });
|
---|
178 | // b + sin(a*c)
|
---|
179 | SymbolList s2 = new SymbolList(new Symbol[] { varB, varA, varC, grammar.Multiplication, grammar.Sin, grammar.Addition });
|
---|
180 |
|
---|
181 | int hash1 = grammar.CalcHashCode(s1);
|
---|
182 | int hash2 = grammar.CalcHashCode(s2);
|
---|
183 |
|
---|
184 | Assert.AreNotEqual(hash1, hash2);
|
---|
185 | }*/
|
---|
186 | }
|
---|
187 | }
|
---|