source: misc/tools/HeuristicLab.Benchmarks/HeuristicLab.Benchmarks/Problems.DataAnalysis.Symbolic/TreeDistanceAlgorithmPerformance.cs @ 16299

Last change on this file since 16299 was 16299, checked in by bburlacu, 4 years ago

#2963: Add first version of HeuristicLab.Benchmarks

File size: 2.4 KB
Line 
1using BenchmarkDotNet.Attributes;
2using HeuristicLab.Core;
3using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
4using HeuristicLab.Problems.DataAnalysis;
5using HeuristicLab.Problems.DataAnalysis.Symbolic;
6using HeuristicLab.Random;
7
8namespace 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}
Note: See TracBrowser for help on using the repository browser.