feat(engine): add an opt-in solve log to the Default nesting engine

Set OPENNEST_NEST_LOG to a directory to write one JSON Lines file of
events and a text summary per Default solve: each candidate engine's
result, time and passes, and why the winning layout was chosen. It only
observes, so layouts are the same with it on or off.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Sonnet 5.5 committed 2026-11-28 14:44:05 -05:00
1 parent bf9b0df816
commit e54a9456f4
11 files changed
+642 -10

No files matched your search

@@ -1,6 +1,7 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using OpenNest.Engine.Jobs;
@@ -55,6 +56,9 @@ public sealed class RectanglesNestingEngine : INestingEngine
remaining[t.Index] = 0;
var demand = types.Select(t => new SheetDemand(t.Index, t.Id, t.Priority, t.MaterialArea)).ToArray();
var trace = NestTrace.Current;
var timer = Stopwatch.StartNew();
var packs = 0;
// One run makes one whole-job plan: report it, then its commits, as the other engines do.
var forward = progress == null ? null : new CandidateProgress(progress);
var result = SheetByStock.Run(job, demand, remaining.ToArray(), Pack, forward, token, BatchSelected);
@@ -64,15 +68,18 @@ public sealed class RectanglesNestingEngine : INestingEngine
// fill each of their sheets' gaps with the small parts, and only then start new sheets for
// what is left. The cheapest whole job wins; ties keep the cost-first run.
var best = Score(result, 0);
TraceRun(null, best);
var order = 1;
foreach (var split in LargeFirstRuns ? LargeSplits(types, remaining) : Enumerable.Empty<double>())
{
token.ThrowIfCancellationRequested();
var scored = Score(LargeFirst(split), order++);
TraceRun(split, scored);
if (scored.IsBetterThan(best))
best = scored;
}
result = best.Result;
trace?.Write("rectangles.chosen", new { run = best.Order });
CandidateProgress.ReportPlan(result, progress);
CandidateProgress.ReportCommits(result, progress, token);
return result;
@@ -97,11 +104,29 @@ public sealed class RectanglesNestingEngine : INestingEngine
foreach (var rule in Rules)
{
plans.Add(SheetPacker.Pack(types, left, stock, rule, mode, token, width, height));
packs++;
SheetPacked?.Invoke(mode);
}
return plans;
}
// Each run's time and sheet packings since the one before it.
void TraceRun(double? largeSplit, CandidateScore score)
{
trace?.Write("rectangles.run", new
{
run = score.Order,
largeSplit,
ms = timer.ElapsedMilliseconds,
packs,
unplaced = score.Unplaced,
cost = score.Cost,
sheets = trace.Sheets(score.Result),
});
timer.Restart();
packs = 0;
}
NestJobResult LargeFirst(double split)
{
var large = types.Select(t => t.BoxArea >= split).ToArray();