Exact solver¶
Exact policy evaluation and optimization of a finite-state MDP — the average
cost per period under an infinite horizon, the expected total cost under a
finite one — see the exact solver guide for
usage.
Everything on this page is imported from the top-level dynaplex package.
ExactSolver ¶
ExactSolver(mdp, *, epsilon: float = 1e-06, max_states: int = 1 << 22, max_event_branches: int = 1 << 20, workers: int = 0, m: int = 10, p_self: float = 0.02, path: str = 'auto', checks: bool | None = False, jit: bool | None = None, verbose: bool = False)
Discover the MDP's reachable decision states and prepare to solve.
epsilon: convergence on the span of the value increments (INFINITE: average cost; with transitions that do not all take one period, on the error bound of the cost per period, and an improvement step in which no state gains more than epsilon ends the solve) or on max|increment| (FINITE: total cost). max_states / max_event_branches: guardrails on the state count and on the leaves of one (state, action). workers: v1 is single-threaded (0). m: evaluation sweeps per improvement (modified policy iteration; not used when transitions do not all take one period: each policy is then evaluated to convergence). path: the state identity, "auto" (the perfect mixed-radix code when one shape fits, else the row table), "perfect" (demand it) or "table". checks: the engines' checks mode (default FAST: the model is the user's, validated elsewhere; pass True to debug a model).
evaluate ¶
evaluate(policy) -> ExactResult
The exact cost of policy (a compiled policy): average per period
(INFINITE) or expected total from get_initial_state (FINITE).
solve ¶
solve(warm_start=None) -> ExactResult
The optimal cost and policy, from warm_start (a compiled policy) or
from the lowest valid action in every state. Finite horizon: the warm
start must reach FINAL w.p. 1 (as must every policy of the MDP).
Infinite horizon with decisions that take no time: the start must let
time advance (ZeroTimeLoopError otherwise); when the lowest valid
action does not, pass a warm_start that does.
states ¶
Every discovered decision state as a CPython object, in global index order (shape by shape; codes on the perfect path, discovery order on the table path), each paired with its global index. Fresh objects: safe to keep or mutate. Meant for inspection at small scale (one decode per state).
close ¶
Release the compiled solve engines and their worker threads. The results already returned stay usable (their tables are numpy arrays).
report ¶
The discovery report: per shape the state count, the identity path (mixed-radix digits and density, or the row table) and the digit order.
Results¶
ExactResult
dataclass
¶
What evaluate / solve return. cost is THE number: the average cost
per period (INFINITE horizon) or the expected total cost from
get_initial_state (FINITE). The tables are in memory in v1 (values per
global index -- relative values under INFINITE, expected cost-to-go under
FINITE; actions per global index, -1 where not live).
get_bias_function ¶
state -> relative value (the bias under the gain; INFINITE horizon), in CPython. Raises KeyError on a state the solver never discovered.
get_value_function ¶
state -> expected cost-to-go from that decision state (FINITE horizon), in CPython. Raises KeyError on a state the solver never discovered.
get_policy ¶
get_policy() -> ExactPolicy | ExactTablePolicy
The action table as a compiled policy: ExactPolicy (mixed-radix code
lookup) on the perfect path, ExactTablePolicy (sorted rows, binary
search) on the table path.
The exact policy¶
The action table as a compiled policy, returned by ExactResult.get_policy():
ExactPolicy on the perfect (mixed-radix) path, ExactTablePolicy on the
table path. Both are normal policies for the
comparer and DCL.
ExactPolicy ¶
The optimal (or evaluated) action table as a policy: actions[encode(state)].
A normal compiled policy — usable in the comparer and in DCL — holding the
mixed-radix basis of the solver's single shape, the live bitmap and the
action per code. Construct through ExactSolution.get_policy().
ExactTablePolicy ¶
The action table as a policy on the TABLE path (several shapes, or a
shape the mixed-radix identity could not take): the discovered rows of every
shape as one lexicographically sorted key table -- key = (shape index, row,
zero padding to the widest row) -- and the action per key. get_action
flattens the state into a key and binary-searches it (log2(states) row
compares, no hashing, arrays only, so the policy is deeply const and
compiles into the comparer). Construct through ExactResult.get_policy().