Alternates iterative policy evaluation, sweeps of
\(V(s) \leftarrow \sum_{s'} P(s'|s,\pi(s)) [r + \gamma V(s')]\)
until the largest change is below tol, with greedy policy
improvement, stopping when the policy is stable. This is the boxed
algorithm in Sutton and Barto (2018), Section 4.3, p. 80; policy
iteration is due to Howard (1960). Ties break to the lowest action
index and a state counts as stable when the incumbent action value
is within 1e-12 of the maximum, which removes the tie-flipping
non-termination of the literal box, per their Exercise 4.4.
Arguments
- P
List of A transition matrices, each (S, S) row-stochastic.
- R
Matrix (S, A) of expected rewards, or a list of A (S, S) per-transition reward matrices averaged under P.
- gamma
Discount factor in 0 to 1.
- tol
Policy-evaluation sweep threshold.
- max_eval
Cap on evaluation sweeps per improvement round.
- max_improve
Cap on improvement rounds.
- pi0
Optional initial 0-based deterministic policy, length S.
Value
List with estimate (values of the final policy),
policy (0-based actions), q, n_improve,
n_eval, policy_stable, method.
