Files
OpenNest/OpenNest.Engine.Tests/NestingEngines/IrregularStockPruningTests.cs
T
ajandClaude Opus 5.5 7f92a637d7 fix(irregular): keep block preparation and plan reporting as before the quick plan
Stock-trial pruning in tail re-plans now starts only once the block budget
is spent. Before that, a skipped trial left a Lattice block attempt and its
work unspent, so a later trial could prepare a block it was refused before
and the decode could choose differently.

The quick plan no longer demotes the normal search's plans to
alternatives. The deadline wrapper, the only caller that enables
SearchUntilCancelled, already keeps the best reported plan that passes the
layout check, so a quick plan that failed the check could have hidden
valid plans. The docs drop the measured timings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 01:36:19 -05:00

48 lines
2.2 KiB
C#

using OpenNest.Engine.Jobs;
using OpenNest.Engine.NestingEngines.Irregular;
using static OpenNest.Engine.Tests.NestingEngines.JobBuilder;
using static OpenNest.Engine.Tests.NestingEngines.Shapes;
namespace OpenNest.Engine.Tests.NestingEngines;
/// <summary>
/// A tail re-plan's decode skips stock trials that can neither be chosen nor change the choice:
/// once the block budget is spent it packs fewer sheets and decides exactly as when it packs every
/// trial.
/// </summary>
public class IrregularStockPruningTests
{
private static NestJob MixedJob(NestJobOptions? options) =>
Job(
new[] { Part("disc", Disc(3), 10), Part("ell", LShape(12, 8, 4), 10), Part("tri", Triangle(9, 7), 8) },
// Small sheets first, so a trial is packed before the large sheet is considered.
new[] { Stock("small", 20, 30, spacing: 0.5), Stock("sheet", 40, 60, spacing: 0.5), Stock("large", 120, 200, spacing: 0.5) },
options
);
private static List<(string, string, int, double, double, double)> Placements(NestJobResult result) =>
result.Plates.SelectMany(p => p.Placements.Select(q => (p.Stock.Id, q.PartId, q.InstanceIndex, q.X, q.Y, q.Rotation)))
.ToList();
// Pruning starts only once the block budget is spent; this small job spends a small budget
// in its variants, leaving its tail re-plans to prune.
private const long Budget = 20_000;
public static TheoryData<bool> Salvage => new() { false, true };
[Theory]
[MemberData(nameof(Salvage))]
public void SkippedTrialsChangeNoChoice(bool salvage)
{
var options = salvage ? new NestJobOptions(salvageRate: 0.5, minimumSalvageDimension: 1) : null;
var all = 0;
var pruned = 0;
var full = new IrregularNestingEngine { WorkBudget = Budget, PruneStockTrials = false, SheetPacked = () => all++ }.Solve(MixedJob(options));
var skipped = new IrregularNestingEngine { WorkBudget = Budget, SheetPacked = () => pruned++ }.Solve(MixedJob(options));
Assert.Equal(Placements(full), Placements(skipped));
Assert.NotEmpty(Placements(full));
Assert.True(pruned < all, $"packed {pruned} of {all} sheets");
}
}