Files
OpenNest/OpenNest.Engine/CuttingPlanning/RaisedRapids.cs
T
ajandClaude Opus 5.5 037d154a91 feat(planner): opt-in heat weight spreads the part order in blocks
Plan Cutting cut dense runs of small neighboring parts back to back, so thin
sheets built up heat in one area (N26-2F2 sheet 2). An opt-in
CuttingParameters.HeatWeight (s of travel per degree C, saved per material and
thickness; 0 = off) lets the tour compare the plain tour with two cutting
fronts and 2x2-6x6 blocks, scored by travel, 2 s per jump and the old
Gaussian heat estimate fed by the Cut Simulator's feeds.

A move to another block across parts already cut is a designed head raise:
the strict search raises that arrival and replay marks it HEAD RAISE, counted
in HeatRaises, not HeadRaises. Locked programs and cut-offs cannot carry the
mark, so they are reached head-down only as a last resort. The user approved
raising the head to jump between blocks, since these small parts can be tabbed.

At weight 0 nothing runs: fingerprint, serializer and plans are unchanged
(corpus part-order and characterization digests pass). On the 121-part 0.12 in
sheet, weight 0.1 lowers the hottest-zone P95 by 37% for 11% more cut time,
with 37 designed raises and no fallback raises.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-02 21:28:59 -05:00

183 lines
9.8 KiB
C#

using System.Collections.Generic;
using System.Linq;
using System.Threading;
using OpenNest.CNC;
using OpenNest.CNC.CuttingPlanning;
using OpenNest.Diagnostics;
using OpenNest.Geometry;
namespace OpenNest.Engine.CuttingPlanning;
/// <summary>
/// The last resort for a route no search could keep head-down: each planner-chosen rapid that
/// crosses parts already cut is marked to raise the head (<see cref="HeadRaise"/>). Only the
/// safest-route fallback uses it, after the strict search found no route avoiding every crossing,
/// so a plate that can be cut head-down never gets a raise. Rapids into or inside a fixed emission
/// are never marked, and neither is a sub-program, which may be shared: the rapid arriving at a hole
/// sub-program is raised by marking its call, and other rapids inside sub-programs keep their
/// crossings as findings.
/// </summary>
internal static class RaisedRapids
{
/// <summary>
/// <paramref name="order"/> with the crossing rapids of regenerated parts marked, walked as
/// replay walks it. Parts with nothing to mark keep their proposal.
/// </summary>
internal static IReadOnlyList<FixedProgramPlacement> Mark(CuttingPlanSnapshot snapshot,
IReadOnlyList<FixedProgramPlacement> order, CancellationToken token)
{
var checker = snapshot.NewReleasedState();
var position = snapshot.StartPoint;
var marked = new List<FixedProgramPlacement>(order.Count);
foreach (var proposal in order)
{
token.ThrowIfCancellationRequested();
var program = proposal.CopyProgram();
var execution = ExecutionMotionReader.Read(program, proposal.Location, position, token);
var placement = proposal;
var rapids = proposal.Prepared == null || proposal.IsCutOff ? []
: Crossing(checker.Copy(), proposal, execution, position, snapshot.WarnsTabLeadOuts(proposal), token).Codes;
if (rapids.Count != 0)
{
var raised = HeadRaise.Mark(program, rapids);
execution = ExecutionMotionReader.Read(raised, proposal.Location, position, token);
placement = proposal.Propose(raised, execution, proposal.ContourChoices, token);
}
checker.Check(execution, position, proposal.SourceOrdinal + 1, proposal.IsCutOff,
proposal.FixedMotions(execution), token, snapshot.WarnsTabLeadOuts(proposal));
marked.Add(placement);
position = execution.DeparturePoint;
}
return marked;
}
/// <summary>
/// <paramref name="order"/> with the arrival rapid of each part the heat-aware order jumps to
/// (<see cref="CuttingPlanSnapshot.HeatJumps"/>) marked to raise the head where it crosses parts
/// already cut, as the search judged it. Every other rapid was kept head-down by the search.
/// </summary>
internal static IReadOnlyList<FixedProgramPlacement> MarkHeatJumps(CuttingPlanSnapshot snapshot,
IReadOnlyList<FixedProgramPlacement> order, CancellationToken token)
{
if (snapshot.HeatJumps.Count == 0)
return order;
var checker = snapshot.NewReleasedState();
var position = snapshot.StartPoint;
var marked = new List<FixedProgramPlacement>(order.Count);
foreach (var proposal in order)
{
token.ThrowIfCancellationRequested();
var program = proposal.CopyProgram();
var execution = ExecutionMotionReader.Read(program, proposal.Location, position, token);
var placement = proposal;
if (snapshot.HeatJumps.Contains(proposal.SourceOrdinal) && proposal.Prepared != null && !proposal.IsCutOff
&& Crossing(checker.Copy(), proposal, execution, position, snapshot.WarnsTabLeadOuts(proposal), token).Codes
is { Count: > 0 } rapids)
{
var raised = HeadRaise.Mark(program, rapids);
execution = ExecutionMotionReader.Read(raised, proposal.Location, position, token);
placement = proposal.Propose(raised, execution, proposal.ContourChoices, token);
}
checker.Check(execution, position, proposal.SourceOrdinal + 1, proposal.IsCutOff,
proposal.FixedMotions(execution), token, snapshot.WarnsTabLeadOuts(proposal));
marked.Add(placement);
position = execution.DeparturePoint;
}
return marked;
}
/// <summary>
/// The places along an unmarked <paramref name="order"/> whose planner-chosen rapids cross parts
/// already cut, each with the source ordinals of the parts crossed, walked as replay walks it.
/// </summary>
internal static IReadOnlyList<(int Index, int[] Crossed)> Crossed(CuttingPlanSnapshot snapshot,
IReadOnlyList<FixedProgramPlacement> order, CancellationToken token)
{
var checker = snapshot.NewReleasedState();
var position = snapshot.StartPoint;
var crossed = new List<(int, int[])>();
for (var index = 0; index < order.Count; index++)
{
token.ThrowIfCancellationRequested();
var proposal = order[index];
var execution = ExecutionMotionReader.Read(proposal.CopyProgram(), proposal.Location, position, token);
if (proposal.Prepared != null && !proposal.IsCutOff
&& Crossing(checker.Copy(), proposal, execution, position, snapshot.WarnsTabLeadOuts(proposal), token)
.Parts is { Count: > 0 } parts)
crossed.Add((index, [.. parts.Order()]));
checker.Check(execution, position, proposal.SourceOrdinal + 1, proposal.IsCutOff,
proposal.FixedMotions(execution), token, snapshot.WarnsTabLeadOuts(proposal));
position = execution.DeparturePoint;
}
return crossed;
}
// The own-code indices of the planner's rapids that cross parts already cut (the rapid itself,
// or the call of the sub-program the rapid arrives at) and the source ordinals of those parts.
private static (HashSet<int> Codes, HashSet<int> Parts) Crossing(ReleasedContourState checker,
FixedProgramPlacement proposal, OwnedExecution execution, Vector arrival, bool tabLeadOuts,
CancellationToken token)
{
var isFixed = proposal.FixedMotions(execution);
var strict = checker.Check(execution, arrival, proposal.SourceOrdinal + 1, false, isFixed, token,
tabLeadOuts).Strict;
var codes = new HashSet<int>();
var parts = new HashSet<int>();
foreach (var finding in strict)
{
if (finding.Kind != PostVerificationKind.RapidCrossing || finding.RapidEnd is not { } end)
continue;
for (var index = 0; index < execution.Motions.Count; index++)
{
var motion = execution.Motions[index];
if (!motion.Rapid || isFixed?.Invoke(index) == true || motion.End != end
|| (index == 0 ? arrival : motion.Start) != finding.RapidStart)
continue;
if (motion.Code is int code)
codes.Add(code);
else if (motion.CallCode is int call
&& (index == 0 || execution.Motions[index - 1].CallCode != call))
codes.Add(call);
else
continue;
if (finding.OtherPartNumber is int other && other - 1 != proposal.SourceOrdinal)
parts.Add(other - 1);
}
}
return (codes, parts);
}
/// <summary>
/// One advisory note per raised rapid of <paramref name="source"/>, naming the cut parts it
/// passes over, from the replay's raised crossings (check positions are source ordinals + 1).
/// </summary>
internal static IEnumerable<CuttingPlanFinding> Notes(CuttingPlanSnapshot snapshot,
FixedProgramPlacement source, IReadOnlyList<PostVerificationFinding> raised) =>
raised.GroupBy(f => (f.RapidStart, f.RapidEnd)).Select(rapid =>
{
var parts = rapid.Select(f => f.OtherPartNumber).OfType<int>().Distinct().Order().ToArray();
var other = parts.Length == 1 ? snapshot.Placements[parts[0] - 1] : null;
var over = other != null ? "a part already cut" : $"parts {string.Join(", ", parts)}, already cut,";
return new CuttingPlanFinding(source.SourceOrdinal, source.SourcePart, other?.SourceOrdinal,
other?.SourcePart, null, $"A rapid raises the head over {over} because no route found keeps the head down.")
{ Witness = rapid.First() };
});
/// <summary>One advisory note per rapid of <paramref name="source"/> a heat-aware order raised to jump to it.</summary>
internal static IEnumerable<CuttingPlanFinding> HeatNotes(CuttingPlanSnapshot snapshot,
FixedProgramPlacement source, IReadOnlyList<PostVerificationFinding> raised) =>
raised.GroupBy(f => (f.RapidStart, f.RapidEnd)).Select(rapid =>
{
var parts = rapid.Select(f => f.OtherPartNumber).OfType<int>().Distinct().Order().ToArray();
var other = parts.Length == 1 ? snapshot.Placements[parts[0] - 1] : null;
var over = other != null ? "a part already cut" : $"parts {string.Join(", ", parts)}, already cut,";
return new CuttingPlanFinding(source.SourceOrdinal, source.SourcePart, other?.SourceOrdinal,
other?.SourcePart, null, $"A rapid raises the head over {over} to cut a cooler area of the sheet next.")
{ Witness = rapid.First() };
});
/// <summary>The raised rapids in <paramref name="raised"/>: crossings of one rapid count once.</summary>
internal static int Count(IEnumerable<PostVerificationFinding> raised) =>
raised.Select(f => (f.RapidStart, f.RapidEnd)).Distinct().Count();
}