From 4259bd02f98280d9fac9ee25f4d01511dbffeb63 Mon Sep 17 00:00:00 2001 From: AJ Isaacs Date: Thu, 26 Nov 2026 11:59:59 -0500 Subject: [PATCH] fix(planning): pull a later part ahead when taking back a swap The free search learns order rules only between the parts a blocked approach crossed. Where an uninvolved part is cut can decide the block: the part cut before the crossed ones sets where the tool arrives from, and so how they are entered and left. On a six-part plate of holed covers the search swapped the crossed pair, learned rules under that swap, took it back and then re-learned the same pair, running out of budget although cutting the last part earlier plans Ready in under 300 expansions. When a swap is taken back, each part still to be cut after the blocked one is now tried in front of the earliest crossed part, nearest first, one stall-bounded pass each, with the part before it planned again facing it. That plate plans Ready again within its budget. --- .../CuttingPlanning/JointCuttingPlanSearch.cs | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs b/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs index 6f8d9368..8f4a0b41 100644 --- a/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs +++ b/OpenNest.Engine/CuttingPlanning/JointCuttingPlanSearch.cs @@ -144,6 +144,34 @@ internal static class JointCuttingPlanSearch // stall after it can mean the swapped pair is what blocks now. var swaps = new List<(Attempt Attempt, int[] Sequence, int First, int Then)>(); var revoked = new HashSet<(int, int)>(); + // Where a part is cut can block a later approach without being crossed by it: the + // part cut just before the crossed ones decides where the tool arrives from, and so + // how they are entered and left. Order rules only move the crossed parts, so when a + // swap is taken back, each part still to be cut after the blocked one is tried in + // front of the earliest crossed part, nearest first: one stall-bounded pass each, from + // the part before (if any), which is planned again facing the part now following it. + IReadOnlyList PullForward(Attempt stopped, int[] stoppedSequence) + { + if (stopped.Blocked is not int blocked || stopped.Crossed.Count == 0) + return null; + var back = stopped.Crossed.Min(part => Array.IndexOf(stoppedSequence, part)); + var kept = stoppedSequence.Take(back).ToHashSet(); + var arrival = stopped.BoundaryAt(back).Position; + var resumeAt = stopped.BoundaryAt(back > 0 ? back - 1 : back); + var later = stoppedSequence.Skip(Array.IndexOf(stoppedSequence, blocked) + 1); + foreach (var part in later + .Where(part => prerequisites[part].All(kept.Contains)) + .OrderBy(part => centers[part].DistanceTo(arrival)).ThenBy(part => part)) + { + int[] pulled = [.. stoppedSequence.Take(back), part, + .. stoppedSequence.Skip(back).Where(p => p != part)]; + var pass = walk.Follow(pulled, stall, resumeAt, preferEndpoints, + prerequisites: prerequisites); + if (pass.Order != null) + return pass.Order; + } + return null; + } while (true) { // A pass that teaches an order rule ends the round: the heavier passes would @@ -175,14 +203,17 @@ internal static class JointCuttingPlanSearch resume = onward; continue; } - // Nothing new to learn after a swap: take back the newest swap and learn the plain - // rule its attempt would have taught instead, backing up from that attempt. + // Nothing new to learn after a swap: take back the newest swap, try pulling a + // later part in front of the parts its attempt crossed, and otherwise learn the + // plain rule that attempt would have taught instead, backing up from it. while (attempt == null && swaps.Count != 0) { var (swapped, swappedSequence, first, then) = swaps[^1]; swaps.RemoveAt(swaps.Count - 1); prerequisites[then].Remove(first); revoked.Add((first, then)); + if (PullForward(swapped, swappedSequence) is { } pulled) + return walk.Ready(pulled); if (Learn(swapped, swappedSequence, prerequisites, revoked, swaps)) { attempt = swapped;