Changeset 16867


Ignore:
Timestamp:
04/23/19 16:08:38 (4 months ago)
Author:
bburlacu
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionTreeBottomUpSimilarityCalculatorTest.cs

    r16283 r16867  
    11using System;
    22using System.Diagnostics;
     3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
    34using HeuristicLab.Random;
    45using Microsoft.VisualStudio.TestTools.UnitTesting;
     
    78  [TestClass]
    89  public class BottomUpSimilarityCalculatorTest {
    9     private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator() { MatchConstantValues = false, MatchVariableWeights = false };
    1010    private readonly SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
     11    private readonly InfixExpressionParser parser = new InfixExpressionParser();
    1112
    12     private const int N = 1000;
     13    private const int N = 200;
    1314    private const int Rows = 1;
    1415    private const int Columns = 10;
     
    2223    [TestProperty("Time", "short")]
    2324    public void BottomUpTreeSimilarityCalculatorTestMapping() {
    24       TestMatchedNodes("(+ 1 1)", "(+ 2 2)", 0, strict: true);
    25       TestMatchedNodes("(+ 1 1)", "(+ 2 2)", 3, strict: false);
    26       TestMatchedNodes("(+ 1 1)", "(+ 1 2)", 1, strict: true);
    27       TestMatchedNodes("(+ 2 1)", "(+ 1 2)", 3, strict: true);
     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);
    2829
    29       TestMatchedNodes("(- 1 1)", "(- 2 2)", 0, strict: true);
    30       TestMatchedNodes("(- 1 1)", "(- 2 2)", 3, strict: false);
     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
    3132
    32       TestMatchedNodes("(- 2 1)", "(- 1 2)", 2, strict: true);
    33       TestMatchedNodes("(- 2 1)", "(- 1 2)", 3, strict: false);
     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);
    3458    }
    3559
    3660    private void TestMatchedNodes(string expr1, string expr2, int expected, bool strict) {
    37       var t1 = importer.Import(expr1);
    38       var t2 = importer.Import(expr2);
     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));
    3966
    4067      var map = SymbolicExpressionTreeBottomUpSimilarityCalculator.ComputeBottomUpMapping(t1, t2, strict);
     68      Console.WriteLine($"Count: {map.Count}");
    4169
    4270      if (map.Count != expected) {
    43         throw new Exception($"Match count {map.Count} is different than expected value {expected} for expressions:\n{expr1} and {expr2} (strict = {strict})");
     71        throw new Exception($"Match count {map.Count} is different than expected value {expected} for expressions:\n{expr1} and {expr2} (strict = {strict})\n");
    4472      }
    4573    }
     
    5886      var sw = new Stopwatch();
    5987
     88      var similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchVariableWeights = false, MatchConstantValues = false };
     89
    6090      sw.Start();
    6191      for (int i = 0; i < trees.Length - 1; ++i) {
     
    6999      Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second.");
    70100    }
     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    }
    71138  }
    72139}
Note: See TracChangeset for help on using the changeset viewer.