Free cookie consent management tool by TermsFeed Policy Generator

source: misc/tools/HeuristicLab.Benchmarks/HeuristicLab.Benchmarks/Encodings.SymbolicExpressionTree/SubtreeCrossoverPerformance.cs @ 16300

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

#2963: Add README.txt and remove unsupported code from benchmarks.

File size: 2.1 KB
RevLine 
[16299]1using System;
2using System.Collections.Generic;
3using BenchmarkDotNet.Attributes;
4using HeuristicLab.Core;
5using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
6using HeuristicLab.Random;
7
8namespace HeuristicLab.Benchmarks {
9  [CoreJob, ClrJob]
10  [MinColumn, MaxColumn, MeanColumn, MedianColumn]
11  public class SubtreeCrossoverPerformance {
12
13    private List<ISymbolicExpressionTree> trees = new List<ISymbolicExpressionTree>();
14    private readonly IRandom random = new MersenneTwister(1234);
15    private readonly ISymbolicExpressionGrammar grammar = Grammars.CreateArithmeticAndAdfGrammar();
16
17    [GlobalSetup]
18    public void Setup() {
19      for (int i = 0; i < PopulationSize; i++) {
20        trees.Add(ProbabilisticTreeCreator.Create(random, grammar, MaxLength, MaxDepth));
21        for (int j = random.Next(3); j < 3; j++)
22          SubroutineCreater.CreateSubroutine(random, trees[i], MaxLength, MaxDepth, 3, 3);
23      }
24    }
25
26    [Params(100)]
27    public int MaxLength { get; set; }
28
29    [Params(10)]
30    public int MaxDepth { get; set; }
31
32    [Params(1_000)]
33    public int PopulationSize { get; set; }
34
35    [Params(5)]
36    public int Generations { get; set; }
37
38    [Benchmark]
39    public void StandardCrossover() => TestCrossoverPerformance(StandardCrossover);
40
41    #region helper methods
42    private void StandardCrossover(IRandom random, ISymbolicExpressionTree par0, ISymbolicExpressionTree par1, double internalProb, int maxLength, int maxDepth)
43      => SubtreeCrossover.Cross(random, par0, par1, internalProb, maxLength, maxDepth);
44
45
46    private void TestCrossoverPerformance(Action<IRandom, ISymbolicExpressionTree, ISymbolicExpressionTree, double, int, int> crossover) {
47      for (int gCount = 0; gCount < Generations; gCount++) {
48        for (int i = 0; i < PopulationSize; i++) {
49          var par0 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
50          var par1 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
51          crossover(random, par0, par1, 0.9, 100, 10);
52        }
53      }
54    }
55
56    #endregion
57  }
58}
Note: See TracBrowser for help on using the repository browser.