Planner: a step that gives its This reach is a move; other things are in reach after it only if near it
Regress refuses a move that leaves a needed reach literal wrong and meets the ones it makes right; Meets broadens a reach literal to every This near its thing. The forward oracle applies the same rule from three x positions; reach literals are drawn more often so the rule changes 69 of 4000 worlds. A verb that also gives reach to a Tool keeps that give: gives stand over the derived reach. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6f6bfe91a9
commit
df92dce668
3 changed files with 162 additions and 14 deletions
|
|
@ -72,6 +72,10 @@ namespace Lodur
|
|||
private readonly uint[]?[] _thisKinds; // the kinds a verb's target may be; null accepts any
|
||||
private readonly uint[]?[] _toolKinds; // the kinds a verb's second thing may be; null when it takes none
|
||||
private readonly ulong[] _givenBits; // per verb, bit i set when one of its gives is on bit i of any subject
|
||||
private readonly bool[] _moves; // per verb, a This give of bit 63 true: the step brings its This into reach
|
||||
private readonly float[] _px; // the slots' remembered positions, copied per call
|
||||
private readonly float[] _py;
|
||||
private readonly float[] _pz;
|
||||
private readonly int _words;
|
||||
private readonly int _capacity;
|
||||
private readonly double _reach;
|
||||
|
|
@ -118,6 +122,7 @@ namespace Lodur
|
|||
_thisKinds = new uint[verbs.Count][];
|
||||
_toolKinds = new uint[verbs.Count][];
|
||||
_givenBits = new ulong[verbs.Count];
|
||||
_moves = new bool[verbs.Count];
|
||||
for (var v = 0; v < _verbs.Length; v++)
|
||||
{
|
||||
var verb = verbs[v] ?? throw new ArgumentException($"Verb {v} is null.", nameof(verbs));
|
||||
|
|
@ -130,6 +135,7 @@ namespace Lodur
|
|||
foreach (var g in _gives[v])
|
||||
{
|
||||
_givenBits[v] |= 1UL << g.Bit;
|
||||
_moves[v] |= g.Subject == FactSubject.This && g.Bit == 63 && g.Value;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -140,6 +146,9 @@ namespace Lodur
|
|||
_facts = new ulong[capacity + 1];
|
||||
_slotId = new ulong[capacity];
|
||||
_slotKind = new uint[capacity];
|
||||
_px = new float[capacity];
|
||||
_py = new float[capacity];
|
||||
_pz = new float[capacity];
|
||||
_set = new int[(maxDepth + 1) * _stride];
|
||||
_setCount = new int[maxDepth + 1];
|
||||
_stepVerb = new int[Math.Max(1, maxDepth)];
|
||||
|
|
@ -163,6 +172,7 @@ namespace Lodur
|
|||
/// 0 when the want already holds, else the length of the shallowest plan of at most <paramref name="depth"/> steps, written into
|
||||
/// <paramref name="plan"/> in execution order, else -1. The want is about self or about the percept in <paramref name="targetSlot"/>.
|
||||
/// A thing's facts are those it was last sensed with, bit 63 replaced by whether it is within reach of the point.
|
||||
/// A step that gives its This reach (bit 63 true) is a move: after it, another thing is in reach exactly when it is within reach of that This.
|
||||
/// <paramref name="budget"/> is the most groundings the call may regress, at least 1; a call that spends it returns -1 and sets <see cref="LastExhausted"/>.
|
||||
/// </summary>
|
||||
public int Plan(Perception perception, ulong selfFacts, float ox, float oy, float oz, int targetSlot, Fact want, int depth, Span<PlanStep> plan, int budget = NodeBudget)
|
||||
|
|
@ -182,6 +192,9 @@ namespace Lodur
|
|||
var reachSquared = _reach * _reach;
|
||||
ids.CopyTo(_slotId);
|
||||
kinds.CopyTo(_slotKind);
|
||||
xs.CopyTo(_px);
|
||||
ys.CopyTo(_py);
|
||||
zs.CopyTo(_pz);
|
||||
for (var s = 0; s < _capacity; s++)
|
||||
{
|
||||
double dx = xs[s] - ox, dy = ys[s] - oy, dz = zs[s] - oz;
|
||||
|
|
@ -336,6 +349,22 @@ namespace Lodur
|
|||
for (var i = 0; i < n; i++)
|
||||
{
|
||||
var l = _set[cur + i];
|
||||
if (_moves[v] && gf.Subject == FactSubject.This && gf.Bit == 63 && gf.Value && ((l >> 1) & 63) == 63 && (l >> 7) > 0)
|
||||
{
|
||||
// A move: the literal on slot o holds after it for every This slot a with o near a exactly as the literal says (a == o is near).
|
||||
var o = (l >> 7) - 1;
|
||||
for (var a = 0; a < _capacity; a++)
|
||||
{
|
||||
if (_slotId[a] != 0 && Near(o, a) == ((l & 1) != 0))
|
||||
{
|
||||
_meetsThis[m + (a >> 6)] |= 1UL << (a & 63);
|
||||
any = true;
|
||||
}
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if (((l >> 1) & 63) != gf.Bit || (l & 1) != (gf.Value ? 1 : 0))
|
||||
{
|
||||
continue;
|
||||
|
|
@ -424,6 +453,17 @@ namespace Lodur
|
|||
given = Ground(gives[g], a, b) == l;
|
||||
}
|
||||
|
||||
if (!given && _moves[v] && ((l >> 1) & 63) == 63 && (l >> 7) > 0 && (l >> 7) - 1 != a)
|
||||
{
|
||||
// After a move to a, every other thing is in reach exactly when it is near a: that literal is met or the step is refused.
|
||||
if (Near((l >> 7) - 1, a) != ((l & 1) != 0))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
given = true;
|
||||
}
|
||||
|
||||
if (!given)
|
||||
{
|
||||
_set[next + count++] = l;
|
||||
|
|
@ -457,6 +497,13 @@ namespace Lodur
|
|||
return true;
|
||||
}
|
||||
|
||||
// The one reach test: squared distance of remembered positions against reach squared. The start state applies it to the observer point.
|
||||
private bool Near(int o, int a)
|
||||
{
|
||||
double dx = _px[o] - _px[a], dy = _py[o] - _py[a], dz = _pz[o] - _pz[a];
|
||||
return dx * dx + dy * dy + dz * dz <= _reach * _reach;
|
||||
}
|
||||
|
||||
private bool Holds(int lit) => (int)((_facts[lit >> 7] >> ((lit >> 1) & 63)) & 1UL) == (lit & 1);
|
||||
|
||||
private static int Lit(int thing, int bit, bool value) => (thing << 7) | (bit << 1) | (value ? 1 : 0);
|
||||
|
|
|
|||
|
|
@ -32,7 +32,7 @@ public sealed class PlannerEquivalenceTests
|
|||
|
||||
private static Fact Fact(FactSubject s, int bit, bool v) => new(s, bit, v);
|
||||
|
||||
private static Fact[] RandomFacts(Random rng, int count, bool hasTool, bool selfAllowed)
|
||||
private static Fact[] RandomFacts(Random rng, int count, bool hasTool, bool selfAllowed, int reachOneIn)
|
||||
{
|
||||
var chosen = new List<Fact>();
|
||||
for (var i = 0; i < count; i++)
|
||||
|
|
@ -43,7 +43,7 @@ public sealed class PlannerEquivalenceTests
|
|||
subject = FactSubject.This;
|
||||
}
|
||||
|
||||
var bit = subject != FactSubject.Self && rng.Next(4) == 0 ? Reach : rng.Next(4);
|
||||
var bit = subject != FactSubject.Self && rng.Next(reachOneIn) == 0 ? Reach : rng.Next(4);
|
||||
if (chosen.Any(f => f.Subject == subject && f.Bit == bit))
|
||||
{
|
||||
continue;
|
||||
|
|
@ -81,7 +81,7 @@ public sealed class PlannerEquivalenceTests
|
|||
{
|
||||
var kind = (uint)(1 + rng.Next(3));
|
||||
var facts = (ulong)rng.Next(16);
|
||||
var x = rng.Next(2) == 0 ? 0.5f : 5f;
|
||||
var x = rng.Next(3) switch { 0 => 0.5f, 1 => 1.2f, _ => 5f };
|
||||
emitters.Add(new Emitter(Channel.Visual, x, 0f, 0f, 100f, 1.0, (ulong)(10 + i), kind, facts));
|
||||
}
|
||||
|
||||
|
|
@ -108,8 +108,8 @@ public sealed class PlannerEquivalenceTests
|
|||
Id = "v" + v,
|
||||
This = RandomFilter(rng, 60),
|
||||
Tool = tool,
|
||||
Needs = RandomFacts(rng, rng.Next(4), tool is not null, true),
|
||||
Gives = RandomFacts(rng, 1 + rng.Next(3), tool is not null, false).DefaultIfEmpty(Fact(FactSubject.This, 0, true)).ToArray(),
|
||||
Needs = RandomFacts(rng, rng.Next(4), tool is not null, true, 3),
|
||||
Gives = RandomFacts(rng, 1 + rng.Next(3), tool is not null, false, 4).DefaultIfEmpty(Fact(FactSubject.This, 0, true)).ToArray(),
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -124,7 +124,7 @@ public sealed class PlannerEquivalenceTests
|
|||
else
|
||||
{
|
||||
w.WantThing = 1 + occupied[rng.Next(occupied.Length)];
|
||||
w.WantBit = rng.Next(4) == 0 ? Reach : rng.Next(4);
|
||||
w.WantBit = rng.Next(2) == 0 ? Reach : rng.Next(4);
|
||||
}
|
||||
|
||||
w.WantValue = rng.Next(2) == 0;
|
||||
|
|
@ -164,7 +164,16 @@ public sealed class PlannerEquivalenceTests
|
|||
|
||||
private static int ThingOf(Fact f, Step s) => f.Subject == FactSubject.Self ? 0 : 1 + (f.Subject == FactSubject.This ? s.This : s.Tool);
|
||||
|
||||
private static ulong[]? Apply(World w, ulong[] state, Step s)
|
||||
// The reach test of the start state and of a move alike: squared distance of the remembered positions against reach squared.
|
||||
private static bool Near(World w, int a, int b)
|
||||
{
|
||||
Percept pa = w.Perception.Percept(a), pb = w.Perception.Percept(b);
|
||||
double dx = pa.X - pb.X, dy = pa.Y - pb.Y, dz = pa.Z - pb.Z;
|
||||
return dx * dx + dy * dy + dz * dz <= 1.0;
|
||||
}
|
||||
|
||||
// moves: a verb with a This give of reach true takes every other thing out of reach unless it is near the verb's This.
|
||||
private static ulong[]? Apply(World w, ulong[] state, Step s, bool moves = true)
|
||||
{
|
||||
var verb = w.Verbs[s.Verb];
|
||||
foreach (var n in verb.Needs)
|
||||
|
|
@ -176,6 +185,18 @@ public sealed class PlannerEquivalenceTests
|
|||
}
|
||||
|
||||
var next = (ulong[])state.Clone();
|
||||
// The move first, then the verb's own gives: a give of reach on a thing is a literal and stands.
|
||||
if (moves && verb.Gives.Any(g => g.Subject == FactSubject.This && g.Bit == Reach && g.Value))
|
||||
{
|
||||
for (var o = 0; o < Slots; o++)
|
||||
{
|
||||
if (o != s.This && w.Perception.Percept(o).Id != 0)
|
||||
{
|
||||
next[o + 1] = Near(w, o, s.This) ? next[o + 1] | Perception.InReachFact : next[o + 1] & ~Perception.InReachFact;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach (var g in verb.Gives)
|
||||
{
|
||||
var thing = ThingOf(g, s);
|
||||
|
|
@ -188,7 +209,7 @@ public sealed class PlannerEquivalenceTests
|
|||
private static string Key(ulong[] state) => string.Join(",", state);
|
||||
|
||||
// The shallowest plan length by breadth-first search over states, or -1 past the depth.
|
||||
private static int Shallowest(World w, List<Step> all)
|
||||
private static int Shallowest(World w, List<Step> all, bool moves = true)
|
||||
{
|
||||
var frontier = new List<ulong[]> { w.Start };
|
||||
var seen = new HashSet<string> { Key(w.Start) };
|
||||
|
|
@ -204,7 +225,7 @@ public sealed class PlannerEquivalenceTests
|
|||
{
|
||||
foreach (var step in all)
|
||||
{
|
||||
var n = Apply(w, state, step);
|
||||
var n = Apply(w, state, step, moves);
|
||||
if (n is not null && seen.Add(Key(n)))
|
||||
{
|
||||
next.Add(n);
|
||||
|
|
@ -219,7 +240,7 @@ public sealed class PlannerEquivalenceTests
|
|||
}
|
||||
|
||||
// The first plan of exactly n steps in the spec's order: the last step's (verb, This, Tool) ascending first, then the step before it.
|
||||
private static Step[]? First(World w, List<Step> all, int n)
|
||||
private static Step[]? First(World w, List<Step> all, int n, bool moves = true)
|
||||
{
|
||||
var chosen = new Step[n];
|
||||
return Choose(n) ? chosen : null;
|
||||
|
|
@ -232,7 +253,7 @@ public sealed class PlannerEquivalenceTests
|
|||
var state = w.Start;
|
||||
foreach (var step in chosen)
|
||||
{
|
||||
var next = Apply(w, state, step);
|
||||
var next = Apply(w, state, step, moves);
|
||||
if (next is null)
|
||||
{
|
||||
return false;
|
||||
|
|
@ -257,23 +278,32 @@ public sealed class PlannerEquivalenceTests
|
|||
}
|
||||
}
|
||||
|
||||
private static string Dump(World w, PlanStep[] plan, int n) =>
|
||||
Environment.NewLine + string.Join(Environment.NewLine, w.Verbs.Select(v => $"{v.Id} this={(v.This is null ? "-" : string.Join(",", v.This.Kinds))} tool={(v.Tool is null ? "-" : string.Join(",", v.Tool.Kinds))} needs={string.Join(",", v.Needs.Select(f => $"{f.Subject}{f.Bit}{f.Value}"))} gives={string.Join(",", v.Gives.Select(f => $"{f.Subject}{f.Bit}{f.Value}"))}"))
|
||||
+ $"{Environment.NewLine}start={Key(w.Start)} kinds={string.Join(",", w.Kind)} want={w.WantThing}/{w.WantBit}/{w.WantValue} plan={string.Join(" ", plan.Take(Math.Max(n, 0)))} xs={string.Join(",", Enumerable.Range(0, Slots).Select(q => w.Perception.Percept(q).X))}";
|
||||
|
||||
[Fact]
|
||||
public void The_plan_is_sound_complete_and_shallowest_against_a_forward_search_on_4000_random_worlds()
|
||||
{
|
||||
var rng = new Random(20261125);
|
||||
int solved = 0, none = 0, ordered = 0;
|
||||
int solved = 0, none = 0, ordered = 0, changed = 0;
|
||||
var plan = new PlanStep[Depth];
|
||||
for (var i = 0; i < 4000; i++)
|
||||
{
|
||||
var w = Generate(rng);
|
||||
var all = Groundings(w).ToList();
|
||||
var expected = Shallowest(w, all);
|
||||
if (expected != Shallowest(w, all, moves: false))
|
||||
{
|
||||
changed++;
|
||||
}
|
||||
|
||||
var planner = new Planner(w.Verbs, Slots, Depth, 1.0);
|
||||
var targetSlot = w.WantThing == 0 ? 0 : w.WantThing - 1;
|
||||
var want = new Fact(w.WantThing == 0 ? FactSubject.Self : FactSubject.This, w.WantBit, w.WantValue);
|
||||
var n = planner.Plan(w.Perception, w.SelfFacts, 0f, 0f, 0f, targetSlot, want, Depth, plan);
|
||||
Assert.True(n == expected, $"world {i}: planner {n}, forward search {expected}");
|
||||
Assert.True(planner.LastNodes <= Planner.NodeBudget, $"world {i} spent the budget");
|
||||
Assert.True(n == expected, $"world {i}: planner {n}, forward search {expected}{Dump(w, plan, n)}");
|
||||
Assert.True(planner.LastNodes <= Planner.NodeBudget, $"world {i} spent the budget{Dump(w, plan, n)}");
|
||||
|
||||
if (n < 0)
|
||||
{
|
||||
|
|
@ -300,9 +330,14 @@ public sealed class PlannerEquivalenceTests
|
|||
ordered++;
|
||||
var first = First(w, all, n)!;
|
||||
Assert.True(first.SequenceEqual(steps), $"world {i}: tie order, planner {string.Join(" ", steps)} against {string.Join(" ", first)}");
|
||||
if (expected == Shallowest(w, all, moves: false) && First(w, all, n, moves: false) is { } without && !without.SequenceEqual(first))
|
||||
{
|
||||
changed++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Assert.True(solved > 1000 && none > 500 && ordered > 100, $"the worlds are too one-sided: {solved} solved, {none} none, {ordered} ordered");
|
||||
Assert.True(changed > 50, $"the move rule changes the plan or verdict of only {changed} worlds");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -533,4 +533,70 @@ public sealed class PlannerTests
|
|||
Assert.Equal(4, catalogue.Plan(world, 0UL, 0f, 0f, 0f, SlotOf(world, 3), This(Off), 8, run));
|
||||
Assert.False(catalogue.LastExhausted);
|
||||
}
|
||||
|
||||
// The test world's verbs but for what a test needs: Approach with no needs and Use of a Switch with a Knife that needs both in reach.
|
||||
private static Verb[] Movers() => new[]
|
||||
{
|
||||
new Verb { Id = "Approach", This = Kinds(Switch, Knife), Gives = new[] { This(Reach) } },
|
||||
new Verb { Id = "Use", This = Kinds(Switch), Tool = Kinds(Knife), Needs = new[] { This(Reach), Tool(Reach) }, Gives = new[] { This(Cut) } },
|
||||
};
|
||||
|
||||
[Fact]
|
||||
public void A_move_takes_reach_of_far_things_away()
|
||||
{
|
||||
var planner = Build(Movers());
|
||||
var far = Make();
|
||||
Sense(far, Thing(1, Switch, 5f), Thing(2, Knife, -5f));
|
||||
Assert.Equal(-1, Run(planner, far, 1, This(Cut), 8).Length);
|
||||
Assert.False(planner.LastExhausted);
|
||||
|
||||
var close = Make();
|
||||
Sense(close, Thing(1, Switch, 5f), Thing(2, Knife, 5.5f));
|
||||
var (n, steps) = Run(planner, close, 1, This(Cut), 8);
|
||||
Assert.Equal(2, n);
|
||||
Expect(steps, (0, 1, 0), (1, 1, 2));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_move_brings_near_things_into_reach()
|
||||
{
|
||||
// The key is not a thing Approach accepts, but a switch beside it is.
|
||||
var planner = Build(new[] { new Verb { Id = "Approach", This = Kinds(Switch), Gives = new[] { This(Reach) } } });
|
||||
var p = Make();
|
||||
Sense(p, Thing(1, Key, 5f), Thing(2, Switch, 5.3f));
|
||||
var (n, steps) = Run(planner, p, 1, This(Reach), 4);
|
||||
Assert.Equal(1, n);
|
||||
Expect(steps, (0, 2, 0));
|
||||
|
||||
// Far from the key it brings nothing.
|
||||
var apart = Make();
|
||||
Sense(apart, Thing(1, Key, 5f), Thing(2, Switch, -5f));
|
||||
Assert.Equal(-1, Run(planner, apart, 1, This(Reach), 4).Length);
|
||||
|
||||
// And out of reach is wanted just the same: going to the switch takes the key out of reach.
|
||||
var kept = Make();
|
||||
Sense(kept, Thing(1, Key, 0.5f), Thing(2, Switch, 5f));
|
||||
(n, steps) = Run(planner, kept, 1, This(Reach, false), 4);
|
||||
Assert.Equal(1, n);
|
||||
Expect(steps, (0, 2, 0));
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_move_away_leaves_the_start_reach_behind()
|
||||
{
|
||||
var verbs = new[]
|
||||
{
|
||||
new Verb { Id = "Approach", This = Kinds(Switch, Knife), Gives = new[] { This(Reach) } },
|
||||
new Verb { Id = "SwitchOff", This = Kinds(Switch), Needs = new[] { This(Reach) }, Gives = new[] { This(Off) } },
|
||||
new Verb { Id = "Pry", This = Kinds(Knife), Tool = Kinds(Switch), Needs = new[] { Tool(Off), This(Reach) }, Gives = new[] { This(Cut) } },
|
||||
};
|
||||
var planner = Build(verbs);
|
||||
var p = Make();
|
||||
Sense(p, Thing(1, Knife, 0.5f), Thing(2, Switch, 5f));
|
||||
|
||||
// The knife is in reach at the start, but not after going to the switch: a second Approach.
|
||||
var (n, steps) = Run(planner, p, 1, This(Cut), 8);
|
||||
Assert.Equal(4, n);
|
||||
Expect(steps, (0, 2, 0), (1, 2, 0), (0, 1, 0), (2, 1, 2));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue