[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 {
|
---|
[11910] | 9 | private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator busCalculator;
|
---|
[11219] | 10 | private readonly SymbolicExpressionImporter importer;
|
---|
| 11 |
|
---|
[11894] | 12 | private const int N = 150;
|
---|
[11219] | 13 | private const int Rows = 1;
|
---|
| 14 | private const int Columns = 10;
|
---|
| 15 |
|
---|
| 16 | public BottomUpSimilarityCalculatorTest() {
|
---|
[11964] | 17 | busCalculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator();
|
---|
[11219] | 18 | importer = new SymbolicExpressionImporter();
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | [TestMethod]
|
---|
| 22 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 23 | [TestProperty("Time", "short")]
|
---|
[11916] | 24 | public void BottomUpTreeSimilarityCalculatorTestMapping() {
|
---|
[11219] | 25 | TestMatchedNodes("(+ 1 2)", "(+ 2 1)", 5);
|
---|
| 26 | TestMatchedNodes("(- 2 1)", "(- 1 2)", 2);
|
---|
[11221] | 27 | TestMatchedNodes("(* (variable 1 X1) (variable 1 X2))", "(* (+ (variable 1 X1) 1) (+ (variable 1 X2) 1))", 2);
|
---|
[11219] | 28 |
|
---|
[11220] | 29 | TestMatchedNodes("(* (variable 1 X1) (variable 1 X2))", "(* (+ (variable 1 X1) 1) (variable 1 X2))", 2);
|
---|
| 30 |
|
---|
| 31 | TestMatchedNodes("(+ (variable 1 a) (variable 1 b))", "(+ (variable 1 a) (variable 1 a))", 1);
|
---|
| 32 | TestMatchedNodes("(+ (+ (variable 1 a) (variable 1 b)) (variable 1 b))", "(+ (* (+ (variable 1 a) (variable 1 b)) (variable 1 b)) (+ (+ (variable 1 a) (variable 1 b)) (variable 1 b)))", 5);
|
---|
| 33 |
|
---|
[11219] | 34 | TestMatchedNodes(
|
---|
| 35 | "(* (+ 2.84 (exp (+ (log (/ (variable 2.0539 X5) (variable -9.2452e-1 X6))) (/ (variable 2.0539 X5) (variable -9.2452e-1 X6))))) 2.9081)",
|
---|
| 36 | "(* (- (variable 9.581e-1 X6) (+ (- (variable 5.1491e-1 X5) 1.614e+1) (+ (/ (variable 2.0539 X5) (variable -9.2452e-1 X6)) (log (/ (variable 2.0539 X5) (variable -9.2452e-1 X6)))))) 2.9081)",
|
---|
| 37 | 9);
|
---|
| 38 |
|
---|
[11986] | 39 | TestMatchedNodes("(* (* (* (variable 1.68 x) (* (variable 1.68 x) (variable 2.55 x))) (variable 1.68 x)) (* (* (variable 1.68 x) (* (variable 1.68 x) (* (variable 1.68 x) (variable 2.55 x)))) (variable 2.55 x)))", "(* (variable 2.55 x) (* (variable 1.68 x) (* (variable 1.68 x) (* (variable 1.68 x) (variable 2.55 x)))))", 9);
|
---|
| 40 |
|
---|
[11219] | 41 | TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ -4.3072 (variable 2.4691 X7)) (exp 2.1033)))", 6);
|
---|
| 42 | TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ (variable 2.4691 X7) -4.3072) (exp 2.1033)))", 4);
|
---|
[11225] | 43 |
|
---|
[11229] | 44 | const string expr1 = "(* (- 1.2175e+1 (+ (/ (exp -1.4134e+1) (exp 9.2013)) (exp (log (exp (/ (exp (- (* -4.2461 (variable 2.2634 X5)) (- -9.6267e-1 3.3243))) (- (/ (/ (variable 1.0883 X1) (variable 6.9620e-1 X2)) (log 1.3011e+1)) (variable -4.3098e-1 X7)))))))) (log 1.3011e+1))";
|
---|
| 45 | const string expr2 = "(* (- 1.2175e+1 (+ (/ (/ (+ (variable 3.0140 X9) (variable 1.3430 X8)) -1.0864e+1) (exp 9.2013)) (exp (log (exp (/ (exp (- (* -4.2461 (variable 2.2634 X5)) (- -9.6267e-1 3.3243))) (- (/ (/ (variable 1.0883 X1) (variable 6.9620e-1 X2)) (log 1.3011e+1)) (variable -4.3098e-1 X7)))))))) (exp (variable 4.0899e-1 X7)))";
|
---|
[11225] | 46 |
|
---|
| 47 | TestMatchedNodes(expr1, expr2, 23);
|
---|
| 48 |
|
---|
[11219] | 49 | }
|
---|
| 50 |
|
---|
| 51 | private void TestMatchedNodes(string expr1, string expr2, int expected) {
|
---|
| 52 | var t1 = importer.Import(expr1);
|
---|
| 53 | var t2 = importer.Import(expr2);
|
---|
| 54 |
|
---|
[11221] | 55 | var mapping = busCalculator.ComputeBottomUpMapping(t1.Root, t2.Root);
|
---|
[11219] | 56 | var c = mapping.Count;
|
---|
| 57 |
|
---|
| 58 | if (c != expected) {
|
---|
| 59 | throw new Exception("Match count " + c + " is different than expected value " + expected);
|
---|
| 60 | }
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | [TestMethod]
|
---|
| 64 | [TestCategory("Problems.DataAnalysis.Symbolic")]
|
---|
| 65 | [TestProperty("Time", "long")]
|
---|
[11916] | 66 | public void BottomUpTreeSimilarityCalculatorTestPerformance() {
|
---|
[11219] | 67 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
| 68 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
| 69 | var twister = new MersenneTwister(31415);
|
---|
| 70 | var ds = Util.CreateRandomDataset(twister, Rows, Columns);
|
---|
| 71 | var trees = Util.CreateRandomTrees(twister, ds, grammar, N, 1, 100, 0, 0);
|
---|
| 72 |
|
---|
| 73 | double s = 0;
|
---|
| 74 | var sw = new Stopwatch();
|
---|
| 75 |
|
---|
| 76 | sw.Start();
|
---|
| 77 | for (int i = 0; i < trees.Length - 1; ++i) {
|
---|
| 78 | for (int j = i + 1; j < trees.Length; ++j) {
|
---|
[11486] | 79 | s += busCalculator.CalculateSimilarity(trees[i], trees[j]);
|
---|
[11219] | 80 | }
|
---|
| 81 | }
|
---|
[11239] | 82 |
|
---|
[11219] | 83 | sw.Stop();
|
---|
[11239] | 84 | Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds / 1000.0 + ", Avg. similarity: " + s / (N * (N - 1) / 2));
|
---|
[11219] | 85 | Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second.");
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 | }
|
---|