Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2963: Add first version of HeuristicLab.Benchmarks

File size: 2.8 KB
Line 
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    [Benchmark]
42    public void SimpleCrossoverRandom() => TestCrossoverPerformance(SimpleCrossoverRandom);
43
44    [Benchmark]
45    public void SimpleCrossoverProportional() => TestCrossoverPerformance(SimpleCrossoverProportional);
46
47    #region helper methods
48    private void SimpleCrossoverRandom(IRandom random, ISymbolicExpressionTree par0, ISymbolicExpressionTree par1, double internalProb, int maxLength, int maxDepth)
49      => SimpleCrossover.Cross(random, par0, par1, internalProb, maxLength, maxDepth, false, false);
50
51
52    private void SimpleCrossoverProportional(IRandom random, ISymbolicExpressionTree par0, ISymbolicExpressionTree par1, double internalProb, int maxLength, int maxDepth)
53      => SimpleCrossover.Cross(random, par0, par1, internalProb, maxLength, maxDepth, false, true);
54
55    private void StandardCrossover(IRandom random, ISymbolicExpressionTree par0, ISymbolicExpressionTree par1, double internalProb, int maxLength, int maxDepth)
56      => SubtreeCrossover.Cross(random, par0, par1, internalProb, maxLength, maxDepth);
57
58
59    private void TestCrossoverPerformance(Action<IRandom, ISymbolicExpressionTree, ISymbolicExpressionTree, double, int, int> crossover) {
60      for (int gCount = 0; gCount < Generations; gCount++) {
61        for (int i = 0; i < PopulationSize; i++) {
62          var par0 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
63          var par1 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone();
64          crossover(random, par0, par1, 0.9, 100, 10);
65        }
66      }
67    }
68
69    #endregion
70  }
71}
Note: See TracBrowser for help on using the repository browser.