[16299] | 1 | using BenchmarkDotNet.Attributes;
|
---|
| 2 | using HeuristicLab.Core;
|
---|
| 3 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 4 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 5 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 6 | using HeuristicLab.Random;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.Benchmarks {
|
---|
| 9 | // [ClrJob]
|
---|
| 10 | //[SimpleJob(RunStrategy.ColdStart, targetCount: 5)]
|
---|
| 11 | [ShortRunJob]
|
---|
| 12 | [MinColumn, MaxColumn, MeanColumn, MedianColumn]
|
---|
| 13 | public class TreeDistancePerformance {
|
---|
| 14 |
|
---|
| 15 | private readonly IRandom random = new FastRandom(1234);
|
---|
| 16 | private readonly Dataset dataset;
|
---|
| 17 | private readonly ISymbolicDataAnalysisGrammar grammar;
|
---|
| 18 | private ISymbolicExpressionTree[] trees;
|
---|
| 19 |
|
---|
| 20 | private readonly SymbolicExpressionTreeBottomUpSimilarityCalculator calculator = new SymbolicExpressionTreeBottomUpSimilarityCalculator { MatchConstantValues = false, MatchVariableWeights = false };
|
---|
| 21 |
|
---|
| 22 | public TreeDistancePerformance() {
|
---|
| 23 | dataset = Util.CreateRandomDataset(random, 1, 10);
|
---|
| 24 | var typeCoherentGrammar = new TypeCoherentExpressionGrammar();
|
---|
| 25 | typeCoherentGrammar.ConfigureAsDefaultRegressionGrammar();
|
---|
| 26 | grammar = typeCoherentGrammar;
|
---|
| 27 | }
|
---|
| 28 |
|
---|
| 29 | private const int Columns = 10;
|
---|
| 30 | private const int minSize = 1;
|
---|
| 31 |
|
---|
| 32 | [Params(5000)]
|
---|
| 33 | public int Trees { get; set; }
|
---|
| 34 |
|
---|
| 35 | [Params(100)]
|
---|
| 36 | public int MaxLength { get; set; }
|
---|
| 37 |
|
---|
| 38 | [GlobalSetup]
|
---|
| 39 | public void Setup() {
|
---|
| 40 | trees = Util.CreateRandomTrees(random, dataset, grammar, Trees, minSize, MaxLength, 0, 0);
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | [Benchmark(Baseline = true)]
|
---|
| 44 | public double BottomUpSimilarity() {
|
---|
| 45 | var sim = 0d;
|
---|
| 46 | var total = trees.Length * (trees.Length - 1) / 2;
|
---|
| 47 |
|
---|
| 48 | for (int i = 0; i < trees.Length - 1; ++i) {
|
---|
| 49 | for (int j = i + 1; j < trees.Length; ++j) {
|
---|
| 50 | sim += calculator.CalculateSimilarity(trees[i], trees[j]);
|
---|
| 51 | }
|
---|
| 52 | }
|
---|
| 53 | return sim / total;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | [Benchmark]
|
---|
| 57 | public double HashBasedSimilarity() {
|
---|
| 58 | var sim = 0d;
|
---|
| 59 |
|
---|
| 60 | var total = trees.Length * (trees.Length - 1) / 2;
|
---|
| 61 |
|
---|
| 62 | for (int i = 0; i < trees.Length - 1; ++i) {
|
---|
| 63 | for (int j = i + 1; j < trees.Length; ++j) {
|
---|
| 64 | sim += SymbolicExpressionTreeHash.ComputeSimilarity(trees[i], trees[j]);
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | return sim / total;
|
---|
| 68 | }
|
---|
| 69 |
|
---|
| 70 | [Benchmark]
|
---|
| 71 | public double HashBasedSimilarityBatched() => SymbolicExpressionTreeHash.ComputeAverageSimilarity(trees);
|
---|
| 72 | }
|
---|
| 73 | }
|
---|