source: branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Tests/BottomUpSimilarityCalculatorTest.cs @ 11229

Last change on this file since 11229 was 11229, checked in by bburlacu, 7 years ago

#2215: Refactored and simplified DirectedGraph and related components API, simplified the BottomUpSimilarityCalculator by not using a directed graph and vertices but a simpler object so that the similarity calculator is self-contained.

File size: 8.0 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Drawing;
5using System.Globalization;
6using System.Linq;
7using System.Text;
8using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
9using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Views;
10using HeuristicLab.Random;
11using Microsoft.VisualStudio.TestTools.UnitTesting;
12
13namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Tests {
14  [TestClass]
15  public class BottomUpSimilarityCalculatorTest {
16    private readonly BottomUpSimilarityCalculator busCalculator;
17    private readonly SymbolicExpressionImporter importer;
18
19    private const int N = 100;
20    private const int Rows = 1;
21    private const int Columns = 10;
22
23    public BottomUpSimilarityCalculatorTest() {
24      busCalculator = new BottomUpSimilarityCalculator();
25      importer = new SymbolicExpressionImporter();
26    }
27
28    [TestMethod]
29    [TestCategory("Problems.DataAnalysis.Symbolic")]
30    [TestProperty("Time", "short")]
31    public void TestBottomUpMatching() {
32      TestMatchedNodes("(+ 1 2)", "(+ 2 1)", 5);
33      TestMatchedNodes("(- 2 1)", "(- 1 2)", 2);
34      TestMatchedNodes("(* (variable 1 X1) (variable 1 X2))", "(* (+ (variable 1 X1) 1) (+ (variable 1 X2) 1))", 2);
35
36      TestMatchedNodes("(* (variable 1 X1) (variable 1 X2))", "(* (+ (variable 1 X1) 1) (variable 1 X2))", 2);
37
38      TestMatchedNodes("(+ (variable 1 a) (variable 1 b))", "(+ (variable 1 a) (variable 1 a))", 1);
39      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);
40
41      TestMatchedNodes(
42        "(* (+ 2.84 (exp (+ (log (/ (variable 2.0539 X5) (variable -9.2452e-1 X6))) (/ (variable 2.0539 X5) (variable -9.2452e-1 X6))))) 2.9081)",
43        "(* (- (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)",
44        9);
45
46      TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ -4.3072 (variable 2.4691 X7)) (exp 2.1033)))", 6);
47      TestMatchedNodes("(+ (exp 2.1033) (/ -4.3072 (variable 2.4691 X7)))", "(/ 1 (+ (/ (variable 2.4691 X7) -4.3072) (exp 2.1033)))", 4);
48
49      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))";
50      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)))";
51
52      TestMatchedNodes(expr1, expr2, 23);
53
54    }
55
56    private void TestMatchedNodes(string expr1, string expr2, int expected) {
57      var t1 = importer.Import(expr1);
58      var t2 = importer.Import(expr2);
59
60      var mapping = busCalculator.ComputeBottomUpMapping(t1.Root, t2.Root);
61      var c = mapping.Count;
62
63      if (c != expected) {
64        throw new Exception("Match count " + c + " is different than expected value " + expected);
65      }
66    }
67
68    [TestMethod]
69    [TestCategory("Problems.DataAnalysis.Symbolic")]
70    [TestProperty("Time", "long")]
71    public void TestBottomUpSimilarityCalculatorPerformance() {
72      var grammar = new TypeCoherentExpressionGrammar();
73      grammar.ConfigureAsDefaultRegressionGrammar();
74      var twister = new MersenneTwister(31415);
75      var ds = Util.CreateRandomDataset(twister, Rows, Columns);
76      var trees = Util.CreateRandomTrees(twister, ds, grammar, N, 1, 100, 0, 0);
77
78      double s = 0;
79      var sw = new Stopwatch();
80
81      sw.Start();
82      for (int i = 0; i < trees.Length - 1; ++i) {
83        for (int j = i + 1; j < trees.Length; ++j) {
84          s += busCalculator.CalculateSolutionSimilarity(trees[i], trees[j]);
85        }
86      }
87      sw.Stop();
88      Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds / 1000.0 + ", Avg. similarity: " + s);
89      Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second.");
90    }
91
92    private static string FormatMapping(ISymbolicExpressionTree t1, ISymbolicExpressionTree t2, Dictionary<ISymbolicExpressionTreeNode, ISymbolicExpressionTreeNode> map) {
93      var symbolNameMap = new Dictionary<string, string>
94    {
95      {"ProgramRootSymbol", "Prog"},
96      {"StartSymbol","RPB"},
97      {"Multiplication", "$\\times$"},
98      {"Division", "$\\div$"},
99      {"Addition", "$+$"},
100      {"Subtraction", "$-$"},
101      {"Exponential", "$\\exp$"},
102      {"Logarithm", "$\\log$"}
103    };
104
105      var sb = new StringBuilder();
106      var nodeIds = new Dictionary<ISymbolicExpressionTreeNode, string>();
107      int offset = 0;
108      var layoutEngine = new ReingoldTilfordLayoutEngine<ISymbolicExpressionTreeNode>(x => x.Subtrees);
109      var nodeCoordinates = layoutEngine.CalculateLayout(t1.Root).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
110
111      double ws = 0.5;
112      double hs = 0.5;
113
114      var nl = Environment.NewLine;
115      sb.Append("\\documentclass[class=minimal,border=0pt]{standalone}" + nl +
116                 "\\usepackage{tikz}" + nl +
117                 "\\begin{document}" + nl +
118                 "\\begin{tikzpicture}" + nl +
119                 "\\def\\ws{1}" + nl +
120                 "\\def\\hs{0.7}" + nl +
121                 "\\def\\offs{" + offset + "}" + nl);
122
123      foreach (var node in t1.IterateNodesBreadth()) {
124        var id = Guid.NewGuid().ToString();
125        nodeIds[node] = id;
126        var coord = nodeCoordinates[node];
127        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
128        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1} + \\offs,\\hs*{2}) {{{3}}};", nodeIds[node], ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
129      }
130
131      foreach (ISymbolicExpressionTreeNode t in t1.IterateNodesBreadth()) {
132        var n = t;
133        foreach (var s in t.Subtrees) {
134          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", nodeIds[n], nodeIds[s]));
135        }
136      }
137
138      nodeCoordinates = layoutEngine.CalculateLayout(t2.Root).ToDictionary(n => n.Content, n => new PointF(n.X, n.Y));
139
140      offset = 20;
141      sb.Append("\\def\\offs{" + offset + "}" + nl);
142      foreach (var node in t2.IterateNodesBreadth()) {
143        var id = Guid.NewGuid().ToString();
144        nodeIds[node] = id;
145        var coord = nodeCoordinates[node];
146        var nodeName = symbolNameMap.ContainsKey(node.Symbol.Name) ? symbolNameMap[node.Symbol.Name] : node.ToString();
147        sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\node ({0}) at (\\ws*{1} + \\offs,\\hs*{2}) {{{3}}};", nodeIds[node], ws * coord.X, -hs * coord.Y, EscapeLatexString(nodeName)));
148      }
149
150      foreach (ISymbolicExpressionTreeNode t in t2.IterateNodesBreadth()) {
151        var n = t;
152        foreach (var s in t.Subtrees) {
153          sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "\\draw ({0}) -- ({1});", nodeIds[n], nodeIds[s]));
154        }
155      }
156
157      foreach (var p in map) {
158        var id1 = nodeIds[p.Key];
159        var id2 = nodeIds[p.Value];
160
161        sb.Append(string.Format(CultureInfo.InvariantCulture, "\\path[draw,->,color=gray] ({0}) edge[bend left,dashed] ({1});" + Environment.NewLine, id1, id2));
162      }
163      sb.Append("\\end{tikzpicture}" + nl +
164                "\\end{document}" + nl);
165      return sb.ToString();
166    }
167
168    private static string EscapeLatexString(string s) {
169      return s.Replace("\\", "\\\\").Replace("{", "\\{").Replace("}", "\\}").Replace("_", "\\_");
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.