diff --git a/OpenNest.Tests/CuttingPlanning/ProblemNestRegressionTests.cs b/OpenNest.Tests/CuttingPlanning/ProblemNestRegressionTests.cs index 16659eee..71fb634a 100644 --- a/OpenNest.Tests/CuttingPlanning/ProblemNestRegressionTests.cs +++ b/OpenNest.Tests/CuttingPlanning/ProblemNestRegressionTests.cs @@ -147,11 +147,13 @@ public partial class ProblemNestRegressionTests(ITestOutputHelper output) ?? throw new InvalidOperationException($"'{record.Name}' has no file."); var baseline = SheetBaseline(record.Comments); var endBaseline = EndBaseline(record.Comments); + var costBaseline = CostBaseline(record.Comments); var watch = Stopwatch.StartNew(); var run = AutoNest(bytes, limit); - var (sheets, end, problem) = (run.Sheets, run.End, run.Problem); - output.WriteLine($"{record.Name}: {(problem ?? $"{sheets} sheets, ends {Number(end)}")} " - + $"(baseline {baseline?.ToString() ?? "none"}{(endBaseline is { } recorded ? $", ends {Number(recorded)}" : "")}), " + var (sheets, end, problem, cost) = (run.Sheets, run.End, run.Problem, run.Cost); + output.WriteLine($"{record.Name}: {(problem ?? $"{sheets} sheets, ends {Number(end)}, cost {Number(cost)}")} " + + $"(baseline {baseline?.ToString() ?? "none"}{(endBaseline is { } recorded ? $", ends {Number(recorded)}" : "")}" + + $"{(costBaseline is { } shownCost ? $", cost {Number(shownCost)}" : "")}), " + $"{watch.Elapsed.TotalSeconds:0.0} s"); var failed = failures.Count; if (problem != null) @@ -161,6 +163,14 @@ public partial class ProblemNestRegressionTests(ITestOutputHelper output) else if (baseline == null) failures.Add($"{record.Name} has no Auto Nest baseline. Add this line to its comments: " + AutoNestBaselineLine(sheets, end)); + else if (costBaseline is { } priced) + { + // A recorded cost decides: a plan can take more sheets for less material. + if (cost > priced + CostTolerance) + failures.Add($"{record.Name}: costs {Number(cost)}, more than baseline {Number(priced)}."); + else if (cost < priced - 2 * CostTolerance) + output.WriteLine($" cheaper than {Number(priced)}; update the baseline: {AutoNestBaselineLine(sheets, end, cost)}"); + } else if (sheets > baseline) failures.Add($"{record.Name}: {sheets} sheets, worse than baseline {baseline}."); else if (sheets < baseline) @@ -181,7 +191,9 @@ public partial class ProblemNestRegressionTests(ITestOutputHelper output) // A layout inside the tolerance past the recorded end keeps that end: recording // again and again must not let the baseline creep. var keptEnd = endBaseline is { } known && sheets == baseline && end > known ? known : (double?)null; - var numbers = keptEnd is { } stays ? $"{AutoNestLine} {sheets} sheets, ends {Number(stays)}" + var keptCost = costBaseline is { } knownCost && cost > knownCost ? knownCost : (double?)null; + var numbers = costBaseline != null ? AutoNestBaselineLine(sheets, end, keptCost ?? cost) + : keptEnd is { } stays ? $"{AutoNestLine} {sheets} sheets, ends {Number(stays)}" : AutoNestBaselineLine(sheets, end); if (CannotSave(guards, run) is { } why) { @@ -203,7 +215,11 @@ public partial class ProblemNestRegressionTests(ITestOutputHelper output) /// One Auto Nest of a problem nest: its sheet count and where its last sheet ends, /// or why the layout cannot be kept; the job and result when the engine returned one. - internal sealed record AutoNestRun(int Sheets, double End, string Problem, NestJob Job, NestJobResult Result); + internal sealed record AutoNestRun(int Sheets, double End, string Problem, NestJob Job, NestJobResult Result) + { + /// The job cost of a kept layout (); zero otherwise. + public double Cost { get; init; } + } // Auto Nest on the nest's demand. private static AutoNestRun AutoNest(byte[] bytes, TimeSpan limit) @@ -218,7 +234,10 @@ public partial class ProblemNestRegressionTests(ITestOutputHelper output) return new AutoNestRun(result.Plates.Count, 0, "invalid layout: " + string.Join(" | ", violations.Take(3)), job, result); var unplaced = result.Fulfillment.Sum(f => f.Unplaced); return unplaced != 0 ? new AutoNestRun(result.Plates.Count, 0, $"{unplaced} parts unplaced", job, result) - : new AutoNestRun(result.Plates.Count, LastSheetEnd(job, result), null, job, result); + : new AutoNestRun(result.Plates.Count, LastSheetEnd(job, result), null, job, result) + { + Cost = OpenNest.Engine.Jobs.NestJobCost.Evaluate(job, result), + }; } catch (OperationCanceledException) { @@ -397,9 +416,23 @@ public partial class ProblemNestRegressionTests(ITestOutputHelper output) // A recorded end is rounded to a tenth; a layout within half of that is the same layout. internal const double EndTolerance = 0.05; - /// The baseline line for a result, its end rounded up to a tenth so the result meets it. - internal static string AutoNestBaselineLine(int sheets, double end) => - $"{AutoNestLine} {sheets} sheets, ends {Number(System.Math.Ceiling(end * 10 - 1e-6) / 10)}"; + /// + /// The job cost from an "auto-nest: 8 sheets, ends 95.5, cost 54144" comment line; null when + /// the line records none. With a cost the cost decides, not the sheet count or the end: for a + /// job whose better plan takes more sheets of less material. + /// + internal static double? CostBaseline(string comments) => + CommentLine(comments, AutoNestLine) is { } line && CostEntry().Match(line) is { Success: true } cost + ? double.Parse(cost.Groups[1].Value, System.Globalization.CultureInfo.InvariantCulture) : null; + + // A recorded cost is rounded up to a whole unit; a layout within half of that is the same. + internal const double CostTolerance = 0.5; + + /// The baseline line for a result, its end rounded up to a tenth and any cost up to a + /// whole unit, so the result meets it. + internal static string AutoNestBaselineLine(int sheets, double end, double? cost = null) => + $"{AutoNestLine} {sheets} sheets, ends {Number(System.Math.Ceiling(end * 10 - 1e-6) / 10)}" + + (cost is { } c ? $", cost {Number(System.Math.Ceiling(c - 1e-6))}" : ""); private static string Number(double value) => value.ToString("0.##", System.Globalization.CultureInfo.InvariantCulture); @@ -763,6 +796,9 @@ public partial class ProblemNestRegressionTests(ITestOutputHelper output) [GeneratedRegex(@"\bends\s+(?:at\s+)?(\d+(?:\.\d+)?)", RegexOptions.IgnoreCase)] private static partial Regex EndEntry(); + + [GeneratedRegex(@"\bcost\s+(\d+(?:\.\d+)?)", RegexOptions.IgnoreCase)] + private static partial Regex CostEntry(); } public class ProblemNestBaselineTests @@ -821,6 +857,19 @@ public class ProblemNestBaselineTests Assert.Null(ProblemNestRegressionTests.EndBaseline("planning: 1=Ready")); } + [Fact] + public void CostBaseline_ReadsTheCostWhenTheLineSaysSo() + { + const string comments = "checks: auto-nest\nauto-nest: 8 sheets, ends 95.5, cost 54144"; + + Assert.Equal(8, ProblemNestRegressionTests.SheetBaseline(comments)); + Assert.Equal(95.5, ProblemNestRegressionTests.EndBaseline(comments)); + Assert.Equal(54144, ProblemNestRegressionTests.CostBaseline(comments)); + Assert.Null(ProblemNestRegressionTests.CostBaseline("auto-nest: 2 sheets, ends 40")); + Assert.Equal("auto-nest: 8 sheets, ends 95.5, cost 54144", + ProblemNestRegressionTests.AutoNestBaselineLine(8, 95.43, 54143.2)); + } + [Fact] public void AutoNestBaselineLine_RoundsTheEndUpSoTheResultMeetsIt() { diff --git a/docs/problem-nests.md b/docs/problem-nests.md index 8f08ef5c..1b58c994 100644 --- a/docs/problem-nests.md +++ b/docs/problem-nests.md @@ -77,6 +77,13 @@ ending sooner is only reported, with the line to record. A line without `ends` c count alone. Every run prints each nest's end, and a missing baseline's failure prints the whole line, the end rounded up to a tenth. +Some jobs get better with more sheets of less material, so a sheet count would pass a regression +there. Their line also records the job's cost: `auto-nest: 8 sheets, cost 54144` (the job cost +of the layout, rounded up to a whole unit; without stock prices, the sheets' area). With a cost the +cost decides: a layout costing more than 0.5 over it fails whatever its sheet count, and one +costing less is only reported, with the line to record. The end, if the line has one, is then +not checked. Every run prints each nest's cost. + #### Saved layouts A nest can also hold the layout its baseline describes, so nobody has to nest the old code again