Planning One Step, Then the Remaining Journey
A long sequence of choices is difficult to compare directly. We can combine the reward from one action with the value of the state it leaves behind, then improve those estimates repeatedly.
13 sections · 4 questions · 3 coding challenges
The arrow keys move between sections too.
Reusing the Remaining Decision Problem
BOTIE
Value iteration repeatedly applies an optimality update under a known model. Our finite teaching environment separates this planning calculation from learning an unknown model through interaction.
Can Botie Plan Without Listing Every Possible Future?
Each watering choice can lead to several future states, each with its own choices. Listing every complete future quickly becomes unwieldy. But many different paths can lead back to the same state.
We can keep one estimate for the remaining value of each state. Compare actions using their immediate rewards and those estimates, then update the state values and repeat. This reuses the same subproblem wherever it occurs.
The transition model and rewards describe a fully specified teaching environment.
Step 1 of 3
Start with values for the remaining problem
Suppose the possible next states currently have estimated values two and six.
| Possible continuation | Next-state values |
|---|---|
| First continuation | 2 |
| Second continuation | 6 |
First continuation
- Next-state values
- 2
Second continuation
- Next-state values
- 6
Step 2 of 3
Compare immediate and later contributions
One action gives a larger immediate reward but a worse continuation.
| Action | Immediate reward | Later value | Discount | Complete score |
|---|---|---|---|---|
| First | 3 | 2 | 0.5 | 3 + (0.5 × 2) = 4 |
| Second | 0 | 6 | 0.5 | 0 + (0.5 × 6) = 3 |
First
- Immediate reward
- 3
- Later value
- 2
- Discount
- 0.5
- Complete score
- 3 + (0.5 × 2) = 4
Second
- Immediate reward
- 0
- Later value
- 6
- Discount
- 0.5
- Complete score
- 0 + (0.5 × 6) = 3
Step 3 of 3
Choose using the complete action score
The first action has the higher complete score at this discount, even though the second leads to a state with a larger supplied value.
| Action | Complete score | Choice |
|---|---|---|
| First | 4 | Choose |
| Second | 3 | Do not choose |
First
- Complete score
- 4
- Choice
- Choose
Second
- Complete score
- 3
- Choice
- Do not choose
Planning combines one decision with the value of continuing
An action's immediate reward and discounted continuation jointly determine its score. The chosen index uses the same action ordering as the input rows.
Is the immediate reward worth the state it leads to?
One action can pay more now but leave less value for later. Change the supplied continuation values and check whether the preferred action changes.
BOTIE
Index of the preferred action
0
Follow the operations
1. Immediate expected rewards
- 03
- 10
2. Discount
0.5
3. Next-state probabilities per action
1 0 0 1 4. Check transition rows
1 0 0 1 5. Next-state value estimates
- 02
- 16
6. Matrix times vector
- 02
- 16
7. Multiply
- 01
- 13
8. Add
- 04
- 13
9. Find the largest
0
10. Result
0
This is one state's greedy improvement step from supplied values. A full planner must update all relevant states and handle terminals.
1. Calculate an Action Value From a Supplied Model
For each action, combine expected immediate reward with the discounted expectation of the next state's value. A transition row averages over next states; the discount then scales the continuation.
Here rewards are already expected immediate rewards for each state-action pair. If a reward depends on the next state, its expectation must also be calculated under that transition row.
Q(s,a) = expected_reward(s,a) + gamma × sum_next P(next|s,a) V(next)The value vector belongs to the next-state axis, not the action axis.
2. Distinguish Evaluating a Policy From Improving It
Suppose we already have a policy. To estimate how it behaves, we average action values using the probabilities with which that policy chooses actions. Value iteration asks a different question: which action currently has the greatest estimated value in this state?
That maximum is the optimality update we will use. Early estimates can be inaccurate, so choosing their current best action does not mean we have already found the optimal policy. We repeat the updates and examine convergence under the model's assumptions.
Fixed policy: V_new(s) = sum_a policy(a|s) Q(s,a)
Optimality update: V_new(s) = max_a Q(s,a)Do not substitute a maximum into a calculation claimed to evaluate a fixed non-greedy policy.
3. Repeat With a Consistent Set of Old Values
In synchronous value iteration, calculate every new state value from the same old vector. Only replace the vector after the complete sweep. Reusing already updated entries within a sweep is a different update schedule.
Terminal states have no continuation and stay at their defined terminal value, typically zero after the terminal reward is counted. Stop using a chosen change tolerance or iteration budget.
V_old → all action scores → V_new
change = max(abs(V_new - V_old))A finite discounted problem with bounded rewards has useful convergence guarantees, but floating-point tolerance is still a practical stopping decision.
Combine a payment now with a continuation later
Compare two actions whose next states are known here. One pays immediately but has little continuation value; the other pays nothing now and reaches a more useful state.
import numpy as np
immediate = np.array([2., 0.])
continuation = np.array([1., 8.])
gamma = 0.5
scores = immediate + gamma * continuation
print(scores.tolist())
print(int(np.argmax(scores)))[2.5, 4.0]
1The second action wins after including the continuation. Practice replaces these supplied next-state values with expectations under a transition model.
4. Inspect the Policy and the Model Assumptions
After updating values, choose a highest-scoring action in each state. Check the resulting actions, terminal handling, and reward accounting in simple states with known answers.
A perfectly solved inaccurate model can still produce a poor real policy. Botie's workshop calculates action scores for one state; the Python example repeats that principle in a complete two-state model.
Planning assumes transitions and rewards are known. The next lesson replaces a model expectation with sampled experience.
5. One Sweep Uses One Consistent Old Value Table
For each state, calculate the expected reward plus discounted continuation for every action, then retain the best action value. A synchronous sweep reads all continuations from the old table and writes a new table.
Updating entries in place creates a different update schedule. It can still be a useful algorithm, but its intermediate values should not be compared as though it performed the same synchronous sweep.
6. Iterate a Small Known Model to a Stable Decision
This two-state model gives Botie an action that stays where he is and an action that moves to the other state. Rewards belong to state-action pairs, and transitions are deterministic here.
Repeat until the largest table change is small or an explicit iteration limit is reached. Then extract the greedy action from the converged action values.
Plan before collecting experience
Rows are states and columns are actions. The transition table stores the next-state index.
import numpy as np
reward = np.array([[0.,1.],[2.,0.]])
next_state = np.array([[0,1],[1,0]])
value = np.zeros(2)
for _ in range(1000):
action_value = reward + .5*value[next_state]
updated = action_value.max(axis=1)
change = np.max(np.abs(updated-value))
value = updated
if change < 1e-10:
break
policy = (reward + .5*value[next_state]).argmax(axis=1)
print(np.round(value, 4).tolist())
print(policy.tolist())[3.0, 4.0]
[1, 0]The plan moves from state zero to state one, then takes the rewarding stay action there. The result relies on knowing the supplied transition and reward model.
7. A Stable Calculation Can Plan for the Wrong Greenhouse
For a finite discounted model with valid probabilities and discount below one, Bellman optimality updates contract differences between value tables. This supports convergence of the planning calculation.
It does not establish that the model accurately describes the greenhouse. A missing action cost or incorrect transition probability can produce a precisely computed but poor real policy. Compare simulated policy behaviour with observed transitions and test sensitivity to uncertain model entries.
Check yourself
Questions on Parts 1 to 7
Question 1 of 4
Which action scores should be compared?
Use the information below.
Action rewards: [3,0]. Expected next values: [2,6]. Gamma=0.5.Try it with Botie
Build with Botie
Write and run Python