diff --git a/docs/lodur-target.md b/docs/lodur-target.md index 33c6462..76143b4 100644 --- a/docs/lodur-target.md +++ b/docs/lodur-target.md @@ -71,7 +71,8 @@ Signal flows one way per tick, in this order; each layer has one owner. Layers 1 A goal can also carry a sensory gain and an arousal target (`SensoryGain`, `EntryArousal`), reached linearly over `EntrySeconds` from their values at takeover (0, the default, is at the takeover tick; the Start goal's apply at construction). The gain multiplies the sense channels' afferents as they leave the conduction delay, so sensitisation still follows the world; `Load` is built from the gated afferents and - `ChannelStimulus` stays the world's. The nociceptive region afferents, the withdrawal reflex and its flinch are never gated. The Actor owns the gain + `ChannelStimulus` stays the world's. Sense reads the gain at the start of each tick, so a change of gain reaches `Load` one tick after the Actor sets it; + the ramp's last tick assigns the targets exactly. The nociceptive region afferents, the withdrawal reflex and its flinch are never gated. The Actor owns the gain in force (`DriveState.SensoryGain`) and, through the ramp, arousal; after it the drive owns arousal again. Calm uses it to silence the office's stressors and drain the stress bar, tailing off over a few seconds rather than cutting (`lodur:ruling:stress-bar-shows-arousal-calm-silences`, `lodur:ruling:flinch-tail-after-stimulus`). diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs index 981aa2f..190080a 100644 --- a/src/Lodur.Core/Actor.cs +++ b/src/Lodur.Core/Actor.cs @@ -189,16 +189,18 @@ namespace Lodur } // 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 - // takeover to the goal's targets, linearly; it ends exactly on them, and after it the drive owns arousal again. Sense reads the gain at the start of the next tick. + // takeover to the goal's targets, linearly; its last tick assigns the targets themselves (a lerp need not land on a nonzero target), and after it the drive + // owns arousal again. Sense reads the gain at the start of the next tick, so a change of gain reaches Load one tick after the Actor sets it. var sinceGoal = _tick - _goalSince; if (sinceGoal <= _rampTicks) { var goal = _brain.Goals[_goal]; - var k = _rampTicks == 0 ? 1.0 : (double)sinceGoal / _rampTicks; - _drive.SensoryGain = _rampGain0 + (goal.SensoryGain - _rampGain0) * k; + var last = sinceGoal == _rampTicks; // also the takeover tick of a zero-length ramp + var k = last ? 1.0 : (double)sinceGoal / _rampTicks; + _drive.SensoryGain = last ? goal.SensoryGain : _rampGain0 + (goal.SensoryGain - _rampGain0) * k; if (goal.EntryArousal is double target) { - _drive.Arousal = _rampArousal0 + (target - _rampArousal0) * k; + _drive.Arousal = last ? target : _rampArousal0 + (target - _rampArousal0) * k; } } diff --git a/src/Lodur.Core/Brain.cs b/src/Lodur.Core/Brain.cs index e3f2c38..c5610ab 100644 --- a/src/Lodur.Core/Brain.cs +++ b/src/Lodur.Core/Brain.cs @@ -73,7 +73,8 @@ namespace Lodur /// /// The gain on the sense channels (not the nociceptive region afferents) while this goal is in force, in [0, 1], default 1; 0 is deaf and blind to the /// world's stimuli, which keep sounding (lodur:ruling:stress-bar-shows-arousal-calm-silences). Applied like : at the tick the goal - /// takes over, and at construction for the Start goal. + /// takes over, and at construction for the Start goal. The sense reads the gain at the start of each tick, so a change reaches + /// one tick after the Actor sets it. /// public double SensoryGain { get; init; } = 1.0; diff --git a/tests/Lodur.Core.Tests/EntryRampTests.cs b/tests/Lodur.Core.Tests/EntryRampTests.cs index 82b985d..a5a5d64 100644 --- a/tests/Lodur.Core.Tests/EntryRampTests.cs +++ b/tests/Lodur.Core.Tests/EntryRampTests.cs @@ -100,6 +100,44 @@ public sealed class EntryRampTests } } + [Fact] + public void A_ramp_ends_exactly_on_a_nonzero_target_not_a_float_step_away_from_it() + { + var brain = new Brain + { + Goals = new[] + { + new GoalSpec { Id = "a", SensoryGain = 0.8, Behaviour = Pose }, + new GoalSpec + { + Id = "b", Priority = 1.0, MinDwellSeconds = 1000.0, SensoryGain = 0.15, EntryArousal = 0.05, EntrySeconds = 0.5, + When = new[] { new Condition(DriveQuantity.Arousal, ConditionOp.AtLeast, 0.2) }, Behaviour = Pose, + }, + }, + }; + var rig = new Rig(brain); + Assert.Equal(0.8, rig.Actor.Drive.SensoryGain); + rig.UntilTakeover(1); + for (var k = 1; k < 50; k++) rig.Tick(); + Assert.NotEqual(0.15, rig.Actor.Drive.SensoryGain); + rig.Tick(); // the ramp's last tick + Assert.Equal(0.15, rig.Actor.Drive.SensoryGain); // 0.8 + (0.15 - 0.8) * 1 is not 0.15 in a double + Assert.Equal(0.05, rig.Actor.Drive.Arousal); // from the arousal at takeover, 0.2142..., a lerp lands a step off 0.05 too + } + + [Fact] + public void A_huge_finite_EntrySeconds_is_an_unending_ramp_not_a_snap_or_a_wrap() + { + // 1e300 / 0.01 is past the long range. On .NET 10 the cast of such a double saturates, so the guard in the Actor is not observable here + // (its mutant is equivalent on this runtime); the behaviour it protects on runtimes where the cast is undefined is pinned. + var rig = new Rig(StressThenCalm(1e300)); + rig.UntilTakeover(1); + for (var i = 0; i < 2000; i++) rig.Tick(); + Assert.Equal(1.0, rig.Actor.Drive.SensoryGain, 12); + Assert.True(rig.Actor.Drive.Arousal > 0.0); + Assert.True(rig.Actor.Load > 0.0); + } + [Fact] public void The_ramp_length_is_rounded_to_whole_ticks_so_a_quotient_just_under_an_integer_gets_that_integer() {