using System; using System.Collections.Generic; using BenchmarkDotNet.Attributes; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Random; namespace HeuristicLab.Benchmarks { [CoreJob, ClrJob] [MinColumn, MaxColumn, MeanColumn, MedianColumn] public class SubtreeCrossoverPerformance { private List trees = new List(); private readonly IRandom random = new MersenneTwister(1234); private readonly ISymbolicExpressionGrammar grammar = Grammars.CreateArithmeticAndAdfGrammar(); [GlobalSetup] public void Setup() { for (int i = 0; i < PopulationSize; i++) { trees.Add(ProbabilisticTreeCreator.Create(random, grammar, MaxLength, MaxDepth)); for (int j = random.Next(3); j < 3; j++) SubroutineCreater.CreateSubroutine(random, trees[i], MaxLength, MaxDepth, 3, 3); } } [Params(100)] public int MaxLength { get; set; } [Params(10)] public int MaxDepth { get; set; } [Params(1_000)] public int PopulationSize { get; set; } [Params(5)] public int Generations { get; set; } [Benchmark] public void StandardCrossover() => TestCrossoverPerformance(StandardCrossover); #region helper methods private void StandardCrossover(IRandom random, ISymbolicExpressionTree par0, ISymbolicExpressionTree par1, double internalProb, int maxLength, int maxDepth) => SubtreeCrossover.Cross(random, par0, par1, internalProb, maxLength, maxDepth); private void TestCrossoverPerformance(Action crossover) { for (int gCount = 0; gCount < Generations; gCount++) { for (int i = 0; i < PopulationSize; i++) { var par0 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone(); var par1 = (ISymbolicExpressionTree)trees.SampleRandom(random).Clone(); crossover(random, par0, par1, 0.9, 100, 10); } } } #endregion } }