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