Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2925_AutoDiffForDynamicalModels/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionTreeBottomUpSimilarityCalculatorTest.cs @ 16555

Last change on this file since 16555 was 16386, checked in by gkronber, 6 years ago

#2925 merged changes r15972:16382 from trunk to branch

File size: 3.0 KB
Line 
1using System;
2using System.Diagnostics;
3using HeuristicLab.Random;
4using Microsoft.VisualStudio.TestTools.UnitTesting;
5
6namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
7  [TestClass]
8  public class BottomUpSimilarityCalculatorTest {
9    private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator() { MatchConstantValues = false, MatchVariableWeights = false };
10    private readonly SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
11
12    private const int N = 1000;
13    private const int Rows = 1;
14    private const int Columns = 10;
15
16    public BottomUpSimilarityCalculatorTest() {
17      var parser = new InfixExpressionParser();
18    }
19
20    [TestMethod]
21    [TestCategory("Problems.DataAnalysis.Symbolic")]
22    [TestProperty("Time", "short")]
23    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);
28
29      TestMatchedNodes("(- 1 1)", "(- 2 2)", 0, strict: true);
30      TestMatchedNodes("(- 1 1)", "(- 2 2)", 3, strict: false);
31
32      TestMatchedNodes("(- 2 1)", "(- 1 2)", 2, strict: true);
33      TestMatchedNodes("(- 2 1)", "(- 1 2)", 3, strict: false);
34    }
35
36    private void TestMatchedNodes(string expr1, string expr2, int expected, bool strict) {
37      var t1 = importer.Import(expr1);
38      var t2 = importer.Import(expr2);
39
40      var map = SymbolicExpressionTreeBottomUpSimilarityCalculator.ComputeBottomUpMapping(t1, t2, strict);
41
42      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})");
44      }
45    }
46
47    [TestMethod]
48    [TestCategory("Problems.DataAnalysis.Symbolic")]
49    [TestProperty("Time", "long")]
50    public void BottomUpTreeSimilarityCalculatorTestPerformance() {
51      var grammar = new TypeCoherentExpressionGrammar();
52      grammar.ConfigureAsDefaultRegressionGrammar();
53      var twister = new MersenneTwister(31415);
54      var ds = Util.CreateRandomDataset(twister, Rows, Columns);
55      var trees = Util.CreateRandomTrees(twister, ds, grammar, N, 1, 100, 0, 0);
56
57      double s = 0;
58      var sw = new Stopwatch();
59
60      sw.Start();
61      for (int i = 0; i < trees.Length - 1; ++i) {
62        for (int j = i + 1; j < trees.Length; ++j) {
63          s += similarityCalculator.CalculateSimilarity(trees[i], trees[j]);
64        }
65      }
66
67      sw.Stop();
68      Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds / 1000.0 + ", Avg. similarity: " + s / (N * (N - 1) / 2));
69      Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second.");
70    }
71  }
72}
Note: See TracBrowser for help on using the repository browser.