Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2886_SymRegGrammarEnumeration/Test/TreeHashingTest.cs @ 15866

Last change on this file since 15866 was 15849, checked in by lkammere, 7 years ago

#2886: Add constants to grammar.

File size: 7.2 KB
RevLine 
[15714]1using System.Linq;
2using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration;
3using HeuristicLab.Algorithms.DataAnalysis.SymRegGrammarEnumeration.GrammarEnumeration;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace Test {
7  [TestClass]
8  public class TreeHashingTest {
9
10    private Grammar grammar;
11    private TerminalSymbol varA;
12    private TerminalSymbol varB;
13    private TerminalSymbol varC;
[15849]14    private TerminalSymbol c;
[15714]15
16    [TestInitialize]
17    public void InitTest() {
18      grammar = new Grammar(new[] { "a", "b", "c" });
19
[15834]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");
[15849]23      c = grammar.Const;
[15714]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
[15832]32      int hash1 = grammar.Hasher.CalcHashCode(s1);
33      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15714]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
[15832]44      int hash1 = grammar.Hasher.CalcHashCode(s1);
45      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15714]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
[15832]56      int hash1 = grammar.Hasher.CalcHashCode(s1);
57      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15714]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
[15832]68      int hash1 = grammar.Hasher.CalcHashCode(s1);
69      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15714]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
[15832]80      int hash1 = grammar.Hasher.CalcHashCode(s1);
81      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15714]82
83      Assert.AreEqual(hash1, hash2);
84    }
[15725]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
[15832]92      int hash1 = grammar.Hasher.CalcHashCode(s1);
93      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15725]94
95      Assert.AreNotEqual(hash1, hash2);
96    }
97
[15746]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 });
[15725]103
[15832]104      int hash1 = grammar.Hasher.CalcHashCode(s1);
105      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15746]106
107      Assert.AreEqual(hash1, hash2);
108    }
109
[15784]110    [TestMethod]
111    [TestCategory("TreeHashing")]
112    public void InverseFactorCancelationSimple() {
[15791]113      // 1/a * b * a * a
[15784]114      SymbolString s1 = new SymbolString(new Symbol[] { varA, grammar.Inv, varB, grammar.Multiplication, varA, grammar.Multiplication, varA, grammar.Multiplication });
[15791]115      // a * b
[15784]116      SymbolString s2 = new SymbolString(new Symbol[] { varA, varB, grammar.Multiplication });
[15746]117
[15832]118      int hash1 = grammar.Hasher.CalcHashCode(s1);
119      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15784]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
[15832]130      int hash1 = grammar.Hasher.CalcHashCode(s1);
131      int hash2 = grammar.Hasher.CalcHashCode(s2);
[15784]132
133      Assert.AreEqual(hash1, hash2);
134    }
135
[15849]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
[15795]149    /* DEPRECATED; SINCE WE DO NOT ALLOW COMPOUND DIVISIONS
[15791]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")]
[15795]176    public void UncancelableCompoundInverse() {
[15791]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);
[15795]186    }*/
[15714]187  }
188}
Note: See TracBrowser for help on using the repository browser.