[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 {
|
---|
[16283] | 9 | private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator similarityCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator() { MatchConstantValues = false, MatchVariableWeights = false };
|
---|
| 10 | private readonly SymbolicExpressionImporter importer = new SymbolicExpressionImporter();
|
---|
[11219] | 11 |
|
---|
[16283] | 12 | private const int N = 1000;
|
---|
[11219] | 13 | private const int Rows = 1;
|
---|
| 14 | private const int Columns = 10;
|
---|
| 15 |
|
---|
| 16 | public BottomUpSimilarityCalculatorTest() {
|
---|
[16283] | 17 | var parser = new InfixExpressionParser();
|
---|
[11219] | 18 | }
|
---|
| 19 |
|
---|
| 20 | [TestMethod]
|
---|
| 21 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 22 | [TestProperty("Time", "short")]
|
---|
[11916] | 23 | public void BottomUpTreeSimilarityCalculatorTestMapping() {
|
---|
[16283] | 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);
|
---|
[11219] | 28 |
|
---|
[16283] | 29 | TestMatchedNodes("(- 1 1)", "(- 2 2)", 0, strict: true);
|
---|
| 30 | TestMatchedNodes("(- 1 1)", "(- 2 2)", 3, strict: false);
|
---|
[11220] | 31 |
|
---|
[16283] | 32 | TestMatchedNodes("(- 2 1)", "(- 1 2)", 2, strict: true);
|
---|
| 33 | TestMatchedNodes("(- 2 1)", "(- 1 2)", 3, strict: false);
|
---|
[11219] | 34 | }
|
---|
| 35 |
|
---|
[16283] | 36 | private void TestMatchedNodes(string expr1, string expr2, int expected, bool strict) {
|
---|
[11219] | 37 | var t1 = importer.Import(expr1);
|
---|
| 38 | var t2 = importer.Import(expr2);
|
---|
| 39 |
|
---|
[16283] | 40 | var map = SymbolicExpressionTreeBottomUpSimilarityCalculator.ComputeBottomUpMapping(t1, t2, strict);
|
---|
[11219] | 41 |
|
---|
[16283] | 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})");
|
---|
[11219] | 44 | }
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | [TestMethod]
|
---|
| 48 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 49 | [TestProperty("Time", "long")]
|
---|
[11916] | 50 | public void BottomUpTreeSimilarityCalculatorTestPerformance() {
|
---|
[11219] | 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) {
|
---|
[16283] | 63 | s += similarityCalculator.CalculateSimilarity(trees[i], trees[j]);
|
---|
[11219] | 64 | }
|
---|
| 65 | }
|
---|
[11239] | 66 |
|
---|
[11219] | 67 | sw.Stop();
|
---|
[11239] | 68 | Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds / 1000.0 + ", Avg. similarity: " + s / (N * (N - 1) / 2));
|
---|
[11219] | 69 | Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second.");
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|