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 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 06:11:38 +01:00
commit f2f5a10226
5 changed files with 219 additions and 9 deletions

View file

@ -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);

View file

@ -70,6 +70,13 @@ namespace Lodur
/// <summary>Resolve drained per second to sustain this goal, finite and not negative, default 0; it is not paid during <see cref="InvoluntarySeconds"/> and is scaled by (1 - Pull) like the rest of the drain.</summary>
public double Effort { get; init; }
/// <summary>
/// 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 <see cref="EntryResolve"/>: at the tick the goal
/// takes over, and at construction for the Start goal.
/// </summary>
public double SensoryGain { get; init; } = 1.0;
/// <summary>
/// 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.");

View file

@ -99,7 +99,10 @@ namespace Lodur
/// <summary>The will that is available to hold reflexes down and to power voluntary behaviour: resolve through attention, capped by consciousness.</summary>
public double Will => Math.Min(Resolve * Attention, Consciousness);
internal static DriveState Fresh => new DriveState { Resolve = 1.0, Attention = 1.0, Consciousness = 1.0 };
/// <summary>The gain on the sense channels in force, 0..1: the goal in force's <see cref="GoalSpec.SensoryGain"/> as the Actor has reached it. Written only by the Actor.</summary>
public double SensoryGain { get; internal set; }
internal static DriveState Fresh => new DriveState { Resolve = 1.0, Attention = 1.0, Consciousness = 1.0, SensoryGain = 1.0 };
/// <summary>A goal's entry effect: sets resolve, then the mend check runs (a break the entry lifts past <see cref="DriveParameters.WillMendsAt"/> is mended). It is not a break and counts none.</summary>
internal void Enter(DriveParameters p, double resolve)

View file

@ -144,18 +144,24 @@ namespace Lodur
/// <summary>The afferent signal arriving this tick: what the nerve took in a conduction delay ago.</summary>
public double Afferent(int region) => _afferent[region];
/// <summary>The sensory signals combined: 1 - the product over channels of (1 - afferent, capped at 1). Zero with no senses or no emitters.</summary>
/// <summary>The sensory signals combined: 1 - the product over channels of (1 - afferent, capped at 1), the afferents being the gated ones (see <see cref="ChannelAfferent"/>). Zero with no senses or no emitters.</summary>
public double Load { get; private set; }
/// <summary>The largest intensity of the channel's emitters at the sense region's point this tick.</summary>
public double ChannelStimulus(Channel channel) => _senseStimulus[(int)channel];
/// <summary>The channel's afferent arriving this tick, after threshold, sensitisation and conduction delay.</summary>
/// <summary>
/// The channel's afferent arriving this tick, after threshold, sensitisation and conduction delay, times the sensory gain
/// <see cref="Sense"/> was given (lodur:ruling:stress-bar-shows-arousal-calm-silences). <see cref="ChannelStimulus"/> stays the world's.
/// </summary>
public double ChannelAfferent(Channel channel) => _senseAfferent[(int)channel];
/// <summary>Senses once per physics step. The spans are the caller's, one point per region.</summary>
/// <summary>
/// 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.
/// </summary>
public void Sense(ReadOnlySpan<float> x, ReadOnlySpan<float> y, ReadOnlySpan<float> z, ReadOnlySpan<BoxStimulus> sources,
ReadOnlySpan<Emitter> emitters = default)
ReadOnlySpan<Emitter> 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<Emitter> emitters)
private void SenseEmitters(float x, float y, float z, ReadOnlySpan<Emitter> 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]);
}