[11239] | 1 | using System;
|
---|
| 2 | using System.Diagnostics;
|
---|
| 3 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 4 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 5 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Tests;
|
---|
| 6 | using HeuristicLab.Random;
|
---|
| 7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
| 8 |
|
---|
| 9 | namespace 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 | }
|
---|