Percepts: emitters carry an id and kind; Perception remembers sensed things in a bounded slot table; phantom id bit reserved
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
e3811644c3
commit
ee23c32898
4 changed files with 507 additions and 10 deletions
|
|
@ -32,8 +32,13 @@ Signal flows one way per tick, in this order; each layer has one owner. Layers 1
|
|||
(from the body binding, read-only) and the game's stimulus sources. Two source kinds today: noxious box
|
||||
volumes with an intensity the game writes each tick, and point emitters on the audio, visual and interrupt
|
||||
channels (position, radius and intensity written each tick, falling off linearly to the radius), sensed at
|
||||
one region and combined into one load, `1 - prod(1 - afferent)`. Output per region: the stimulus, and
|
||||
the afferent signal after threshold, sensitisation and conduction delay; and the load. A poke that barely moves the body
|
||||
one region and combined into one load, `1 - prod(1 - afferent)`. An emitter may carry an `Id` (the game's
|
||||
stable id; 0 is anonymous and only loads the nerve) and a `Kind` (the game's tag, never interpreted by Lodur).
|
||||
Perception keeps a bounded percept memory (`PerceptParameters`, `Percept`): one slot per identified thing
|
||||
strictly inside an emitter's radius, with its kind, the position of the emitter that reached it hardest,
|
||||
salience (that reach times the sensory gain) and seconds since sensed, forgotten after a memory time. The top
|
||||
id bit (`PhantomIdBit`) is reserved to Lodur's phantoms and `Sense` refuses it from the game. Output per region: the stimulus, and
|
||||
the afferent signal after threshold, sensitisation and conduction delay; and the load, and the percepts. A poke that barely moves the body
|
||||
is still a stimulus.
|
||||
2. **Reflex** (`WithdrawalReflex`, `WithdrawalParameters`). Fast, local responses to afferent signals
|
||||
(withdrawal now; startle and bracing later), each with a latency, overlaid on whatever the body is doing.
|
||||
|
|
@ -97,7 +102,7 @@ behaviour writes strengths and targets at the will's weight. The tick that commi
|
|||
|
||||
| Owner | Inputs | Outputs | Derived state | Forbidden writers |
|
||||
|---|---|---|---|---|
|
||||
| `Perception` | region points, the game's stimulus sources and emitters, the sensory gain passed in | per-region stimulus and afferent, per-channel stimulus and afferent, `Load` | sensitisation, the conduction rings; the channel afferents are gated, the stimuli are not | the drive or a goal writing an afferent; a gain outside [0, 1] (`Sense` refuses it, NaN and infinity included); gating a region (nociceptive) afferent |
|
||||
| `Perception` | region points, the game's stimulus sources and emitters, the sensory gain passed in | per-region stimulus and afferent, per-channel stimulus and afferent, `Load`, the percept slots | sensitisation, the conduction rings, percept salience and slot occupancy; the channel afferents are gated, the stimuli are not | the drive or a goal writing an afferent; the game setting `PhantomIdBit` (`Sense` refuses it); the Brain or a session writing a percept; a gain outside [0, 1] (`Sense` refuses it, NaN and infinity included); gating a region (nociceptive) afferent |
|
||||
| Reflex (`WithdrawalReflex`) | its region's afferent and the drive's `Will` (the pull acts through resolve's drain, not here) | leak, strain load, flinch | none beyond its latency ring | the Brain or the behaviour setting a reflex gain |
|
||||
| Drive (`DriveState`) | afferent sum, strain load, `Load`, the goal's pull and effort | pain, strain, arousal, resolve, `Breaks`, `Will` (= min(resolve x attention, consciousness)), the break and mend events | `Will` is derived and never stored; the break recurs (it mends at `WillMendsAt`) | the Brain, a session or the body writing resolve, arousal or `Will`; the one sanctioned jumps are `Enter` (a goal's entry resolve) and the Actor's entry ramp (arousal) |
|
||||
| Attention, Consciousness slots | nothing yet: both read 1 (startle and physiology are the writers to come) | the two factors of `Will` | none | anything but the startle reflex (`lodur-startle-attention`) and physiology; the Actor's test seam `SetWillSlots` is internal |
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ namespace Lodur
|
|||
public Actor(
|
||||
double tickSeconds, int regionCount,
|
||||
NerveParameters nerve, WithdrawalParameters withdrawal, DriveParameters drive, Brain brain,
|
||||
NerveParameters? senses = null, int senseRegion = 0)
|
||||
NerveParameters? senses = null, int senseRegion = 0, PerceptParameters? percepts = null)
|
||||
{
|
||||
brain.Validate();
|
||||
drive.Validate();
|
||||
|
|
@ -81,7 +81,7 @@ namespace Lodur
|
|||
_entryTicks[g] = entryTicks >= long.MaxValue ? long.MaxValue : (long)Math.Round(entryTicks);
|
||||
}
|
||||
|
||||
_perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion);
|
||||
_perception = new Perception(nerve, tickSeconds, regionCount, senses, senseRegion, percepts);
|
||||
_reflexes = new WithdrawalReflex[regionCount];
|
||||
for (var r = 0; r < regionCount; r++)
|
||||
{
|
||||
|
|
@ -123,6 +123,11 @@ namespace Lodur
|
|||
|
||||
public double ChannelAfferent(Channel channel) => _perception.ChannelAfferent(channel);
|
||||
|
||||
/// <summary>The number of percept slots (0 with no senses); a free slot reads as Id 0.</summary>
|
||||
public int PerceptCount => _perception.PerceptCount;
|
||||
|
||||
public Percept Percept(int slot) => _perception.Percept(slot);
|
||||
|
||||
/// <summary>
|
||||
/// Runs one physics step's worth of nervous system. The point spans hold one entry per region; the strengths
|
||||
/// and targets spans one entry per muscle, written for the body to follow. The emitters are the game's point
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ namespace Lodur
|
|||
/// <summary>
|
||||
/// A game stimulus at a point: sound, flicker, a notification. The game writes its position and intensity every
|
||||
/// tick; Lodur decides what reaches the sense region. What arrives is the intensity falling off linearly to zero
|
||||
/// at the radius.
|
||||
/// at the radius. With an <see cref="Id"/> it is also a thing the puppet can come to know, remembered as a percept.
|
||||
/// </summary>
|
||||
public struct Emitter
|
||||
{
|
||||
|
|
@ -46,12 +46,20 @@ namespace Lodur
|
|||
public float Radius;
|
||||
public double Intensity;
|
||||
|
||||
public Emitter(Channel channel, float x, float y, float z, float radius, double intensity = 0.0)
|
||||
/// <summary>The game's stable id for the thing this emitter belongs to. 0 is anonymous: it loads the nerve and is never a percept. The top bit is reserved (<see cref="Perception.PhantomIdBit"/>); <see cref="Perception.Sense"/> refuses it.</summary>
|
||||
public ulong Id;
|
||||
|
||||
/// <summary>The game's tag for the thing (what kind it is). Lodur carries it and never interprets it; emitters sharing an id must share a kind.</summary>
|
||||
public uint Kind;
|
||||
|
||||
public Emitter(Channel channel, float x, float y, float z, float radius, double intensity = 0.0, ulong id = 0, uint kind = 0)
|
||||
{
|
||||
Channel = channel;
|
||||
X = x; Y = y; Z = z;
|
||||
Radius = radius;
|
||||
Intensity = intensity;
|
||||
Id = id;
|
||||
Kind = kind;
|
||||
}
|
||||
|
||||
/// <summary>Intensity * max(0, 1 - distance / Radius) at the point; zero at and beyond the radius.</summary>
|
||||
|
|
@ -76,11 +84,57 @@ namespace Lodur
|
|||
public double Gain { get; init; } = 1.0;
|
||||
}
|
||||
|
||||
/// <summary>The percept memory's tunables.</summary>
|
||||
public sealed record PerceptParameters
|
||||
{
|
||||
/// <summary>How many things can be remembered at once, 1..256.</summary>
|
||||
public int Capacity { get; init; } = 16;
|
||||
|
||||
/// <summary>How long a thing stays remembered after it was last sensed; finite and not negative.</summary>
|
||||
public double MemorySeconds { get; init; } = 5.0;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
if (Capacity < 1 || Capacity > 256)
|
||||
{
|
||||
throw new ArgumentException("The percept capacity must be in 1..256.", nameof(Capacity));
|
||||
}
|
||||
|
||||
if (!(MemorySeconds >= 0.0 && MemorySeconds < double.PositiveInfinity))
|
||||
{
|
||||
throw new ArgumentException("The percept memory time must be finite and not negative.", nameof(MemorySeconds));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One remembered thing. Id 0 is a free slot. Nothing here says whether the thing is real.</summary>
|
||||
public readonly struct Percept
|
||||
{
|
||||
public ulong Id { get; }
|
||||
public uint Kind { get; }
|
||||
|
||||
/// <summary>Where the emitter that reached the puppet hardest was, the last tick the thing was sensed.</summary>
|
||||
public float X { get; }
|
||||
public float Y { get; }
|
||||
public float Z { get; }
|
||||
|
||||
/// <summary>The largest intensity received among the thing's emitters times the sensory gain; 0 on a tick it is not sensed.</summary>
|
||||
public double Salience { get; }
|
||||
|
||||
/// <summary>0 on a tick the thing is sensed.</summary>
|
||||
public double SecondsSinceSensed { get; }
|
||||
|
||||
public Percept(ulong id, uint kind, float x, float y, float z, double salience, double secondsSinceSensed)
|
||||
{
|
||||
Id = id; Kind = kind; X = x; Y = y; Z = z; Salience = salience; SecondsSinceSensed = secondsSinceSensed;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Layer 1 for one character, a fixed number of body regions. Each tick it takes one point per region and the
|
||||
/// game's stimulus sources and produces, per region, the stimulus there and the afferent signal after
|
||||
/// threshold, sensitisation and conduction delay. With a sensory nerve it also senses point emitters at one
|
||||
/// region on three channels and combines the channels into one load. Everything is allocated in the constructor.
|
||||
/// region on three channels and combines the channels into one load and remembers the identified things it senses in a bounded slot table. Everything is allocated in the constructor.
|
||||
/// </summary>
|
||||
public sealed class Perception
|
||||
{
|
||||
|
|
@ -99,13 +153,23 @@ namespace Lodur
|
|||
private readonly double[] _senseSens = new double[ChannelCount];
|
||||
private readonly double[] _senseStimulus = new double[ChannelCount];
|
||||
private readonly double[] _senseAfferent = new double[ChannelCount];
|
||||
private readonly double _memorySeconds;
|
||||
private readonly ulong[] _pId; // percept slots; id 0 is free
|
||||
private readonly uint[] _pKind;
|
||||
private readonly float[] _pX, _pY, _pZ;
|
||||
private readonly double[] _pSalience;
|
||||
private readonly double[] _pSince;
|
||||
private readonly bool[] _pSensed; // sensed this tick
|
||||
private long _tick;
|
||||
|
||||
private const int ChannelCount = 3;
|
||||
|
||||
/// <summary>The top bit of an emitter id, reserved to Lodur's phantoms; <see cref="Sense"/> refuses a caller emitter that sets it.</summary>
|
||||
public const ulong PhantomIdBit = 1UL << 63;
|
||||
|
||||
/// <param name="senses">The nerve of the sensory channels; null for a body with no senses (the load stays zero).</param>
|
||||
/// <param name="senseRegion">The region whose point the emitters are sensed at.</param>
|
||||
public Perception(NerveParameters parameters, double tickSeconds, int regionCount, NerveParameters? senses = null, int senseRegion = 0)
|
||||
public Perception(NerveParameters parameters, double tickSeconds, int regionCount, NerveParameters? senses = null, int senseRegion = 0, PerceptParameters? percepts = null)
|
||||
{
|
||||
_p = parameters;
|
||||
_dt = tickSeconds;
|
||||
|
|
@ -129,10 +193,26 @@ namespace Lodur
|
|||
_senseSlots = Math.Max(1, (int)Math.Round(senses.ConductionDelaySeconds / tickSeconds));
|
||||
_senseRing = new double[ChannelCount * _senseSlots];
|
||||
Array.Fill(_senseSens, 1.0);
|
||||
var pp = percepts ?? new PerceptParameters();
|
||||
pp.Validate();
|
||||
_memorySeconds = pp.MemorySeconds;
|
||||
_pId = new ulong[pp.Capacity];
|
||||
_pKind = new uint[pp.Capacity];
|
||||
_pX = new float[pp.Capacity];
|
||||
_pY = new float[pp.Capacity];
|
||||
_pZ = new float[pp.Capacity];
|
||||
_pSalience = new double[pp.Capacity];
|
||||
_pSince = new double[pp.Capacity];
|
||||
_pSensed = new bool[pp.Capacity];
|
||||
}
|
||||
else
|
||||
{
|
||||
_senseRing = Array.Empty<double>();
|
||||
_pId = Array.Empty<ulong>();
|
||||
_pKind = Array.Empty<uint>();
|
||||
_pX = _pY = _pZ = Array.Empty<float>();
|
||||
_pSalience = _pSince = Array.Empty<double>();
|
||||
_pSensed = Array.Empty<bool>();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -156,10 +236,18 @@ namespace Lodur
|
|||
/// </summary>
|
||||
public double ChannelAfferent(Channel channel) => _senseAfferent[(int)channel];
|
||||
|
||||
/// <summary>The number of percept slots: the capacity, or 0 with no senses. A slot is free when its percept's id is 0.</summary>
|
||||
public int PerceptCount => _pId.Length;
|
||||
|
||||
/// <summary>The percept in the slot. A thing keeps its slot while it is remembered; a free slot reads as Id 0.</summary>
|
||||
public Percept Percept(int slot) =>
|
||||
new Percept(_pId[slot], _pKind[slot], _pX[slot], _pY[slot], _pZ[slot], _pSalience[slot], _pSince[slot]);
|
||||
|
||||
/// <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, so a change of gain reaches <see cref="Load"/> on the next call it is passed to; the nociceptive region
|
||||
/// afferents are never gated. A gain outside [0, 1] (NaN and infinity included) is refused.
|
||||
/// afferents are never gated. A gain outside [0, 1] (NaN and infinity included) is refused,
|
||||
/// as is an emitter that sets <see cref="PhantomIdBit"/> and two emitters of one id with different kinds.
|
||||
/// </summary>
|
||||
public void Sense(ReadOnlySpan<float> x, ReadOnlySpan<float> y, ReadOnlySpan<float> z, ReadOnlySpan<BoxStimulus> sources,
|
||||
ReadOnlySpan<Emitter> emitters = default, double sensoryGain = 1.0)
|
||||
|
|
@ -183,6 +271,23 @@ namespace Lodur
|
|||
{
|
||||
throw new ArgumentException($"Emitter {i} has channel {(int)emitters[i].Channel}; the channels are Audio, Visual and Interrupt.", nameof(emitters));
|
||||
}
|
||||
|
||||
var id = emitters[i].Id;
|
||||
if ((id & PhantomIdBit) != 0)
|
||||
{
|
||||
throw new ArgumentException($"Emitter {i} sets the phantom id bit, which is reserved to Lodur.", nameof(emitters));
|
||||
}
|
||||
|
||||
if (id != 0)
|
||||
{
|
||||
for (var j = 0; j < i; j++)
|
||||
{
|
||||
if (emitters[j].Id == id && emitters[j].Kind != emitters[i].Kind)
|
||||
{
|
||||
throw new ArgumentException($"Emitters {j} and {i} share id {id} with different kinds.", nameof(emitters));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -247,6 +352,140 @@ namespace Lodur
|
|||
}
|
||||
|
||||
Load = 1.0 - quiet;
|
||||
SensePercepts(x, y, z, emitters, gain);
|
||||
}
|
||||
|
||||
// A thing is sensed when it has an id, the gain is above zero and the sense point is strictly inside the emitter's radius.
|
||||
private void SensePercepts(float x, float y, float z, ReadOnlySpan<Emitter> emitters, double gain)
|
||||
{
|
||||
for (var s = 0; s < _pId.Length; s++)
|
||||
{
|
||||
if (_pId[s] == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
_pSensed[s] = false;
|
||||
_pSalience[s] = 0.0;
|
||||
_pSince[s] += _dt;
|
||||
}
|
||||
|
||||
if (gain > 0.0)
|
||||
{
|
||||
// Things already remembered first, so a newcomer can never evict one that is sensed this tick.
|
||||
for (var i = 0; i < emitters.Length; i++)
|
||||
{
|
||||
var slot = Reaches(emitters[i], x, y, z) ? SlotOf(emitters[i].Id) : -1;
|
||||
if (slot >= 0)
|
||||
{
|
||||
Touch(slot, emitters[i], x, y, z, gain);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (var s = 0; s < _pId.Length; s++)
|
||||
{
|
||||
if (_pId[s] != 0 && !_pSensed[s] && _pSince[s] > _memorySeconds)
|
||||
{
|
||||
_pId[s] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (gain > 0.0)
|
||||
{
|
||||
for (var i = 0; i < emitters.Length; i++)
|
||||
{
|
||||
if (!Reaches(emitters[i], x, y, z))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var slot = SlotOf(emitters[i].Id);
|
||||
if (slot < 0)
|
||||
{
|
||||
slot = Admit(emitters[i].Id, emitters[i].Kind);
|
||||
}
|
||||
|
||||
if (slot >= 0)
|
||||
{
|
||||
Touch(slot, emitters[i], x, y, z, gain);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static bool Reaches(in Emitter e, float x, float y, float z)
|
||||
{
|
||||
if (e.Id == 0 || !(e.Radius > 0f))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
double dx = x - e.X, dy = y - e.Y, dz = z - e.Z;
|
||||
return Math.Sqrt(dx * dx + dy * dy + dz * dz) < e.Radius;
|
||||
}
|
||||
|
||||
private int SlotOf(ulong id)
|
||||
{
|
||||
for (var s = 0; s < _pId.Length; s++)
|
||||
{
|
||||
if (_pId[s] == id)
|
||||
{
|
||||
return s;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The lowest free slot, else the unsensed slot longest unsensed (ties lowest slot); -1 when every slot was sensed this tick.
|
||||
private int Admit(ulong id, uint kind)
|
||||
{
|
||||
var slot = -1;
|
||||
for (var s = 0; s < _pId.Length; s++)
|
||||
{
|
||||
if (_pId[s] == 0)
|
||||
{
|
||||
slot = s;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (slot < 0)
|
||||
{
|
||||
for (var s = 0; s < _pId.Length; s++)
|
||||
{
|
||||
if (!_pSensed[s] && (slot < 0 || _pSince[s] > _pSince[slot]))
|
||||
{
|
||||
slot = s;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (slot >= 0)
|
||||
{
|
||||
_pId[slot] = id;
|
||||
_pKind[slot] = kind;
|
||||
_pSensed[slot] = false;
|
||||
_pSalience[slot] = 0.0;
|
||||
_pSince[slot] = 0.0;
|
||||
}
|
||||
|
||||
return slot;
|
||||
}
|
||||
|
||||
// The first emitter of the tick fills the slot; a later one replaces it only by reaching him strictly harder.
|
||||
private void Touch(int slot, in Emitter e, float x, float y, float z, double gain)
|
||||
{
|
||||
var salience = e.Received(x, y, z) * gain;
|
||||
if (!_pSensed[slot] || salience > _pSalience[slot])
|
||||
{
|
||||
_pX[slot] = e.X; _pY[slot] = e.Y; _pZ[slot] = e.Z;
|
||||
_pSalience[slot] = salience;
|
||||
}
|
||||
|
||||
_pSensed[slot] = true;
|
||||
_pSince[slot] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
248
tests/Lodur.Core.Tests/PerceptTests.cs
Normal file
248
tests/Lodur.Core.Tests/PerceptTests.cs
Normal file
|
|
@ -0,0 +1,248 @@
|
|||
using System;
|
||||
using Lodur;
|
||||
|
||||
namespace Lodur.Core.Tests;
|
||||
|
||||
public sealed class PerceptTests
|
||||
{
|
||||
private const double Dt = 0.01;
|
||||
private static readonly float[] Zero = { 0f };
|
||||
private static readonly BoxStimulus[] NoBoxes = Array.Empty<BoxStimulus>();
|
||||
|
||||
private static Perception Make(int capacity = 4, double memory = 0.05, NerveParameters? senses = null) =>
|
||||
new Perception(new NerveParameters(), Dt, 1, senses ?? new NerveParameters { Threshold = 0.05, SensitisationGrowth = 0.0, ConductionDelaySeconds = 0.0 },
|
||||
0, new PerceptParameters { Capacity = capacity, MemorySeconds = memory });
|
||||
|
||||
private static void Sense(Perception p, Emitter[] emitters, double gain = 1.0) => p.Sense(Zero, Zero, Zero, NoBoxes, emitters, gain);
|
||||
|
||||
private static Emitter At(ulong id, float x = 0f, float radius = 2f, double intensity = 0.5, uint kind = 7, Channel channel = Channel.Audio) =>
|
||||
new Emitter(channel, x, 0f, 0f, radius, intensity, id, kind);
|
||||
|
||||
private static int SlotOf(Perception p, ulong id)
|
||||
{
|
||||
for (var s = 0; s < p.PerceptCount; s++)
|
||||
{
|
||||
if (p.Percept(s).Id == id) return s;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
private static int Occupied(Perception p)
|
||||
{
|
||||
var n = 0;
|
||||
for (var s = 0; s < p.PerceptCount; s++)
|
||||
{
|
||||
if (p.Percept(s).Id != 0) n++;
|
||||
}
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_anonymous_emitter_loads_the_nerve_and_makes_no_percept()
|
||||
{
|
||||
var anonymous = Make();
|
||||
var named = Make();
|
||||
var emitters = new[] { At(0, 0.5f, 2f, 0.6), At(0, 1f, 3f, 0.4, channel: Channel.Visual) };
|
||||
var withIds = new[] { At(11, 0.5f, 2f, 0.6), At(12, 1f, 3f, 0.4, channel: Channel.Visual) };
|
||||
for (var i = 0; i < 30; i++)
|
||||
{
|
||||
Sense(anonymous, emitters);
|
||||
Sense(named, withIds);
|
||||
Assert.Equal(named.Load, anonymous.Load);
|
||||
Assert.Equal(named.ChannelAfferent(Channel.Audio), anonymous.ChannelAfferent(Channel.Audio));
|
||||
}
|
||||
|
||||
Assert.True(anonymous.Load > 0.0);
|
||||
Assert.Equal(0, Occupied(anonymous));
|
||||
Assert.Equal(2, Occupied(named));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_thing_is_sensed_strictly_inside_its_radius_and_only_with_gain()
|
||||
{
|
||||
var p = Make();
|
||||
Sense(p, new[] { At(1, x: 2f, radius: 2f) }); // exactly at the radius
|
||||
Assert.Equal(0, Occupied(p));
|
||||
Sense(p, new[] { At(1, x: 1.999f, radius: 2f) });
|
||||
Assert.Equal(1, Occupied(p));
|
||||
Assert.Equal(0.0, p.Percept(SlotOf(p, 1)).SecondsSinceSensed);
|
||||
|
||||
var deaf = Make();
|
||||
Sense(deaf, new[] { At(1) }, gain: 0.0);
|
||||
Assert.Equal(0, Occupied(deaf));
|
||||
Sense(deaf, new[] { At(1) }, gain: 0.25);
|
||||
Assert.Equal(1, Occupied(deaf));
|
||||
|
||||
var noRadius = Make();
|
||||
Sense(noRadius, new[] { At(1, radius: 0f) });
|
||||
Assert.Equal(0, Occupied(noRadius));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Salience_is_the_largest_received_times_the_gain_and_position_follows_it()
|
||||
{
|
||||
var p = Make();
|
||||
// Received: 0.5 * (1 - 1/2) = 0.25 at x=1; 0.5 * (1 - 0.5/2) = 0.375 at x=0.5; a tied second emitter at x=-0.5 must not take the position.
|
||||
Sense(p, new[] { At(5, 1f, 2f, 0.5, kind: 9), At(5, 0.5f, 2f, 0.5, kind: 9), At(5, -0.5f, 2f, 0.5, kind: 9), At(0, 0f, 9f, 9.0) }, gain: 0.5);
|
||||
var percept = p.Percept(SlotOf(p, 5));
|
||||
Assert.Equal(1, Occupied(p));
|
||||
Assert.Equal(0.375 * 0.5, percept.Salience, 12);
|
||||
Assert.Equal(0.5f, percept.X);
|
||||
Assert.Equal(9u, percept.Kind);
|
||||
|
||||
// A thing that is not sensed this tick keeps its place and has no salience.
|
||||
Sense(p, Array.Empty<Emitter>());
|
||||
Assert.Equal(0.0, p.Percept(SlotOf(p, 5)).Salience);
|
||||
Assert.Equal(0.5f, p.Percept(SlotOf(p, 5)).X);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_unsensed_percept_ages_and_is_forgotten_after_the_memory_time()
|
||||
{
|
||||
var p = Make(memory: Dt);
|
||||
Sense(p, new[] { At(3) });
|
||||
Sense(p, Array.Empty<Emitter>());
|
||||
var s = SlotOf(p, 3);
|
||||
Assert.True(s >= 0); // since == memory is still remembered
|
||||
Assert.Equal(Dt, p.Percept(s).SecondsSinceSensed);
|
||||
Sense(p, Array.Empty<Emitter>());
|
||||
Assert.Equal(-1, SlotOf(p, 3)); // since > memory frees it
|
||||
|
||||
var instant = Make(memory: 0.0);
|
||||
Sense(instant, new[] { At(3) });
|
||||
Assert.Equal(1, Occupied(instant));
|
||||
Sense(instant, Array.Empty<Emitter>());
|
||||
Assert.Equal(0, Occupied(instant));
|
||||
|
||||
// Out of range or deaf, a thing ages the same; sensed again it returns to zero.
|
||||
var q = Make(memory: 0.05);
|
||||
Sense(q, new[] { At(3) });
|
||||
Sense(q, new[] { At(3, x: 5f) });
|
||||
Sense(q, new[] { At(3) }, gain: 0.0);
|
||||
Assert.Equal(2 * Dt, q.Percept(SlotOf(q, 3)).SecondsSinceSensed, 12);
|
||||
Sense(q, new[] { At(3) });
|
||||
Assert.Equal(0.0, q.Percept(SlotOf(q, 3)).SecondsSinceSensed);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_full_table_evicts_the_longest_unsensed_and_drops_a_newcomer_when_all_are_sensed()
|
||||
{
|
||||
var p = Make(capacity: 3, memory: 1.0);
|
||||
Sense(p, new[] { At(1), At(2), At(3) });
|
||||
Assert.Equal(0, SlotOf(p, 1));
|
||||
Assert.Equal(1, SlotOf(p, 2));
|
||||
Assert.Equal(2, SlotOf(p, 3));
|
||||
|
||||
// 2 and 3 are sensed on, 1 goes unsensed: the newcomer takes slot 0.
|
||||
Sense(p, new[] { At(2), At(3), At(4) });
|
||||
Assert.Equal(0, SlotOf(p, 4));
|
||||
Assert.Equal(1, SlotOf(p, 2));
|
||||
Assert.Equal(2, SlotOf(p, 3));
|
||||
Assert.Equal(-1, SlotOf(p, 1));
|
||||
|
||||
// 3 has gone unsensed longest (two ticks to 2's one): it is the one evicted, and the others keep their slots.
|
||||
Sense(p, new[] { At(2), At(4) });
|
||||
Sense(p, new[] { At(4), At(6) });
|
||||
Assert.Equal(2, SlotOf(p, 6));
|
||||
Assert.Equal(-1, SlotOf(p, 3));
|
||||
Assert.Equal(1, SlotOf(p, 2));
|
||||
Assert.Equal(0, SlotOf(p, 4));
|
||||
|
||||
// Every slot sensed this tick: the newcomer is dropped, and it stays dropped for every emitter of its id.
|
||||
var full = Make(capacity: 2, memory: 1.0);
|
||||
Sense(full, new[] { At(1), At(2), At(8), At(8, 0.5f) });
|
||||
Assert.Equal(-1, SlotOf(full, 8));
|
||||
Assert.Equal(0, SlotOf(full, 1));
|
||||
Assert.Equal(1, SlotOf(full, 2));
|
||||
|
||||
// Equal time unsensed: the lowest slot goes.
|
||||
var tie = Make(capacity: 2, memory: 1.0);
|
||||
Sense(tie, new[] { At(1), At(2) });
|
||||
Sense(tie, new[] { At(9) });
|
||||
Assert.Equal(0, SlotOf(tie, 9));
|
||||
Assert.Equal(1, SlotOf(tie, 2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Sense_refuses_a_phantom_id_and_a_kind_disagreement_before_stepping()
|
||||
{
|
||||
var p = Make();
|
||||
Sense(p, new[] { At(1, intensity: 0.6) });
|
||||
var load = p.Load;
|
||||
var since = p.Percept(0).SecondsSinceSensed;
|
||||
var afferent = p.ChannelStimulus(Channel.Audio);
|
||||
|
||||
Assert.Throws<ArgumentException>(() => Sense(p, new[] { At(1), At(Perception.PhantomIdBit | 2UL) }));
|
||||
Assert.Throws<ArgumentException>(() => Sense(p, new[] { At(ulong.MaxValue) }));
|
||||
Assert.Throws<ArgumentException>(() => Sense(p, new[] { At(1, kind: 7), At(2), At(1, kind: 8) }));
|
||||
Assert.Equal(load, p.Load);
|
||||
Assert.Equal(since, p.Percept(0).SecondsSinceSensed);
|
||||
Assert.Equal(afferent, p.ChannelStimulus(Channel.Audio));
|
||||
|
||||
// The state did not step: the next good tick is the second, not the fourth.
|
||||
Sense(p, Array.Empty<Emitter>());
|
||||
Assert.Equal(Dt, p.Percept(0).SecondsSinceSensed);
|
||||
|
||||
// Anonymous emitters may share kinds freely, and one id may speak on several channels.
|
||||
Sense(p, new[] { At(0, kind: 1), At(0, kind: 2), At(4, kind: 3), At(4, kind: 3, channel: Channel.Visual) });
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Percept_parameters_are_validated()
|
||||
{
|
||||
static void Build(PerceptParameters pp) =>
|
||||
_ = new Perception(new NerveParameters(), Dt, 1, new NerveParameters(), 0, pp);
|
||||
|
||||
Build(new PerceptParameters { Capacity = 1, MemorySeconds = 0.0 });
|
||||
Build(new PerceptParameters { Capacity = 256 });
|
||||
Assert.Throws<ArgumentException>(() => Build(new PerceptParameters { Capacity = 0 }));
|
||||
Assert.Throws<ArgumentException>(() => Build(new PerceptParameters { Capacity = 257 }));
|
||||
Assert.Throws<ArgumentException>(() => Build(new PerceptParameters { MemorySeconds = -0.001 }));
|
||||
Assert.Throws<ArgumentException>(() => Build(new PerceptParameters { MemorySeconds = double.NaN }));
|
||||
Assert.Throws<ArgumentException>(() => Build(new PerceptParameters { MemorySeconds = double.PositiveInfinity }));
|
||||
Assert.Equal(16, new PerceptParameters().Capacity);
|
||||
Assert.Equal(5.0, new PerceptParameters().MemorySeconds);
|
||||
|
||||
// No senses, no percepts, whatever the parameters.
|
||||
var deaf = new Perception(new NerveParameters(), Dt, 1);
|
||||
Assert.Equal(0, deaf.PerceptCount);
|
||||
Assert.Equal(4, Make().PerceptCount);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void Sensing_with_percepts_allocates_nothing()
|
||||
{
|
||||
var p = Make(capacity: 4, memory: 0.05);
|
||||
var near = new[] { At(1), At(2), At(3), At(4), At(5), At(5, 0.5f), At(0, 0.2f) };
|
||||
var far = new[] { At(1, x: 9f), At(6) };
|
||||
for (var i = 0; i < 50; i++) Sense(p, i % 2 == 0 ? near : far); // warm, and churn the table
|
||||
var before = GC.GetAllocatedBytesForCurrentThread();
|
||||
for (var i = 0; i < 10000; i++) Sense(p, i % 3 == 0 ? near : far);
|
||||
Assert.Equal(0L, GC.GetAllocatedBytesForCurrentThread() - before);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_actor_passes_percepts_through()
|
||||
{
|
||||
var nerve = new NerveParameters();
|
||||
var senses = new NerveParameters { SensitisationGrowth = 0.0, ConductionDelaySeconds = 0.0 };
|
||||
var actor = new Actor(Dt, 1, nerve, new WithdrawalParameters(), new DriveParameters(), PainBoxBrain.Make(0.5),
|
||||
senses, 0, new PerceptParameters { Capacity = 3, MemorySeconds = 1.0 });
|
||||
Assert.Equal(3, actor.PerceptCount);
|
||||
|
||||
var strengths = new float[2];
|
||||
var targets = new Quat[2];
|
||||
actor.Tick(Zero, Zero, Zero, NoBoxes, strengths, targets, new[] { At(42, 0.5f, 2f, 0.8, kind: 5) });
|
||||
var seen = actor.Percept(0);
|
||||
Assert.Equal(42UL, seen.Id);
|
||||
Assert.Equal(5u, seen.Kind);
|
||||
Assert.Equal(0.5f, seen.X);
|
||||
Assert.True(seen.Salience > 0.0);
|
||||
Assert.Equal(0UL, actor.Percept(1).Id);
|
||||
|
||||
var deaf = new Actor(Dt, 1, nerve, new WithdrawalParameters(), new DriveParameters(), PainBoxBrain.Make(0.5));
|
||||
Assert.Equal(0, deaf.PerceptCount);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue