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