feat(circles): place a smaller disc between two large ones when that shortens the sheet

Largest first sets the large discs side by side before any smaller one is
placed. Each sheet of 24 discs or fewer is now also packed with the order of
its discs searched for a shorter layout, and the room that frees is refilled
from the remaining demand.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
ajandClaude Fable 5.2 committed 2026-11-24 18:17:54 -05:00
1 parent 67d150f95f
commit d37cc93371
3 files changed
+164 -3

No files matched your search

@@ -54,6 +54,40 @@ public class CirclesNestingEngineTests
Assert.Equal(9, Assert.Single(result.Plates).Placements.Count);
}
[Fact]
public void ASmallerDiscGoesBetweenTwoLargeOnesWhenThatShortensTheSheet()
{
// Largest first sets the 54 and the 53 side by side (107.3 long) and the 20 beyond them,
// at the far end of the sheet. With the 20 against the top edge between them the layout
// ends near 111.1. Neither leaves a 12-inch drop, so only the tie-break tells them apart.
var job = Job([Part("a", Disc(27), 1), Part("b", Disc(26.5), 1), Part("c", Disc(10), 1)],
[Stock("s", 60, 120, spacing: 0.25, quantity: 1)],
new NestJobOptions(minimumSalvageDimension: 12));
var result = new CirclesNestingEngine().Solve(job);
LayoutAssert.Valid(job, result);
Assert.Equal(NestJobStatus.Complete, result.Status);
var radius = new Dictionary<string, double> { ["a"] = 27, ["b"] = 26.5, ["c"] = 10 };
var placements = Assert.Single(result.Plates).Placements;
Assert.InRange(placements.Max(p => p.X + radius[p.PartId]), 107, 111.5);
var large = placements.Where(p => p.PartId != "c").Select(p => p.X).ToArray();
Assert.InRange(placements.Single(p => p.PartId == "c").X, large.Min(), large.Max());
}
[Fact]
public void RoomFreedByAShorterOrderTakesAnotherDisc()
{
// Largest first leaves no room for the fourth disc on one sheet; with the 21 and the 20
// between the two large discs, all four fit.
var job = Job([Part("a", Disc(27), 1), Part("b", Disc(26.5), 1), Part("c", Disc(10.5), 1), Part("d", Disc(10), 1)],
[Stock("s", 60, 120, spacing: 0.25, quantity: 1)]);
var result = new CirclesNestingEngine().Solve(job);
LayoutAssert.Valid(job, result);
Assert.Equal(NestJobStatus.Complete, result.Status);
Assert.Equal(4, Assert.Single(result.Plates).Placements.Count);
}
[Fact]
public void ClampZonesAndEdgeClearanceAreKept()
{
@@ -21,8 +21,9 @@ namespace OpenNest.Engine.NestingEngines.Circles;
///
/// Sheet by sheet (<see cref="SheetByStock"/>), each available stock is packed bottom-left (rows)
/// and left-bottom (columns), plain and seeded with a hexagonal lattice of the type with the most
/// remaining area, and bottom-left over a square lattice of it; the candidate with the lowest
/// estimated whole-job cost wins. The whole job runs once with that free choice and once
/// remaining area, and bottom-left over a square lattice of it, and each way again with the
/// order of its discs searched for a shorter layout (<see cref="DiscPacker.PackSqueezed"/>);
/// the candidate with the lowest estimated whole-job cost wins. The whole job runs once with that free choice and once
/// preferring each offered stock, and the best result by <see cref="CandidateScore"/> is kept;
/// its last sheet, and its last two sheets together, are then re-packed onto each single stock
/// and replaced when that lowers the whole job's cost. Each run or tail that beats the best so
@@ -138,6 +139,15 @@ public sealed class CirclesNestingEngine : INestingEngine
{
if (!types.Any(t => left[t.Index] > 0 && DiscPacker.Fits(stock, t)))
yield break;
// First, and along the sheet's longer side first, so a shortened layout wins a tie
// with the layout it shortens; a sheet the order search does not shorten offers none.
var work = stock.WorkArea;
var columns = work.Right - work.Left >= work.Top - work.Bottom;
foreach (var order in columns
? new[] { DiscOrder.LeftBottom, DiscOrder.BottomLeft }
: new[] { DiscOrder.BottomLeft, DiscOrder.LeftBottom })
if (DiscPacker.PackSqueezed(types, left, stock, order, token) is { } squeezed)
yield return squeezed;
foreach (var (order, seed) in Variants)
yield return DiscPacker.Pack(types, left, stock, order, seed, token);
}
@@ -52,6 +52,13 @@ internal static class DiscPacker
private const double Epsilon = 1e-9;
// The order search decodes the whole sheet for every move it tries, so it is kept to sheets
// of a few discs, where the order decides the layout; lattices serve sheets of many.
private const int SqueezeMaxDiscs = 24;
private const int SqueezeDecodes = 600;
private const int SqueezeRounds = 3;
private const double SqueezeGain = 1e-6;
public static SheetLayout Pack(IReadOnlyList<DiscType> types, IReadOnlyList<int> remaining,
NestPlateStock stock, DiscOrder order, DiscSeed seed, CancellationToken token)
{
@@ -71,6 +78,47 @@ internal static class DiscPacker
left[lattice.Index]--;
}
}
Greedy(sheet, fits, left, token);
return sheet.Layout(types);
}
/// <summary>
/// The plain greedy pack, then the same discs packed again in other orders to end sooner along
/// the fill axis, and the room that frees refilled from the remaining demand.
/// </summary>
/// <remarks>Largest first sets the large discs against each other before any smaller one is
/// placed, so a disc that would fit between two of them, were they a little further apart, ends
/// up beyond both. Here the placement rule stays (lowest along the fill axis first) and the
/// order is searched: the best move of one disc to another position, while a move shortens the
/// layout. Only sheets of <see cref="SqueezeMaxDiscs"/> discs or fewer are searched, within
/// <see cref="SqueezeDecodes"/> decodes. Null when no order shortens the greedy layout, which
/// <see cref="Pack"/> already offers.</remarks>
public static SheetLayout? PackSqueezed(IReadOnlyList<DiscType> types, IReadOnlyList<int> remaining,
NestPlateStock stock, DiscOrder order, CancellationToken token)
{
var left = remaining.ToArray();
var fits = types.Where(t => left[t.Index] > 0 && Fits(stock, t)).ToList();
var maxRadius = types.Where(t => Fits(stock, t)).Select(t => t.Radius).DefaultIfEmpty(0).Max();
var sheet = new Sheet(stock, maxRadius, order);
Greedy(sheet, fits, left, token);
var shortened = false;
for (var round = 0; round < SqueezeRounds; round++)
{
if (Squeeze(sheet, stock, maxRadius, order, token) is not { } squeezed)
break;
sheet = squeezed;
shortened = true;
if (Greedy(sheet, fits, left, token) == 0)
break;
}
return shortened ? sheet.Layout(types) : null;
}
/// <summary>Places the remaining demand, higher priority number first, then largest first;
/// returns how many discs it placed.</summary>
private static int Greedy(Sheet sheet, IReadOnlyList<DiscType> fits, int[] left, CancellationToken token)
{
var added = 0;
// Stable: equal radii keep requirement order.
foreach (var type in fits.OrderByDescending(t => t.Priority).ThenByDescending(t => t.Radius))
{
@@ -82,9 +130,69 @@ internal static class DiscPacker
break;
sheet.Place(type, center);
left[type.Index]--;
added++;
}
}
return sheet.Layout(types);
return added;
}
/// <summary>The sheet's discs in the order that ends soonest along the fill axis, or null
/// when no order tried beats the sheet's own.</summary>
private static Sheet? Squeeze(Sheet sheet, NestPlateStock stock, double maxRadius, DiscOrder order,
CancellationToken token)
{
if (sheet.Count < 2 || sheet.Count > SqueezeMaxDiscs)
return null;
var current = sheet.Types.ToList();
var extent = sheet.Extent;
Sheet? best = null;
// Copies of one type are interchangeable: orders are compared by their type sequence.
var tried = new HashSet<string>(StringComparer.Ordinal) { OrderKey(current) };
var decodes = 0;
while (decodes < SqueezeDecodes)
{
List<DiscType>? improvedOrder = null;
Sheet? improved = null;
for (var from = 0; from < current.Count && decodes < SqueezeDecodes; from++)
{
for (var to = 0; to < current.Count && decodes < SqueezeDecodes; to++)
{
if (to == from)
continue;
var candidate = new List<DiscType>(current);
candidate.RemoveAt(from);
candidate.Insert(to, current[from]);
if (!tried.Add(OrderKey(candidate)))
continue;
token.ThrowIfCancellationRequested();
decodes++;
if (Decode(candidate) is not { } decoded || decoded.Extent >= extent - SqueezeGain)
continue;
extent = decoded.Extent;
improved = decoded;
improvedOrder = candidate;
}
}
if (improved == null)
break;
best = improved;
current = improvedOrder!;
}
return best;
Sheet? Decode(List<DiscType> sequence)
{
var trial = new Sheet(stock, maxRadius, order);
foreach (var type in sequence)
{
if (trial.Best(type.Radius) is not { } center)
return null;
trial.Place(type, center);
}
return trial;
}
static string OrderKey(List<DiscType> sequence) => string.Join(',', sequence.Select(t => t.Index));
}
/// <summary>True when the disc fits the work area in both directions.</summary>
@@ -157,6 +265,15 @@ internal static class DiscPacker
public double Gap { get; }
public int Count => placed.Count;
/// <summary>The placed discs' types in placement order.</summary>
public IEnumerable<DiscType> Types => placed.Select(p => p.Type);
/// <summary>How far the placed discs reach along the fill axis: up for rows, right for columns.</summary>
public double Extent => placed.Count == 0 ? double.NegativeInfinity : placed.Max(p =>
(order == DiscOrder.BottomLeft ? p.Center.Y : p.Center.X) + p.Type.Radius);
public void Place(DiscType type, Vector center)
{
var key = Key(center);