Skip to content

perf(planner): declare what a dynamic cost reads and ask it once per input - #120

Merged
bsbodden merged 1 commit into
mainfrom
perf/dynamic-costs
Nov 24, 2026
Merged

bsbodden merged 1 commit into
mainfrom
perf/dynamic-costs

Conversation

@bsbodden

@bsbodden bsbodden commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #118. Fixes #119.

What. ActionSpec.cost_keys (also on goap_action and GoapAction) names the state keys a cost function reads, () when it reads nothing the search changes. A declared cost is asked once per distinct value of those keys in a planning call, anytime iterations and plan building included; one whose keys no searched action writes is constant for the search and its value feeds the heuristic instead of forcing it to zero. A GoapAction subclass that keeps the default cost() gets a static 1.0 instead of a callable, so class-based actions keep the heuristic by default.

Contract. A cost function is a function of the state it is given. Declared: asked once per distinct value of its keys per planning call, and the relevance filter and the backward pass treat those keys as read by the action (the enabler discount is searched and kept). Undeclared: asked at every evaluation as before, with no lookup in between; an action whose only purpose is to lower that cost is not searched; the heuristic falls back to zero. The memo is deliberately not applied to undeclared costs: a plain A* never asks the same (action, state) twice, so a lookup there only costs.

Fix (#119). The plan shortener dropped discount from discount, buy and returned buy at 10 for a path A* found at 2. A shortened plan that costs more than the path is now replaced by the path, for declared and undeclared costs alike.

Measured with the new tests/benchmarks/test_dynamic_cost_bench.py (eight required steps, seven optional flags that together enable an expensive route). Both sources ran under the branch's own interpreter, main at f141700 imported from a worktree, before and after interleaved twice, minimum per row. On main the declared variants run undeclared, since the field does not exist there.

costs before after
static numbers 0.541 ms, 9 nodes 0.547 ms, 9 nodes
callable reading a constant, undeclared 16.6 ms, 577 nodes, 2824 calls 16.8 ms, 577 nodes, 2824 calls
the same with cost_keys=() 16.4 ms, 577 nodes, 2824 calls 0.63 ms, 9 nodes, 15 calls
callable reading g0, cost_keys=("g0",) 16.5 ms, 569 nodes, 2809 calls 17.6 ms, 569 nodes, 22 calls
undeclared, anytime schedule 69.1 ms, 11296 calls 69.1 ms, 11296 calls
keyed, anytime schedule 69.0 ms, 11236 calls 71.9 ms, 22 calls

The keyed rows use a trivial lambda: the lookup costs about what the function does, so declaring keys is for a cost worth remembering (a store read, a tool call, real arithmetic) or (). Their node counts do not move because g0 is written by the search; a declared floor for such costs would be the next lever and is not in this change. A first table taken across two virtual environments put every row a few percent apart, unchanged code included; the table above comes from one environment.

Not changed. MCTS, the utility planner, plan repair and the lookahead strategy still call get_cost directly; the realised cost at execution is untouched. make check green: 2335 passed.

@github-actions github-actions Bot added the maintenance CI/build/tooling maintenance label Nov 24, 2026
@bsbodden
bsbodden force-pushed the perf/dynamic-costs branch 2 times, most recently from c94af74 to c94a408 Compare November 24, 2026 14:20
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

✅ Code Quality Report

Metric Score Status
MFCQI Score 0.8099674061450423 ✅ Good
Test Coverage 94% ✅ Good
Security Issues 0 ✅ Clean

Quality Gates

  • MFCQI Score: ✅ Minimum 0.75 (Current: 0.8099674061450423)
  • Test Coverage: ✅ Minimum 80% (Current: 94%)
  • Security: ✅ Zero vulnerabilities (Current: 0)

🎉 All quality gates passed! This PR maintains high code quality standards.

📊 View detailed MFCQI breakdown

Run uv run python -m mfcqi analyze langgoap locally to see detailed metrics breakdown.

…input

ActionSpec.cost_keys (also on goap_action and GoapAction) names the state
keys a cost function reads, () when it reads nothing the search changes.
A declared cost is asked once per distinct value of those keys in a
planning call, anytime iterations and plan building included; one whose
keys no searched action writes is constant for the search and its value
feeds the heuristic instead of forcing it to zero. A GoapAction subclass
that keeps the default cost() gets a static 1.0, so class-based actions
keep the heuristic. Undeclared costs are asked at every evaluation as
before, with no lookup in between.

The plan shortener no longer returns a plan dearer than the path A* found
(discount then buy at 2 came back as buy at 10): such a plan is replaced
by the path. With cost_keys declared the relevance filter keeps the
actions that write those keys and the backward pass keeps them as needs.

Measured with the new tests/benchmarks/test_dynamic_cost_bench.py (both
sources under one interpreter, interleaved, min of two runs of 20+
rounds): constant costs 577 nodes / 2824 calls / 16.4 ms -> 9 / 15 /
0.63 ms; keyed costs 2809 -> 22 calls at the same 569 nodes, 16.5 ->
17.6 ms for a trivial function; static and undeclared within 2 percent.

Fixes #118
Fixes #119
@bsbodden
bsbodden merged commit 2d94058 into main Nov 24, 2026
48 of 54 checks passed
@bsbodden
bsbodden deleted the perf/dynamic-costs branch November 24, 2026 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

maintenance CI/build/tooling maintenance

Projects

None yet

1 participant