source: branches/HeuristicLab.BottomUpTreeDistance/HeuristicLab.Tests/MaxCommonSubtreeSimilarityCalculatorTest.cs @ 11239

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

#2215:

  • Renamed BottomUpSimilarityCalculator to BottomUpTreeSimilarityCalculator.
  • Refactored the BottomUpTreeSimilarityCalculator to accept a configurable list of commutative symbols (the children of commutative symbols need to be sorted according to their label).
  • Added MaxCommonSubtreeSimilarityCalculator performance test
  • Updated BottomUpTreeSimilarityCalculatorTest
File size: 3.0 KB
Line 
1using System;
2using System.Diagnostics;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using HeuristicLab.Problems.DataAnalysis.Symbolic;
5using HeuristicLab.Problems.DataAnalysis.Symbolic.Tests;
6using HeuristicLab.Random;
7using Microsoft.VisualStudio.TestTools.UnitTesting;
8
9namespace HeuristicLab.Tests {
10  /// <summary>
11  /// Summary description for MaxCommonSubtreeSimilarityCalculatorTest
12  /// </summary>
13  [TestClass]
14  public class MaxCommonSubtreeSimilarityCalculatorTest {
15    private ISymbolicExpressionTreeNodeSimilarityComparer comparer;
16
17    private const int N = 100;
18    private const int Rows = 1;
19    private const int Columns = 10;
20
21    public MaxCommonSubtreeSimilarityCalculatorTest() {
22      comparer = new SymbolicExpressionTreeNodeSimilarityComparer();
23    }
24
25    private TestContext testContextInstance;
26
27    //    /// <summary>
28    //    ///Gets or sets the test context which provides
29    //    ///information about and functionality for the current test run.
30    //    ///</summary>
31    //    public TestContext TestContext {
32    //      get {
33    //        return testContextInstance;
34    //      }
35    //      set {
36    //        testContextInstance = value;
37    //      }
38    //    }
39
40    #region Additional test attributes
41    //
42    // You can use the following additional attributes as you write your tests:
43    //
44    // Use ClassInitialize to run code before running the first test in the class
45    // [ClassInitialize()]
46    // public static void MyClassInitialize(TestContext testContext) { }
47    //
48    // Use ClassCleanup to run code after all tests in a class have run
49    // [ClassCleanup()]
50    // public static void MyClassCleanup() { }
51    //
52    // Use TestInitialize to run code before running each test
53    // [TestInitialize()]
54    // public void MyTestInitialize() { }
55    //
56    // Use TestCleanup to run code after each test has run
57    // [TestCleanup()]
58    // public void MyTestCleanup() { }
59    //
60    #endregion
61
62    [TestMethod]
63    public void TestMaxCommonSubtreeSimilarityCalculatorPerformance() {
64      var grammar = new TypeCoherentExpressionGrammar();
65      grammar.ConfigureAsDefaultRegressionGrammar();
66      var twister = new MersenneTwister(31415);
67      var ds = Util.CreateRandomDataset(twister, Rows, Columns);
68      var trees = Util.CreateRandomTrees(twister, ds, grammar, N, 1, 100, 0, 0);
69
70      double s = 0;
71      var sw = new Stopwatch();
72
73      sw.Start();
74      for (int i = 0; i < trees.Length - 1; ++i) {
75        for (int j = i + 1; j < trees.Length; ++j) {
76          s += MaxCommonSubtreeSimilarityCalculator.MaxCommonSubtreeSimilarity(trees[i], trees[j], comparer);
77        }
78      }
79      sw.Stop();
80      Console.WriteLine("Elapsed time: " + sw.ElapsedMilliseconds / 1000.0 + ", Avg. similarity: " + s / (N * (N - 1) / 2));
81      Console.WriteLine(N * (N + 1) / (2 * sw.ElapsedMilliseconds / 1000.0) + " similarity calculations per second.");
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.