The plan is the decision's: made by the Select that chose or re-chose the pair, carried with it, checked on carried ticks; the per-tick Derive is deleted
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
f2669847fe
commit
8cfd4d743d
4 changed files with 254 additions and 34 deletions
|
|
@ -62,7 +62,7 @@ namespace Lodur
|
|||
private readonly OrientRig? _rig;
|
||||
private readonly bool _layered;
|
||||
private Side _arm = Side.Right;
|
||||
// The want gate's thinking space (null when no goal has a want) and the plan length of the committed pair, derived each tick.
|
||||
// The want gate's thinking space (null when no goal has a want), which also holds the plan of the decision in force, and its length as read.
|
||||
private readonly Deliberation? _deliberation;
|
||||
private int _planLength;
|
||||
|
||||
|
|
@ -216,12 +216,13 @@ namespace Lodur
|
|||
public ulong TargetId => _target;
|
||||
|
||||
/// <summary>
|
||||
/// The length of the plan to the committed pair's want, derived each tick from the percepts (never kept across ticks): n steps, -1 when he goes at it direct (no plan within
|
||||
/// <see cref="Brain.PlanDepth"/>), 0 when the goal has no want, the want holds, or its target is no longer in memory.
|
||||
/// The length of the plan of the decision in force, made by the Select that chose or re-chose the pair and carried with it (a tick that decides nothing only checks it): n steps,
|
||||
/// -1 when he goes at it direct (no plan within <see cref="Brain.PlanDepth"/>, the think budget gone, or a step's thing forgotten), 0 when the goal has no want, the want holds,
|
||||
/// or its target is no longer in memory.
|
||||
/// </summary>
|
||||
public int PlanLength => _planLength;
|
||||
|
||||
/// <summary>Step <paramref name="i"/> of the committed pair's plan, for i below <see cref="PlanLength"/>: a verb of the Actor's list and the percept ids it is grounded on.</summary>
|
||||
/// <summary>Step <paramref name="i"/> of the plan of the decision in force, for i below <see cref="PlanLength"/>: a verb of the Actor's list and the percept ids it is grounded on.</summary>
|
||||
public PlanStep PlanStep(int i)
|
||||
{
|
||||
if ((uint)i >= (uint)Math.Max(_planLength, 0))
|
||||
|
|
@ -357,6 +358,7 @@ namespace Lodur
|
|||
|
||||
// A decision in flight commits at its tick even if the condition that chose it has lapsed; none is made while one is
|
||||
// pending. Within the goal in force's minimum dwell only a strictly higher-priority goal is decided on.
|
||||
var planned = false;
|
||||
if (_pending < 0)
|
||||
{
|
||||
var sr = _perception.SenseRegion;
|
||||
|
|
@ -373,6 +375,20 @@ namespace Lodur
|
|||
_pending = wanted.Goal;
|
||||
_pendingTarget = wanted.Target;
|
||||
_switchTick = _tick + _latency;
|
||||
_deliberation?.Adopt();
|
||||
}
|
||||
else if (_deliberation != null)
|
||||
{
|
||||
// The Select re-chose the pair in force, or was refused by the dwell and the pair in force is planned again with what is left of its budget.
|
||||
if (wanted == now)
|
||||
{
|
||||
_deliberation.Refresh();
|
||||
planned = true;
|
||||
}
|
||||
else if (_brain.Goals[_goal].Want is { } inForceWant)
|
||||
{
|
||||
planned = _deliberation.ReplanInForce(_perception, regionX[sr], regionY[sr], regionZ[sr], inForceWant, _target);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -383,6 +399,12 @@ namespace Lodur
|
|||
_target = _pendingTarget;
|
||||
_pending = -1;
|
||||
_rig?.Commit();
|
||||
if (_deliberation != null)
|
||||
{
|
||||
_deliberation.Commit();
|
||||
planned = true;
|
||||
}
|
||||
|
||||
if (_layered && _brain.Goals[_goal].Behaviour.Arms is { Side: ArmSide.Nearer } && _target != 0UL)
|
||||
{
|
||||
var committed = Target;
|
||||
|
|
@ -406,13 +428,18 @@ namespace Lodur
|
|||
}
|
||||
}
|
||||
|
||||
// The committed pair's plan, derived afresh from the percepts of this tick with the whole think budget.
|
||||
// The plan of the decision in force was written above by the Select that chose it, or is carried: a tick that wrote none only checks it against the percepts.
|
||||
_planLength = 0;
|
||||
if (_deliberation != null && _brain.Goals[_goal].Want is { } committedWant)
|
||||
{
|
||||
var sr = _perception.SenseRegion;
|
||||
_deliberation.SelfFacts = selfFacts;
|
||||
_planLength = _deliberation.Derive(_perception, regionX[sr], regionY[sr], regionZ[sr], committedWant, _target);
|
||||
if (!planned)
|
||||
{
|
||||
var sr = _perception.SenseRegion;
|
||||
_deliberation.SelfFacts = selfFacts;
|
||||
_deliberation.CheckCarried(_perception, regionX[sr], regionY[sr], regionZ[sr], committedWant, _target);
|
||||
}
|
||||
|
||||
_planLength = _deliberation.InForceLength;
|
||||
}
|
||||
|
||||
// For EntrySeconds after a takeover the Actor, not the drive, takes the gain (and the arousal, if the goal declares one) from their values at
|
||||
|
|
|
|||
|
|
@ -430,7 +430,10 @@ namespace Lodur
|
|||
}
|
||||
|
||||
var factor = length > 0 ? goal.PlanFactor(length) : goal.PlanFactor(PlanDepth + 1) * DirectDiscount;
|
||||
Offer(pair.Goal, pair.Slot, pair.Target, pair.Raw * factor, pair.Current, ref best);
|
||||
if (Offer(pair.Goal, pair.Slot, pair.Target, pair.Raw * factor, pair.Current, ref best))
|
||||
{
|
||||
d.KeepBest(pair.Goal, length, goal.Want!.Value.Subject == FactSubject.Self);
|
||||
}
|
||||
}
|
||||
|
||||
if (!d.NextRound())
|
||||
|
|
@ -456,7 +459,10 @@ namespace Lodur
|
|||
|
||||
if (spec.Want is not { } want)
|
||||
{
|
||||
Offer(goal, slot, target, raw, isCurrent, ref best);
|
||||
if (Offer(goal, slot, target, raw, isCurrent, ref best))
|
||||
{
|
||||
d?.KeepBest(goal, 0, false);
|
||||
}
|
||||
}
|
||||
else if (!d!.Holds(goal, want, slot))
|
||||
{
|
||||
|
|
@ -464,14 +470,17 @@ namespace Lodur
|
|||
}
|
||||
}
|
||||
|
||||
// Ties resolve by key (current, then lowest goal, then lowest slot), not by call order: pass 2 offers out of index order.
|
||||
private static void Offer(int goal, int slot, ulong target, double score, bool isCurrent, ref Pick best)
|
||||
// Ties resolve by key (current, then lowest goal, then lowest slot), not by call order: pass 2 offers out of index order. True when the offer became the best, so its plan is kept.
|
||||
private static bool Offer(int goal, int slot, ulong target, double score, bool isCurrent, ref Pick best)
|
||||
{
|
||||
if (!best.Found || score > best.Score
|
||||
|| (score == best.Score && (isCurrent != best.IsCurrent ? isCurrent : goal < best.Goal || (goal == best.Goal && slot < best.Slot))))
|
||||
{
|
||||
best = new Pick { Goal = goal, Slot = slot, Target = target, Score = score, IsCurrent = isCurrent, Found = true };
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private struct Pick
|
||||
|
|
|
|||
|
|
@ -17,8 +17,9 @@ namespace Lodur
|
|||
/// <summary>
|
||||
/// 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. Owned by one Actor;
|
||||
/// allocates only in its constructor. Not thread-safe.
|
||||
/// 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.
|
||||
/// </summary>
|
||||
internal sealed class Deliberation
|
||||
{
|
||||
|
|
@ -40,8 +41,16 @@ namespace Lodur
|
|||
private readonly double[] _bound; // per goal g and searched depth k in 0..PlanDepth: the most a pair with no plan of k or fewer steps can score, as a factor on raw
|
||||
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<Verb> verbs, int capacity)
|
||||
{
|
||||
|
|
@ -53,6 +62,11 @@ namespace Lodur
|
|||
_selfLength = new int[brain.Goals.Count];
|
||||
_perception = null!;
|
||||
_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];
|
||||
_selfDepth = new int[brain.Goals.Count];
|
||||
_bound = new double[brain.Goals.Count * (_depth + 1)];
|
||||
|
|
@ -81,8 +95,11 @@ namespace Lodur
|
|||
/// <summary>The groundings the current Select has paid out of <see cref="Brain.ThinkBudget"/>.</summary>
|
||||
internal int Spent => _spent;
|
||||
|
||||
/// <summary>The plan of the last derivation, in execution order: step i for i below the length <see cref="Derive"/> returned.</summary>
|
||||
internal PlanStep Step(int i) => _plan[i];
|
||||
/// <summary>The plan of the decision in force, in execution order: step i for i below <see cref="InForceLength"/>.</summary>
|
||||
internal PlanStep Step(int i) => _inForce[i];
|
||||
|
||||
/// <summary>The length of the plan of the decision in force: n steps, -1 to go direct, 0 when its want holds, it has none, or its target left memory.</summary>
|
||||
internal int InForceLength => _inForceLength;
|
||||
|
||||
/// <summary>Starts a Select: nothing waits, nothing is spent, nothing is known of the Self wants.</summary>
|
||||
internal void Begin(Perception perception, float ox, float oy, float oz)
|
||||
|
|
@ -95,6 +112,7 @@ namespace Lodur
|
|||
_spent = 0;
|
||||
_nextCount = 0;
|
||||
_gone = false;
|
||||
_bestLength = 0;
|
||||
for (var g = 0; g < _selfHeld.Length; g++)
|
||||
{
|
||||
_selfHeld[g] = -1;
|
||||
|
|
@ -161,6 +179,10 @@ namespace Lodur
|
|||
if (n > 0)
|
||||
{
|
||||
result = n;
|
||||
if (self)
|
||||
{
|
||||
Array.Copy(_plan, 0, _selfPlan, goal * _width, n);
|
||||
}
|
||||
}
|
||||
else if (_planner.LastExhausted)
|
||||
{
|
||||
|
|
@ -208,33 +230,121 @@ namespace Lodur
|
|||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// The committed pair's plan, derived with the full budget: 0 when the want holds or its target is not in memory; n for an n-step plan, readable through
|
||||
/// <see cref="Step"/>; -1 for a direct try.
|
||||
/// </summary>
|
||||
internal int Derive(Perception perception, float ox, float oy, float oz, Fact want, ulong target)
|
||||
/// <summary>The pair just offered is the best so far in this Select: keep its plan (<paramref name="length"/> steps, 0 for a pair without a want, -1 for a direct try).</summary>
|
||||
internal void KeepBest(int goal, int length, bool self)
|
||||
{
|
||||
var slot = 0;
|
||||
if (want.Subject == FactSubject.This)
|
||||
_bestLength = length;
|
||||
if (length > 0)
|
||||
{
|
||||
slot = -1;
|
||||
var ids = perception.PerceptIds;
|
||||
for (var s = 0; s < ids.Length; s++)
|
||||
{
|
||||
if (target != 0UL && ids[s] == target)
|
||||
{
|
||||
slot = s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
Array.Copy(self ? _selfPlan : _plan, self ? goal * _width : 0, _best, 0, length);
|
||||
}
|
||||
}
|
||||
|
||||
if (slot < 0)
|
||||
/// <summary>The Select's best pair became the pending decision: its plan travels with it.</summary>
|
||||
internal void Adopt()
|
||||
{
|
||||
(_pendingPlan, _best) = (_best, _pendingPlan);
|
||||
_pendingLength = _bestLength;
|
||||
}
|
||||
|
||||
/// <summary>The Select re-chose the pair in force: its plan replaces the one in force.</summary>
|
||||
internal void Refresh()
|
||||
{
|
||||
(_inForce, _best) = (_best, _inForce);
|
||||
_inForceLength = _bestLength;
|
||||
}
|
||||
|
||||
/// <summary>The pending decision took force: its plan is the plan in force.</summary>
|
||||
internal void Commit()
|
||||
{
|
||||
(_inForce, _pendingPlan) = (_pendingPlan, _inForce);
|
||||
_inForceLength = _pendingLength;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 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.
|
||||
/// </summary>
|
||||
internal bool ReplanInForce(Perception perception, float ox, float oy, float oz, Fact want, ulong target)
|
||||
{
|
||||
var left = _brain.ThinkBudget - _spent;
|
||||
if (left < 1)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var slot = SlotOf(perception, want, target);
|
||||
if (slot < 0)
|
||||
{
|
||||
_inForceLength = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
_inForceLength = _planner.Plan(perception, SelfFacts, ox, oy, oz, slot, want, _depth, _inForce, left);
|
||||
_spent = Math.Min(_brain.ThinkBudget, _spent + _planner.LastNodes);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A tick that wrote no plan: the plan in force is checked, never searched. It becomes 0 when the want holds now or its target left memory, and -1 (go direct) when one of its
|
||||
/// steps' This or Tool left memory. Lodur has no step-execution signal, so a step done shows as a shorter plan at the next Select that re-chooses the pair.
|
||||
/// </summary>
|
||||
internal void CheckCarried(Perception perception, float ox, float oy, float oz, Fact want, ulong target)
|
||||
{
|
||||
if (_inForceLength == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var slot = SlotOf(perception, want, target);
|
||||
if (slot < 0 || _planner.Plan(perception, SelfFacts, ox, oy, oz, slot, want, 0, _inForce, 1) == 0)
|
||||
{
|
||||
_inForceLength = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
var ids = perception.PerceptIds;
|
||||
for (var i = 0; i < _inForceLength; i++)
|
||||
{
|
||||
if (!Has(ids, _inForce[i].This) || (_inForce[i].Tool != 0UL && !Has(ids, _inForce[i].Tool)))
|
||||
{
|
||||
return 0;
|
||||
_inForceLength = -1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The planner's target slot for a want: 0 for a Self want, else the slot holding the target's id, -1 when the target is 0 or not in memory.
|
||||
private static int SlotOf(Perception perception, Fact want, ulong target)
|
||||
{
|
||||
if (want.Subject != FactSubject.This)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
var ids = perception.PerceptIds;
|
||||
for (var s = 0; s < ids.Length; s++)
|
||||
{
|
||||
if (target != 0UL && ids[s] == target)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
return _planner.Plan(perception, SelfFacts, ox, oy, oz, slot, want, _brain.PlanDepth, _plan, _brain.ThinkBudget);
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static bool Has(ReadOnlySpan<ulong> ids, ulong id)
|
||||
{
|
||||
for (var s = 0; s < ids.Length; s++)
|
||||
{
|
||||
if (ids[s] == id)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
internal void Add(int goal, int slot, ulong target, double raw, bool current)
|
||||
|
|
|
|||
|
|
@ -344,17 +344,91 @@ public sealed class PlannerSelectTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void The_plan_is_derived_each_tick_from_the_percepts()
|
||||
public void The_plan_is_made_with_the_decision_and_carried()
|
||||
{
|
||||
var brain = new Brain { PlanDepth = 4, Goals = new[] { Goal("idle", 1.0), Goal("open", 3.0, Kinds(Door), This(Open)) } };
|
||||
var a = Build(brain);
|
||||
Run(a, Settle, new[] { BoltedDoor(1) });
|
||||
Assert.Equal(4, a.PlanLength);
|
||||
// The door is sensed unlocked next tick: the plan shortens at once, without a new decision.
|
||||
|
||||
// The plan in force is the one the Select made, step for step.
|
||||
var probe = new Planner(Catalogue(), 8, 4, 0.6);
|
||||
var expected = new PlanStep[4];
|
||||
var seen = See(8, BoltedDoor(1));
|
||||
Assert.Equal(4, probe.Plan(seen, 0UL, 0f, 0f, 0f, 0, This(Open), 4, expected));
|
||||
for (var i = 0; i < 4; i++)
|
||||
{
|
||||
Assert.Equal(expected[i], a.PlanStep(i));
|
||||
}
|
||||
|
||||
// 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);
|
||||
// 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);
|
||||
Assert.Equal(1UL, a.TargetId);
|
||||
Assert.Equal(0, a.PlanLength);
|
||||
Run(a, Settle, new[] { Thing(1, Door, 6f, 1UL << Open) });
|
||||
Assert.Equal(0, a.GoalIndex);
|
||||
Assert.Equal(0, a.PlanLength);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_carried_plan_goes_direct_when_a_step_s_thing_is_forgotten()
|
||||
{
|
||||
var brain = new Brain { PlanDepth = 4, Goals = new[] { Goal("idle", 1.0), Goal("calm", 3.0, Kinds(Knife), Self(Fed)) } };
|
||||
var a = Build(brain, memory: 0.5);
|
||||
Run(a, Settle, new[] { Thing(1, Knife, 5f) });
|
||||
Assert.Equal(1, a.GoalIndex);
|
||||
Assert.Equal(2, a.PlanLength);
|
||||
|
||||
// The knife leaves memory: the Select lets the goal go, a latency later; in between the plan in force is checked, and its steps have nothing to act on.
|
||||
var lengths = new HashSet<int>();
|
||||
for (var i = 0; i < 120; i++)
|
||||
{
|
||||
Run(a, 1, Array.Empty<Emitter>());
|
||||
lengths.Add(a.PlanLength);
|
||||
}
|
||||
|
||||
Assert.Contains(-1, lengths);
|
||||
Assert.Equal(0, a.GoalIndex);
|
||||
Assert.Equal(0, a.PlanLength);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_refused_select_replans_the_pair_in_force()
|
||||
{
|
||||
// The open goal is in force and inside its minimum dwell; a knife appears that a second goal of equal priority would take at once, so the Select is refused.
|
||||
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 a = Build(Make(Planner.NodeBudget));
|
||||
Run(a, Settle, new[] { BoltedDoor(1) });
|
||||
Assert.Equal(1, a.GoalIndex);
|
||||
Assert.Equal(4, a.PlanLength);
|
||||
Run(a, 1, new[] { PlainDoor(1), Thing(2, Knife, 0.3f) });
|
||||
Assert.Equal(1, a.GoalIndex);
|
||||
Assert.Equal(2, a.PlanLength); // the refused Select planned the pair in force again, from this tick's percepts
|
||||
|
||||
// With the budget spent by the Select itself nothing is left to plan the pair in force: its plan is carried, only checked.
|
||||
var b = Build(Make(1));
|
||||
Run(b, Settle, new[] { BoltedDoor(1) });
|
||||
Assert.Equal(1, b.GoalIndex);
|
||||
Assert.Equal(-1, b.PlanLength); // no budget to plan: he goes direct
|
||||
Run(b, 1, new[] { PlainDoor(1), Thing(2, Knife, 0.3f) });
|
||||
Assert.Equal(1, b.GoalIndex);
|
||||
Assert.Equal(-1, b.PlanLength);
|
||||
Run(b, 1, new[] { Thing(1, Door, 6f, 1UL << Open), Thing(2, Knife, 0.3f) });
|
||||
Assert.Equal(0, b.PlanLength);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
|
|||
Loading…
Reference in a new issue