mirror of
https://github.com/ajisaacs/OpenNest.git
synced 2026-11-22 05:28:41 -05:00
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>
91 lines
4.1 KiB
C#
91 lines
4.1 KiB
C#
#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);
|
|
}
|
|
}
|