|
| 1 | +using Alligator.SixMaking.Logics; |
| 2 | +using Alligator.SixMaking.Model; |
| 3 | +using Alligator.Solver; |
| 4 | +using BenchmarkDotNet.Attributes; |
| 5 | +using BenchmarkDotNet.Engines; |
| 6 | + |
| 7 | +namespace Alligator.Benchmark |
| 8 | +{ |
| 9 | + [MemoryDiagnoser] |
| 10 | + [SimpleJob(RunStrategy.Monitoring, warmupCount: 1, iterationCount: 5)] |
| 11 | + public class SolverBenchmarks |
| 12 | + { |
| 13 | + private SolverProvider<IPosition, Step> solverFactory = null!; |
| 14 | + private ISolver<Step> solver = null!; |
| 15 | + private IList<Step> example1History = null!; |
| 16 | + private IList<Step> example2History = null!; |
| 17 | + private IList<Step> example3History = null!; |
| 18 | + private IList<Step> example5History = null!; |
| 19 | + |
| 20 | + [GlobalSetup] |
| 21 | + public void GlobalSetup() |
| 22 | + { |
| 23 | + var rules = new Rules(new StepPool(), new MovingRules()); |
| 24 | + var solverConfiguration = new Configuration(); |
| 25 | + solverFactory = new SolverProvider<IPosition, Step>(rules, solverConfiguration); |
| 26 | + |
| 27 | + example1History = BuildHistory(Example1()); |
| 28 | + example2History = BuildHistory(Example2()); |
| 29 | + example3History = BuildHistory(Example3()); |
| 30 | + example5History = BuildHistory(Example5()); |
| 31 | + } |
| 32 | + |
| 33 | + [IterationSetup] |
| 34 | + public void IterationSetup() |
| 35 | + { |
| 36 | + solver = solverFactory.Create(); |
| 37 | + } |
| 38 | + |
| 39 | + [Benchmark(Description = "Example1 - Empty board")] |
| 40 | + public Step Example1_EmptyBoard() => solver.OptimizeNextStep(example1History); |
| 41 | + |
| 42 | + [Benchmark(Description = "Example2 - Four pieces")] |
| 43 | + public Step Example2_FourPieces() => solver.OptimizeNextStep(example2History); |
| 44 | + |
| 45 | + [Benchmark(Description = "Example3 - Twelve pieces")] |
| 46 | + public Step Example3_TwelvePieces() => solver.OptimizeNextStep(example3History); |
| 47 | + |
| 48 | + [Benchmark(Description = "Example5 - Complex midgame")] |
| 49 | + public Step Example5_ComplexMidgame() => solver.OptimizeNextStep(example5History); |
| 50 | + |
| 51 | + private static IList<Step> BuildHistory(IPosition position) |
| 52 | + { |
| 53 | + return position.History.ToList(); |
| 54 | + } |
| 55 | + |
| 56 | + private static IPosition Example1() |
| 57 | + { |
| 58 | + return new Position(); |
| 59 | + } |
| 60 | + |
| 61 | + private static IPosition Example2() |
| 62 | + { |
| 63 | + IPosition position = new Position(); |
| 64 | + position.Take(new Placement(12)); |
| 65 | + position.Take(new Placement(13)); |
| 66 | + position.Take(new Placement(11)); |
| 67 | + position.Take(new Placement(17)); |
| 68 | + return position; |
| 69 | + } |
| 70 | + |
| 71 | + private static IPosition Example3() |
| 72 | + { |
| 73 | + IPosition position = new Position(); |
| 74 | + for (int i = 6; i < 18; i++) |
| 75 | + { |
| 76 | + position.Take(new Placement(i)); |
| 77 | + } |
| 78 | + return position; |
| 79 | + } |
| 80 | + |
| 81 | + private static IPosition Example5() |
| 82 | + { |
| 83 | + IPosition position = new Position(); |
| 84 | + position.Take(new Placement(0)); |
| 85 | + position.Take(new Placement(12)); |
| 86 | + position.Take(new Placement(1)); |
| 87 | + position.Take(new Placement(11)); |
| 88 | + position.Take(new Placement(2)); |
| 89 | + position.Take(new Placement(17)); |
| 90 | + position.Take(new Placement(3)); |
| 91 | + position.Take(new Placement(13)); |
| 92 | + position.Take(new Placement(4)); |
| 93 | + position.Take(new Placement(10)); |
| 94 | + position.Take(new Placement(5)); |
| 95 | + position.Take(new Placement(15)); |
| 96 | + position.Take(new Placement(20)); |
| 97 | + position.Take(new Placement(16)); |
| 98 | + position.Take(new Movement(5, 10, 1)); |
| 99 | + position.Take(new Movement(15, 10, 1)); |
| 100 | + position.Take(new Placement(5)); |
| 101 | + position.Take(new Placement(6)); |
| 102 | + position.Take(new Movement(1, 6, 1)); |
| 103 | + position.Take(new Movement(12, 11, 1)); |
| 104 | + position.Take(new Movement(10, 17, 1)); |
| 105 | + position.Take(new Placement(12)); |
| 106 | + position.Take(new Placement(7)); |
| 107 | + position.Take(new Placement(22)); |
| 108 | + position.Take(new Placement(1)); |
| 109 | + position.Take(new Movement(11, 16, 2)); |
| 110 | + position.Take(new Placement(21)); |
| 111 | + position.Take(new Placement(18)); |
| 112 | + return position; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments