Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2974_Constants_Optimization/Benchmarks/Benchmarks/ConstantsOptimizationPerformance.cs @ 16525

Last change on this file since 16525 was 16525, checked in by bburlacu, 5 years ago

#2974: Add benchmarks solution for testing constant optimization performance.

File size: 2.8 KB
Line 
1using BenchmarkDotNet.Attributes;
2using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
3using HeuristicLab.Problems.DataAnalysis;
4using HeuristicLab.Problems.DataAnalysis.Symbolic;
5using HeuristicLab.Problems.DataAnalysis.Symbolic.ConstantsOptimization;
6using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
7using HeuristicLab.Problems.Instances.DataAnalysis;
8using HeuristicLab.Random;
9using System.Collections.Generic;
10using System.Linq;
11
12namespace HeuristicLab.Benchmarks {
13  [ClrJob]
14  [MinColumn, MaxColumn, MeanColumn, MedianColumn]
15  public class ConstantsOptimizationPerformance {
16    private static readonly int seed = 1234;
17    private static readonly int maxTreeSize = 50;
18    private static readonly int nTrees = 1000;
19
20    private ISymbolicExpressionTree[] trees;
21    private IRegressionProblemData problemData;
22    private TypeCoherentExpressionGrammar grammar;
23    private int[] rows;
24
25    private readonly ISymbolicDataAnalysisExpressionTreeInterpreter interpreter = new SymbolicDataAnalysisExpressionTreeLinearInterpreter();
26
27    [GlobalSetup]
28    public void Setup() {
29      var twister = new MersenneTwister((uint)seed);
30      problemData = new RegressionRealWorldInstanceProvider().LoadData(new Tower());
31      rows = Enumerable.Range(0, Rows).ToArray();
32
33      grammar = new TypeCoherentExpressionGrammar();
34      grammar.ConfigureAsDefaultRegressionGrammar();
35
36      trees = Util.CreateRandomTrees(twister, problemData.Dataset, grammar, nTrees, 1, maxTreeSize, 0, 0);
37      foreach (SymbolicExpressionTree tree in trees) {
38        Util.InitTree(tree, twister, problemData.AllowedInputVariables.ToList());
39      }
40    }
41
42    //public IEnumerable<int> RowsValues => Util.IntRange(100, 5000, 100, closed: true);
43    //public IEnumerable<int> RowsValues => new[] { 100, 250, 500, 750, 1000, 1500, 2000, 3000, 4000, 4999 };
44    public IEnumerable<int> RowsValues => new[] { 4999 };
45    public IEnumerable<int> IterationsValues => Util.IntRange(5, 30, 5, closed: true);
46    //public IEnumerable<int> IterationsValues => new[] { 0 };
47
48    [ParamsSource(nameof(RowsValues))]
49    public int Rows { get; set; }
50
51    [ParamsSource(nameof(IterationsValues))]
52    public int Iterations { get; set; }
53
54    [Benchmark]
55    public double Tower() {
56      double avgQuality = 0;
57
58      if (Iterations > 0) {
59        for (int i = 0; i < trees.Length; i++) {
60          avgQuality += LMConstantsOptimizer.OptimizeConstants(trees[i], problemData.Dataset, problemData.TargetVariable, rows, true, Iterations);
61        }
62      } else {
63        for (int i = 0; i < trees.Length; i++) {
64          avgQuality += SymbolicRegressionSingleObjectivePearsonRSquaredEvaluator.Calculate(interpreter, trees[i], double.MinValue, double.MaxValue, problemData, rows, true);
65        }
66      }
67      return avgQuality / trees.Length;
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.