From f2f5a10226efcfe69f751251983f9f05ad9ccac4 Mon Sep 17 00:00:00 2001 From: Meta Vens Date: Fri, 20 Nov 2026 06:11:38 +0100 Subject: [PATCH] Perception: a goal's sensory gain gates the sense channels' afferents GoalSpec.SensoryGain (default 1, in [0,1]) is held in DriveState.SensoryGain, written only by the Actor at takeover (at construction for the Start goal) and handed to Perception.Sense. The gain multiplies each channel's afferent as it leaves the conduction ring, so sensitisation still follows the world; Load is built from the gated afferents; ChannelStimulus stays the world's; the nociceptive region afferents, the withdrawal reflex and its flinch are not gated. lodur:ruling:stress-bar-shows-arousal-calm-silences Co-Authored-By: Claude Sonnet 5.5 --- src/Lodur.Core/Actor.cs | 4 +- src/Lodur.Core/Brain.cs | 12 ++ src/Lodur.Core/DriveState.cs | 5 +- src/Lodur.Core/Perception.cs | 21 ++- tests/Lodur.Core.Tests/CalmSensesTests.cs | 186 ++++++++++++++++++++++ 5 files changed, 219 insertions(+), 9 deletions(-) create mode 100644 tests/Lodur.Core.Tests/CalmSensesTests.cs diff --git a/src/Lodur.Core/Actor.cs b/src/Lodur.Core/Actor.cs index 35f76c2..cf7dcfb 100644 --- a/src/Lodur.Core/Actor.cs +++ b/src/Lodur.Core/Actor.cs @@ -50,6 +50,7 @@ namespace Lodur _driveP = drive; _brain = brain; _goal = brain.Start; + _drive.SensoryGain = brain.Goals[brain.Start].SensoryGain; // The Start goal takes over at construction: its entry effect applies like any other goal's. if (brain.Goals[brain.Start].EntryResolve is double startEntry) { @@ -123,7 +124,7 @@ namespace Lodur // A flinch is the leak of a suppressed reflex; with no will nothing is suppressed, so none is reported. var holding = _drive.Will > 0.0; - _perception.Sense(regionX, regionY, regionZ, sources, emitters); + _perception.Sense(regionX, regionY, regionZ, sources, emitters, _drive.SensoryGain); double afferent = 0.0, strainLoad = 0.0, leak = 0.0; for (var r = 0; r < _reflexes.Length; r++) @@ -165,6 +166,7 @@ namespace Lodur _pending = -1; _goalSince = _tick; events |= TickEvents.GoalChanged; + _drive.SensoryGain = _brain.Goals[_goal].SensoryGain; if (_brain.Goals[_goal].EntryResolve is double entry) { _drive.Enter(_driveP, entry); diff --git a/src/Lodur.Core/Brain.cs b/src/Lodur.Core/Brain.cs index 33d3440..a90c635 100644 --- a/src/Lodur.Core/Brain.cs +++ b/src/Lodur.Core/Brain.cs @@ -70,6 +70,13 @@ namespace Lodur /// Resolve drained per second to sustain this goal, finite and not negative, default 0; it is not paid during and is scaled by (1 - Pull) like the rest of the drain. public double Effort { get; init; } + /// + /// 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. + /// + public double SensoryGain { get; init; } = 1.0; + /// /// How long after it takes over the goal runs without will (rounded to whole ticks once, like the dwell), not negative, default 0 (voluntary from the first tick); /// +infinity is involuntary throughout. While involuntary the behaviour writes at full weight and pays no Effort; @@ -200,6 +207,11 @@ namespace Lodur throw new ArgumentException($"Goal {goal.Id}: EntryResolve must be in [0, 1]."); } + if (!(goal.SensoryGain >= 0.0 && goal.SensoryGain <= 1.0)) + { + throw new ArgumentException($"Goal {goal.Id}: SensoryGain must be in [0, 1]."); + } + if (!(goal.Effort >= 0.0 && double.IsFinite(goal.Effort))) { throw new ArgumentException($"Goal {goal.Id}: Effort must be finite and not negative."); diff --git a/src/Lodur.Core/DriveState.cs b/src/Lodur.Core/DriveState.cs index 6d493b9..caa3016 100644 --- a/src/Lodur.Core/DriveState.cs +++ b/src/Lodur.Core/DriveState.cs @@ -99,7 +99,10 @@ namespace Lodur /// The will that is available to hold reflexes down and to power voluntary behaviour: resolve through attention, capped by consciousness. public double Will => Math.Min(Resolve * Attention, Consciousness); - internal static DriveState Fresh => new DriveState { Resolve = 1.0, Attention = 1.0, Consciousness = 1.0 }; + /// The gain on the sense channels in force, 0..1: the goal in force's as the Actor has reached it. Written only by the Actor. + public double SensoryGain { get; internal set; } + + internal static DriveState Fresh => new DriveState { Resolve = 1.0, Attention = 1.0, Consciousness = 1.0, SensoryGain = 1.0 }; /// A goal's entry effect: sets resolve, then the mend check runs (a break the entry lifts past is mended). It is not a break and counts none. internal void Enter(DriveParameters p, double resolve) diff --git a/src/Lodur.Core/Perception.cs b/src/Lodur.Core/Perception.cs index 6f6a60f..f64b289 100644 --- a/src/Lodur.Core/Perception.cs +++ b/src/Lodur.Core/Perception.cs @@ -144,18 +144,24 @@ namespace Lodur /// The afferent signal arriving this tick: what the nerve took in a conduction delay ago. public double Afferent(int region) => _afferent[region]; - /// The sensory signals combined: 1 - the product over channels of (1 - afferent, capped at 1). Zero with no senses or no emitters. + /// The sensory signals combined: 1 - the product over channels of (1 - afferent, capped at 1), the afferents being the gated ones (see ). Zero with no senses or no emitters. public double Load { get; private set; } /// The largest intensity of the channel's emitters at the sense region's point this tick. public double ChannelStimulus(Channel channel) => _senseStimulus[(int)channel]; - /// The channel's afferent arriving this tick, after threshold, sensitisation and conduction delay. + /// + /// The channel's afferent arriving this tick, after threshold, sensitisation and conduction delay, times the sensory gain + /// was given (lodur:ruling:stress-bar-shows-arousal-calm-silences). stays the world's. + /// public double ChannelAfferent(Channel channel) => _senseAfferent[(int)channel]; - /// Senses once per physics step. The spans are the caller's, one point per region. + /// + /// Senses once per physics step. The spans are the caller's, one point per region. The sensory gain (default 1, the identity) multiplies the sense + /// channels' afferents as they leave the conduction delay; the nociceptive region afferents are never gated. + /// public void Sense(ReadOnlySpan x, ReadOnlySpan y, ReadOnlySpan z, ReadOnlySpan sources, - ReadOnlySpan emitters = default) + ReadOnlySpan emitters = default, double sensoryGain = 1.0) { if (x.Length != _regions || y.Length != _regions || z.Length != _regions) { @@ -199,13 +205,13 @@ namespace Lodur if (_senses != null) { - SenseEmitters(x[_senseRegion], y[_senseRegion], z[_senseRegion], emitters); + SenseEmitters(x[_senseRegion], y[_senseRegion], z[_senseRegion], emitters, sensoryGain); } _tick++; } - private void SenseEmitters(float x, float y, float z, ReadOnlySpan emitters) + private void SenseEmitters(float x, float y, float z, ReadOnlySpan emitters, double gain) { var senses = _senses!; var slot = (int)(_tick % _senseSlots); @@ -228,7 +234,8 @@ namespace Lodur sens = Math.Min(Math.Max(sens, 1.0), senses.SensitisationCap); _senseSens[c] = sens; var ring = c * _senseSlots + slot; - _senseAfferent[c] = _senseRing[ring]; // written one conduction delay ago + // Written one conduction delay ago; the gate sits after the ring, so sensitisation still follows the world. + _senseAfferent[c] = _senseRing[ring] * gain; _senseRing[ring] = above * sens * senses.Gain; quiet *= 1.0 - Math.Min(1.0, _senseAfferent[c]); } diff --git a/tests/Lodur.Core.Tests/CalmSensesTests.cs b/tests/Lodur.Core.Tests/CalmSensesTests.cs new file mode 100644 index 0000000..e47d1bf --- /dev/null +++ b/tests/Lodur.Core.Tests/CalmSensesTests.cs @@ -0,0 +1,186 @@ +using Lodur; + +namespace Lodur.Core.Tests; + +/// +/// A goal's sensory gain (lodur:ruling:stress-bar-shows-arousal-calm-silences) gates the sense channels' afferents after the conduction delay, +/// builds Load from the gated values, and never touches the world's stimulus or the nociceptive region afferents. +/// +public sealed class SensoryGainTests +{ + private const double Dt = 0.01; + private static readonly float[] Zero = { 0f }; + private static readonly BoxStimulus[] NoBoxes = System.Array.Empty(); + private static readonly Emitter[] Loud = { new Emitter(Channel.Audio, 0f, 0f, 0f, 1f, 0.6), new Emitter(Channel.Visual, 0f, 0f, 0f, 1f, 0.4) }; + + private static Perception Senses(NerveParameters senses, double delay = 0.0) => + new Perception(new NerveParameters(), Dt, 1, senses with { ConductionDelaySeconds = delay }, 0); + + [Fact] + public void Gain_one_leaves_every_afferent_and_the_load_bit_identical_to_no_gain() + { + var a = Senses(new NerveParameters { Threshold = 0.05, SensitisationGrowth = 2.0 }, 0.03); + var b = Senses(new NerveParameters { Threshold = 0.05, SensitisationGrowth = 2.0 }, 0.03); + for (var i = 0; i < 300; i++) + { + a.Sense(Zero, Zero, Zero, NoBoxes, Loud); + b.Sense(Zero, Zero, Zero, NoBoxes, Loud, 1.0); + Assert.Equal(a.Load, b.Load); + Assert.Equal(a.ChannelAfferent(Channel.Audio), b.ChannelAfferent(Channel.Audio)); + Assert.Equal(a.ChannelAfferent(Channel.Visual), b.ChannelAfferent(Channel.Visual)); + } + + Assert.True(a.Load > 0.0); + } + + [Fact] + public void Gain_scales_each_channels_afferent_and_builds_the_load_from_the_scaled_afferents_while_the_stimulus_is_the_worlds() + { + var p = Senses(new NerveParameters { Threshold = 0.05, SensitisationGrowth = 0.0 }); + p.Sense(Zero, Zero, Zero, NoBoxes, Loud, 0.5); + p.Sense(Zero, Zero, Zero, NoBoxes, Loud, 0.5); + double audio = (0.6 - 0.05) * 0.5, visual = (0.4 - 0.05) * 0.5; + Assert.Equal(audio, p.ChannelAfferent(Channel.Audio), 12); + Assert.Equal(visual, p.ChannelAfferent(Channel.Visual), 12); + Assert.Equal(1.0 - (1.0 - audio) * (1.0 - visual), p.Load, 12); + Assert.Equal(0.6, p.ChannelStimulus(Channel.Audio), 12); + + p.Sense(Zero, Zero, Zero, NoBoxes, Loud, 0.0); + Assert.Equal(0.0, p.ChannelAfferent(Channel.Audio)); + Assert.Equal(0.0, p.ChannelAfferent(Channel.Visual)); + Assert.Equal(0.0, p.Load); + Assert.Equal(0.6, p.ChannelStimulus(Channel.Audio), 12); + Assert.Equal(0.4, p.ChannelStimulus(Channel.Visual), 12); + } + + [Fact] + public void The_gain_in_force_when_the_afferent_leaves_the_delay_applies_not_the_gain_when_the_stimulus_entered() + { + var p = Senses(new NerveParameters { Threshold = 0.05, SensitisationGrowth = 0.0 }, 0.05); // 5 ticks in flight + for (var i = 0; i < 20; i++) p.Sense(Zero, Zero, Zero, NoBoxes, Loud, 1.0); + Assert.True(p.ChannelAfferent(Channel.Audio) > 0.5); + p.Sense(Zero, Zero, Zero, NoBoxes, Loud, 0.0); // the same signal, gated as it arrives + Assert.Equal(0.0, p.ChannelAfferent(Channel.Audio)); + p.Sense(Zero, Zero, Zero, NoBoxes, Loud, 1.0); + Assert.Equal(0.55, p.ChannelAfferent(Channel.Audio), 12); + } + + [Fact] + public void Sensitisation_follows_the_world_not_the_gain() + { + var nerve = new NerveParameters { Threshold = 0.05, SensitisationGrowth = 2.0, SensitisationCap = 4.0 }; + var deaf = Senses(nerve); + var hearing = Senses(nerve); + for (var i = 0; i < 200; i++) + { + deaf.Sense(Zero, Zero, Zero, NoBoxes, Loud, 0.0); + hearing.Sense(Zero, Zero, Zero, NoBoxes, Loud, 1.0); + } + + deaf.Sense(Zero, Zero, Zero, NoBoxes, Loud, 1.0); + hearing.Sense(Zero, Zero, Zero, NoBoxes, Loud, 1.0); + Assert.Equal(hearing.ChannelAfferent(Channel.Audio), deaf.ChannelAfferent(Channel.Audio)); + Assert.True(deaf.ChannelAfferent(Channel.Audio) > 0.55, "the channel sensitised while it was gated"); + } + + private static Brain GainGoal(double gain, string id = "g") => new() + { + Goals = new[] { new GoalSpec { Id = id, SensoryGain = gain, Behaviour = new PoseBehaviour { Keys = new[] { PainBoxBrain.Hold }, FlinchPose = PainBoxBrain.Withdraw } } }, + }; + + [Fact] + public void A_goal_with_gain_zero_silences_load_and_channel_afferents_but_not_the_pain_path_or_the_world_stimulus() + { + var events = new List[2] { new(), new() }; + var afferent = new List[2] { new(), new() }; + var flinches = new int[2]; + var gains = new[] { 1.0, 0.0 }; + var p = new[] { 0f }; + for (var k = 0; k < 2; k++) + { + var actor = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters() with { ResolveDrainPain = 0.0, ResolveDrainStrain = 0.0, ResolveDrainArousal = 0.0 }, + GainGoal(gains[k]), new NerveParameters { Threshold = 0.05, SensitisationGrowth = 0.0 }, 0); + actor.SetWillSlots(0.4, 1.0); // a will that holds only part of the reflex down, so flinches leak + Assert.Equal(gains[k], actor.Drive.SensoryGain); + var strengths = new float[2]; + var targets = new Quat[2]; + var box = new[] { new BoxStimulus(0f, 0f, 0f, 1f, 1f, 1f) { Intensity = 0.8 } }; + for (var i = 0; i < 600; i++) + { + var e = actor.Tick(p, p, p, box, strengths, targets, Loud); + events[k].Add(e); + afferent[k].Add(actor.Afferent(0)); + if ((e & TickEvents.Flinch) != 0) flinches[k]++; + if (i >= 10) + { + Assert.Equal(0.6, actor.ChannelStimulus(Channel.Audio), 12); + if (k == 1) + { + Assert.Equal(0.0, actor.Load); + Assert.Equal(0.0, actor.ChannelAfferent(Channel.Audio)); + } + else + { + Assert.True(actor.Load > 0.0); + } + } + } + } + + Assert.True(flinches[1] > 0, "the fixture flinches"); + Assert.Equal(events[0], events[1]); + Assert.Equal(afferent[0], afferent[1]); + } + + [Fact] + public void The_start_goals_gain_holds_at_construction_and_a_later_goal_sets_it_the_tick_it_takes_over() + { + var pose = new PoseBehaviour { Keys = new[] { PainBoxBrain.Hold }, FlinchPose = PainBoxBrain.Withdraw }; + var brain = new Brain + { + Start = 0, + Goals = new[] + { + new GoalSpec { Id = "a", SensoryGain = 0.25, Behaviour = pose }, + new GoalSpec { Id = "b", Priority = 1.0, SensoryGain = 0.75, When = new[] { new Condition(DriveQuantity.Pain, ConditionOp.AtLeast, 0.05) }, Behaviour = pose }, + }, + }; + var actor = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), brain); + Assert.Equal(0.25, actor.Drive.SensoryGain); + var p = new[] { 0f }; + var box = new[] { new BoxStimulus(0f, 0f, 0f, 1f, 1f, 1f) { Intensity = 0.9 } }; + var strengths = new float[2]; + var targets = new Quat[2]; + for (var i = 0; i < 2000; i++) + { + var e = actor.Tick(p, p, p, box, strengths, targets); + if ((e & TickEvents.GoalChanged) != 0) + { + Assert.Equal(1, actor.GoalIndex); + Assert.Equal(0.75, actor.Drive.SensoryGain); + return; + } + + Assert.Equal(0.25, actor.Drive.SensoryGain); + } + + Assert.Fail("the second goal never took over"); + } + + [Fact] + public void SensoryGain_outside_zero_to_one_or_not_a_number_is_refused_naming_the_goal() + { + void Refused(double gain) + { + var ex = Assert.Throws(() => new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), GainGoal(gain, "bad"))); + Assert.Contains("bad", ex.Message); + } + + Refused(-0.01); + Refused(1.01); + Refused(double.NaN); + Refused(double.PositiveInfinity); + _ = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), GainGoal(0.0)); + _ = new Actor(Dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), GainGoal(1.0)); + } +}