feat(engine): place large parts first and keep small ones as filler in strips

A sheet that cannot take all the remaining parts now keeps whichever
places more part area: the ordinary fill or strips largest part first.
The ordinary fill spent skinny parts early because they cover the most
area per length, leaving awkward ones for a ragged last sheet.

Inside a strip, the strip's own part fills its column first and the
others fill around it; before, pairs of smaller parts (counted as twice
a part's area) took the strip and the large part lost it. Bands also try
largest part first as a third order.

At about twice the README sample quantities the second sheet ends at
99.9" instead of 106.2" on master (104.7" before this change), and the
help tutorial's parts at 33.6" instead of 34.9". Every other benchmark
job is unchanged or better; mixed jobs solve about two to three times
slower than master.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Opus 5.5 committed 2026-11-15 14:33:38 -05:00
1 parent 81e70af5b6
commit 163c23e8bd
3 files changed
+41 -13

No files matched your search

@@ -65,6 +65,8 @@ internal sealed class BandedFill(
{
bands.Values.OrderByDescending(b => b.Length).ThenBy(b => b.Type).Select(b => b.Type).ToArray(),
bands.Values.OrderBy(b => b.Length).ThenBy(b => b.Type).Select(b => b.Type).ToArray(),
// Largest part first: small parts stay last, as filler for the gaps.
bands.Values.OrderByDescending(b => types[b.Type].Area).ThenBy(b => b.Type).Select(b => b.Type).ToArray(),
};
int[]? bestOrder = null;
SheetFill? best = null;
@@ -100,7 +102,8 @@ internal sealed class BandedFill(
/// carried strip to strip, so a strip is judged by what it leaves for the rest of the sheet,
/// not by its own density alone; the best finished sheet wins.
/// </summary>
public SheetFill? FillStrips(IReadOnlyList<int> remaining, Func<Box, FrontierPacker> within, CancellationToken token)
public SheetFill? FillStrips(IReadOnlyList<int> remaining, Func<Box, FrontierPacker> within, CancellationToken token,
int beamWidth = Beam)
{
if (stock.Outline != null)
return null;
@@ -175,8 +178,20 @@ internal sealed class BandedFill(
if (!widths.Any(w => System.Math.Abs(w - width) < 0.05))
widths.Add(width);
}
// The strip's own part fills its column first, then the others fill around it.
foreach (var width in widths.OrderBy(w => w))
Offer(new List<Placed>(), start + width);
{
var reach = System.Math.Min(region.Right, start + width + 1e-6);
if (reach <= state.Front)
continue;
var own = new int[types.Count];
own[t] = state.Rest[t];
var left = System.Math.Max(region.Left, start - lookBack);
var column = within(new Box(left, region.Bottom, reach - left, region.Width))
.Fill(own, token, state.Placed, region.Top);
if (column.Parts.Count > 0)
Offer(column.Parts.ToList(), start + width);
}
if (offered == 0)
finished.Add(state);
}
@@ -184,7 +199,7 @@ internal sealed class BandedFill(
beam = new List<StripState>();
foreach (var child in children.OrderBy(Rank).ThenBy(c => c.Front))
{
if (beam.Count >= Beam)
if (beam.Count >= beamWidth)
break;
if (beam.Any(b => System.Math.Abs(b.Front - child.Front) < 1e-6 && System.Math.Abs(b.Area - child.Area) < 1e-6
&& b.Rest.SequenceEqual(child.Rest)))
@@ -411,11 +411,19 @@ public sealed class IrregularNestingEngine : INestingEngine
private SheetFill Banded(NestPlateStock stock, int[] remaining, IReadOnlyList<double>? weights,
Func<IReadOnlyList<int>?, double?, FrontierPacker> packer)
{
// Bands shape where a sheet ends; a sheet that cannot take all the remaining parts
// fills to its end anyway, so it keeps the ordinary fill.
// A sheet that cannot take all the remaining parts fills to its end anyway: it keeps
// whichever places more, the ordinary fill or strips largest part first (one partial
// sheet carried), which leave the small parts for the gaps of the sheets after it.
var work = stock.WorkArea;
FrontierPacker Within(OpenNest.Geometry.Box box) => new(types, CacheFor(stock), PairsFor(stock), stock,
PackAxis.Y, 1.0, Work, null, ClampsFor(stock), null, weights, null, null, box);
if (types.Sum(t => remaining[t.Index] * t.Area) > work.Length * work.Width)
return packer(null, null).Fill(remaining, token);
{
var plain = packer(null, null).Fill(remaining, token);
var full = new BandedFill(types, BlocksFor(stock), stock, ClampsFor(stock), () => packer(null, null),
CompareSheets, Work, Work.Value + workBudget).FillStrips(remaining, Within, token, 1);
return full != null && CompareSheets(full, plain) < 0 ? full : plain;
}
var bands = new BandedFill(types, BlocksFor(stock), stock, ClampsFor(stock), () => packer(null, null),
CompareSheets, Work, groupedBudget).Fill(remaining, token);
var strips = new BandedFill(types, BlocksFor(stock), stock, ClampsFor(stock), () => packer(null, null),