Computer Engineering is coming soon

We're still writing and reviewing the notes for every subject, and we'll open it when every subject is ready. Meanwhile you can practise aptitude, ask Aria about any subject, or see the branches that are ready.

Dynamic programming for reinforcement learning

Dynamic programming (policy evaluation, policy improvement, policy iteration, value iteration) gives exact, convergent methods for solving finite Markov decision processes used in planning and as foundations for reinforcement learning.

Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.

Why it matters

Dynamic programming methods are used by engineers to compute optimal behavior in systems modelled as Markov decision processes (MDPs). They give exact, provable procedures for computing value functions and optimal policies when the model (transition probabilities and rewards) is known, and form the theoretical backbone of model-free RL methods like Q-learning and deep Q-networks.

Key ideas

Start from definitions and first principles. Bold each definition when first used.

  • Markov decision process (MDP): a tuple (S, A, P, R, gamma) where S is a finite set of states, A is a finite set of actions, P(s' | s,a) is the probability of next state s' given current state s and action a, R(s,a,s') is the scalar reward received on that transition, and discount factor gamma is in [0,1). Assumption for DP methods here: S and A are finite and model P and R are known.

  • Policy: a rule for choosing actions. A deterministic policy pi is a mapping pi: S -> A. A stochastic policy pi(a | s) gives a probability distribution over actions in each state.

  • State-value function for policy pi: V_pi(s) is the expected discounted return starting from state s and following policy pi thereafter. Exact definition: V_pi(s) = E_pi[ sum_{t=0..infty} gamma^t R_{t+1} | S_0 = s ]. This expectation is taken over stochastic transitions and possibly stochastic policy.

  • Action-value function for policy pi: Q_pi(s,a) is the expected discounted return starting from state s, taking action a once, then following pi: Q_pi(s,a) = E_pi[ R_{1} + gamma V_pi(S_1) | S_0=s, A_0=a ].

  • Bellman expectation equation (exact recursive relation for V_pi). For all s: V_pi(s) = sum_a pi(a|s) sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V_pi(s') ]. This is a system of |S| linear equations in the |S| unknowns V_pi(s) (when gamma < 1 and finite S).

Derivation sketch asked in exams: write expected return as R_{1} + gamma * expected discounted return from S_1, then expand expectation under policy and transition model to obtain the sum above.

  • Bellman optimality equation for the optimal state-value function V*(s): V*(s) = max_a sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V*(s') ]. This is a nonlinear system; solution gives optimal values. The corresponding optimal action-value function satisfies Q*(s,a) = sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma max_{a'} Q*(s',a') ].

  • Policy improvement theorem (exact statement): If for two deterministic policies pi and pi' we have for all s: Q_pi(s, pi'(s)) >= V_pi(s), then the policy pi' is at least as good as pi for all states: V_pi'(s) >= V_pi(s) for all s. If strict inequality holds for some state then pi' is strictly better for at least one state. Assumptions: finite MDP, same gamma.

  • Algorithms built from these are:

    • Policy evaluation: solve the Bellman expectation equations for V_pi (either by direct linear algebra for small |S|, or by iterative evaluation V_{k+1} = T_pi V_k until convergence). Here T_pi is the Bellman expectation operator.
    • Policy improvement: compute Q_pi(s,a) from V_pi and choose a new greedy policy pi'(s) = argmax_a Q_pi(s,a).
    • Policy iteration: alternate evaluation (to convergence) and improvement; guaranteed to converge to an optimal policy in finite steps for finite MDPs.
    • Value iteration: repeatedly apply the Bellman optimality operator T to value estimates: V_{k+1}(s) = max_a sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V_k(s') ]. This is essentially truncated policy evaluation + immediate improvement; it converges to V* under gamma in [0,1).
  • Convergence theory (short proof sketch often asked): the Bellman operators are contractions under the max norm (sup norm). For any two value functions V and W and operator T_pi (or T for optimality) we have ||T V - T W||_infty <= gamma ||V - W||_infty. Since gamma < 1, repeated application converges geometrically to a unique fixed point (Banach fixed-point theorem). For policy iteration, monotonic improvement and finite policy space imply termination with optimal policy.

Diagrams to draw in answers: draw circles for states, label them s0, s1, etc. Draw arrows for actions with transition probabilities on arrows, and write reward numbers near arrows. For policy iteration sketch, draw a flow chart with boxes: evaluate -> improve -> check stable -> stop.

Formulas

V_pi(s) = sum_a pi(a|s) sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V_pi(s') ] where V_pi(s) is state-value under policy pi (units: reward units, unitless time), pi(a|s) is probability (unitless), P(s'|s,a) is probability (unitless), R(s,a,s') is reward (reward units), gamma is discount factor (unitless). Applies for any finite MDP and stationary policy pi.

Q_pi(s,a) = sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma sum_{a'} pi(a'|s') Q_pi(s',a') ] Q_pi units: reward units. Applies under policy pi.

V*(s) = max_a sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V*(s') ] V* is optimal value. Applies for finite MDP with gamma in [0,1).

Q*(s,a) = sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma max_{a'} Q*(s',a') ] Q* is optimal action-value function.

T_pi V (s) = sum_a pi(a|s) sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V(s') ] T_pi is Bellman expectation operator.

|| T V - T W ||_infty <= gamma || V - W ||_infty contraction property; ||·||_infty is the maximum absolute difference over states. Applies when 0 <= gamma < 1.

Algorithmic syntax to remember (pseudocode):

  • Policy evaluation iterative: V <- arbitrary; repeat V <- T_pi V until convergence.
  • Policy improvement: pi'(s) <- argmax_a sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V(s') ].
  • Value iteration: V <- arbitrary; repeat for all s: V(s) <- max_a sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V(s') ] until small change.

Worked examples

Example 1 — direct evaluation (deterministic small MDP) Problem: S = {s0, s1, s2}. s2 is terminal with V(s2)=0. Transitions: from s0 one action takes s0 -> s1 with reward 5. From s1 one action takes s1 -> s2 with reward 2. gamma = 0.9. Policy deterministic follows those actions. Steps:

  1. V(s2) = 0 by terminal definition.
  2. Using Bellman: V(s1) = R(s1->s2) + gamma V(s2) = 2 + 0.9*0 = 2.
  3. V(s0) = R(s0->s1) + gamma V(s1) = 5 + 0.9*2 = 5 + 1.8 = 6.8. Final answer: V(s0) = 6.8 reward units, V(s1) = 2 reward units, V(s2) = 0.

Example 2 — policy iteration on a 2-state MDP Problem: States A,B. Actions {0,1}. Transitions: From A: action 0 stays in A with reward 1. action 1 moves to B with reward 0. From B: any action moves to A with reward 2. gamma = 0.5. Start with policy pi that picks action 0 in both states. Steps:

  1. Policy evaluation: V(A) = 1 + 0.5 V(A) => V(A) - 0.5 V(A) = 1 => V(A) = 2. V(B) = 2 + 0.5 V(A) = 2 + 0.5*2 = 3.
  2. Policy improvement: compute Q for alternatives. Q(A,1) = 0 + 0.5 V(B) = 0 + 0.53 = 1.5 < V(A)=2 => keep action 0 in A. Q(B,0) = 2 + 0.5 V(A) = 2 + 0.52 = 3 = V(B) tie => either action is fine. Policy is stable; optimal policy is same as initial. Final values: V(A)=2, V(B)=3.

(These examples are the kind of numerical steps examiners expect: show Bellman equation, substitute numbers, show arithmetic, declare final bold answers.)

Common mistakes

  • Forgetting to treat terminal states (V=0) correctly or to include terminal rewards.
  • Mixing up V and Q when doing policy improvement (must compute Q or use one-step lookahead correctly).
  • Using iterative policy evaluation but stopping too early without convergence criterion; not stating the tolerance or number of sweeps.

For your exam

Types of questions: definitions (MDP, policy, value, Q), short notes (Bellman equations, contraction mapping), derivations (derive Bellman expectation equation from return definition; show contraction), numericals (small MDPs for policy evaluation, one step of policy iteration or value iteration), algorithms (write policy iteration/value iteration pseudocode), diagrams (draw MDP with transitions and rewards). Full-mark answers include: precise definitions with bold terms, assumptions (finite S,A; gamma range), derivations step-by-step showing expectations or algebra, correct diagram labeling (states, transitions, probabilities, rewards), worked numerical substitution with units and final bold values, and a convergence statement or justification when asked.

Quick check

  1. State the Bellman optimality equation for V*(s).
  2. Give one condition required for the Bellman operator to be a contraction.
  3. In policy iteration, what two main steps alternate? Answers: 1) V*(s) = max_a sum_{s'} P(s'|s,a) [ R(s,a,s') + gamma V*(s') ]. 2) gamma in [0,1) (strictly less than 1 for contraction) using the max norm. 3) Policy evaluation and policy improvement.

Finished this topic? Mark it so your progress, study plan and readiness keep up.

Stuck on something here?