mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-22 21:27:44 -05:00
refactor(engine): split Irregular's engine into passes, solver and plan
IrregularNestingEngine.cs was one 1,000-line file: Solve ran its nine passes inline over 135 lines, each repeating the same "decode, report the alternatives, take the plan if it beats the best, re-plan its tail, report again" by hand; the nested Solver took eleven constructor arguments and held the decode, the banded sheet, three plan-improvement passes and the per-stock caches together; and Decode was a single 135-line loop threading ten parameters. The next pass or decode rule had to be added inside all of that. Solve now reads as the list of passes. A Search holds the best plan and the reporting, so each pass is a short method (QuickPlan, StrategyVariants, GroupedVariants, BandedVariant, ReduceSheets, SqueezeLastSheet, SeededVariants) over three shared steps (Offer, Adopt, Replace). The Solver takes the engine instead of its settings one by one, and is split in two files: decoding a strategy into a plan (TrySheets, FillSheet, Choose, PassedOver, Copies) and improving a plan already made (ImproveTail/ReplanTail, ReduceSheets/OnOneSheetFewer/ PackEdgeFirst, Squeeze). A decode's front direction, exponent, weights and sheet rule travel as one Strategy rather than five arguments, with the grouped/banded flag pair as a SheetRule. Plan, Run, Trial and the sheet costing have their own file, as does WorkCounter. The test hooks build their solver as Solve does, so they now also pass on TailReplanning and PruneStockTrials; no test sets either on a hook. No layout changes. Every progress report, plan, alternative, repeat batch, tail re-plan (with its work-meter reading) and final result was traced before and after on 96 solves: 26 jobs (one to three stocks, finite stock, a sheet cap, priorities, explicit costs with salvage, clamp rail, outlined sheets, oversize parts, mixed spacings), six engine settings, ten searches until cancelled and the plan and quick-plan hooks. The traces are byte-identical. Engine suite and the problem-nest corpus's auto-nest area pass. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
0fd912cceb
commit
9aed8dcda1
5 files changed
+1019
-828
No files matched your search
@@ -0,0 +1,233 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenNest.Engine.Jobs;
|
||||
|
||||
namespace OpenNest.Engine.NestingEngines.Irregular;
|
||||
|
||||
public sealed partial class IrregularNestingEngine
|
||||
{
|
||||
// The passes that improve a plan already made: each returns the plan it was given unless it
|
||||
// found one that beats it.
|
||||
private sealed partial class Solver
|
||||
{
|
||||
/// <summary>Most sheets, counted from the last, a tail re-plan takes up.</summary>
|
||||
private const int MaxTail = 3;
|
||||
|
||||
private readonly Dictionary<int, IReadOnlyList<Orientation>> squeezeOrientations = new();
|
||||
|
||||
/// <summary>Next private orientation index: far above the catalog's, pair and block poses.</summary>
|
||||
private int solvedOrientations = 1_000_000;
|
||||
|
||||
/// <summary>
|
||||
/// Takes the parts off the last k sheets (k = 1..3) and re-plans just that demand with
|
||||
/// every stock forced as the first sheet, under every variant; the cheapest complete
|
||||
/// re-plan that beats the current tail replaces it. Tails are small and effort is metered.
|
||||
/// </summary>
|
||||
public Plan ImproveTail(Plan plan, long budget)
|
||||
{
|
||||
engine.TailReplanning?.Invoke(budget - Work.Value);
|
||||
var sheets = plan.Sheets.ToList();
|
||||
for (var k = 1; k <= System.Math.Min(MaxTail, sheets.Count); k++)
|
||||
{
|
||||
if (Work.Value >= budget)
|
||||
break;
|
||||
var prefix = sheets.Take(sheets.Count - k).ToList();
|
||||
var tail = sheets.Skip(sheets.Count - k).ToList();
|
||||
var tailNet = tail.Sum(s => NetCost(job.Options, s));
|
||||
var replan = ReplanTail(prefix, tail, tailNet);
|
||||
if (replan == null)
|
||||
continue;
|
||||
sheets = prefix.Concat(replan.Sheets).ToList();
|
||||
plan = plan with { Sheets = sheets.ToList(), Cost = plan.Cost - (tailNet - replan.Net) };
|
||||
engine.TailReplaced?.Invoke(k);
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The cheapest decode of the tail's parts that places them all for less than
|
||||
/// <paramref name="tailNet"/>, the stock the sheets before it used counted as spent; null
|
||||
/// when none does.
|
||||
/// </summary>
|
||||
private Run? ReplanTail(List<SheetFill> prefix, List<SheetFill> tail, double tailNet)
|
||||
{
|
||||
var tailParts = tail.Sum(s => s.Parts.Count);
|
||||
var tailDemand = new int[types.Count];
|
||||
foreach (var part in tail.SelectMany(s => s.Parts))
|
||||
tailDemand[part.Orientation.TypeIndex]++;
|
||||
var used = prefix
|
||||
.GroupBy(s => s.Stock.Id)
|
||||
.ToDictionary(g => g.Key, g => g.Count(), StringComparer.Ordinal);
|
||||
int? cap = job.Options.MaxPlates is int max ? max - prefix.Count : null;
|
||||
|
||||
Run? best = null;
|
||||
var bestNet = tailNet - (NestJobCost.UsesExplicitCosts(job) ? 0 : CostTie * System.Math.Max(1, tailNet));
|
||||
foreach (var (axis, beta) in Variants)
|
||||
foreach (var first in job.Plates)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var run = Decode(tailDemand, new Strategy(axis, beta), used, cap, first);
|
||||
if (run.Sheets.Sum(s => s.Parts.Count) != tailParts || run.Net >= bestNet)
|
||||
continue;
|
||||
best = run;
|
||||
bestNet = run.Net;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Drops sheets while it can: the parts of a complete plan on one stock are split over
|
||||
/// one sheet fewer (by area, largest first onto the lightest sheet, then dealt in a snake)
|
||||
/// and each sheet packed by <see cref="EdgeFirstFill"/>. A split that packs every sheet
|
||||
/// replaces the plan when it beats it.
|
||||
/// </summary>
|
||||
public Plan ReduceSheets(Plan plan)
|
||||
{
|
||||
while (plan.Unplaced == 0 && plan.Sheets.Count >= 2)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var fewer = OnOneSheetFewer(plan);
|
||||
if (fewer == null)
|
||||
break;
|
||||
plan = fewer;
|
||||
}
|
||||
return plan;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The plan's parts packed on one sheet fewer, when the first split that packs beats the
|
||||
/// plan; null otherwise, and for a plan on outlined or mixed stock, with too many parts or
|
||||
/// with more part area than the fewer sheets hold.
|
||||
/// </summary>
|
||||
private Plan? OnOneSheetFewer(Plan plan)
|
||||
{
|
||||
var stock = plan.Sheets[0].Stock;
|
||||
if (stock.Outline != null || plan.Sheets.Any(s => !ReferenceEquals(s.Stock, stock)))
|
||||
return null;
|
||||
var instances = plan.Sheets.SelectMany(s => s.Parts).Select(p => p.Orientation.TypeIndex)
|
||||
.OrderByDescending(t => types[t].Area).ThenBy(t => t).ToList();
|
||||
var target = plan.Sheets.Count - 1;
|
||||
var room = stock.WorkArea.Length * stock.WorkArea.Width;
|
||||
if (instances.Count > MaxReduceParts || instances.Sum(t => types[t].Area) > target * room)
|
||||
return null;
|
||||
|
||||
foreach (var bins in Splits(instances, target))
|
||||
{
|
||||
var sheets = PackEdgeFirst(stock, bins);
|
||||
if (sheets == null)
|
||||
continue;
|
||||
var candidate = PlanOf(job, sheets, NestJobCost.RequireFinite(sheets.Sum(s => NetCost(job.Options, s))), 0,
|
||||
NestJobStopReason.Completed);
|
||||
return candidate.IsBetterThan(plan) ? candidate : null;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>Each bin's parts packed edge first on a sheet of its own; null when a bin does not pack.</summary>
|
||||
private List<SheetFill>? PackEdgeFirst(NestPlateStock stock, int[][] bins)
|
||||
{
|
||||
var sheets = new List<SheetFill>();
|
||||
foreach (var bin in bins)
|
||||
{
|
||||
var fill = new EdgeFirstFill(types, CacheFor(stock), stock, ClampsFor(stock), Work,
|
||||
() => new FrontierPacker(types, CacheFor(stock), PairsFor(stock), stock, PackAxis.X, 1.0, Work,
|
||||
null, ClampsFor(stock)),
|
||||
Orient).Fill(bin, token);
|
||||
if (fill == null)
|
||||
return null;
|
||||
sheets.Add(fill);
|
||||
}
|
||||
return sheets;
|
||||
}
|
||||
|
||||
/// <summary>Per-sheet demands for <paramref name="count"/> sheets: largest part first onto
|
||||
/// the lightest sheet, then the same parts dealt out in a snake.</summary>
|
||||
private IEnumerable<int[][]> Splits(IReadOnlyList<int> instances, int count)
|
||||
{
|
||||
var greedy = Enumerable.Range(0, count).Select(_ => new int[types.Count]).ToArray();
|
||||
var load = new double[count];
|
||||
foreach (var t in instances)
|
||||
{
|
||||
var lightest = 0;
|
||||
for (var i = 1; i < count; i++)
|
||||
if (load[i] < load[lightest] - 1e-9)
|
||||
lightest = i;
|
||||
greedy[lightest][t]++;
|
||||
load[lightest] += types[t].Area;
|
||||
}
|
||||
yield return greedy;
|
||||
if (count < 2)
|
||||
yield break;
|
||||
var snake = Enumerable.Range(0, count).Select(_ => new int[types.Count]).ToArray();
|
||||
for (var i = 0; i < instances.Count; i++)
|
||||
{
|
||||
var round = i / count;
|
||||
var slot = i % count;
|
||||
snake[round % 2 == 0 ? slot : count - 1 - slot][instances[i]]++;
|
||||
}
|
||||
if (!snake.Zip(greedy).All(p => p.First.SequenceEqual(p.Second)))
|
||||
yield return snake;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Packs the last sheet of a complete plan again inside ever shorter strips
|
||||
/// (<see cref="OrderedSqueeze"/>), along each of <see cref="SqueezeAxes"/>; a shorter
|
||||
/// sheet replaces it when the plan then beats the one given.
|
||||
/// </summary>
|
||||
public Plan Squeeze(Plan plan)
|
||||
{
|
||||
if (plan.Unplaced != 0 || plan.Sheets.Count == 0)
|
||||
return plan;
|
||||
var last = plan.Sheets[^1];
|
||||
var stock = last.Stock;
|
||||
if (stock.Outline != null || last.Parts.Count < 2 || last.Parts.Count > OrderedSqueeze.MaxParts)
|
||||
return plan;
|
||||
var best = plan;
|
||||
foreach (var axis in SqueezeAxes(job, stock))
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
var fill = new OrderedSqueeze(types, CacheFor(stock), stock, ClampsFor(stock), Work, SqueezeOrientations)
|
||||
.Fill(last.Parts, axis, token);
|
||||
if (fill == null)
|
||||
continue;
|
||||
var sheets = plan.Sheets.Take(plan.Sheets.Count - 1).Append(fill).ToList();
|
||||
var candidate = plan with
|
||||
{
|
||||
Sheets = sheets,
|
||||
Cost = NestJobCost.RequireFinite(plan.Cost - NetCost(job.Options, last) + NetCost(job.Options, fill)),
|
||||
};
|
||||
if (candidate.IsBetterThan(best))
|
||||
best = candidate;
|
||||
}
|
||||
return best;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The orientations the squeeze offers <paramref name="type"/>: the catalog's, and for a
|
||||
/// part that may turn freely also every <see cref="SqueezeAngleStep"/> that gives another
|
||||
/// outline. Built once per type per solve, as each owns no-fit polygons.
|
||||
/// </summary>
|
||||
private IReadOnlyList<Orientation> SqueezeOrientations(PartType type)
|
||||
{
|
||||
if (squeezeOrientations.TryGetValue(type.Index, out var found))
|
||||
return found;
|
||||
var all = type.Orientations.ToList();
|
||||
if (type.Perimeter != null && all.Count > 0 && type.Part.Rotation.Kind == RotationPolicyKind.Automatic)
|
||||
{
|
||||
var angles = all.Select(o => o.Rotation).ToList();
|
||||
for (var turn = 0; turn * SqueezeAngleStep < 2 * System.Math.PI - 1e-9; turn++)
|
||||
angles.Add(turn * SqueezeAngleStep);
|
||||
foreach (var angle in RotationCandidates.DistinctOutlines(type.Perimeter, angles).Skip(all.Count))
|
||||
if (Orient(type, angle) is { } extra)
|
||||
all.Add(extra);
|
||||
}
|
||||
return squeezeOrientations[type.Index] = all;
|
||||
}
|
||||
|
||||
/// <summary>A pose of <paramref name="type"/> at a solved angle, with its own NFP cache key.</summary>
|
||||
private Orientation? Orient(PartType type, double angle) =>
|
||||
PartCatalog.CreateOrientation(type, solvedOrientations++, angle);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
#nullable enable
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenNest.Engine.Jobs;
|
||||
|
||||
namespace OpenNest.Engine.NestingEngines.Irregular;
|
||||
|
||||
// What a decode is asked for and what it gives back, and how sheets and plans are costed.
|
||||
public sealed partial class IrregularNestingEngine
|
||||
{
|
||||
/// <summary>Relative difference within which two plan or tail costs tie.</summary>
|
||||
private const double CostTie = 1e-9;
|
||||
|
||||
/// <summary>How a decode fills each sheet.</summary>
|
||||
private enum SheetRule
|
||||
{
|
||||
/// <summary>The frontier packer alone, offered Lattice blocks.</summary>
|
||||
Frontier,
|
||||
|
||||
/// <summary>Like parts packed as groups, where the remaining demand has any.</summary>
|
||||
Grouped,
|
||||
|
||||
/// <summary>Full-height bands or strips.</summary>
|
||||
Banded,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One whole-job decode: the front direction and area exponent of the placement rule, the
|
||||
/// per-type weights on it (none outside a seeded search), and how each sheet is filled.
|
||||
/// </summary>
|
||||
private sealed record Strategy(PackAxis Axis, double Beta, IReadOnlyList<double>? Weights = null,
|
||||
SheetRule Rule = SheetRule.Frontier)
|
||||
{
|
||||
public static Strategy Grouped(PackAxis axis, IReadOnlyList<double>? weights = null) =>
|
||||
new(axis, 1.0, weights, SheetRule.Grouped);
|
||||
|
||||
public static Strategy Banded { get; } = new(PackAxis.X, 1.0, null, SheetRule.Banded);
|
||||
}
|
||||
|
||||
/// <summary>One sheet a decode packed on trial, with its net cost.</summary>
|
||||
private readonly record struct Trial(SheetFill Fill, double Net);
|
||||
|
||||
/// <summary>A decode's sheets in order, their net cost and why it stopped.</summary>
|
||||
private sealed record Run(IReadOnlyList<SheetFill> Sheets, double Net, NestJobStopReason Reason);
|
||||
|
||||
/// <summary>Ranked by unplaced parts, cost, sheet count, then compactness offcut (without salvage
|
||||
/// credit every one-sheet plan costs the same); full ties keep the incumbent.</summary>
|
||||
private sealed record Plan(IReadOnlyList<SheetFill> Sheets, double Cost, int Unplaced, NestJobStopReason Reason,
|
||||
bool Priced, NestJobOptions Options)
|
||||
{
|
||||
private double Offcut => Sheets.Sum(s => IrregularNestingEngine.Offcut(Options, s));
|
||||
|
||||
public bool IsBetterThan(Plan other)
|
||||
{
|
||||
if (Unplaced != other.Unplaced)
|
||||
return Unplaced < other.Unplaced;
|
||||
if (Priced && Cost != other.Cost)
|
||||
return Cost < other.Cost;
|
||||
var scale = System.Math.Max(1, System.Math.Max(Cost, other.Cost));
|
||||
if (System.Math.Abs(Cost - other.Cost) > CostTie * scale)
|
||||
return Cost < other.Cost;
|
||||
if (Sheets.Count != other.Sheets.Count)
|
||||
return Sheets.Count < other.Sheets.Count;
|
||||
var offcut = Offcut;
|
||||
var otherOffcut = other.Offcut;
|
||||
return offcut - otherOffcut > CostTie * System.Math.Max(1, System.Math.Max(offcut, otherOffcut));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>A plan of <paramref name="job"/>, ranked as the job is priced.</summary>
|
||||
private static Plan PlanOf(NestJob job, IReadOnlyList<SheetFill> sheets, double cost, int unplaced,
|
||||
NestJobStopReason reason) =>
|
||||
new(sheets, cost, unplaced, reason, NestJobCost.UsesExplicitCosts(job), job.Options);
|
||||
|
||||
private static double NetCost(NestJobOptions options, SheetFill fill) =>
|
||||
fill.Parts.Count == 0 ? NestJobCost.GrossSheetCost(fill.Stock)
|
||||
: NestJobCost.NetSheetCost(options, fill.Stock, Envelope(fill));
|
||||
|
||||
/// <summary>Equal-cost tie-break: the fill's <see cref="NestJobCost.CompactnessOffcut(NestJobOptions, NestPlateStock, OpenNest.Geometry.Box)"/>.</summary>
|
||||
private static double Offcut(NestJobOptions options, SheetFill fill) =>
|
||||
fill.Parts.Count == 0 ? 0 : NestJobCost.CompactnessOffcut(options, fill.Stock, Envelope(fill));
|
||||
|
||||
private static OpenNest.Geometry.Box Envelope(SheetFill fill)
|
||||
{
|
||||
var left = fill.Parts.Min(p => p.Left);
|
||||
var bottom = fill.Parts.Min(p => p.Bottom);
|
||||
return new OpenNest.Geometry.Box(left, bottom,
|
||||
fill.Parts.Max(p => p.Right) - left, fill.Parts.Max(p => p.Top) - bottom);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,463 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using OpenNest.Engine.Jobs;
|
||||
|
||||
namespace OpenNest.Engine.NestingEngines.Irregular;
|
||||
|
||||
public sealed partial class IrregularNestingEngine
|
||||
{
|
||||
/// <summary>
|
||||
/// Shared state for one solve: job, catalog, NFP caches, effort meter. This half decodes a
|
||||
/// strategy into a plan, sheet by sheet; the other improves a plan it is given.
|
||||
/// </summary>
|
||||
private sealed partial class Solver(
|
||||
IrregularNestingEngine engine,
|
||||
NestJob job,
|
||||
IReadOnlyList<PartType> types,
|
||||
IProgress<NestJobProgress>? progress,
|
||||
CancellationToken token
|
||||
) : IDisposable
|
||||
{
|
||||
/// <summary>Most plans one <see cref="Plan"/> call keeps beside the one it returns.</summary>
|
||||
private const int MaxAlternatives = 6;
|
||||
|
||||
private readonly long workBudget = engine.WorkBudget;
|
||||
private readonly Dictionary<double, NoFitCache> caches = new();
|
||||
private readonly Dictionary<NestPlateStock, ClampObstacles> clamps = new();
|
||||
private readonly Dictionary<NestPlateStock, PackBoundary?> boundaries = new();
|
||||
private readonly Dictionary<double, IReadOnlyDictionary<int, IReadOnlyList<PairPose>>> pairs = new();
|
||||
private readonly Dictionary<double, BlockCatalog> blocks = new();
|
||||
private readonly IReadOnlyDictionary<string, NestJobPart> parts = job.Parts.ToDictionary(p => p.Id, StringComparer.Ordinal);
|
||||
|
||||
/// <summary>Work at which the current grouped plan stops starting new fills.</summary>
|
||||
private long groupedBudget;
|
||||
|
||||
/// <summary>Whether the decode under way is <see cref="QuickPlan"/>'s, which offers no blocks.</summary>
|
||||
private bool quick;
|
||||
|
||||
public WorkCounter Work { get; private set; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// The complete plans the last <see cref="Plan"/> call found beside the one it returned,
|
||||
/// best first: the job finished on another stock at some sheet, or the decode's own plan
|
||||
/// when one of those beat it.
|
||||
/// </summary>
|
||||
public IReadOnlyList<Plan> Alternatives { get; private set; } = Array.Empty<Plan>();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
foreach (var catalog in blocks.Values)
|
||||
catalog.Dispose();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Each type's demand, or zero for a type no stock can hold. 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.
|
||||
/// </summary>
|
||||
public int[] PlaceableDemand()
|
||||
{
|
||||
var demand = new int[types.Count];
|
||||
foreach (var type in types)
|
||||
{
|
||||
var placeable = job.Plates.Any(stock =>
|
||||
stock.Quantity != 0
|
||||
&& type.Orientations.Any(o => stock.Fits(o.Width, o.Height))
|
||||
);
|
||||
demand[type.Index] = placeable || PairFits(type)
|
||||
|| (type.Part.Quantity > 2 && type.Orientations.Count > 0) ? type.Part.Quantity : 0;
|
||||
}
|
||||
return demand;
|
||||
}
|
||||
|
||||
private bool PairFits(PartType type)
|
||||
{
|
||||
if (type.Part.Quantity < 2 || type.Orientations.Count == 0)
|
||||
return false;
|
||||
return job.Plates.Any(stock => stock.Quantity != 0
|
||||
&& PairsFor(stock).TryGetValue(type.Index, out var candidates)
|
||||
&& candidates.Any(pair => stock.Fits(pair.Width, pair.Height)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The first variant decoded without Lattice blocks, on a work meter of its own, so the
|
||||
/// search after it runs as without it: what it adds to the shared caches (no-fit polygons,
|
||||
/// pairs, clamps, boundaries) depends only on the job, never on when it was built.
|
||||
/// </summary>
|
||||
public Plan QuickPlan(int[] demand)
|
||||
{
|
||||
var meter = Work;
|
||||
Work = new WorkCounter();
|
||||
quick = true;
|
||||
try
|
||||
{
|
||||
return Plan(demand, new Strategy(Variants[0].Axis, Variants[0].Beta));
|
||||
}
|
||||
finally
|
||||
{
|
||||
Work = meter;
|
||||
quick = false;
|
||||
Alternatives = Array.Empty<Plan>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Decodes the whole demand under <paramref name="strategy"/> and returns the best plan
|
||||
/// found, leaving the others in <see cref="Alternatives"/>.
|
||||
/// </summary>
|
||||
public Plan Plan(int[] demand, Strategy strategy)
|
||||
{
|
||||
groupedBudget = Work.Value + workBudget;
|
||||
var finishers = new List<Run>();
|
||||
var run = Decode(demand, strategy, new Dictionary<string, int>(StringComparer.Ordinal), job.Options.MaxPlates, null,
|
||||
finishers);
|
||||
// The decode picks each sheet's stock on an estimate of what the leftovers will cost,
|
||||
// so a stock that finished the job outright can lose the pick to a plan that ends up
|
||||
// dearer. Each such finished plan competes with the decode's own; a tie keeps the decode.
|
||||
var decoded = ToPlan(run);
|
||||
var best = decoded;
|
||||
var plans = new List<Plan> { decoded };
|
||||
foreach (var finisher in finishers)
|
||||
{
|
||||
var plan = ToPlan(finisher);
|
||||
plans.Add(plan);
|
||||
if (plan.IsBetterThan(best))
|
||||
best = plan;
|
||||
}
|
||||
plans.Remove(best);
|
||||
plans.Sort((a, b) => a.IsBetterThan(b) ? -1 : b.IsBetterThan(a) ? 1 : 0);
|
||||
Alternatives = plans.Take(MaxAlternatives).ToList();
|
||||
return best;
|
||||
}
|
||||
|
||||
private Plan ToPlan(Run run)
|
||||
{
|
||||
var unplaced = types.Sum(t => t.Part.Quantity) - run.Sheets.Sum(s => s.Parts.Count);
|
||||
var reason = run.Reason;
|
||||
if (unplaced > 0 && reason == NestJobStopReason.Completed)
|
||||
reason = NestJobStopReason.NoPlacementFound; // Demand no stock can hold.
|
||||
var penalty = NestJobCost.UnplacedPartPenalty(job);
|
||||
return PlanOf(job, run.Sheets, NestJobCost.RequireFinite(run.Net + unplaced * penalty),
|
||||
unplaced, reason);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Greedy sheet-by-sheet decode. <paramref name="usedBefore"/> seeds finite-stock
|
||||
/// accounting, <paramref name="sheetCap"/> bounds the sheets this run may add, and
|
||||
/// <paramref name="first"/>, when set, forces the stock of the first sheet.
|
||||
/// <paramref name="finishers"/>, when set, receives every complete run the decode passed
|
||||
/// over: the sheets chosen so far plus a trial sheet that took all the remaining parts
|
||||
/// but was not the sheet chosen. Collecting them never changes the decode.
|
||||
/// </summary>
|
||||
private Run Decode(
|
||||
int[] demand,
|
||||
Strategy strategy,
|
||||
IReadOnlyDictionary<string, int> usedBefore,
|
||||
int? sheetCap,
|
||||
NestPlateStock? first,
|
||||
List<Run>? finishers = null
|
||||
)
|
||||
{
|
||||
var remaining = (int[])demand.Clone();
|
||||
var used = job.Plates.ToDictionary(s => s.Id, s => usedBefore.GetValueOrDefault(s.Id), StringComparer.Ordinal);
|
||||
var sheets = new List<SheetFill>();
|
||||
var net = 0.0;
|
||||
|
||||
while (true)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
if (remaining.All(r => r == 0))
|
||||
return new Run(sheets, net, NestJobStopReason.Completed);
|
||||
if (sheetCap is int cap && sheets.Count >= cap)
|
||||
return new Run(sheets, net, NestJobStopReason.PlateLimitReached);
|
||||
|
||||
var forced = sheets.Count == 0 ? first : null;
|
||||
var trials = TrySheets(strategy, remaining, used, sheets.Count, forced, prune: finishers == null);
|
||||
if (trials.Count == 0)
|
||||
{
|
||||
var exhausted = job.Plates.Any(s => s.Quantity is int q && used[s.Id] >= q);
|
||||
return new Run(sheets, net,
|
||||
exhausted ? NestJobStopReason.StockExhausted : NestJobStopReason.NoPlacementFound);
|
||||
}
|
||||
|
||||
var chosen = Choose(trials, remaining);
|
||||
finishers?.AddRange(PassedOver(trials, chosen, remaining, sheets, net));
|
||||
|
||||
var (fill, fillNet) = trials[chosen];
|
||||
var counts = fill.Parts.GroupBy(p => types[p.Orientation.TypeIndex].Part.Id, StringComparer.Ordinal)
|
||||
.ToDictionary(g => g.Key, g => g.Count(), StringComparer.Ordinal);
|
||||
// Count all stock still available, not just the forced-first trial subset.
|
||||
var availableEntries = job.Plates.Count(s => s.Quantity == null || used[s.Id] < s.Quantity);
|
||||
var copies = Copies(fill, counts, remaining, availableEntries, fill.Stock.Quantity - used[fill.Stock.Id],
|
||||
sheetCap - sheets.Count);
|
||||
engine.BatchSelected?.Invoke(new RepeatBatch(fill.Stock.Id, copies, availableEntries, sheets.Count,
|
||||
forced != null));
|
||||
for (var copy = 0; copy < copies; copy++)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
// Poses are immutable records over the read-only catalog; each physical sheet
|
||||
// owns a separate list so tail replanning can remove/replace individual copies.
|
||||
sheets.Add(fill with { Parts = fill.Parts.ToList() });
|
||||
net = NestJobCost.RequireFinite(net + fillNet);
|
||||
}
|
||||
used[fill.Stock.Id] = checked(used[fill.Stock.Id] + copies);
|
||||
foreach (var type in types)
|
||||
if (counts.TryGetValue(type.Part.Id, out var count))
|
||||
remaining[type.Index] = checked(remaining[type.Index] - checked(count * copies));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Packs the remaining demand on one sheet of every stock still on offer (only
|
||||
/// <paramref name="only"/> when set) and returns the trials that placed a part, in stock
|
||||
/// order.
|
||||
/// </summary>
|
||||
private List<Trial> TrySheets(Strategy strategy, int[] remaining, Dictionary<string, int> used, int sheetIndex,
|
||||
NestPlateStock? only, bool prune)
|
||||
{
|
||||
var trials = new List<Trial>();
|
||||
var areaLeft = AreaOf(remaining);
|
||||
foreach (var stock in job.Plates)
|
||||
{
|
||||
token.ThrowIfCancellationRequested();
|
||||
if (only != null && !ReferenceEquals(stock, only))
|
||||
continue;
|
||||
if (stock.Quantity is int available && used[stock.Id] >= available)
|
||||
continue;
|
||||
// Only once the block budget is spent: before that a skipped trial would leave
|
||||
// block attempts and work unspent that a later trial could then use.
|
||||
if (engine.PruneStockTrials && prune && Work.Value >= workBudget && CannotWin(stock, trials, areaLeft))
|
||||
continue;
|
||||
progress?.Report(new NestJobProgress(NestJobStage.EvaluatingCandidate, stock.Id, sheetIndex, 0, 0));
|
||||
var fill = FillSheet(stock, strategy, remaining);
|
||||
engine.SheetPacked?.Invoke();
|
||||
if (fill.Parts.Count > 0)
|
||||
trials.Add(new Trial(fill, NetCost(job.Options, fill)));
|
||||
}
|
||||
return trials;
|
||||
}
|
||||
|
||||
/// <summary>One sheet of <paramref name="stock"/> filled as the strategy's rule says.</summary>
|
||||
private SheetFill FillSheet(NestPlateStock stock, Strategy strategy, int[] remaining)
|
||||
{
|
||||
// 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 already; its
|
||||
// gap fill takes singles and pairs.
|
||||
var offersBlocks = strategy.Rule == SheetRule.Frontier && !quick;
|
||||
FrontierPacker Packer(IReadOnlyList<int>? ranks, double? reach) => new(types, CacheFor(stock),
|
||||
PairsFor(stock), stock, strategy.Axis, strategy.Beta, Work, offersBlocks ? BlocksFor(stock) : null,
|
||||
ClampsFor(stock), BoundaryFor(stock), strategy.Weights, ranks, reach);
|
||||
|
||||
return strategy.Rule switch
|
||||
{
|
||||
SheetRule.Banded => Banded(stock, remaining, strategy.Weights, Packer),
|
||||
SheetRule.Grouped when GroupedFill.Applies(types, remaining) =>
|
||||
new GroupedFill(types, Packer, strategy.Axis, stock.Outline == null, CompareSheets, Work, groupedBudget)
|
||||
.Fill(remaining, token),
|
||||
_ => Packer(null, null).Fill(remaining, token),
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One banded sheet: like parts as full-height bands side by side, or strip by strip with
|
||||
/// the largest part's band or column and the others in its gaps; the better sheet wins, a
|
||||
/// tie keeping the bands. Falls back to the ordinary fill when nothing bands.
|
||||
/// </summary>
|
||||
private SheetFill Banded(NestPlateStock stock, int[] remaining, IReadOnlyList<double>? weights,
|
||||
Func<IReadOnlyList<int>?, double?, FrontierPacker> packer)
|
||||
{
|
||||
FrontierPacker Ordinary() => packer(null, null);
|
||||
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);
|
||||
BandedFill Bands(long budget) => new(types, BlocksFor(stock), stock, ClampsFor(stock), Ordinary,
|
||||
CompareSheets, Work, budget);
|
||||
|
||||
// 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;
|
||||
if (AreaOf(remaining) > work.Length * work.Width)
|
||||
{
|
||||
var plain = Ordinary().Fill(remaining, token);
|
||||
var full = Bands(Work.Value + workBudget).FillStrips(remaining, Within, token, 1);
|
||||
return full != null && CompareSheets(full, plain) < 0 ? full : plain;
|
||||
}
|
||||
var bands = Bands(groupedBudget).Fill(remaining, token);
|
||||
var strips = Bands(Work.Value + workBudget).FillStrips(remaining, Within, token);
|
||||
if (bands == null)
|
||||
return strips ?? Ordinary().Fill(remaining, token);
|
||||
return strips != null && CompareSheets(strips, bands) < 0 ? strips : bands;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// One sheet against another for the same demand: more part area placed first, then lower net
|
||||
/// cost, then the larger compactness offcut; negative when <paramref name="a"/> is better.
|
||||
/// </summary>
|
||||
private int CompareSheets(SheetFill a, SheetFill b)
|
||||
{
|
||||
static int Compare(double x, double y) =>
|
||||
System.Math.Abs(x - y) <= CostTie * System.Math.Max(1, System.Math.Max(System.Math.Abs(x), System.Math.Abs(y)))
|
||||
? 0 : x.CompareTo(y);
|
||||
var area = Compare(b.PartArea, a.PartArea);
|
||||
if (area != 0)
|
||||
return area;
|
||||
var cost = Compare(NetCost(job.Options, a), NetCost(job.Options, b));
|
||||
return cost != 0 ? cost : Compare(Offcut(job.Options, b), Offcut(job.Options, a));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Whether a trial of <paramref name="stock"/> 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.
|
||||
/// </summary>
|
||||
private bool CannotWin(NestPlateStock stock, List<Trial> 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 = BestRatio(trials);
|
||||
var estimate = trials.Min(t => Estimate(t, areaLeft, ratio));
|
||||
const double Margin = 1 + 1e-9;
|
||||
return least > estimate * Margin && least >= ratio * areaLeft * Margin;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The trial to commit, by look-ahead: whatever a trial leaves behind is charged at the best
|
||||
/// efficiency any trial achieved, so a sheet that finishes the job competes fairly with a
|
||||
/// denser partial one. Ties go to more parts, then the larger offcut, then stock order.
|
||||
/// </summary>
|
||||
private int Choose(List<Trial> trials, int[] remaining)
|
||||
{
|
||||
var areaLeft = AreaOf(remaining);
|
||||
var ratio = BestRatio(trials);
|
||||
return trials
|
||||
.Select((t, order) => (t.Fill, order, Estimate: NestJobCost.RequireFinite(Estimate(t, areaLeft, ratio))))
|
||||
.OrderBy(t => t.Estimate)
|
||||
.ThenByDescending(t => t.Fill.Parts.Count)
|
||||
.ThenByDescending(t => NestJobCost.TieBreakKey(Offcut(job.Options, t.Fill)))
|
||||
.ThenBy(t => t.order)
|
||||
.First()
|
||||
.order;
|
||||
}
|
||||
|
||||
/// <summary>The lowest net cost per unit of part area among the trials.</summary>
|
||||
private static double BestRatio(List<Trial> trials) =>
|
||||
trials.Min(t => t.Net / System.Math.Max(t.Fill.PartArea, 1e-12));
|
||||
|
||||
/// <summary>A trial's whole-job cost estimate: its own net cost, and the part area it
|
||||
/// leaves at <paramref name="ratio"/>.</summary>
|
||||
private static double Estimate(Trial trial, double areaLeft, double ratio) =>
|
||||
trial.Net + System.Math.Max(0, areaLeft - trial.Fill.PartArea) * ratio;
|
||||
|
||||
/// <summary>
|
||||
/// The complete runs a choice passed over: the sheets so far plus each trial, other than
|
||||
/// the chosen one, that took all the remaining parts.
|
||||
/// </summary>
|
||||
private static IEnumerable<Run> PassedOver(List<Trial> trials, int chosen, int[] remaining,
|
||||
List<SheetFill> sheets, double net)
|
||||
{
|
||||
var left = remaining.Sum();
|
||||
for (var i = 0; i < trials.Count; i++)
|
||||
{
|
||||
if (i == chosen || trials[i].Fill.Parts.Count != left)
|
||||
continue;
|
||||
// Each run owns its sheets' part lists, as the chosen copies do.
|
||||
var finished = sheets.Select(s => s with { Parts = s.Parts.ToList() }).Append(trials[i].Fill).ToList();
|
||||
yield return new Run(finished, NestJobCost.RequireFinite(net + trials[i].Net), NestJobStopReason.Completed);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// How many sheets in a row take the chosen layout: as many as the demand, the stock and
|
||||
/// the sheet cap allow, but one unless the runner's candidate validation certifies it.
|
||||
/// </summary>
|
||||
private int Copies(SheetFill fill, Dictionary<string, int> counts, int[] remaining, int availableEntries,
|
||||
int? stockLeft, int? sheetsLeft)
|
||||
{
|
||||
var outstanding = types.ToDictionary(t => t.Part.Id, t => remaining[t.Index], StringComparer.Ordinal);
|
||||
// Fill has no partial/budget exit: it returns after states empty or Choose finds
|
||||
// no supported placement. WorkBudget only controls later variants/block preparation.
|
||||
var copies = CompletedLayoutRepeat.Limit(outstanding, counts, searchCompleted: true, availableEntries,
|
||||
stockLeft, sheetsLeft);
|
||||
if (copies <= 1)
|
||||
return copies;
|
||||
try
|
||||
{
|
||||
NestJobValidator.ValidateCandidate(new PlateCandidate(fill.Parts.Select(p =>
|
||||
new NestJobPlacement(types[p.Orientation.TypeIndex].Part.Id, 0, p.X, p.Y, p.Orientation.Rotation))),
|
||||
fill.Stock, outstanding, parts);
|
||||
return copies;
|
||||
}
|
||||
catch (InvalidOperationException)
|
||||
{
|
||||
return 1; // Uncertified candidates retain ordinary allocation/final validation.
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The part area of <paramref name="counts"/> copies of each type.</summary>
|
||||
private double AreaOf(int[] counts) => types.Sum(t => counts[t.Index] * t.Area);
|
||||
|
||||
private static double Clearance(NestPlateStock stock) => System.Math.Max(0, stock.PartSpacing);
|
||||
|
||||
private NoFitCache CacheFor(NestPlateStock stock)
|
||||
{
|
||||
var clearance = Clearance(stock);
|
||||
if (!caches.TryGetValue(clearance, out var cache))
|
||||
caches[clearance] = cache = new NoFitCache(clearance);
|
||||
return cache;
|
||||
}
|
||||
|
||||
/// <summary>An outlined sheet's usable material as the packer's boundary, built once per
|
||||
/// stock per solve; null for a rectangular sheet, which keeps the work-area path.</summary>
|
||||
private PackBoundary? BoundaryFor(NestPlateStock stock)
|
||||
{
|
||||
if (stock.Outline == null)
|
||||
return null;
|
||||
if (!boundaries.TryGetValue(stock, out var found))
|
||||
boundaries[stock] = found = new PackBoundary(stock.Outline.Material(stock.PlacementOrigin),
|
||||
stock.OutlineInset, stock.WorkArea, CacheFor(stock));
|
||||
return found;
|
||||
}
|
||||
|
||||
private ClampObstacles? ClampsFor(NestPlateStock stock)
|
||||
{
|
||||
if (stock.ClampZones.Count == 0)
|
||||
return null;
|
||||
if (!clamps.TryGetValue(stock, out var found))
|
||||
clamps[stock] = found = new ClampObstacles(stock);
|
||||
return found;
|
||||
}
|
||||
|
||||
/// <summary>Best-fit pairs for this stock's spacing, built once per solve.</summary>
|
||||
private IReadOnlyDictionary<int, IReadOnlyList<PairPose>> PairsFor(NestPlateStock stock)
|
||||
{
|
||||
var clearance = Clearance(stock);
|
||||
if (!pairs.TryGetValue(clearance, out var found))
|
||||
{
|
||||
// The best-fit plate filter only needs the largest sheet at this spacing;
|
||||
// each packer still checks the pair against its own work area.
|
||||
var sheets = job.Plates.Where(p => Clearance(p) == clearance).ToList();
|
||||
pairs[clearance] = found = PairCatalog.Build(types, clearance,
|
||||
sheets.Max(p => p.Size.Length), sheets.Max(p => p.Size.Width), token, engine.DrawingFor);
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
/// <summary>Lattice blocks for this stock's spacing, built once per solve.</summary>
|
||||
private BlockCatalog BlocksFor(NestPlateStock stock)
|
||||
{
|
||||
var spacing = Clearance(stock);
|
||||
if (!blocks.TryGetValue(spacing, out var found))
|
||||
blocks[spacing] = found = new BlockCatalog(spacing, types, PairsFor(stock), Work, workBudget, engine.DrawingFor);
|
||||
return found;
|
||||
}
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,11 @@
|
||||
using System.Threading;
|
||||
|
||||
namespace OpenNest.Engine.NestingEngines.Irregular;
|
||||
|
||||
/// <summary>Deterministic effort meter shared by all packers in one solve.</summary>
|
||||
internal sealed class WorkCounter
|
||||
{
|
||||
private long value;
|
||||
public long Value => Interlocked.Read(ref value);
|
||||
public void Add(long amount) => Interlocked.Add(ref value, amount);
|
||||
}
|
||||
Reference in new issue
Block a user