mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-22 16:57:41 -05:00
feat(rectangles): also nest large parts first and fill their gaps
Sheet-by-sheet look-ahead cost picks the densest sheet, and a sheet of small parts packs densest. On a braze casing mixing 35x57 to 65x89 plates with 5x28 angles and 9x4 tabs, sheet 1 took 109 small parts and the plates followed one or two to a sheet with empty strips beside them: 28 sheets, 220,032 sq in. Rectangles now also runs the job large parts first, splitting types at the widest drops in box area: the large types nest alone, each of their sheets is repacked with every waiting small part (keeping all its large parts), and leftovers get new sheets. The cheapest whole job wins; ties keep the cost-first run. The braze casing takes 27 sheets and 203,616 sq in, every sheet but the last a plate with small parts in its gaps. Problem-nest corpus (auto-nest): all saved layouts unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
c48fbd87b8
commit
7964785af5
4 files changed
+178
-3
No files matched your search
@@ -35,6 +35,8 @@ public sealed class RectanglesNestingEngine : INestingEngine
|
||||
// Instance-owned diagnostics; no process-global counters or mutable solver state.
|
||||
internal Action<PickMode>? SheetPacked { get; init; }
|
||||
internal Action<RepeatBatch>? BatchSelected { get; init; }
|
||||
/// <summary>Tests turn the large-first runs off to compare against the cost-first run alone.</summary>
|
||||
internal bool LargeFirstRuns { get; init; } = true;
|
||||
|
||||
public NestJobResult Solve(
|
||||
NestJob job,
|
||||
@@ -55,7 +57,22 @@ public sealed class RectanglesNestingEngine : INestingEngine
|
||||
var demand = types.Select(t => new SheetDemand(t.Index, t.Id, t.Priority, t.MaterialArea)).ToArray();
|
||||
// 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, Pack, forward, token, BatchSelected);
|
||||
var result = SheetByStock.Run(job, demand, remaining.ToArray(), Pack, forward, token, BatchSelected);
|
||||
// Sheet by sheet, cost picks the densest sheet, which on a job mixing large plates with
|
||||
// small parts packs the small parts onto early sheets of their own and leaves the large
|
||||
// plates' sheets nothing to fill their gaps. Large-first runs nest the large parts alone,
|
||||
// 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);
|
||||
var order = 1;
|
||||
foreach (var split in LargeFirstRuns ? LargeSplits(types, remaining) : Enumerable.Empty<double>())
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var scored = Score(LargeFirst(split), order++);
|
||||
if (scored.IsBetterThan(best))
|
||||
best = scored;
|
||||
}
|
||||
result = best.Result;
|
||||
CandidateProgress.ReportPlan(result, progress);
|
||||
CandidateProgress.ReportCommits(result, progress, token);
|
||||
return result;
|
||||
@@ -85,10 +102,82 @@ public sealed class RectanglesNestingEngine : INestingEngine
|
||||
return plans;
|
||||
}
|
||||
|
||||
NestJobResult LargeFirst(double split)
|
||||
{
|
||||
var large = types.Select(t => t.BoxArea >= split).ToArray();
|
||||
var small = remaining.Select((r, i) => large[i] ? 0 : r).ToArray();
|
||||
var sheets = SheetByStock.Run(job, demand, remaining.Select((r, i) => large[i] ? r : 0).ToArray(),
|
||||
Pack, forward, token);
|
||||
var stocks = job.Plates.ToDictionary(p => p.Id, StringComparer.Ordinal);
|
||||
var index = types.ToDictionary(t => t.Id, t => t.Index, StringComparer.Ordinal);
|
||||
var filled = new List<SheetLayout>(sheets.Plates.Count);
|
||||
foreach (var sheet in sheets.Plates)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var stock = stocks[sheet.StockId];
|
||||
var own = new int[types.Count];
|
||||
foreach (var p in sheet.Placements)
|
||||
own[index[p.PartId]]++;
|
||||
var poses = sheet.Placements.Select(p => (p.PartId, p.X, p.Y, p.Rotation)).ToArray();
|
||||
var layout = new SheetLayout(stock, poses, types.Sum(t => own[t.Index] * t.MaterialArea), null);
|
||||
if (small.Any(r => r > 0))
|
||||
{
|
||||
// Repack the sheet's own large parts with every small part still waiting;
|
||||
// a plan counts only if it keeps all of the sheet's large parts.
|
||||
var wanted = own.Select((n, i) => n + small[i]).ToArray();
|
||||
SheetPlan? fill = null;
|
||||
foreach (var plan in PackAll(stock, wanted, null, null))
|
||||
{
|
||||
var counts = new int[types.Count];
|
||||
foreach (var p in plan.Parts)
|
||||
counts[p.Type.Index]++;
|
||||
if (types.Any(t => large[t.Index] && counts[t.Index] < own[t.Index]))
|
||||
continue;
|
||||
if (fill == null || plan.MaterialArea > fill.MaterialArea)
|
||||
fill = plan;
|
||||
}
|
||||
if (fill != null && fill.MaterialArea > layout.MaterialArea)
|
||||
{
|
||||
layout = Layout(fill);
|
||||
foreach (var p in fill.Parts)
|
||||
if (!large[p.Type.Index])
|
||||
small[p.Type.Index]--;
|
||||
}
|
||||
}
|
||||
filled.Add(layout);
|
||||
}
|
||||
return SheetByStock.Run(job, demand, small, Pack, forward, token, prefix: filled);
|
||||
}
|
||||
|
||||
// Both runs come from the same validated box packer, so they rank on demand met, cost and
|
||||
// offcut without a layout check.
|
||||
CandidateScore Score(NestJobResult r, int at)
|
||||
{
|
||||
var unplaced = System.Math.Max(0, job.Parts.Sum(p => p.Quantity) - r.Plates.Sum(p => p.Placements.Count));
|
||||
return new CandidateScore(r, true, unplaced, NestJobCost.Evaluate(job, r),
|
||||
NestJobCost.CompactnessOffcut(job, r), at, NestJobCost.UsesExplicitCosts(job));
|
||||
}
|
||||
|
||||
static SheetLayout Layout(SheetPlan plan) =>
|
||||
new(plan.Stock, plan.Poses().ToArray(), plan.MaterialArea, plan.Envelope);
|
||||
}
|
||||
|
||||
/// <summary>Box-area thresholds splitting the job's types into large and small: below each of
|
||||
/// the widest drops in box area (to half or less of the next larger type), at most
|
||||
/// <see cref="MaxLargeSplits"/>, widest first.</summary>
|
||||
private static IEnumerable<double> LargeSplits(IReadOnlyList<BoxType> types, int[] remaining)
|
||||
{
|
||||
var areas = types.Where(t => remaining[t.Index] > 0).Select(t => t.BoxArea)
|
||||
.Distinct().OrderByDescending(a => a).ToArray();
|
||||
return Enumerable.Range(1, System.Math.Max(0, areas.Length - 1))
|
||||
.Where(i => areas[i] <= areas[i - 1] * 0.5)
|
||||
.OrderBy(i => areas[i] / areas[i - 1]).ThenBy(i => i)
|
||||
.Take(MaxLargeSplits)
|
||||
.Select(i => areas[i - 1]);
|
||||
}
|
||||
|
||||
private const int MaxLargeSplits = 3;
|
||||
|
||||
private static bool FitsStock(NestPlateStock stock, BoxOrientation o)
|
||||
{
|
||||
if (stock.Outline != null)
|
||||
|
||||
Reference in new issue
Block a user