Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionTreeBottomUpSimilarityCalculatorTest.cs @ 16867

Last change on this file since 16867 was 16867, checked in by bburlacu, 5 years ago

#2959: Add more unit tests for tree matching, and test bottom-up calculator against hash-based calculator for consistency.

File size: 6.3 KB
Line 
1using System;
2using System.Diagnostics;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using HeuristicLab.Random;
5using Microsoft.VisualStudio.TestTools.UnitTesting;
6
7namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
8  [TestClass]
9  public class BottomUpSimilarityCalculatorTest {
10    private readonly SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
11    private readonly InfixExpressionParser parser = new InfixExpressionParser();
12
13    private const int N = 200;
14    private const int Rows = 1;
15    private const int Columns = 10;
16
17    public BottomUpSimilarityCalculatorTest() {
18      var parser = new InfixExpressionParser();
19    }
20
21    [TestMethod]
22    [TestCategory("Problems.DataAnalysis.Symbolic")]
23    [TestProperty("Time", "short")]
24    public void BottomUpTreeSimilarityCalculatorTestMapping() {
25      TestMatchedNodes("1 + 1", "2 + 2", 0, strict: true);
26      TestMatchedNodes("1 + 1", "2 + 2", 3, strict: false);
27      TestMatchedNodes("1 + 1", "1 + 2", 1, strict: true);
28      TestMatchedNodes("1 + 2", "2 + 1", 3, strict: true);
29
30      TestMatchedNodes("1 - 1", "2 - 2", 0, strict: true);
31      TestMatchedNodes("1 - 1", "2 - 2", 4, strict: false); // 4, because of the way strings are parsed into trees by the infix parser
32
33      TestMatchedNodes("2 - 1", "1 - 2", 2, strict: true);
34      TestMatchedNodes("2 - 1", "1 - 2", 4, strict: false);
35
36      TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X3 * X4)", 7, strict: true);
37      TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X3 * X4)", 7, strict: false);
38
39      TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X5 * X6)", 3, strict: true);
40      TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) + (X5 * X6)", 3, strict: false);
41
42      TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) - (X5 * X6)", 3, strict: true);
43      TestMatchedNodes("(X1 * X2) + (X3 * X4)", "(X1 * X2) - (X5 * X6)", 3, strict: false);
44
45      TestMatchedNodes("SIN(SIN(SIN(X1)))", "SIN(SIN(SIN(X1)))", 4, strict: true);
46      TestMatchedNodes("SIN(SIN(SIN(X1)))", "COS(SIN(SIN(X1)))", 3, strict: true);
47      TestMatchedNodes("SIN(SIN(SIN(X1)))", "COS(COS(SIN(X1)))", 2, strict: true);
48      TestMatchedNodes("SIN(SIN(SIN(X1)))", "COS(COS(COS(X1)))", 1, strict: true);
49
50      const string lhs = "(0.006153 + (X9 * X7 * X2 * 0.229506) + (X6 * X10 * X3 * 0.924598) + (X2 * X1 * 0.951272) + (X4 * X3 * 0.992570) + (X6 * X5 * 1.027299))";
51      const string rhs = "(0.006153 + (X10 * X7 * X2 * 0.229506) + (X6 * X10 * X3 * 0.924598) + (X2 * X1 * 0.951272) + (X4 * X3 * 0.992570) + (X6 * X5 * 1.027299))";
52
53      TestMatchedNodes(lhs, lhs, 24, strict: true);
54      TestMatchedNodes(lhs, lhs, 24, strict: false);
55
56      TestMatchedNodes(lhs, rhs, 21, strict: true);
57      TestMatchedNodes(lhs, rhs, 21, strict: false);
58    }
59
60    private void TestMatchedNodes(string expr1, string expr2, int expected, bool strict) {
61      var t1 = parser.Parse(expr1);
62      var t2 = parser.Parse(expr2);
63
64      Console.WriteLine(SymbolicExpressionTreeHierarchicalFormatter.FormatTree(t1));
65      Console.WriteLine(SymbolicExpressionTreeHierarchicalFormatter.FormatTree(t2));
66
67      var map = SymbolicExpressionTreeBottomUpSimilarityCalculator.ComputeBottomUpMapping(t1, t2, strict);
68      Console.WriteLine($"Count: {map.Count}");
69
70      if (map.Count != expected) {
71        throw new Exception($"Match count {map.Count} is different than expected value {expected} for expressions:\n{expr1} and {expr2} (strict = {strict})\n");
72      }
73    }
74
75    [TestMethod]
76    [TestCategory("Problems.DataAnalysis.Symbolic")]
77    [TestProperty("Time", "long")]
78    public void BottomUpTreeSimilarityCalculatorTestPerformance() {
79      var grammar = new TypeCoherentExpressionGrammar();
80      grammar.ConfigureAsDefaultRegressionGrammar();
81      var twister = new MersenneTwister(31415);
82      var ds = Util.CreateRandomDataset(twister, Rows, Columns);
83      var trees = Util.CreateRandomTrees(twister, ds, grammar, N, 1, 100, 0, 0);
84
85      double s = 0;
86      var sw = new Stopwatch();
87
88      var similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchVariableWeights = false, MatchConstantValues = false };
89
90      sw.Start();
91      for (int i = 0; i < trees.Length - 1; ++i) {
92        for (int j = i + 1; j < trees.Length; ++j) {
93          s += similarityCalculator.CalculateSimilarity(trees[i], trees[j]);
94        }
95      }
96
97      sw.Stop();
98      Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds / 1000.0 + ", Avg. similarity: " + s / (N * (N - 1) / 2));
99      Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second.");
100    }
101
102    [TestMethod]
103    [TestCategory("Problems.DataAnalysis.Symbolic")]
104    [TestProperty("Time", "long")]
105    public void BottomUpTreeSimilarityCalculatorStrictMatchingConsistency() {
106      TestMatchingConsistency(strict: true);
107    }
108
109    [TestMethod]
110    [TestCategory("Problems.DataAnalysis.Symbolic")]
111    [TestProperty("Time", "long")]
112    public void BottomUpTreeSimilarityCalculatorRelaxedMatchingConsistency() {
113      TestMatchingConsistency(strict: false);
114    }
115
116    private static void TestMatchingConsistency(bool strict = false) {
117      var grammar = new TypeCoherentExpressionGrammar();
118      grammar.ConfigureAsDefaultRegressionGrammar();
119      var twister = new MersenneTwister(31415);
120      var ds = Util.CreateRandomDataset(twister, Rows, Columns);
121      var trees = Util.CreateRandomTrees(twister, ds, grammar, N, 1, 100, 0, 0);
122
123      var similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchConstantValues = strict, MatchVariableWeights = strict };
124      var bottomUpSimilarity = 0d;
125      for (int i = 0; i < trees.Length - 1; ++i) {
126        for (int j = i + 1; j < trees.Length; ++j) {
127          bottomUpSimilarity += similarityCalculator.CalculateSimilarity(trees[i], trees[j]);
128        }
129      }
130      bottomUpSimilarity /= N * (N - 1) / 2;
131
132      var hashBasedSimilarity = SymbolicExpressionTreeHash.ComputeAverageSimilarity(trees, false, strict);
133
134      Assert.AreEqual(bottomUpSimilarity, hashBasedSimilarity, 1e-6);
135
136      Console.WriteLine($"Bottom-up similarity: {bottomUpSimilarity}, hash-based similarity: {hashBasedSimilarity}");
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.