diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs index 961c9f6..1863f07 100644 --- a/src/Lodur.Core/Actor.cs +++ b/src/Lodur.Core/Actor.cs @@ -371,10 +371,10 @@ 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. - // Order: the bars lift on what he knows now, then a direct try that has lasted its time is barred (and the next decision made impatient), then the decision. + // Order: the bars lift on time or a forgotten target, then a direct try that has lasted its time is barred (and the next decision made impatient), then the decision. if (_deliberation != null) { - _deliberation.Lift(_perception, selfFacts, _tick); + _deliberation.Lift(_perception, _tick); if (_directSince >= 0 && _tick - _directSince >= _frustrationTicks && !_deliberation.Barred(_goal, _target)) { _deliberation.Bar(_goal, _target, _retryTicks > long.MaxValue - _tick ? long.MaxValue : _tick + _retryTicks); diff --git a/src/Lodur.Core/Brain.cs b/src/Lodur.Core/Brain.cs index 201a2f3..5185da8 100644 --- a/src/Lodur.Core/Brain.cs +++ b/src/Lodur.Core/Brain.cs @@ -331,7 +331,7 @@ namespace Lodur /// How long a direct try ( -1) of one (goal, target) pair may last before the Actor gives it up: it bars the pair's direct try, reports and decides again past the goal's dwell. Not NaN, not negative; +infinity never frustrates. Default 5 (a placeholder; ruling bds-unplannable-tries-direct). public double FrustrationSeconds { get; init; } = 5.0; - /// How long a bar on a direct try lasts if the world he knows does not change first: the pair is tried direct again after it. Positive, not NaN; +infinity never lifts by time. Default 30 (a placeholder). + /// How long a bar on a direct try lasts if its target is not forgotten first: the pair is tried direct again after it. Positive, not NaN; +infinity never lifts by time. Default 30 (a placeholder). public double RetrySeconds { get; init; } = 30.0; /// The most groundings one Select may regress across all its plans, at least 1, default 4096: one budget per decision, spent by deepening across the want pairs, short plans first (lodur:question:bds-think-budget-per-decision). diff --git a/src/Lodur.Core/Deliberation.cs b/src/Lodur.Core/Deliberation.cs index 171c266..c6ad828 100644 --- a/src/Lodur.Core/Deliberation.cs +++ b/src/Lodur.Core/Deliberation.cs @@ -51,8 +51,6 @@ namespace Lodur private PlanStep[] _best; // the plan of the pair best so far in this Select, BestLength steps private PlanStep[] _inForce; // the plan of the decision in force private readonly ulong[] _barTarget; // per goal: the barred target's id (0 for a Self want's untargeted goal) - private readonly ulong[] _barFacts; // per goal: the barred target's facts when barred - private readonly ulong[] _barSelf; // per goal: the actor's own facts when barred private readonly long[] _barUntil; // per goal: the tick the bar expires at; -1 is no bar internal Deliberation(Brain brain, IReadOnlyList verbs, int capacity) @@ -72,8 +70,6 @@ namespace Lodur _next = new WantPair[_heap.Length]; _selfDepth = new int[brain.Goals.Count]; _barTarget = new ulong[brain.Goals.Count]; - _barFacts = new ulong[brain.Goals.Count]; - _barSelf = new ulong[brain.Goals.Count]; _barUntil = new long[brain.Goals.Count]; Array.Fill(_barUntil, -1L); _bound = new double[brain.Goals.Count * (_depth + 1)]; @@ -108,37 +104,23 @@ namespace Lodur /// 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. internal int InForceLength => _inForceLength; - /// - /// Bars the direct try of the pair (, ) until , remembering the target's facts and the actor's own as they stand now - /// (the percepts and self facts of the last Select). One bar per goal: a new one replaces it. - /// + /// Bars the direct try of the pair (, ) until . One bar per goal: a new one replaces it. internal void Bar(int goal, ulong target, long untilTick) { _barTarget[goal] = target; - _barSelf[goal] = SelfFacts; - var ids = _perception.PerceptIds; - var facts = _perception.PerceptFacts; - _barFacts[goal] = 0UL; - for (var s = 0; target != 0UL && s < ids.Length; s++) - { - if (ids[s] == target) - { - _barFacts[goal] = facts[s]; - break; - } - } - _barUntil[goal] = untilTick; } /// Whether the direct try of the pair is barred: an equal bar, live ( runs each tick before the decision). internal bool Barred(int goal, ulong target) => _barUntil[goal] >= 0 && _barTarget[goal] == target; - /// Clears every bar whose time has come, whose target is no longer in memory, or whose remembered facts or self facts differ from the bar's. - internal void Lift(Perception perception, ulong selfFacts, long tick) + /// + /// Clears every bar whose time has come or whose target is no longer in memory. A changed fact does not lift a bar (ruling frustration-lifts-without-fact-changes): + /// a pair that becomes plannable is offered through it by Select. + /// + internal void Lift(Perception perception, long tick) { var ids = perception.PerceptIds; - var facts = perception.PerceptFacts; for (var g = 0; g < _barUntil.Length; g++) { if (_barUntil[g] < 0) @@ -146,7 +128,7 @@ namespace Lodur continue; } - var lift = tick >= _barUntil[g] || _barSelf[g] != selfFacts; + var lift = tick >= _barUntil[g]; if (!lift && _barTarget[g] != 0UL) { lift = true; @@ -154,7 +136,7 @@ namespace Lodur { if (ids[s] == _barTarget[g]) { - lift = facts[s] != _barFacts[g]; + lift = false; break; } } diff --git a/tests/Lodur.Core.Tests/PlannerFrustrationTests.cs b/tests/Lodur.Core.Tests/PlannerFrustrationTests.cs index 6a6b9a0..9434bcf 100644 --- a/tests/Lodur.Core.Tests/PlannerFrustrationTests.cs +++ b/tests/Lodur.Core.Tests/PlannerFrustrationTests.cs @@ -6,7 +6,7 @@ namespace Lodur.Core.Tests; /// /// A futile direct try ends (ruling bds-unplannable-tries-direct): after FrustrationSeconds at PlanLength -1 the Actor bars the pair's direct try, reports Frustrated and decides again -/// past the dwell; the bar lifts when what he knows changes, or after RetrySeconds. +/// past the dwell; the bar ends when the target is forgotten or after RetrySeconds, and a pair that becomes plannable is offered through it; a changed fact does not lift it (ruling frustration-lifts-without-fact-changes). /// public sealed class PlannerFrustrationTests { @@ -215,10 +215,10 @@ public sealed class PlannerFrustrationTests } [Fact] - public void A_changed_fact_a_changed_self_fact_or_a_forgotten_door_lifts_the_bar_at_once() + public void A_changed_fact_or_self_fact_does_not_lift_the_bar_but_a_forgotten_door_does() { - // Barred, idle in force; then the change; the door goal is back in force a latency (and a tick) later. - void Lifts(Action change) + // Barred, idle in force; a change of the door's facts or his own leaves the bar standing (ruling frustration-lifts-without-fact-changes). + void Holds(Action change) { var b = new Bench(Build(Make(), memory: 0.5)); var door = new[] { BoltedDoor() }; @@ -226,24 +226,37 @@ public sealed class PlannerFrustrationTests b.Run(Latency + 20, door); Assert.Equal(0, b.A.GoalIndex); change(b, door); - Assert.Equal(1, b.A.GoalIndex); - Assert.Equal(1UL, b.A.TargetId); + b.Run(Latency + 200, new[] { BoltedDoor(1UL << Noise) }, 1UL << Noise); + Assert.Equal(0, b.A.GoalIndex); + Assert.Single(b.Frustrated); } - Lifts((b, _) => b.Run(Latency + 2, new[] { BoltedDoor(1UL << Noise) })); // an unrelated bit of the door's facts - Lifts((b, door) => b.Run(Latency + 2, door, 1UL << Noise)); // an unrelated self fact - Lifts((b, _) => - { - b.Run(60, None); // the door leaves memory (0.5 s) - b.Run(Latency + 2, new[] { BoltedDoor() }); // and is sensed again, unchanged - }); + Holds((b, _) => b.Run(Latency + 2, new[] { BoltedDoor(1UL << Noise) })); // an unrelated bit of the door's facts + Holds((b, door) => b.Run(Latency + 2, door, 1UL << Noise)); // an unrelated self fact - // With nothing changed the bar holds well past all of that. - var held = new Bench(Build(Make(), memory: 0.5)); + // The door leaving memory (0.5 s) ends the bar: sensed again, unchanged, the try resumes. + var f = new Bench(Build(Make(), memory: 0.5)); var same = new[] { BoltedDoor() }; - held.RunToFrustration(same); - held.Run(Latency + 200, same); - Assert.Equal(0, held.A.GoalIndex); + f.RunToFrustration(same); + f.Run(Latency + 20, same); + Assert.Equal(0, f.A.GoalIndex); + f.Run(60, None); + f.Run(Latency + 2, same); + Assert.Equal(1, f.A.GoalIndex); + Assert.Equal(1UL, f.A.TargetId); + } + + [Fact] + public void A_fact_that_flips_for_200_seconds_frustrates_a_bounded_number_of_times() + { + // His own futile try may rattle the door's facts; with the bar standing the count is that of the retries (30 s), not of the flips (every 0.5 s). + var b = new Bench(Build(Make(frustration: 1.0, retry: 30.0))); + for (var i = 0; i < 20000; i++) + { + b.Step(new[] { BoltedDoor((i / 50) % 2 == 0 ? 0UL : 1UL << Noise) }); + } + + Assert.InRange(b.Frustrated.Count, 1, 7); } [Fact] @@ -324,7 +337,7 @@ public sealed class PlannerFrustrationTests } [Fact] - public void A_self_want_with_no_target_frustrates_and_lifts_on_a_self_fact() + public void A_self_want_with_no_target_frustrates_and_lifts_only_after_the_retry() { var brain = new Brain { @@ -335,7 +348,13 @@ public sealed class PlannerFrustrationTests b.RunToFrustration(None); b.Run(Latency + 20, None); Assert.Equal(0, b.A.GoalIndex); - b.Run(Latency + 2, None, 1UL << Held); // a self fact changes: the bar lifts, the try resumes + b.Run(Latency + 200, None, 1UL << Held); // a self fact changes: the bar stands + Assert.Equal(0, b.A.GoalIndex); + for (var i = 0; i < 3000 && b.A.GoalIndex == 0; i++) // the retry passes: the try resumes + { + b.Step(None); + } + Assert.Equal(1, b.A.GoalIndex); Assert.Equal(0UL, b.A.TargetId); Assert.Equal(-1, b.A.PlanLength);