refactor(benchmark): drive engines through INestingEngine.Solve instead of a hand-rolled multi-plate loop

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
aj
2026-09-19 08:19:08 -04:00
co-authored by Claude Sonnet 5
parent ae704478af
commit a2dcfc7484
2 changed files with 75 additions and 143 deletions
+63 -101
View File
@@ -7,23 +7,23 @@ using System.Threading;
namespace OpenNest.Benchmark
{
/// <summary>
/// Runs every candidate engine against every job. A job may need several
/// plates to place everything it asks for; this drives that loop itself,
/// since NestEngineBase.Nest() fills exactly one already-sized plate and
/// has no say in picking its own size. For each plate the loop needs, the
/// smallest candidate size that fits the largest still-unplaced drawing is
/// chosen via the codebase's own MultiPlateNester.CreatePlate, then the
/// engine's Nest() fills that plate with whatever of the remaining items
/// fit. This is applied identically to every engine, so no engine gets to
/// (or has to) implement sheet-size selection itself.
/// Runs every candidate engine against every job. Each engine is a full
/// INestingEngine: it owns its own plate/size selection and multi-plate
/// strategy for the whole job, rather than being handed one already-sized
/// plate at a time by this harness. A per-run timeout guards against a
/// runaway or hanging engine — cooperative cancellation, so it reliably
/// stops engines built on NestJobRunner (all four built-ins) but can't
/// forcibly interrupt an engine that never checks its token.
/// </summary>
public static class BenchmarkRunner
{
/// <summary>Safety cap so a degenerate engine (placing almost nothing
/// per plate) can't loop indefinitely.</summary>
/// <summary>Physical-sheet cap passed to every job's NestJobOptions.MaxPlates.</summary>
private const int MaxPlates = 40;
public static List<JobResult> Run(List<BenchmarkJob> jobs, IReadOnlyList<NestEngineInfo> engines)
/// <summary>Wall-clock budget for one engine solving one job.</summary>
private static readonly TimeSpan Timeout = TimeSpan.FromMinutes(5);
public static List<JobResult> Run(List<BenchmarkJob> jobs, IReadOnlyList<NestingEngineInfo> engines)
{
var results = new List<JobResult>(jobs.Count * engines.Count);
@@ -38,60 +38,53 @@ namespace OpenNest.Benchmark
return results;
}
private static JobResult RunOne(BenchmarkJob job, NestEngineInfo engineInfo)
private static JobResult RunOne(BenchmarkJob job, NestingEngineInfo engineInfo)
{
var template = job.CreateTemplatePlate();
var options = job.BuildPlateOptions();
var remaining = job.CreateItems();
var nestJob = job.BuildNestJob(MaxPlates);
var requested = job.TotalRequestedQuantity;
var plateRuns = new List<(Plate Plate, List<Part> Parts)>();
string engineError = null;
var sw = Stopwatch.StartNew();
try
{
while (remaining.Any(i => i.Quantity > 0) && plateRuns.Count < MaxPlates)
var engine = engineInfo.Factory();
using var cts = new CancellationTokenSource(Timeout);
var jobResult = engine.Solve(nestJob, null, cts.Token);
var materialized = NestResultMaterializer.Materialize(nestJob, jobResult);
var plateRuns = materialized.Nest.Plates
.Select(plate => (Plate: plate, Parts: plate.Parts.ToList()))
.ToList();
var validation = NestValidator.Validate(plateRuns, job);
var totalPlaced = plateRuns.Sum(pr => pr.Parts.Count);
var placedArea = validation.Valid ? plateRuns.Sum(pr => pr.Parts.Sum(p => p.BaseDrawing.Area)) : 0;
var plateArea = plateRuns.Sum(pr => pr.Plate.Area());
var sizeBreakdown = plateRuns
.GroupBy(pr => pr.Plate.Size.ToString(1))
.OrderByDescending(g => g.Count())
.ToDictionary(g => g.Key, g => g.Count());
sw.Stop();
return new JobResult
{
var largest = remaining
.Where(i => i.Quantity > 0)
.OrderByDescending(i => BoundsArea(i))
.First();
var plate = MultiPlateNester.CreatePlate(template, options, largest.Drawing.Program.BoundingBox());
var engine = engineInfo.Factory(plate);
var itemsClone = CloneItems(remaining);
var parts = engine.Nest(itemsClone, null, CancellationToken.None) ?? new List<Part>();
if (parts.Count == 0)
{
// Not even the largest available candidate size could fit
// the current largest remaining part - stop here rather
// than loop forever; whatever's left is reported unplaced.
break;
}
plateRuns.Add((plate, parts));
foreach (var item in remaining)
{
var placed = parts.Count(p => p.BaseDrawing.Id == item.Drawing.Id);
if (placed > 0)
item.Quantity = System.Math.Max(0, item.Quantity - placed);
}
}
EngineName = engineInfo.Name,
JobName = job.Name,
Valid = validation.Valid,
Violations = validation.Violations,
PartsPlaced = totalPlaced,
PartsRequested = requested,
PlacedArea = placedArea,
PlateArea = plateArea,
PlatesUsed = plateRuns.Count,
SizeBreakdown = sizeBreakdown,
ElapsedMs = sw.ElapsedMilliseconds,
};
}
catch (Exception ex)
{
engineError = $"{ex.GetType().Name}: {ex.Message}";
}
sw.Stop();
if (engineError != null)
catch (OperationCanceledException)
{
sw.Stop();
return new JobResult
{
EngineName = engineInfo.Name,
@@ -99,53 +92,22 @@ namespace OpenNest.Benchmark
Valid = false,
PartsRequested = requested,
ElapsedMs = sw.ElapsedMilliseconds,
Error = engineError,
Error = $"Timed out after {Timeout.TotalMinutes:F0} minute(s)",
};
}
var validation = NestValidator.Validate(plateRuns, job);
var totalPlaced = plateRuns.Sum(pr => pr.Parts.Count);
var placedArea = validation.Valid ? plateRuns.Sum(pr => pr.Parts.Sum(p => p.BaseDrawing.Area)) : 0;
var plateArea = plateRuns.Sum(pr => pr.Plate.Area());
var sizeBreakdown = plateRuns
.GroupBy(pr => pr.Plate.Size.ToString(1))
.OrderByDescending(g => g.Count())
.ToDictionary(g => g.Key, g => g.Count());
return new JobResult
catch (Exception ex)
{
EngineName = engineInfo.Name,
JobName = job.Name,
Valid = validation.Valid,
Violations = validation.Violations,
PartsPlaced = totalPlaced,
PartsRequested = requested,
PlacedArea = placedArea,
PlateArea = plateArea,
PlatesUsed = plateRuns.Count,
SizeBreakdown = sizeBreakdown,
ElapsedMs = sw.ElapsedMilliseconds,
};
}
private static double BoundsArea(NestItem item)
{
var bb = item.Drawing.Program.BoundingBox();
return bb.Width * bb.Length;
}
private static List<NestItem> CloneItems(List<NestItem> items)
{
return items.Select(i => new NestItem
{
Drawing = i.Drawing,
Quantity = i.Quantity,
Priority = i.Priority,
StepAngle = i.StepAngle,
RotationStart = i.RotationStart,
RotationEnd = i.RotationEnd,
}).ToList();
sw.Stop();
return new JobResult
{
EngineName = engineInfo.Name,
JobName = job.Name,
Valid = false,
PartsRequested = requested,
ElapsedMs = sw.ElapsedMilliseconds,
Error = $"{ex.GetType().Name}: {ex.Message}",
};
}
}
}
}