Evaluate, then improve
The expectation backup uses only the action prescribed by the current policy. Once that policy is sufficiently evaluated, a separate improvement step compares actions.
Learn the classic Jack’s Car Rental problem by watching expected value emerge from thousands of possible rental-and-return outcomes.
THE PROBLEM
Jack manages two car-rental locations.
A rented car earns $10. Moving a car overnight costs $2. Demand and returns vary independently at each location, so Jack must optimize for expected long-run reward—not for one lucky day.
THE STATES
The state is a pair, not a place.
Each location can hold 0–20 cars. One square in the grid is one complete state. The selected state below, (10, 10), means ten cars are ready at each location.
Markov property: once we know these counts, the past adds no information needed to predict the next state.
First coordinate
Cars at location 1
Second coordinate
Cars at location 2
Action
Positive means 1 → 2
RANDOMNESS IN THE ENVIRONMENT
A distribution, not a forecast.
λ is an average, not a promise. With λ = 4, four requests is the most characteristic count—but zero, two, seven, or more can still occur. Policy iteration sums over all these possibilities.
ONE DAY
Sample the environment.
$10 × rentals − $2 × cars movedWhat did the simulator just do? It randomly drew four numbers from the Poisson distributions above: requests at location 1 (λ = 3), requests at location 2 (λ = 4), returns at location 1 (λ = 3), and returns at location 2 (λ = 2). Together, those four draws make one possible day—often called one Monte Carlo sample. The Bellman calculation below does not trust this one day; it averages all possible days, giving more likely combinations more weight.
THE VALUE
Value looks beyond today.
p(…) weights each possible rental/return outcome.
r is rental income minus transfer cost.
γV(s′) keeps 90% of tomorrow’s value.
Moving 0 cars costs $0.
Demand is not replaced by its average. We consider every possible Poisson demand count d, limit rentals to the cars available, then weight that result by how likely d is:
$10 × (3.00 + 4.00) − $0= $69.950.9 × Σs′ P(s′ | s,a)V(s′) = 0.9 × $0.00 = $0.00This is $0 at the start because policy evaluation initializes every next-state value V(s′) to zero. After one sweep, those values contain estimated future rewards, so this term becomes positive.
Where do returns enter? Poisson returns do not create today’s rental income. Together with demand, they determine the probability of each next state s′—so they affect the future-value term and tomorrow’s available cars.
Manual mode separates the loop: first evaluate the current policy by updating V, then improve the policy once by choosing the best action in every state.
Run the first sweep. Values begin at zero.
The policy is stable only when improvement chooses the same action as before in every state: πnew(s) = πold(s) for all 441 states. In this lab, that means actions changed = 0. If even one state changes action, evaluate the new policy and improve again.
Do not confuse this with value convergence. A small max ΔV says evaluation of the current policy is nearly settled; zero changed actions says policy iteration itself can stop. For animation speed, “Run full loop” improves when max ΔV < 0.5 or after 12 evaluation sweeps.
Teaching implementation: the canonical 21×21 state space and ±5 actions are exact. Poisson sums collect probability mass at 11+ events into a tail bucket; evaluation is improved after convergence or 12 sweeps so the animation stays responsive.
VALUE ITERATION
Same model. A tighter loop.
Policy iteration keeps an action fixed while it evaluates that policy, then pauses to improve it. Value iteration skips that separation: every sweep evaluates all legal actions and immediately keeps the best one.
The expectation backup uses only the action prescribed by the current policy. Once that policy is sufficiently evaluated, a separate improvement step compares actions.
Each state gets one Bellman optimality backup. The winning action is also the current greedy policy for that state.
The new ingredient is maxa. For each of the 441 states, we still average over every Poisson request-and-return combination. But now we do that once for every legal overnight move, then store the largest action value.
Vk+1(s) ← Σs′,r p(s′,r | s,π(s))[r + γVk(s′)]Use one action: the action π(s) currently prescribes. This asks, “How good is this state if I keep following this policy?”
Vk+1(s) ← maxa Σs′,r p(s′,r | s,a)[r + γVk(s′)]Try every legal action: calculate the expectation for each one and keep the largest. That single backup evaluates and improves at the same time.
Everything inside the expectation is otherwise the same. Both methods use the same rental reward, movement cost, discount factor, and Poisson transition probabilities. The difference is whether the action is fixed by π or selected by maxa.
| Question | Policy iteration | Value iteration |
|---|---|---|
| What happens in one update? | Use π(s) to update Vπ(s) | Try every action and store maxa q(s,a) |
| When does the policy change? | In a separate improvement phase | During every optimality sweep |
| How much evaluation before improvement? | Several sweeps—ideally until Vπ settles | One sweep, then immediately maximize again |
| What does the lab stop on? | No state changes action | The largest value change is below $0.50 |
| What do they converge to? | The same optimal value V* and an optimal policy, for this finite discounted MDP. | |
Important: both are dynamic-programming methods. Both use the known Poisson transition probabilities and expected rewards; neither learns from the single-day samples above. The $0.50 threshold is for a readable animation—the mathematical sequence continues toward V*.
What to carry forward
Jack’s Car Rental sums over known transition probabilities to perform expected-value backups.
Demand and returns are random. Planning weights possible outcomes by their Poisson probabilities.
Policy iteration evaluates a fixed policy before choosing better actions.
Value iteration compares all legal actions at each update and keeps the largest expected value.
How can we learn without a transition table? Explore Monte Carlo prediction with Blackjack →