Repository navigation
perf(planner): declare what a dynamic cost reads and ask it once per input - #120
Merged
Merged
Conversation
bsbodden
force-pushed
the
perf/dynamic-costs
branch
2 times, most recently
from
November 24, 2026 14:20
c94af74 to
c94a408
Compare
✅ Code Quality Report
Quality Gates
🎉 All quality gates passed! This PR maintains high code quality standards. 📊 View detailed MFCQI breakdownRun |
bsbodden
force-pushed
the
perf/dynamic-costs
branch
from
November 24, 2026 15:20
c94a408 to
6e1b7e6
Compare
…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
force-pushed
the
perf/dynamic-costs
branch
from
November 24, 2026 16:24
6e1b7e6 to
0f65134
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #118. Fixes #119.
What.
ActionSpec.cost_keys(also ongoap_actionandGoapAction) 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. AGoapActionsubclass that keeps the defaultcost()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
discountis 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
discountfromdiscount, buyand returnedbuyat 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,mainat f141700 imported from a worktree, before and after interleaved twice, minimum per row. Onmainthe declared variants run undeclared, since the field does not exist there.cost_keys=()g0,cost_keys=("g0",)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 becauseg0is 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_costdirectly; the realised cost at execution is untouched.make checkgreen: 2335 passed.