1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using BenchmarkDotNet.Attributes;
|
---|
4 | using HeuristicLab.Core;
|
---|
5 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
6 | using HeuristicLab.Random;
|
---|
7 |
|
---|
8 | namespace 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 | }
|
---|