Actor: the involuntary onset is rounded to whole ticks once

Compared as (tick - goalSince) x dt >= seconds in floating point, an onset of
1.85 s at dt 1/120 lasted 223 ticks, not 222. It is now precomputed per goal
beside the dwell and compared as integers (+infinity stays involuntary
throughout); effort and weight read the same count. Addresses finding
onset-boundary-float.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-20 04:58:19 +01:00
commit 91a3c6ffd0
3 changed files with 31 additions and 3 deletions

View file

@ -28,6 +28,7 @@ namespace Lodur
private readonly Brain _brain;
private readonly long _latency;
private readonly long[] _dwell;
private readonly long[] _onset;
private readonly Perception _perception;
private readonly WithdrawalReflex[] _reflexes;
private DriveState _drive = DriveState.Fresh;
@ -51,9 +52,13 @@ namespace Lodur
_goal = brain.Start;
_latency = Math.Max(1, (long)Math.Round(brain.DeliberateLatencySeconds / tickSeconds));
_dwell = new long[brain.Goals.Count];
_onset = new long[brain.Goals.Count];
for (var g = 0; g < _dwell.Length; g++)
{
_dwell[g] = (long)Math.Round(brain.Goals[g].MinDwellSeconds / tickSeconds);
// Rounded to whole ticks once, like the dwell, so the onset is the same number of ticks whatever the tick length does to a product.
var onset = brain.Goals[g].InvoluntarySeconds;
_onset[g] = double.IsPositiveInfinity(onset) ? long.MaxValue : (long)Math.Round(onset / tickSeconds);
}
_perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion);
@ -123,7 +128,7 @@ namespace Lodur
// Effort is paid only once the goal is voluntary: the onset (InvoluntarySeconds) runs without will, and without cost.
var inForce = _brain.Goals[_goal];
var effort = (_tick - _goalSince) * _dt >= inForce.InvoluntarySeconds ? inForce.Effort : 0.0;
var effort = _tick - _goalSince >= _onset[_goal] ? inForce.Effort : 0.0;
if (_drive.Step(_driveP, _dt, afferent, strainLoad, _perception.Load, inForce.Pull, effort))
{
events |= TickEvents.WillBroke;
@ -155,7 +160,7 @@ namespace Lodur
// Every voluntary layer is held by will, in contest with the reflex; an involuntary one (the onset of a goal) is not.
var secondsInGoal = (_tick - _goalSince) * _dt;
var weight = secondsInGoal >= _brain.Goals[_goal].InvoluntarySeconds ? Math.Pow(_drive.Will, _driveP.WillCurve) : 1.0;
var weight = _tick - _goalSince >= _onset[_goal] ? Math.Pow(_drive.Will, _driveP.WillCurve) : 1.0;
_brain.Goals[_goal].Behaviour.Write(weight, leak, _drive.Strain, _tick * _dt, secondsInGoal, strengths, targets);
_tick++;
return events;

View file

@ -71,7 +71,7 @@ namespace Lodur
public double Effort { get; init; }
/// <summary>
/// How long after it takes over the goal runs without will, not negative, default 0 (voluntary from the first tick);
/// 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;
/// after it the goal is voluntary: sustained by <see cref="DriveState.Will"/> like every other voluntary layer.
/// </summary>

View file

@ -363,4 +363,27 @@ public sealed class WillTests
[MemberData(nameof(BadDrives))]
public void A_drive_value_the_drive_cannot_run_on_is_refused_at_construction_before_any_tick(DriveParameters bad) =>
Assert.Throws<ArgumentException>(() => new Actor(0.01, 1, new NerveParameters(), new WithdrawalParameters(), bad, PainBoxBrain.Make(0.5)));
[Theory]
[InlineData(1.0 / 120.0, 1.85, 222)] // 222 x (1/120) is just under 1.85 in floating point
[InlineData(1.0 / 60.0, 0.9, 54)]
[InlineData(0.01, 0.5, 50)]
public void The_involuntary_onset_is_the_requested_seconds_in_whole_ticks_whatever_the_tick_length(double dt, double onset, long ticks)
{
var brain = new Brain { Goals = new[] { new GoalSpec { Id = "g", Effort = 1.0, InvoluntarySeconds = onset, Behaviour = Pose } } };
var actor = new Actor(dt, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters { ResolveRecovery = 0.0 }, brain);
float[] p = { 0f }, strengths = new float[2];
var targets = new Quat[2];
long firstDrain = -1, firstWeighted = -1;
for (long i = 0; i < ticks + 20; i++)
{
var before = actor.Drive.Resolve;
actor.Tick(p, p, p, System.Array.Empty<BoxStimulus>(), strengths, targets);
if (firstDrain < 0 && actor.Drive.Resolve < before) firstDrain = i;
if (firstWeighted < 0 && strengths[0] < 1f) firstWeighted = i;
}
Assert.Equal(ticks, firstDrain);
Assert.Equal(ticks, firstWeighted);
}
}