From 1986520bec02cfa985252e86da6e2078e08b4bb8 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Thu, 26 Nov 2026 04:43:53 +0100 Subject: [PATCH] Planner select: the commit tick checks the plan, an exhausted replan keeps the plan in force, one buffer for the pending plan, a Self goal's plan shared after the budget is gone, carried paths pinned, crowd rate as a margin Co-Authored-By: Claude Sonnet 5.5 --- docs/goal-ai-map.md | 4 +- src/Lodur.Core/Actor.cs | 4 +- src/Lodur.Core/Deliberation.cs | 61 +++--- tests/Lodur.Core.Tests/PlannerSelectTests.cs | 192 ++++++++++++++++++- 4 files changed, 219 insertions(+), 42 deletions(-) diff --git a/docs/goal-ai-map.md b/docs/goal-ai-map.md index 13f4eda..d0e4351 100644 --- a/docs/goal-ai-map.md +++ b/docs/goal-ai-map.md @@ -212,8 +212,8 @@ in `F:\aeth-wt\imag-reach-lodur`, net8 Release; humanoid, 120 Hz, 4 substeps; er 13.3 ms); `Actor.Tick` over a utility-only Brain, extra 441 us at depth 4 and 924 us at depth 6, the difference from Select being the committed pair's plan, a second Plan call with the full budget every tick. A grounding costs about 0.1-0.2 us. Deepening across pairs, with the plan made by the Select that chooses the pair and carried with - it, as built: decisions that differ are 0 of 100 at 48 things at depths 4, 5, 6, 7 and 8, and 2 of 100 at 64 things - at depths 4-8. Paired interleaved medians, 48 things, budget 4096, net10 Release: `Brain.Select` 206, 205 and 206 us at depths 4, 6 and 8 + it, as built: decisions that differ are a rate, about 0.5% at 48 things and 3% at 64, at every depth (400 seeds, 5000-5399: 1 and 11; + seeds 0-399: 1 and 13; seeds 9000-9399: 3 and 23; the first 100 seeds gave 0 and 2, a property of those seeds). Paired interleaved medians, 48 things, budget 4096, net10 Release: `Brain.Select` 206, 205 and 206 us at depths 4, 6 and 8 (unbounded 205, 208, 206; a Select spends 1592 groundings at the median, 4096 at most), `Actor.Tick` extra over a utility-only Brain 68 us at depth 4 and 79 us at depth 6: a tick spends at most one `ThinkBudget`. At depth 4 the move rule's plan cost is about 2x on movers and 1.0x elsewhere (Soul s1 on `lodur-planner-moves`, paired medians, 6 wants); the budget diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs index fe3d635..1feac5c 100644 --- a/src/Lodur.Core/Actor.cs +++ b/src/Lodur.Core/Actor.cs @@ -375,7 +375,6 @@ namespace Lodur _pending = wanted.Goal; _pendingTarget = wanted.Target; _switchTick = _tick + _latency; - _deliberation?.Adopt(); } else if (_deliberation != null) { @@ -401,8 +400,7 @@ namespace Lodur _rig?.Commit(); if (_deliberation != null) { - _deliberation.Commit(); - planned = true; + _deliberation.Refresh(); // the pending decision's plan, made up to a latency ago: checked below like any carried plan } if (_layered && _brain.Goals[_goal].Behaviour.Arms is { Side: ArmSide.Nearer } && _target != 0UL) diff --git a/src/Lodur.Core/Deliberation.cs b/src/Lodur.Core/Deliberation.cs index 25992dd..5767303 100644 --- a/src/Lodur.Core/Deliberation.cs +++ b/src/Lodur.Core/Deliberation.cs @@ -18,8 +18,9 @@ namespace Lodur /// What one Actor thinks with while its Brain selects: the planner, the want pairs waiting for the gate (a max-heap by bound, ties current, lowest goal, lowest slot), /// the pairs deferred to the next round of deepening, the bound table, and the think budget spent so far in this Select. Pass 2 deepens across pairs (Korf's iterative /// deepening, anytime): round k asks every live pair for a plan of exactly k steps, so short plans are found first and no pair starves the rest. - /// It also carries the plan of the decision: the plan of the pair best so far in this Select, the plan that travels with a pending decision, and the plan of the decision in force, - /// each made by the Select that chose (or re-chose) the pair and carried until the next one; Select never reads them. Owned by one Actor; allocates only in its constructor. Not thread-safe. + /// It also carries the plan of the decision: the plan of the pair best so far in this Select and the plan of the decision in force, + /// each made by the Select that chose (or re-chose) the pair and carried until the next one; Select never reads them. The best plan is also the plan of a pending decision: the Actor runs no Select + /// between a decision becoming pending and its commit, so nothing writes the best plan in that window (that rule is load-bearing here). Owned by one Actor; allocates only in its constructor. Not thread-safe. /// internal sealed class Deliberation { @@ -42,14 +43,12 @@ namespace Lodur private readonly WantPair[] _next; // the pairs with no plan of this round's k steps, waiting for round k + 1 private int _nextCount; private int _bestLength; - private int _pendingLength; private int _inForceLength; private readonly int[] _selfDepth; // per goal this Select: the depth a Self want has been searched to private bool _gone; // this Select's budget is gone: every later pair goes direct private readonly PlanStep[] _selfPlan; // per goal this Select: the plan found for its Self want, shared by its pairs private readonly int _width; // the steps a plan buffer holds private PlanStep[] _best; // the plan of the pair best so far in this Select, BestLength steps - private PlanStep[] _pendingPlan; // the plan that travels with the pending decision private PlanStep[] _inForce; // the plan of the decision in force internal Deliberation(Brain brain, IReadOnlyList verbs, int capacity) @@ -64,7 +63,6 @@ namespace Lodur _depth = brain.PlanDepth; _width = Math.Max(1, _depth); _best = new PlanStep[_width]; - _pendingPlan = new PlanStep[_width]; _inForce = new PlanStep[_width]; _selfPlan = new PlanStep[brain.Goals.Count * _width]; _next = new WantPair[_heap.Length]; @@ -147,23 +145,25 @@ namespace Lodur /// internal int PlanAt(int goal, Fact want, int slot, int k) { - if (_depth == 0 || _gone) + if (_depth == 0) { return -1; } var self = want.Subject == FactSubject.Self; - if (self) + if (self && _selfLength[goal] != Unplanned) { - if (_selfLength[goal] != Unplanned) - { - return _selfLength[goal]; - } + return _selfLength[goal]; // planned once for all its pairs, and shared at no cost even when the budget is gone + } - if (_selfDepth[goal] >= k) - { - return k == _depth ? -1 : 0; - } + if (_gone) + { + return -1; + } + + if (self && _selfDepth[goal] >= k) + { + return k == _depth ? -1 : 0; } var left = _brain.ThinkBudget - _spent; @@ -240,30 +240,16 @@ namespace Lodur } } - /// The Select's best pair became the pending decision: its plan travels with it. - internal void Adopt() - { - (_pendingPlan, _best) = (_best, _pendingPlan); - _pendingLength = _bestLength; - } - - /// The Select re-chose the pair in force: its plan replaces the one in force. + /// The Select's best pair takes force, as the re-chosen pair in force or the pending decision at its commit: its plan is the plan in force. internal void Refresh() { (_inForce, _best) = (_best, _inForce); _inForceLength = _bestLength; } - /// The pending decision took force: its plan is the plan in force. - internal void Commit() - { - (_inForce, _pendingPlan) = (_pendingPlan, _inForce); - _inForceLength = _pendingLength; - } - /// /// The Select was refused (inside the goal's dwell) and the pair in force stays: its plan is made again with what is left of this Select's budget, to PlanDepth. - /// False when none is left; the plan is carried, and checked. + /// False when none is left or the search exhausts it; the plan is carried, and checked. /// internal bool ReplanInForce(Perception perception, float ox, float oy, float oz, Fact want, ulong target) { @@ -280,8 +266,19 @@ namespace Lodur return true; } - _inForceLength = _planner.Plan(perception, SelfFacts, ox, oy, oz, slot, want, _depth, _inForce, left); + var n = _planner.Plan(perception, SelfFacts, ox, oy, oz, slot, want, _depth, _plan, left); _spent = Math.Min(_brain.ThinkBudget, _spent + _planner.LastNodes); + if (_planner.LastExhausted) + { + return false; // a sliver of budget proves nothing: the plan in force is carried, and checked + } + + if (n > 0) + { + Array.Copy(_plan, 0, _inForce, 0, n); + } + + _inForceLength = n; return true; } diff --git a/tests/Lodur.Core.Tests/PlannerSelectTests.cs b/tests/Lodur.Core.Tests/PlannerSelectTests.cs index 2234053..cf843f2 100644 --- a/tests/Lodur.Core.Tests/PlannerSelectTests.cs +++ b/tests/Lodur.Core.Tests/PlannerSelectTests.cs @@ -378,6 +378,12 @@ public sealed class PlannerSelectTests // The door is sensed unlocked while the Select re-chooses the pair: the plan shortens the same tick. Run(a, 1, new[] { PlainDoor(1) }); Assert.Equal(2, a.PlanLength); + var shorter = new PlanStep[4]; + Assert.Equal(2, probe.Plan(See(8, PlainDoor(1)), 0UL, 0f, 0f, 0f, 0, This(Open), 4, shorter)); + for (var i = 0; i < 2; i++) + { + Assert.Equal(shorter[i], a.PlanStep(i)); // the re-chosen pair's new steps, not the old ones under a new length + } // The door is seen open: the pair is no longer offered and the switch is pending, so this tick only checks the plan, and the want holds. Run(a, 1, new[] { Thing(1, Door, 6f, 1UL << Open) }); Assert.Equal(1, a.GoalIndex); @@ -445,6 +451,179 @@ public sealed class PlannerSelectTests Assert.Equal(0, b.PlanLength); } + [Fact] + public void The_pending_plan_is_checked_on_the_tick_it_takes_force() + { + // The door is seen on the first tick only and forgotten (0.06 s) before the decision commits (10 ticks): the plan the Select made takes force, and is checked that tick. + var brain = new Brain { PlanDepth = 4, Goals = new[] { Goal("idle", 1.0), Goal("open", 3.0, Kinds(Door), This(Open)) } }; + var a = Build(brain, memory: 0.06); + Run(a, 1, new[] { BoltedDoor(1) }); + var committed = false; + for (var i = 0; i < 20 && !committed; i++) + { + Run(a, 1, Array.Empty()); + if (a.GoalIndex == 1) + { + committed = true; + Assert.Equal(0, a.PlanLength); // its target has left memory + } + } + + Assert.True(committed); + } + + [Fact] + public void A_refused_select_that_exhausts_its_leftover_budget_keeps_the_plan_in_force() + { + Brain Make(int budget) => new() + { + PlanDepth = 4, ThinkBudget = budget, + Goals = new[] + { + Goal("idle", 0.5), + Goal("open", 3.0, Kinds(Door), This(Open), Shorter()) with { MinDwellSeconds = 50.0 }, + Goal("grab", 3.0, Kinds(Knife), This(Held), Shorter()), + }, + }; + + var planned = 0; + for (var budget = 1; budget <= 60; budget++) + { + var a = Build(Make(budget)); + Run(a, Settle, new[] { BoltedDoor(1) }); + var before = a.PlanLength; + Run(a, 1, new[] { BoltedDoor(1), Thing(2, Knife, 0.3f) }); + var refused = a.PlanLength; + Run(a, 1, new[] { BoltedDoor(1), Thing(2, Knife, 0.3f) }); + if (before == 4) + { + // Unchanged percepts: a valid plan is never replaced by a search that ran out. + Assert.Equal(1, a.GoalIndex); + Assert.Equal(4, refused); + Assert.Equal(4, a.PlanLength); + planned++; + } + } + + Assert.True(planned > 20); + foreach (var budget in new[] { 28, 29 }) + { + var a = Build(Make(budget)); + Run(a, Settle, new[] { BoltedDoor(1) }); + Assert.Equal(4, a.PlanLength); + } + } + + [Fact] + public void A_refused_select_checks_the_plan_in_force_against_a_forgotten_target() + { + var brain = new Brain + { + PlanDepth = 4, + Goals = new[] + { + Goal("idle", 0.5), + Goal("open", 3.0, Kinds(Door), This(Open), Shorter()) with { MinDwellSeconds = 50.0 }, + Goal("grab", 3.0, Kinds(Knife), This(Held), Shorter()), + }, + }; + var a = Build(brain, memory: 0.06); + Run(a, Settle, new[] { BoltedDoor(1) }); + Assert.Equal(4, a.PlanLength); + // The door goes and a knife comes: the Select would switch, the dwell refuses it, and the plan in force has no target to act on. + Run(a, 20, new[] { Thing(2, Knife, 0.3f) }); + Assert.Equal(1, a.GoalIndex); + Assert.Equal(0, a.PlanLength); + } + + [Fact] + public void A_carried_plan_goes_direct_when_a_step_s_tool_is_forgotten() + { + const uint Strap = 5, Other = 7; + var verbs = new[] + { + new Verb { Id = "Approach", Gives = new[] { This(Reach) } }, + new Verb { Id = "Grab", This = Kinds(Knife), Needs = new[] { This(Reach) }, Gives = new[] { This(Held) } }, + new Verb { Id = "Cut", This = Kinds(Strap), Tool = Kinds(Knife), Needs = new[] { Tool(Held), This(Reach) }, Gives = new[] { This(Open) } }, + }; + var brain = new Brain + { + PlanDepth = 6, ReachMeters = 1.0, + Goals = new[] { Goal("idle", 0.5), Goal("free", 2.0, Kinds(Strap), This(Open), Shorter()), Goal("other", 3.0, Kinds(Other)) }, + }; + var a = Build(brain, memory: 0.06, verbs: verbs); + var room = new[] { Thing(1, Strap, 0.3f), Thing(2, Knife, 5f, 1UL << Held) }; // the knife is already held: the tool appears in one step only, as its Tool + Run(a, Settle, room); + Assert.Equal(1, a.GoalIndex); + Assert.True(a.PlanLength > 0); + var tooled = false; + for (var i = 0; i < a.PlanLength; i++) + { + tooled |= a.PlanStep(i).Tool != 0UL; + } + + Assert.True(tooled); + + // Something outranks the goal; while that decision is in flight the knife is forgotten and the strap is not. + Run(a, 1, new[] { room[0], room[1], Thing(3, Other, 4f) }); + var lengths = new HashSet(); + for (var i = 0; i < 9; i++) + { + Run(a, 1, new[] { room[0], Thing(3, Other, 4f) }); + if (a.GoalIndex == 1) + { + lengths.Add(a.PlanLength); + } + } + + Assert.Contains(-1, lengths); + } + + [Fact] + public void A_self_goal_s_plan_is_shared_with_its_later_pairs_after_the_budget_is_gone() + { + var percepts = See(4, Thing(1, Knife, 5f), Thing(2, Knife, 3f), Thing(3, Door, 6f, (1UL << Locked) | (1UL << Latched))); + var goals = new[] { Goal("idle", 0.5), Goal("calm", 3.0, Kinds(Knife), Self(Fed), Shorter()), Goal("open", 2.0, Kinds(Door), This(Open), Shorter()) }; + var rich = new Brain { PlanDepth = 4, Goals = goals }; + var d = new Deliberation(rich, Catalogue(), 4); + d.Begin(percepts, 0f, 0f, 0f); + Assert.Equal(0, d.PlanAt(1, Self(Fed), 0, 1)); + Assert.Equal(2, d.PlanAt(1, Self(Fed), 0, 2)); + var found = d.Spent; + + // The budget is exactly what the Self want's search cost: another goal's pair finds none left, and the Self goal's second pair still takes the plan found. + var poor = rich with { ThinkBudget = found }; + var d2 = new Deliberation(poor, Catalogue(), 4); + d2.Begin(percepts, 0f, 0f, 0f); + Assert.Equal(0, d2.PlanAt(1, Self(Fed), 0, 1)); + Assert.Equal(2, d2.PlanAt(1, Self(Fed), 0, 2)); + Assert.Equal(-1, d2.PlanAt(2, This(Open), 2, 1)); // gone + Assert.Equal(-1, d2.PlanAt(2, This(Open), 2, 2)); // and it stays gone + Assert.Equal(2, d2.PlanAt(1, Self(Fed), 1, 2)); // shared at no cost + Assert.Equal(found, d2.Spent); + } + + [Fact] + public void A_kept_self_plan_is_not_overwritten_by_another_goal_s_search() + { + var percepts = See(4, Thing(1, Knife, 5f), Thing(3, Door, 6f)); + var brain = new Brain { PlanDepth = 4, Goals = new[] { Goal("idle", 0.5), Goal("calm", 3.0, Kinds(Knife), Self(Fed), Shorter()), Goal("open", 2.0, Kinds(Door), This(Open), Shorter()) } }; + var d = new Deliberation(brain, Catalogue(), 4); + d.Begin(percepts, 0f, 0f, 0f); + Assert.Equal(0, d.PlanAt(1, Self(Fed), 0, 1)); + Assert.Equal(2, d.PlanAt(1, Self(Fed), 0, 2)); + Assert.Equal(0, d.PlanAt(2, This(Open), 1, 1)); + Assert.Equal(2, d.PlanAt(2, This(Open), 1, 2)); // the planner's last plan is now the door's: approach, open + d.KeepBest(1, 2, true); + d.Refresh(); + var probe = new Planner(Catalogue(), 4, 4, 0.6); + var expected = new PlanStep[4]; + Assert.Equal(2, probe.Plan(percepts, 0UL, 0f, 0f, 0f, 0, Self(Fed), 4, expected)); + Assert.Equal(2, d.InForceLength); + Assert.Equal(expected[0], d.Step(0)); + Assert.Equal(expected[1], d.Step(1)); + } + [Fact] public void Self_facts_reach_the_planner_and_bit_63_is_refused() { @@ -641,10 +820,10 @@ public sealed class PlannerSelectTests }, }; - private static int Differing(int things, int depth, int seeds) + private static int Differing(int things, int depth, int firstSeed, int seeds) { var differ = 0; - for (var seed = 0; seed < seeds; seed++) + for (var seed = firstSeed; seed < firstSeed + seeds; seed++) { var rng = new Random(seed); var (p, verbs, _) = Crowd(rng, things); @@ -689,13 +868,16 @@ public sealed class PlannerSelectTests [Fact] public void A_crowded_room_changes_few_decisions() { + // A rate, not a bound, and it is depth-independent: a bounded budget changes 1 of 400 decisions at 48 things and 11 of 400 at 64 (seeds 5000-5399; seeds 0-399 give 1 and 13, + // seeds 9000-9399 give 3 and 23). The seeds are fixed so the counts are exact; the ceilings sit well above every range measured, so the pin is a margin, not one seed set. foreach (var depth in new[] { 4, 5, 6, 8 }) { - Assert.Equal(0, Differing(48, depth, 100)); + Assert.InRange(Differing(48, depth, 5000, 400), 0, Differ48Of400Max); } - Assert.Equal(CrowdedDepth6At64Differing, Differing(64, 6, 100)); + Assert.InRange(Differing(64, 6, 5000, 400), 0, Differ64Of400Max); } - private const int CrowdedDepth6At64Differing = 2; + private const int Differ48Of400Max = 8; // 2%: above 3 of 400, the worst of three ranges + private const int Differ64Of400Max = 40; // 10%: above 23 of 400, the worst of three ranges }