1 | using System;
|
---|
2 | using System.Diagnostics;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
5 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
6 | using HeuristicLab.Random;
|
---|
7 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.EvolutionTracking.Tests {
|
---|
10 | [TestClass]
|
---|
11 | public class QueryMatchPerformanceTest {
|
---|
12 | private const int MaxDepth = 12;
|
---|
13 | private const int MaxLength = 50;
|
---|
14 | private const int N = 50000;
|
---|
15 | private const int Repetitions = 5;
|
---|
16 |
|
---|
17 | [TestMethod]
|
---|
18 | public void TestQueryMatchPerformance() {
|
---|
19 | var grammar = new TypeCoherentExpressionGrammar();
|
---|
20 | grammar.ConfigureAsDefaultRegressionGrammar();
|
---|
21 | var creator = new ProbabilisticTreeCreator();
|
---|
22 | var random = new MersenneTwister();
|
---|
23 |
|
---|
24 | var trees = Enumerable.Range(0, N).Select(x => creator.CreateTree(random, grammar, MaxLength, MaxDepth)).ToList();
|
---|
25 | var comparer = new SymbolicExpressionTreeNodeEqualityComparer {
|
---|
26 | MatchConstantValues = false,
|
---|
27 | MatchVariableWeights = false,
|
---|
28 | MatchVariableNames = true
|
---|
29 | };
|
---|
30 | var qm = new QueryMatch(comparer) { MatchParents = true };
|
---|
31 | // warmup
|
---|
32 | int count = trees.Skip(1).Count(x => qm.Match(x, trees[0]));
|
---|
33 |
|
---|
34 | var sw = new Stopwatch();
|
---|
35 | sw.Start();
|
---|
36 | for (int i = 0; i < Repetitions; ++i)
|
---|
37 | count = trees.Skip(1).Count(x => x.Length >= trees[0].Length && qm.Match(x, trees[0]));
|
---|
38 | sw.Stop();
|
---|
39 |
|
---|
40 | Console.WriteLine("Count: " + count);
|
---|
41 | Console.WriteLine("Query matches per second: " + (N - 1) / (sw.ElapsedMilliseconds / 1000.0) * Repetitions);
|
---|
42 | Console.WriteLine("Total time: " + sw.ElapsedMilliseconds / 1000.0 + "s");
|
---|
43 |
|
---|
44 | sw.Restart();
|
---|
45 | for (int i = 0; i < Repetitions; ++i)
|
---|
46 | count = qm.GetMatchingTrees(trees.Skip(1), trees[0]).Count();
|
---|
47 | sw.Stop();
|
---|
48 |
|
---|
49 | Console.WriteLine("Count: " + count);
|
---|
50 | Console.WriteLine("Optimized query matches per second: " + (N - 1) / (sw.ElapsedMilliseconds / 1000.0) * Repetitions);
|
---|
51 | Console.WriteLine("Total time: " + sw.ElapsedMilliseconds / 1000.0 + "s");
|
---|
52 | }
|
---|
53 | }
|
---|
54 | }
|
---|