Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Tests/HeuristicLab.Problems.DataAnalysis.Symbolic-3.4/SymbolicExpressionTreeBottomUpSimilarityCalculatorTest.cs @ 14353

Last change on this file since 14353 was 14353, checked in by bburlacu, 8 years ago

#2685: Add correction step for values miscalculated due to cyclical symbol dependencies in the grammar. Updated unit test.

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