Brain: goals carry an entry effect, an effort and an involuntary onset
GoalSpec.EntryResolve sets resolve on the tick the goal takes over (then the mend check runs); Effort is resolve drained per second to sustain the goal, paid only once it is voluntary and scaled by (1 - Pull); InvoluntarySeconds is the onset that runs without will (+infinity throughout). Brain.Validate refuses out-of-range values naming the goal. The pain-box fixtures mark their release goal involuntary throughout. The behaviour weight arrives next. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
74883f85e5
commit
c93bdb7e1f
6 changed files with 186 additions and 6 deletions
|
|
@ -124,7 +124,10 @@ namespace Lodur
|
|||
}
|
||||
}
|
||||
|
||||
if (_drive.Step(_driveP, _dt, afferent, strainLoad, _perception.Load, _brain.Goals[_goal].Pull))
|
||||
// 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;
|
||||
if (_drive.Step(_driveP, _dt, afferent, strainLoad, _perception.Load, inForce.Pull, effort))
|
||||
{
|
||||
events |= TickEvents.WillBroke;
|
||||
}
|
||||
|
|
@ -147,6 +150,10 @@ namespace Lodur
|
|||
_pending = -1;
|
||||
_goalSince = _tick;
|
||||
events |= TickEvents.GoalChanged;
|
||||
if (_brain.Goals[_goal].EntryResolve is double entry)
|
||||
{
|
||||
_drive.Enter(_driveP, entry);
|
||||
}
|
||||
}
|
||||
|
||||
_brain.Goals[_goal].Behaviour.Write(leak, _drive.Strain, _tick * _dt, (_tick - _goalSince) * _dt, strengths, targets);
|
||||
|
|
|
|||
|
|
@ -61,6 +61,22 @@ namespace Lodur
|
|||
/// </summary>
|
||||
public double MinDwellSeconds { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The resolve the character has the tick this goal takes over, in [0, 1]; null leaves resolve as it is. Rage is what
|
||||
/// the will breaking leads to, and it resets the will (lodur:ruling:rage-involuntary-onset-then-voluntary).
|
||||
/// </summary>
|
||||
public double? EntryResolve { get; init; }
|
||||
|
||||
/// <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>
|
||||
/// How long after it takes over the goal runs without will, 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>
|
||||
public double InvoluntarySeconds { get; init; }
|
||||
|
||||
private Condition[] _when = Array.Empty<Condition>();
|
||||
private IReadOnlyList<Condition> _whenView = Array.AsReadOnly(Array.Empty<Condition>());
|
||||
|
||||
|
|
@ -179,6 +195,21 @@ namespace Lodur
|
|||
throw new ArgumentException($"Goal {goal.Id}: MinDwellSeconds must be finite (not infinite) and not negative.");
|
||||
}
|
||||
|
||||
if (goal.EntryResolve is double entry && !(entry >= 0.0 && entry <= 1.0))
|
||||
{
|
||||
throw new ArgumentException($"Goal {goal.Id}: EntryResolve 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.");
|
||||
}
|
||||
|
||||
if (!(goal.InvoluntarySeconds >= 0.0))
|
||||
{
|
||||
throw new ArgumentException($"Goal {goal.Id}: InvoluntarySeconds must not be negative or NaN (infinity is allowed).");
|
||||
}
|
||||
|
||||
foreach (var c in goal.When)
|
||||
{
|
||||
if (!double.IsFinite(c.Threshold))
|
||||
|
|
|
|||
|
|
@ -65,13 +65,23 @@ namespace Lodur
|
|||
|
||||
internal static DriveState Fresh => new DriveState { Resolve = 1.0 };
|
||||
|
||||
/// <summary>Steps one tick; returns true on the tick the will breaks (each break, not only the first). <paramref name="pull"/> is the active goal's: it scales resolve drain by (1 - pull).</summary>
|
||||
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load, double pull)
|
||||
/// <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)
|
||||
{
|
||||
Resolve = resolve;
|
||||
if (WillBroken && Resolve >= p.WillMendsAt)
|
||||
{
|
||||
WillBroken = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>Steps one tick; returns true on the tick the will breaks (each break, not only the first). <paramref name="pull"/> is the active goal's: it scales resolve drain by (1 - pull). <paramref name="effort"/> is the active goal's resolve drained per second to sustain it (0 while it is involuntary); it joins the drain sum, so pull scales it too.</summary>
|
||||
internal bool Step(DriveParameters p, double dt, double afferent, double strainLoad, double load, double pull, double effort)
|
||||
{
|
||||
Arousal = Math.Min(Math.Max(Arousal + dt * (p.ArousalRise * load * (1.0 - Arousal) - p.ArousalDecay * Arousal), 0.0), 1.0);
|
||||
Pain = Math.Min(Math.Max(Pain + dt * (p.PainRise * afferent * (1.0 - Pain) - p.PainDecay * Pain), 0.0), 1.0);
|
||||
Strain = Math.Max(0.0, Strain + dt * (strainLoad - p.StrainRecover * Strain));
|
||||
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal) * (1.0 - pull) * p.DrainTrait;
|
||||
var drain = (p.ResolveDrainPain * Pain + p.ResolveDrainStrain * Strain + p.ResolveDrainArousal * Arousal + effort) * (1.0 - pull) * p.DrainTrait;
|
||||
var recovery = Pain < p.ResolveRecoversBelowPain && Arousal < p.ResolveRecoversBelowArousal ? p.ResolveRecovery * (1.0 - Resolve) : 0.0;
|
||||
Resolve = Math.Min(Math.Max(Resolve + dt * (recovery - drain), 0.0), 1.0);
|
||||
if (WillBroken)
|
||||
|
|
|
|||
|
|
@ -38,7 +38,7 @@ internal static class PainBoxBrain
|
|||
},
|
||||
new GoalSpec
|
||||
{
|
||||
Id = "release", Priority = 1.0, Pull = pull,
|
||||
Id = "release", Priority = 1.0, Pull = pull, InvoluntarySeconds = double.PositiveInfinity,
|
||||
When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) },
|
||||
Behaviour = new PoseBehaviour
|
||||
{
|
||||
|
|
|
|||
|
|
@ -131,4 +131,136 @@ public sealed class WillTests
|
|||
|
||||
[Fact]
|
||||
public void A_WillMendsAt_of_one_is_allowed() => _ = new PainBoxReplay(1, drive: new DriveParameters { WillMendsAt = 1.0 });
|
||||
|
||||
private static PoseBehaviour Pose => new() { Keys = new[] { PainBoxBrain.Hold }, FlinchPose = PainBoxBrain.Withdraw };
|
||||
|
||||
private static Brain Rage(double? entry) => new()
|
||||
{
|
||||
Goals = new[]
|
||||
{
|
||||
new GoalSpec { Id = "composure", Behaviour = Pose },
|
||||
new GoalSpec
|
||||
{
|
||||
Id = "rage", Priority = 1.0, EntryResolve = entry,
|
||||
When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) },
|
||||
Behaviour = Pose,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
[Theory]
|
||||
[InlineData(1.0, false)]
|
||||
[InlineData(0.1, true)]
|
||||
public void A_goals_entry_resolve_is_set_on_the_tick_it_takes_over_and_mends_the_will_only_if_it_reaches_WillMendsAt(double entry, bool stillBroken)
|
||||
{
|
||||
var r = new PainBoxReplay(1, brain: Rage(entry));
|
||||
TickEvents e;
|
||||
do { e = r.Tick(true); } while ((e & TickEvents.GoalChanged) == 0 && r.Actor.TickCount < 5000);
|
||||
Assert.NotEqual(0, (int)(e & TickEvents.GoalChanged));
|
||||
Assert.Equal(1, r.Actor.GoalIndex);
|
||||
Assert.Equal(entry, r.Actor.Drive.Resolve);
|
||||
Assert.Equal(stillBroken, r.Actor.Drive.WillBroken);
|
||||
Assert.Equal(1, r.Actor.Drive.Breaks);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_goal_with_no_entry_resolve_leaves_resolve_alone()
|
||||
{
|
||||
var r = new PainBoxReplay(1, brain: Rage(null));
|
||||
TickEvents e;
|
||||
do { e = r.Tick(true); } while ((e & TickEvents.GoalChanged) == 0 && r.Actor.TickCount < 5000);
|
||||
Assert.Equal(0.0, r.Actor.Drive.Resolve);
|
||||
Assert.True(r.Actor.Drive.WillBroken);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(1.0, 0.0, 0.5)]
|
||||
[InlineData(1.0, 0.4, 2.0)]
|
||||
[InlineData(0.0, 0.0, 0.5)]
|
||||
public void Effort_drains_resolve_only_after_the_involuntary_onset_by_effort_times_one_minus_pull_times_the_trait(double onset, double pull, double effort)
|
||||
{
|
||||
var drive = new DriveParameters { ResolveRecovery = 0.0, DrainTrait = 1.0 };
|
||||
var brain = new Brain { Goals = new[] { new GoalSpec { Id = "g", Pull = pull, Effort = effort, InvoluntarySeconds = onset, Behaviour = Pose } } };
|
||||
var r = new PainBoxReplay(1, drive: drive, brain: brain, jitter: 0.0);
|
||||
var first = -1L;
|
||||
var last = 1.0;
|
||||
var steps = 0;
|
||||
for (long i = 0; i < 400; i++)
|
||||
{
|
||||
r.Tick(false);
|
||||
var now = r.Actor.Drive.Resolve;
|
||||
if (now <= 0.0) break; // the floor clips the last step
|
||||
if (now < 1.0 && first < 0)
|
||||
{
|
||||
first = i;
|
||||
last = 1.0;
|
||||
}
|
||||
|
||||
if (first >= 0)
|
||||
{
|
||||
Assert.Equal(0.01 * effort * (1.0 - pull), last - now, 12);
|
||||
steps++;
|
||||
}
|
||||
|
||||
last = now;
|
||||
}
|
||||
|
||||
Assert.Equal((long)Math.Round(onset / 0.01), first);
|
||||
Assert.True(steps > 20);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_infinitely_involuntary_goal_never_pays_its_effort()
|
||||
{
|
||||
var brain = new Brain { Goals = new[] { new GoalSpec { Id = "g", Effort = 5.0, InvoluntarySeconds = double.PositiveInfinity, Behaviour = Pose } } };
|
||||
var r = new PainBoxReplay(1, brain: brain);
|
||||
for (var i = 0; i < 500; i++) r.Tick(false);
|
||||
Assert.Equal(1.0, r.Actor.Drive.Resolve);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Onset_is_counted_from_the_tick_the_goal_takes_over_not_from_the_start()
|
||||
{
|
||||
var late = new GoalSpec { Id = "late", Priority = 1.0, Effort = 1.0, InvoluntarySeconds = 0.5, When = new[] { new Condition(DriveQuantity.Pain, ConditionOp.AtLeast, 0.3) }, Behaviour = Pose };
|
||||
var brain = new Brain { Goals = new[] { new GoalSpec { Id = "calm", Behaviour = Pose }, late } };
|
||||
var r = new PainBoxReplay(1, brain: brain, drive: new DriveParameters { ResolveRecovery = 0.0 });
|
||||
long took = -1;
|
||||
long firstEffort = -1;
|
||||
for (long i = 0; i < 3000 && firstEffort < 0; i++)
|
||||
{
|
||||
var before = r.Actor.Drive;
|
||||
var e = r.Tick(true);
|
||||
if ((e & TickEvents.GoalChanged) != 0) took = i;
|
||||
var drainedWithoutEffort = before.Resolve - r.Actor.Drive.Resolve;
|
||||
if (took >= 0 && drainedWithoutEffort > 0.01 * 1.0 * 0.5 * 0.99) firstEffort = i;
|
||||
}
|
||||
|
||||
Assert.True(took > 0);
|
||||
Assert.True(firstEffort - took >= 50, $"effort began {firstEffort - took} ticks after the goal took over, under the 50 of the onset");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Goal_values_outside_their_ranges_are_refused_naming_the_goal()
|
||||
{
|
||||
Brain Of(GoalSpec g) => new() { Goals = new[] { g } };
|
||||
GoalSpec Base() => new() { Id = "bad", Behaviour = Pose };
|
||||
void Refused(GoalSpec g)
|
||||
{
|
||||
var ex = Assert.Throws<ArgumentException>(() => new Actor(0.01, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), Of(g)));
|
||||
Assert.Contains("bad", ex.Message);
|
||||
}
|
||||
|
||||
Refused(Base() with { EntryResolve = -0.01 });
|
||||
Refused(Base() with { EntryResolve = 1.01 });
|
||||
Refused(Base() with { EntryResolve = double.NaN });
|
||||
Refused(Base() with { Effort = -1.0 });
|
||||
Refused(Base() with { Effort = double.PositiveInfinity });
|
||||
Refused(Base() with { Effort = double.NaN });
|
||||
Refused(Base() with { InvoluntarySeconds = -0.1 });
|
||||
Refused(Base() with { InvoluntarySeconds = double.NaN });
|
||||
|
||||
_ = new Actor(0.01, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(),
|
||||
Of(Base() with { EntryResolve = 0.0, Effort = 0.0, InvoluntarySeconds = double.PositiveInfinity }));
|
||||
_ = new Actor(0.01, 1, new NerveParameters(), new WithdrawalParameters(), new DriveParameters(), Of(Base() with { EntryResolve = 1.0 }));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -56,7 +56,7 @@ internal sealed class ConnectedPainBox : IDisposable
|
|||
},
|
||||
new GoalSpec
|
||||
{
|
||||
Id = "release", Priority = 1.0, Pull = 0.5,
|
||||
Id = "release", Priority = 1.0, Pull = 0.5, InvoluntarySeconds = double.PositiveInfinity,
|
||||
When = new[] { new Condition(DriveQuantity.Breaks, ConditionOp.AtLeast, 1.0) },
|
||||
Behaviour = new PoseBehaviour
|
||||
{
|
||||
|
|
|
|||
Loading…
Reference in a new issue