diff --git a/src/Lodur.Core/Planner.cs b/src/Lodur.Core/Planner.cs
index ffbd167..74b658f 100644
--- a/src/Lodur.Core/Planner.cs
+++ b/src/Lodur.Core/Planner.cs
@@ -61,8 +61,8 @@ namespace Lodur
private const int MaxNeeds = 8;
///
- /// The most groundings one call may regress, counted across all its depths. A counter, not a clock, so the cut-off is the same on every machine.
- /// A call that spends it returns -1 as if no plan existed.
+ /// The default budget of a call: the most groundings it may regress, counted across all its depths. A counter, not a clock, so the cut-off is the same on every machine.
+ /// A call that spends its budget returns -1 and sets .
///
public const int NodeBudget = 4096;
@@ -87,6 +87,8 @@ namespace Lodur
private readonly ulong[] _meetsThis; // per level, the slots whose literal a This give of the verb in hand meets, _words per level
private readonly ulong[] _meetsTool;
private int _nodes;
+ private int _budget;
+ private bool _exhausted;
public Planner(IReadOnlyList verbs, int capacity, int maxDepth, double reachMeters)
{
@@ -151,18 +153,24 @@ namespace Lodur
/// The deepest plan this planner can be asked for.
public int MaxDepth { get; }
- /// The groundings the last call regressed; above means it ran out and returned -1.
+ /// The groundings the last call regressed; above the call budget means it ran out and returned -1.
public int LastNodes => _nodes;
+ /// True when the last call returned -1 because it spent its budget, not because no plan within the depth exists.
+ public bool LastExhausted => _exhausted;
+
///
/// 0 when the want already holds, else the length of the shallowest plan of at most steps, written into
/// in execution order, else -1. The want is about self or about the percept in .
/// A thing's facts are those it was last sensed with, bit 63 replaced by whether it is within reach of the point.
+ /// is the most groundings the call may regress, at least 1; a call that spends it returns -1 and sets .
///
- public int Plan(Perception perception, ulong selfFacts, float ox, float oy, float oz, int targetSlot, Fact want, int depth, Span plan)
+ public int Plan(Perception perception, ulong selfFacts, float ox, float oy, float oz, int targetSlot, Fact want, int depth, Span plan, int budget = NodeBudget)
{
- Check(perception, targetSlot, want, depth, plan.Length);
+ Check(perception, targetSlot, want, depth, plan.Length, budget);
_nodes = 0;
+ _budget = budget;
+ _exhausted = false;
_facts[0] = selfFacts & ~Perception.InReachFact;
var ids = perception.PerceptIds;
@@ -200,12 +208,13 @@ namespace Lodur
return d;
}
- if (_nodes > NodeBudget)
+ if (_nodes > _budget)
{
break;
}
}
+ _exhausted = _nodes > _budget;
return -1;
}
@@ -259,7 +268,7 @@ namespace Lodur
return true;
}
- if (_nodes > NodeBudget)
+ if (_nodes > _budget)
{
return false;
}
@@ -279,7 +288,7 @@ namespace Lodur
return true;
}
- if (_nodes > NodeBudget)
+ if (_nodes > _budget)
{
return false;
}
@@ -294,7 +303,7 @@ namespace Lodur
private bool Try(int r, int v, int a, int b, int cur, int n)
{
_nodes++;
- if (_nodes > NodeBudget || !Regress(v, a, b, cur, n, (r - 1) * _stride, out var count))
+ if (_nodes > _budget || !Regress(v, a, b, cur, n, (r - 1) * _stride, out var count))
{
return false;
}
@@ -457,8 +466,13 @@ namespace Lodur
private static PlanStep Step(int verb, ulong thisId, ulong toolId) => new PlanStep(verb, thisId, toolId);
- private void Check(Perception perception, int targetSlot, Fact want, int depth, int planLength)
+ private void Check(Perception perception, int targetSlot, Fact want, int depth, int planLength, int budget)
{
+ if (budget < 1)
+ {
+ throw new ArgumentOutOfRangeException(nameof(budget), "The budget must be at least 1.");
+ }
+
if (perception is null)
{
throw new ArgumentNullException(nameof(perception));
diff --git a/tests/Lodur.Core.Tests/PlannerTests.cs b/tests/Lodur.Core.Tests/PlannerTests.cs
index 70dcfd3..19fc84a 100644
--- a/tests/Lodur.Core.Tests/PlannerTests.cs
+++ b/tests/Lodur.Core.Tests/PlannerTests.cs
@@ -506,10 +506,31 @@ public sealed class PlannerTests
{
Assert.Equal(-1, planner.Plan(p, 0UL, 0f, 0f, 0f, 0, Self(1), 8, steps));
Assert.Equal(Planner.NodeBudget + 1, planner.LastNodes);
+ Assert.True(planner.LastExhausted);
}
+ // The call's budget is the cut-off, not the constant.
+ Assert.Equal(-1, planner.Plan(p, 0UL, 0f, 0f, 0f, 0, Self(1), 8, steps, budget: 100));
+ Assert.Equal(101, planner.LastNodes);
+ Assert.True(planner.LastExhausted);
+
// A short plan in the same world is not touched by the budget the long one spent.
Assert.Equal(1, planner.Plan(p, 1UL << 2, 0f, 0f, 0f, 0, Self(1), 8, steps));
Assert.Equal(new PlanStep(0, 1, 0), steps[0]);
+ Assert.False(planner.LastExhausted);
+
+ // No plan within the depth is not a spent budget.
+ Assert.Equal(-1, planner.Plan(p, 0UL, 0f, 0f, 0f, 0, Self(3), 8, steps));
+ Assert.False(planner.LastExhausted);
+ Assert.ThrowsAny(() => planner.Plan(p, 0UL, 0f, 0f, 0f, 0, Self(1), 8, steps, budget: 0));
+
+ // The switch plan needs four steps: a small budget gives up on it, the default finds it.
+ var catalogue = Build();
+ var world = World();
+ var run = new PlanStep[8];
+ Assert.Equal(-1, catalogue.Plan(world, 0UL, 0f, 0f, 0f, SlotOf(world, 3), This(Off), 8, run, budget: 5));
+ Assert.True(catalogue.LastExhausted);
+ Assert.Equal(4, catalogue.Plan(world, 0UL, 0f, 0f, 0f, SlotOf(world, 3), This(Off), 8, run));
+ Assert.False(catalogue.LastExhausted);
}
}