Week 04 · Dynamic programming and value iteration
Compute an optimal route using known transitions.
← Week 3 · Subject contents · Week 5 →
Compute an optimal route using known transitions.
Work through the problem
Value iteration applies V_new(s)=maxₐΣₛ′P(s′|s,a)[r+γV(s′)] repeatedly. Terminal value is zero. Start all values at zero. If a safe route pays 1 now and 5 one step later, its return is 1+5γ. A shortcut paying 3 immediately wins whenever γ<0.4.
The lab solves that known transition model at two discounts. When γ<1 the discounted Bellman operator contracts differences under the maximum norm. The one-step update starts to reflect downstream rewards once their values have propagated back.
Run the Python lab
model = {"start":[("short",None,3),("safe","middle",1)],"middle":[("finish",None,5)]}
for gamma in [.2,.9]:
value = dict.fromkeys(model,0.)
for _ in range(5):
value = {s:max(r+gamma*value.get(t,0) for _,t,r in actions) for s,actions in model.items()}
choice = max(model["start"],key=lambda a:a[2]+gamma*value.get(a[1],0))[0]
print(gamma,choice,value["start"])
assert choice==("short" if gamma<.4 else "safe")
Output from the lab
0.2 short 3.0
0.9 safe 5.5
Practice
- Does value iteration here learn the environment from experience?
Check your reasoning
- It plans with transitions and rewards already specified in the model.