From e589731828f7a2a6469e7b188efcf4093a414e00 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Tue, 17 Nov 2026 23:38:24 -0500 Subject: [PATCH] perf(irregular): skip tail re-plan stock trials that cannot win A tail re-plan's decode no longer packs a stock whose least possible net cost (the whole sheet, less full salvage credit) already exceeds the best estimate so far and whose cost ratio over the remaining part area cannot beat the best so far. Later trials only lower the ratio and the estimates, so the skipped trial could neither be chosen nor change the choice. On N26-2GT this skips ~60% of the tail re-plans' trials, a fifth of the ungrouped search's packing time. Layouts and reported plans are unchanged on every unlimited benchmark job and on the N26-2GT and readme-mixed oracles; first-nest's unlimited Irregular solve ran in 75 s rather than 100 s. Whole-job variants still pack every trial, since a skipped stock may have finished the job. Co-Authored-By: Claude Opus 5.5 --- .../IrregularStockPruningTests.cs | 42 +++++++++++++++++++ .../Irregular/IrregularNestingEngine.cs | 35 +++++++++++++++- docs/nesting-engines.md | 12 ++++++ 3 files changed, 87 insertions(+), 2 deletions(-) create mode 100644 OpenNest.Engine.Tests/NestingEngines/IrregularStockPruningTests.cs diff --git a/OpenNest.Engine.Tests/NestingEngines/IrregularStockPruningTests.cs b/OpenNest.Engine.Tests/NestingEngines/IrregularStockPruningTests.cs new file mode 100644 index 00000000..a2cd60d8 --- /dev/null +++ b/OpenNest.Engine.Tests/NestingEngines/IrregularStockPruningTests.cs @@ -0,0 +1,42 @@ +using OpenNest.Engine.Jobs; +using OpenNest.Engine.NestingEngines.Irregular; +using static OpenNest.Engine.Tests.NestingEngines.JobBuilder; +using static OpenNest.Engine.Tests.NestingEngines.Shapes; + +namespace OpenNest.Engine.Tests.NestingEngines; + +/// +/// A tail re-plan's decode skips stock trials that can neither be chosen nor change the choice: +/// it packs fewer sheets and decides exactly as when it packs every trial. +/// +public class IrregularStockPruningTests +{ + private static NestJob MixedJob(NestJobOptions? options) => + Job( + new[] { Part("disc", Disc(3), 10), Part("ell", LShape(12, 8, 4), 10), Part("tri", Triangle(9, 7), 8) }, + // Small sheets first, so a trial is packed before the large sheet is considered. + new[] { Stock("small", 20, 30, spacing: 0.5), Stock("sheet", 40, 60, spacing: 0.5), Stock("large", 120, 200, spacing: 0.5) }, + options + ); + + private static List<(string, string, int, double, double, double)> Placements(NestJobResult result) => + result.Plates.SelectMany(p => p.Placements.Select(q => (p.Stock.Id, q.PartId, q.InstanceIndex, q.X, q.Y, q.Rotation))) + .ToList(); + + public static TheoryData Salvage => new() { false, true }; + + [Theory] + [MemberData(nameof(Salvage))] + public void SkippedTrialsChangeNoChoice(bool salvage) + { + var options = salvage ? new NestJobOptions(salvageRate: 0.5, minimumSalvageDimension: 1) : null; + var all = 0; + var pruned = 0; + var full = new IrregularNestingEngine { PruneStockTrials = false, SheetPacked = () => all++ }.Solve(MixedJob(options)); + var skipped = new IrregularNestingEngine { SheetPacked = () => pruned++ }.Solve(MixedJob(options)); + + Assert.Equal(Placements(full), Placements(skipped)); + Assert.NotEmpty(Placements(full)); + Assert.True(pruned < all, $"packed {pruned} of {all} sheets"); + } +} diff --git a/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs b/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs index da7dfb09..ab8b2689 100644 --- a/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs +++ b/OpenNest.Engine/NestingEngines/Irregular/IrregularNestingEngine.cs @@ -75,6 +75,12 @@ public sealed class IrregularNestingEngine : INestingEngine /// internal long WorkBudget { get; init; } = 1_500_000; + /// + /// Whether a tail re-plan's decode skips stock trials that can neither be chosen nor change the + /// choice; off only in tests that compare against packing every trial. + /// + internal bool PruneStockTrials { get; init; } = true; + /// /// One private drawing per part geometry, shared by the best-fit pair and Fill block /// catalogs and by every solve of this instance (a pipeline's cutout trial and its ordinary @@ -103,7 +109,7 @@ public sealed class IrregularNestingEngine : INestingEngine token.ThrowIfCancellationRequested(); var types = PartCatalog.Build(job); using var solver = new Solver(job, types, progress, token, WorkBudget, DrawingFor, SheetPacked, BatchSelected, TailReplaced, - TailReplanning); + TailReplanning, PruneStockTrials); // Pair-only orientations may fit stock even when the sampled single poses do not. // Demand that neither a single nor a pair can fit is reported unplaced. @@ -363,7 +369,8 @@ public sealed class IrregularNestingEngine : INestingEngine Action? sheetPacked, Action? batchSelected, Action? tailReplaced, - Action? tailReplanning = null + Action? tailReplanning = null, + bool pruneTrials = true ) : IDisposable { public void Dispose() @@ -742,6 +749,7 @@ public sealed class IrregularNestingEngine : INestingEngine } var trials = new List<(SheetFill Fill, double Net)>(); + var areaLeft = types.Sum(t => remaining[t.Index] * t.Area); foreach (var stock in job.Plates) { token.ThrowIfCancellationRequested(); @@ -749,6 +757,8 @@ public sealed class IrregularNestingEngine : INestingEngine continue; if (stock.Quantity is int available && used[stock.Id] >= available) continue; + if (pruneTrials && finishers == null && CannotWin(stock, trials, areaLeft)) + continue; progress?.Report(new NestJobProgress(NestJobStage.EvaluatingCandidate, stock.Id, sheets.Count, 0, 0)); // Grouped fills pack groups from singles and pairs: Fill blocks cost far more per // fill there and gave no denser groups. A banded fill's bands are Fill patterns @@ -839,6 +849,27 @@ public sealed class IrregularNestingEngine : INestingEngine return new Run(sheets, net, reason); } + + /// + /// Whether a trial of could neither be chosen nor change the + /// choice, so the decode may skip it: its least possible net cost (the whole sheet, less + /// full salvage credit) already exceeds the best estimate so far, and over the remaining + /// part area it cannot have a better cost ratio than the best so far. Later trials only + /// lower the ratio and the estimates, so the decode picks the same sheet without it. + /// + private bool CannotWin(NestPlateStock stock, List<(SheetFill Fill, double Net)> trials, double areaLeft) + { + if (trials.Count == 0 || stock.Cost < 0 || areaLeft <= 0) + return false; + var rate = job.Options.SalvageRate != 0 && job.Options.MinimumSalvageDimension > 0 ? job.Options.SalvageRate : 0; + if (rate is < 0 or > 1) + return false; + var least = (stock.Cost ?? stock.Area) * (1 - rate); + var ratio = trials.Min(t => t.Net / System.Math.Max(t.Fill.PartArea, 1e-12)); + var estimate = trials.Min(t => t.Net + System.Math.Max(0, areaLeft - t.Fill.PartArea) * ratio); + const double Margin = 1 + 1e-9; + return least > estimate * Margin && least >= ratio * areaLeft * Margin; + } } private sealed record Run(IReadOnlyList Sheets, double Net, NestJobStopReason Reason); diff --git a/docs/nesting-engines.md b/docs/nesting-engines.md index 443524c4..81bc18cb 100644 --- a/docs/nesting-engines.md +++ b/docs/nesting-engines.md @@ -275,6 +275,18 @@ or invalid Lattice proposal leaves singles and pairs available. Large enclosed-p pending the hole-geometry integration; containment cutting order and shop-use safety acceptance remain separate sequencer/verification work. +Each sheet of a decode trial-packs every stock still available and keeps the one with the lowest +estimate: its net cost plus the part area it leaves, charged at the best cost per part area any +trial reached. A tail re-plan's decode skips a stock whose least possible net cost (the whole +sheet, less full salvage credit) already exceeds the best estimate so far, and that over the +remaining part area could not reach a better ratio than the best so far. Later trials only lower +the ratio and the estimates, so the skipped trial could neither be chosen nor change the choice: +each decode picks the sheets it picked before. Mostly large stocks on the last sheets are skipped: +on the 260-part job above, about 60% of the tail re-plans' trials and a fifth of the +ungrouped search's packing time. The work meter counts only packed trials, so later passes may +start where before the budget had stopped them. Whole-job variants still pack every trial: a +skipped stock may have finished the job, and their finished plans compete with the decode's. + ### Grouped variants After its six variants and the tail re-plan, Irregular decodes the job again with like parts