Select: pass 2 deepens across the want pairs (round k asks for exactly k steps, a bound per pair), so short plans are found first and one dead end cannot starve the budget
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
237f3346aa
commit
f2669847fe
3 changed files with 214 additions and 53 deletions
|
|
@ -328,7 +328,7 @@ namespace Lodur
|
|||
/// <summary>The factor on a pair's score when its goal's want has no plan within <see cref="PlanDepth"/> (or the think budget ran out): he goes at the target directly, in [0, 1], default 0.5 (a placeholder).</summary>
|
||||
public double DirectDiscount { get; init; } = 0.5;
|
||||
|
||||
/// <summary>The most groundings one Select may regress across all its plans, at least 1, default 4096: one budget per decision, spent on the best raw pairs first (lodur:question:bds-think-budget-per-decision).</summary>
|
||||
/// <summary>The most groundings one Select may regress across all its plans, at least 1, default 4096: one budget per decision, spent by deepening across the want pairs, short plans first (lodur:question:bds-think-budget-per-decision).</summary>
|
||||
public int ThinkBudget { get; init; } = Planner.NodeBudget;
|
||||
|
||||
/// <summary>How close a thing must be to be in reach, in meters, finite and above 0, default 0.6 (an arm's length, 0.575 m, as a placeholder).</summary>
|
||||
|
|
@ -371,9 +371,11 @@ namespace Lodur
|
|||
|
||||
/// <summary>
|
||||
/// <see cref="Select(in DriveState, Perception, float, float, float, Choice)"/> with the want gate. Pass 1 scores every pair raw (commitment applied) and offers
|
||||
/// the untargeted-by-want pairs; a want pair whose want holds is not offered, the rest wait in the Deliberation. Pass 2 takes them by raw, best first, plans each with what is
|
||||
/// left of <see cref="ThinkBudget"/>, and offers raw times the plan-length factor, or raw times the factor at PlanDepth + 1 times <see cref="DirectDiscount"/> when no plan was found
|
||||
/// or the budget is gone; it stops once no remaining raw can beat or tie-win the best (every factor is at most 1).
|
||||
/// the untargeted-by-want pairs; a want pair whose want holds is not offered, the rest wait in the Deliberation. Pass 2 deepens across the waiting pairs: round k takes them by
|
||||
/// bound (raw times the best factor any plan longer than k - 1 steps, or going direct, could earn), asks each for a plan of exactly k steps with what is left of <see cref="ThinkBudget"/>,
|
||||
/// and offers raw times the plan-length factor for one found; a pair with none of k steps waits for round k + 1, and one with no plan within PlanDepth (or the budget gone) is offered
|
||||
/// at raw times the factor at PlanDepth + 1 times <see cref="DirectDiscount"/>. A round ends when no remaining bound can beat or tie-win the best, so short plans are found
|
||||
/// first and one pair's dead end cannot starve the rest.
|
||||
/// </summary>
|
||||
internal Choice Select(in DriveState drive, Perception perception, float ox, float oy, float oz, Choice current, Deliberation? d)
|
||||
{
|
||||
|
|
@ -406,17 +408,36 @@ namespace Lodur
|
|||
}
|
||||
}
|
||||
|
||||
while (d is not null && d.TryPop(out var pair))
|
||||
if (d is not null)
|
||||
{
|
||||
if (best.Found && pair.Raw < best.Score)
|
||||
for (var k = PlanDepth == 0 ? 0 : 1; ; k++)
|
||||
{
|
||||
break;
|
||||
}
|
||||
while (d.TryPop(out var pair))
|
||||
{
|
||||
if (best.Found && pair.Bound < best.Score)
|
||||
{
|
||||
d.ClearRound();
|
||||
break;
|
||||
}
|
||||
|
||||
var goal = Goals[pair.Goal];
|
||||
var length = d.Plan(pair.Goal, goal.Want!.Value, pair.Slot);
|
||||
var factor = length > 0 ? goal.PlanFactor(length) : goal.PlanFactor(PlanDepth + 1) * DirectDiscount;
|
||||
Offer(pair.Goal, pair.Slot, pair.Target, pair.Raw * factor, pair.Current, ref best);
|
||||
var goal = Goals[pair.Goal];
|
||||
var length = d.PlanAt(pair.Goal, goal.Want!.Value, pair.Slot, k);
|
||||
if (length == 0)
|
||||
{
|
||||
pair.Bound = pair.Raw * d.BoundFactor(pair.Goal, k);
|
||||
d.Defer(pair);
|
||||
continue;
|
||||
}
|
||||
|
||||
var factor = length > 0 ? goal.PlanFactor(length) : goal.PlanFactor(PlanDepth + 1) * DirectDiscount;
|
||||
Offer(pair.Goal, pair.Slot, pair.Target, pair.Raw * factor, pair.Current, ref best);
|
||||
}
|
||||
|
||||
if (!d.NextRound())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return best.Found ? Pair(best.Goal, best.Target) : Pair(current.Goal, 0UL);
|
||||
|
|
|
|||
|
|
@ -3,19 +3,22 @@ using System.Collections.Generic;
|
|||
|
||||
namespace Lodur
|
||||
{
|
||||
/// <summary>A want pair held back for the planner's gate: its goal, percept slot (-1 for an untargeted goal), the percept's id, its raw score (commitment applied) and whether it is the current pair.</summary>
|
||||
/// <summary>A want pair held back for the planner's gate: its goal, percept slot (-1 for an untargeted goal), the percept's id, its raw score (commitment applied), the most it can still score (raw times the bound factor of the depth searched so far) and whether it is the current pair.</summary>
|
||||
internal struct WantPair
|
||||
{
|
||||
public int Goal;
|
||||
public int Slot;
|
||||
public ulong Target;
|
||||
public double Raw;
|
||||
public double Bound;
|
||||
public bool Current;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What one Actor thinks with while its Brain selects: the planner, the want pairs waiting for the gate (a max-heap by raw score, ties current, lowest goal, lowest slot),
|
||||
/// the think budget spent so far in this Select, and the plan of the last call. Owned by one Actor; allocates only in its constructor. Not thread-safe.
|
||||
/// What one Actor thinks with while its Brain selects: the planner, the want pairs waiting for the gate (a max-heap by bound, ties current, lowest goal, lowest slot),
|
||||
/// the pairs deferred to the next round of deepening, the bound table, and the think budget spent so far in this Select. Pass 2 deepens across pairs (Korf's iterative
|
||||
/// deepening, anytime): round k asks every live pair for a plan of exactly k steps, so short plans are found first and no pair starves the rest. Owned by one Actor;
|
||||
/// allocates only in its constructor. Not thread-safe.
|
||||
/// </summary>
|
||||
internal sealed class Deliberation
|
||||
{
|
||||
|
|
@ -33,6 +36,12 @@ namespace Lodur
|
|||
private float _oz;
|
||||
private int _count;
|
||||
private int _spent;
|
||||
private readonly int _depth;
|
||||
private readonly double[] _bound; // per goal g and searched depth k in 0..PlanDepth: the most a pair with no plan of k or fewer steps can score, as a factor on raw
|
||||
private readonly WantPair[] _next; // the pairs with no plan of this round's k steps, waiting for round k + 1
|
||||
private int _nextCount;
|
||||
private readonly int[] _selfDepth; // per goal this Select: the depth a Self want has been searched to
|
||||
private bool _gone; // this Select's budget is gone: every later pair goes direct
|
||||
|
||||
internal Deliberation(Brain brain, IReadOnlyList<Verb> verbs, int capacity)
|
||||
{
|
||||
|
|
@ -43,8 +52,29 @@ namespace Lodur
|
|||
_selfHeld = new sbyte[brain.Goals.Count];
|
||||
_selfLength = new int[brain.Goals.Count];
|
||||
_perception = null!;
|
||||
_depth = brain.PlanDepth;
|
||||
_next = new WantPair[_heap.Length];
|
||||
_selfDepth = new int[brain.Goals.Count];
|
||||
_bound = new double[brain.Goals.Count * (_depth + 1)];
|
||||
for (var g = 0; g < brain.Goals.Count; g++)
|
||||
{
|
||||
var direct = brain.Goals[g].PlanFactor(_depth + 1) * brain.DirectDiscount;
|
||||
for (var k = 0; k <= _depth; k++)
|
||||
{
|
||||
var b = direct;
|
||||
for (var x = k + 1; x <= _depth; x++)
|
||||
{
|
||||
b = Math.Max(b, brain.Goals[g].PlanFactor(x));
|
||||
}
|
||||
|
||||
_bound[g * (_depth + 1) + k] = b;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>The most a pair of <paramref name="goal"/> with no plan of <paramref name="k"/> or fewer steps can score, as a factor on its raw score. No monotonicity of the plan-length factor is assumed.</summary>
|
||||
internal double BoundFactor(int goal, int k) => _bound[goal * (_depth + 1) + k];
|
||||
|
||||
/// <summary>The actor's own facts this tick, supplied by the game (bit 63 never set).</summary>
|
||||
internal ulong SelfFacts { get; set; }
|
||||
|
||||
|
|
@ -63,10 +93,13 @@ namespace Lodur
|
|||
_oz = oz;
|
||||
_count = 0;
|
||||
_spent = 0;
|
||||
_nextCount = 0;
|
||||
_gone = false;
|
||||
for (var g = 0; g < _selfHeld.Length; g++)
|
||||
{
|
||||
_selfHeld[g] = -1;
|
||||
_selfLength[g] = Unplanned;
|
||||
_selfDepth[g] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -90,32 +123,89 @@ namespace Lodur
|
|||
_planner.Plan(_perception, SelfFacts, _ox, _oy, _oz, slot, want, 0, _plan, 1) == 0;
|
||||
|
||||
/// <summary>
|
||||
/// The plan length of a want pair that does not hold: n steps, or -1 to go at it direct (no plan within the depth, the call spent the rest of the budget, or none was left).
|
||||
/// A Self want is planned once per goal and shared by its pairs. Spends from the Select's one budget.
|
||||
/// Round <paramref name="k"/> of the deepening for a want pair that does not hold: n = k when the pair has a plan of exactly k steps (none shorter was found in an earlier round),
|
||||
/// 0 when it has none of k steps and k is below PlanDepth (defer to round k + 1), -1 to go at it direct (no plan within PlanDepth, PlanDepth is 0, or the budget is gone, and then
|
||||
/// it stays gone for the rest of the Select). A Self want is searched once per goal and depth and shared by its pairs. Spends from the Select's one budget.
|
||||
/// </summary>
|
||||
internal int Plan(int goal, Fact want, int slot)
|
||||
internal int PlanAt(int goal, Fact want, int slot, int k)
|
||||
{
|
||||
var self = want.Subject == FactSubject.Self;
|
||||
if (self && _selfLength[goal] != Unplanned)
|
||||
if (_depth == 0 || _gone)
|
||||
{
|
||||
return _selfLength[goal];
|
||||
return -1;
|
||||
}
|
||||
|
||||
var length = -1;
|
||||
var left = _brain.ThinkBudget - _spent;
|
||||
if (_brain.PlanDepth > 0 && left >= 1)
|
||||
var self = want.Subject == FactSubject.Self;
|
||||
if (self)
|
||||
{
|
||||
var n = _planner.Plan(_perception, SelfFacts, _ox, _oy, _oz, self ? 0 : slot, want, _brain.PlanDepth, _plan, left);
|
||||
_spent = Math.Min(_brain.ThinkBudget, _spent + _planner.LastNodes);
|
||||
length = n > 0 ? n : -1;
|
||||
if (_selfLength[goal] != Unplanned)
|
||||
{
|
||||
return _selfLength[goal];
|
||||
}
|
||||
|
||||
if (_selfDepth[goal] >= k)
|
||||
{
|
||||
return k == _depth ? -1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
var left = _brain.ThinkBudget - _spent;
|
||||
if (left < 1)
|
||||
{
|
||||
_gone = true;
|
||||
return -1;
|
||||
}
|
||||
|
||||
var n = _planner.Plan(_perception, SelfFacts, _ox, _oy, _oz, self ? 0 : slot, want, k, _plan, left, k);
|
||||
_spent = Math.Min(_brain.ThinkBudget, _spent + _planner.LastNodes);
|
||||
int result;
|
||||
if (n > 0)
|
||||
{
|
||||
result = n;
|
||||
}
|
||||
else if (_planner.LastExhausted)
|
||||
{
|
||||
_gone = true;
|
||||
result = -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
result = k == _depth ? -1 : 0;
|
||||
}
|
||||
|
||||
if (self)
|
||||
{
|
||||
_selfLength[goal] = length;
|
||||
_selfDepth[goal] = k;
|
||||
if (result != 0)
|
||||
{
|
||||
_selfLength[goal] = result;
|
||||
}
|
||||
}
|
||||
|
||||
return length;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>Holds a pair with no plan of this round's steps for the next round.</summary>
|
||||
internal void Defer(in WantPair pair) => _next[_nextCount++] = pair;
|
||||
|
||||
/// <summary>Drops every pair still waiting in this round: none of them can beat the best found.</summary>
|
||||
internal void ClearRound() => _count = 0;
|
||||
|
||||
/// <summary>Starts the next round with the deferred pairs; false when nothing was deferred.</summary>
|
||||
internal bool NextRound()
|
||||
{
|
||||
if (_nextCount == 0)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_count = 0;
|
||||
for (var i = 0; i < _nextCount; i++)
|
||||
{
|
||||
Push(_next[i]);
|
||||
}
|
||||
|
||||
_nextCount = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
|
@ -148,13 +238,14 @@ namespace Lodur
|
|||
}
|
||||
|
||||
internal void Add(int goal, int slot, ulong target, double raw, bool current)
|
||||
{
|
||||
Push(new WantPair { Goal = goal, Slot = slot, Target = target, Raw = raw, Bound = raw * BoundFactor(goal, 0), Current = current });
|
||||
}
|
||||
|
||||
private void Push(in WantPair pair)
|
||||
{
|
||||
var i = _count++;
|
||||
_heap[i].Goal = goal;
|
||||
_heap[i].Slot = slot;
|
||||
_heap[i].Target = target;
|
||||
_heap[i].Raw = raw;
|
||||
_heap[i].Current = current;
|
||||
_heap[i] = pair;
|
||||
while (i > 0)
|
||||
{
|
||||
var parent = (i - 1) >> 1;
|
||||
|
|
@ -168,7 +259,7 @@ namespace Lodur
|
|||
}
|
||||
}
|
||||
|
||||
/// <summary>The waiting want pair with the highest raw score (ties current, lowest goal, lowest slot).</summary>
|
||||
/// <summary>The waiting want pair with the highest bound (ties current, lowest goal, lowest slot).</summary>
|
||||
internal bool TryPop(out WantPair pair)
|
||||
{
|
||||
if (_count == 0)
|
||||
|
|
@ -202,7 +293,7 @@ namespace Lodur
|
|||
}
|
||||
|
||||
private static bool Before(in WantPair a, in WantPair b) =>
|
||||
a.Raw != b.Raw ? a.Raw > b.Raw
|
||||
a.Bound != b.Bound ? a.Bound > b.Bound
|
||||
: a.Current != b.Current ? a.Current
|
||||
: a.Goal != b.Goal ? a.Goal < b.Goal
|
||||
: a.Slot < b.Slot;
|
||||
|
|
|
|||
|
|
@ -209,36 +209,81 @@ public sealed class PlannerSelectTests
|
|||
}
|
||||
|
||||
[Fact]
|
||||
public void One_budget_serves_the_whole_select_and_is_spent_best_raw_first()
|
||||
public void Deepening_finds_short_plans_first()
|
||||
{
|
||||
// One want goal on two doors at equal raw: the bolted door (slot 0, four steps) is planned first, then the plain one (two steps).
|
||||
// One want goal on two doors at equal raw 4: the bolted door (slot 0, four steps) and the plain one (slot 1, two steps).
|
||||
var percepts = See(4, BoltedDoor(1), PlainDoor(2, 9f));
|
||||
var probe = new Planner(Catalogue(), 4, 4, 0.6);
|
||||
var steps = new PlanStep[4];
|
||||
Assert.Equal(4, probe.Plan(percepts, 0UL, 0f, 0f, 0f, 0, This(Open), 4, steps));
|
||||
var boltedNodes = probe.LastNodes;
|
||||
Assert.Equal(2, probe.Plan(percepts, 0UL, 0f, 0f, 0f, 1, This(Open), 4, steps));
|
||||
var plainNodes = probe.LastNodes;
|
||||
var rounds = 0;
|
||||
for (var k = 1; k <= 2; k++)
|
||||
{
|
||||
for (var slot = 0; slot < 2; slot++)
|
||||
{
|
||||
probe.Plan(percepts, 0UL, 0f, 0f, 0f, slot, This(Open), k, steps, Planner.NodeBudget, k);
|
||||
rounds += probe.LastNodes;
|
||||
}
|
||||
}
|
||||
|
||||
(Choice Pick, int Spent) Select(int budget)
|
||||
(Choice Pick, int Spent) Select(int budget, double idle = 1.5)
|
||||
{
|
||||
var brain = new Brain
|
||||
{
|
||||
PlanDepth = 4, ThinkBudget = budget, DirectDiscount = 0.5,
|
||||
Goals = new[] { Goal("idle", 1.5), Goal("open", 4.0, Kinds(Door), This(Open), Shorter()) },
|
||||
Goals = new[] { Goal("idle", idle), Goal("open", 4.0, Kinds(Door), This(Open), Shorter()) },
|
||||
};
|
||||
var d = new Deliberation(brain, Catalogue(), 4);
|
||||
return (brain.Select(default, percepts, 0f, 0f, 0f, new Choice(0, 0), d), d.Spent);
|
||||
}
|
||||
|
||||
// Both plans fit: bolted 4 x 0.625 = 2.5, plain 4 x 0.875 = 3.5 wins.
|
||||
Assert.Equal((new Choice(1, 2), boltedNodes + plainNodes), Select(boltedNodes + plainNodes));
|
||||
// The plain plan runs one short: it goes direct (4 x 0.5 x 0.5 = 1 < idle), the planned bolted pair wins.
|
||||
Assert.Equal((new Choice(1, 1), boltedNodes + plainNodes - 1), Select(boltedNodes + plainNodes - 1));
|
||||
// Exactly the bolted plan: the plain pair finds the budget gone, and goes direct.
|
||||
Assert.Equal((new Choice(1, 1), boltedNodes), Select(boltedNodes));
|
||||
// One short of it: both go direct at 1, which loses to idle.
|
||||
Assert.Equal((new Choice(0, 0), boltedNodes - 1), Select(boltedNodes - 1));
|
||||
// The plain door (4 x 0.875 = 3.5) wins in round 2 and the bolted door, bounded by 4 x 0.75 after it, is pruned before round 3:
|
||||
// the budget paid for round 1 and round 2 of both pairs and nothing more, not the four-step search.
|
||||
Assert.Equal((new Choice(1, 2), rounds), Select(rounds));
|
||||
Assert.Equal((new Choice(1, 2), rounds), Select(int.MaxValue));
|
||||
// One grounding short, the plain door's round 2 runs the budget out: it goes direct (4 x 0.25 = 1 < idle), and so does the bolted one.
|
||||
Assert.Equal((new Choice(0, 0), rounds - 1), Select(rounds - 1));
|
||||
Assert.Equal(new Choice(1, 1), Select(rounds - 1, idle: 0.5).Pick);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void A_dead_end_does_not_starve_a_short_plan()
|
||||
{
|
||||
// Two doors with four bolts (approach, four unbolts, open: six steps, past PlanDepth 4) at raw 6, a knife with a two-step want (approach, calm) at raw 3.
|
||||
const int Barred = 5, Chained = 6;
|
||||
var verbs = new[]
|
||||
{
|
||||
Catalogue()[0], Catalogue()[1], Catalogue()[2],
|
||||
new Verb { Id = "Unbar", This = Kinds(Door), Needs = new[] { This(Reach), This(Barred) }, Gives = new[] { This(Barred, false) } },
|
||||
new Verb { Id = "Unchain", This = Kinds(Door), Needs = new[] { This(Reach), This(Chained) }, Gives = new[] { This(Chained, false) } },
|
||||
new Verb { Id = "Open", This = Kinds(Door), Needs = new[] { This(Reach), This(Locked, false), This(Latched, false), This(Barred, false), This(Chained, false) }, Gives = new[] { This(Open) } },
|
||||
Catalogue()[5],
|
||||
};
|
||||
const ulong Bolts = (1UL << Locked) | (1UL << Latched) | (1UL << Barred) | (1UL << Chained);
|
||||
var percepts = See(4, Thing(1, Door, 6f, Bolts), Thing(2, Door, 8f, Bolts), Thing(3, Knife, 3f));
|
||||
var probe = new Planner(verbs, 4, 4, 0.6);
|
||||
var steps = new PlanStep[4];
|
||||
var rounds = 0;
|
||||
for (var k = 1; k <= 2; k++)
|
||||
{
|
||||
for (var slot = 0; slot < 3; slot++)
|
||||
{
|
||||
probe.Plan(percepts, 0UL, 0f, 0f, 0f, slot == 2 ? 0 : slot, slot == 2 ? Self(Fed) : This(Open), k, steps, Planner.NodeBudget, k);
|
||||
rounds += probe.LastNodes;
|
||||
}
|
||||
}
|
||||
|
||||
Assert.Equal(-1, probe.Plan(percepts, 0UL, 0f, 0f, 0f, 0, This(Open), 4, steps));
|
||||
Assert.True(probe.LastNodes > rounds, $"one full dead-end search ({probe.LastNodes}) must cost more than rounds 1 and 2 of all three pairs ({rounds})");
|
||||
|
||||
var brain = new Brain
|
||||
{
|
||||
PlanDepth = 4, ThinkBudget = rounds, DirectDiscount = 0.5,
|
||||
Goals = new[] { Goal("idle", 0.5), Goal("dead", 6.0, Kinds(Door), This(Open), Shorter()), Goal("calm", 3.0, Kinds(Knife), Self(Fed), Shorter()) },
|
||||
};
|
||||
var d = new Deliberation(brain, verbs, 4);
|
||||
// The doors go direct at 6 x 0.25 = 1.5; the knife's plan, 3 x 0.875 = 2.625, wins. Best raw first would have spent the whole budget on the first door.
|
||||
Assert.Equal(new Choice(2, 3), brain.Select(default, percepts, 0f, 0f, 0f, new Choice(0, 0), d));
|
||||
Assert.Equal(rounds, d.Spent);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
|
@ -534,9 +579,13 @@ public sealed class PlannerSelectTests
|
|||
[Fact]
|
||||
public void A_crowded_room_changes_few_decisions()
|
||||
{
|
||||
Assert.Equal(0, Differing(48, 4, 100));
|
||||
Assert.Equal(CrowdedDepth6Differing, Differing(48, 6, 100));
|
||||
foreach (var depth in new[] { 4, 5, 6, 8 })
|
||||
{
|
||||
Assert.Equal(0, Differing(48, depth, 100));
|
||||
}
|
||||
|
||||
Assert.Equal(CrowdedDepth6At64Differing, Differing(64, 6, 100));
|
||||
}
|
||||
|
||||
private const int CrowdedDepth6Differing = 59;
|
||||
private const int CrowdedDepth6At64Differing = 2;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue