Planner.Plan: a first depth, so a call can search for a plan of exactly k steps

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-26 04:13:00 +01:00
commit 237f3346aa
2 changed files with 39 additions and 4 deletions

View file

@ -174,10 +174,12 @@ namespace Lodur
/// A thing's facts are those it was last sensed with, bit 63 replaced by whether it is within reach of the point.
/// A step that gives its This reach (bit 63 true) is a move: after it, another thing is in reach exactly when it is within reach of that This.
/// <paramref name="budget"/> is the most groundings the call may regress, at least 1; a call that spends it returns -1 and sets <see cref="LastExhausted"/>.
/// <paramref name="fromDepth"/> (1..max(depth, 1)) is where the deepening starts: a call with depth k and fromDepth k searches for a plan of exactly k steps and returns none shallower
/// (0 still means the want holds).
/// </summary>
public int Plan(Perception perception, ulong selfFacts, float ox, float oy, float oz, int targetSlot, Fact want, int depth, Span<PlanStep> plan, int budget = NodeBudget)
public int Plan(Perception perception, ulong selfFacts, float ox, float oy, float oz, int targetSlot, Fact want, int depth, Span<PlanStep> plan, int budget = NodeBudget, int fromDepth = 1)
{
Check(perception, targetSlot, want, depth, plan.Length, budget);
Check(perception, targetSlot, want, depth, plan.Length, budget, fromDepth);
_nodes = 0;
_budget = budget;
_exhausted = false;
@ -207,7 +209,7 @@ namespace Lodur
return 0;
}
for (var d = 1; d <= depth; d++)
for (var d = fromDepth; d <= depth; d++)
{
_set[d * _stride] = wantLit;
_setCount[d] = 1;
@ -513,7 +515,7 @@ namespace Lodur
private static PlanStep Step(int verb, ulong thisId, ulong toolId) => new PlanStep(verb, thisId, toolId);
private void Check(Perception perception, int targetSlot, Fact want, int depth, int planLength, int budget)
private void Check(Perception perception, int targetSlot, Fact want, int depth, int planLength, int budget, int fromDepth)
{
if (budget < 1)
{
@ -535,6 +537,11 @@ namespace Lodur
throw new ArgumentOutOfRangeException(nameof(depth), $"The depth must be in 0..{MaxDepth}.");
}
if (fromDepth < 1 || fromDepth > Math.Max(depth, 1))
{
throw new ArgumentOutOfRangeException(nameof(fromDepth), "The first depth must be in 1..max(depth, 1).");
}
if (planLength < depth)
{
throw new ArgumentException("The plan span is shorter than the depth.", "plan");

View file

@ -115,6 +115,34 @@ public sealed class PlannerTests
Expect(deeper, (GrabV, 1, 0), (CutStrapV, 2, 1), (ApproachV, 3, 0), (SwitchOffV, 3, 0));
}
[Fact]
public void FromDepth_searches_exactly_that_depth()
{
var planner = Build();
var p = World();
var steps = new PlanStep[8];
var off = SlotOf(p, 3);
var cut = SlotOf(p, 2);
// Switching off is four steps: found at exactly four, none at exactly three.
Assert.Equal(4, planner.Plan(p, 0UL, 0f, 0f, 0f, off, This(Off), 4, steps, Planner.NodeBudget, 4));
Expect(steps, (GrabV, 1, 0), (CutStrapV, 2, 1), (ApproachV, 3, 0), (SwitchOffV, 3, 0));
Assert.Equal(-1, planner.Plan(p, 0UL, 0f, 0f, 0f, off, This(Off), 3, steps, Planner.NodeBudget, 3));
Assert.False(planner.LastExhausted);
// Cutting the strap is two: a search that starts at three returns no shallower plan.
Assert.Equal(2, planner.Plan(p, 0UL, 0f, 0f, 0f, cut, This(Cut), 2, steps, Planner.NodeBudget, 2));
Assert.Equal(-1, planner.Plan(p, 0UL, 0f, 0f, 0f, cut, This(Cut), 3, steps, Planner.NodeBudget, 3));
Assert.Equal(2, planner.Plan(p, 0UL, 0f, 0f, 0f, cut, This(Cut), 3, steps));
// A want that holds is still 0, whatever the first depth.
Assert.Equal(0, planner.Plan(p, 0UL, 0f, 0f, 0f, SlotOf(p, 4), This(Locked, false), 3, steps, Planner.NodeBudget, 3));
Assert.ThrowsAny<ArgumentException>(() => planner.Plan(p, 0UL, 0f, 0f, 0f, off, This(Off), 4, steps, Planner.NodeBudget, 0));
Assert.ThrowsAny<ArgumentException>(() => planner.Plan(p, 0UL, 0f, 0f, 0f, off, This(Off), 4, steps, Planner.NodeBudget, 5));
Assert.ThrowsAny<ArgumentException>(() => planner.Plan(p, 0UL, 0f, 0f, 0f, off, This(Off), 0, steps, Planner.NodeBudget, 2));
}
[Fact]
public void Opening_the_locked_door_needs_seven_and_six_finds_none()
{