Reinforcement Learning Exercise 4.4

Exercise 4.4 The policy iteration algorithm on page 80 has a subtle bug in that it may never terminate if the policy continually switches between two or more policies that are equally good. This is OK for pedagogy, but not for actual use. Modify the pseudocode so that convergence is guaranteed.
1 Initialization V ( s ) ∈ R  and  π ( s ) ∈ A ( s )  arbitrarily  s ∈ S For each  s ∈ S  create an empty list :  o l d _ l i s t _ o f _ a ( s ) For each  s ∈ S  create an iterator :  i t e r a t o r _ o l d _ l i s t _ o f _ a ( s ) 2 Policy Evaluation Loop: Δ ← 0 Loop for each  s ∈ S : v ← V ( s ) V ( s ) ← ∑ s ′ , r p ( s ′ , r ∣ s , π ( s ) ) [ r + γ V ( s ′ ) ] Δ ← max ⁡ ( Δ , ∣ v − V ( s ) ∣ ) until  Δ < θ  (a small positive number determining the accuracy of estimation)  3 Policy Improvement p o l i c y - s t a b l e ← t r u e For each  s ∈ S : V m a x ( s ) ← max ⁡ a ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ V ( s ′ ) ] Create an empty list:  n e w _ l i s t _ o f _ a ( s ) For each  a ∈ ∣ A ( s ) ∣ : If  ∑ s ′ , r p ( s ′ , r ∣ s , a ) [ r + γ V ( s ′ ) ]  is equal to  V m a x ( s ) : Append  a  to  n e w _ l i s t _ o f _ a ( s ) If  n e w _ l i s t _ o f _ a ( s )  is not equal to  o l d _ l i s t _ o f _ a ( s )  : p o l i c y - s t a b l e ← f a l s e o l d _ l i s t _ o f _ a ( s ) ← n e w _ l i s t _ o f _ a ( s ) i t e r a t o r _ o l d _ l i s t _ o f _ a ( s ) ← the beginning of  o l d _ l i s t _ o f _ a ( s ) else : If  o l d _ l i s t _ o f _ a ( s )  is empty : o l d _ l i s t _ o f _ a ( s ) ← n e w _ l i s t _ o f _ a ( s ) i t e r a t o r _ o l d _ l i s t _ o f _ a ( s ) ← the beginning of  o l d _ l i s t _ o f _ a ( s ) p o l i c y - s t a b l e ← f a l s e else : If  i t e r a t o r _ o l d _ l i s t _ o f _ a ( s )  is not equal to the end of  o l d _ l i s t _ o f _ a ( s )  : Move  i t e r a t o r _ o l d _ l i s t _ o f _ a ( s )  to next. p o l i c y - s t a b l e ← f a l s e If  i t e r a t o r _ o l d _ l i s t _ o f _ a ( s )  is not equal to the end of  o l d _ l i s t _ o f _ a ( s )  : π ( s ) ← Select  a  in  o l d _ l i s t _ o f _ a ( s )  by  i t e r a t o r _ o l d _ l i s t _ o f _ a ( s ) else :  π ( s ) ← Select  a  in  o l d _ l i s t _ o f _ a ( s )  randomly If  p o l i c y - s t a b l e = t r u e  then stop and return  V ≈ v ∗  and  π ≈ π ∗  else go to 2. \begin{aligned} &\text{1 Initialization} \\ &\qquad V(s) \in \mathbb R \text{ and } \pi (s) \in \mathcal A(s) \text{ arbitrarily } s \in \mathcal S \\ &\qquad \text{For each } s \in \mathcal S \text{ create an empty list : } old\_list\_of\_a(s)\\ &\qquad \text{For each } s \in \mathcal S \text{ create an iterator : } iterator\_old\_list\_of\_a(s)\\ &\text{2 Policy Evaluation} \\ &\qquad \text{Loop:} \\ &\qquad \qquad \Delta \leftarrow 0 \\ &\qquad \qquad \text{Loop for each } s \in \mathcal S: \\ &\qquad \qquad \qquad v \leftarrow V(s) \\ &\qquad \qquad \qquad V(s) \leftarrow \sum_{s',r}p(s',r \mid s,\pi(s)) \Bigl [ r + \gamma V(s')\Bigr ] \\ &\qquad \qquad \qquad \Delta \leftarrow \max (\Delta , |v-V(s)|) \\ &\qquad \text{until } \Delta \lt \theta \text{ (a small positive number determining the accuracy of estimation) } \\ &\text{3 Policy Improvement} \\ &\qquad policy\text-stable \leftarrow true \\ &\qquad \text{For each }s \in \mathcal S: \\ &\qquad \qquad V_{max}(s) \leftarrow \max_a \sum_{s',r}p(s',r \mid s,a) \Bigl [ r + \gamma V(s')\Bigr ] \\ &\qquad \qquad \text{Create an empty list: }new\_list\_of\_a(s)\\ &\qquad \qquad \text{For each }a \in |\mathcal A(s)|:\\ &\qquad \qquad \qquad \text{If }\sum_{s',r}p(s',r \mid s,a) \Bigl [ r + \gamma V(s')\Bigr ] \text{ is equal to } V_{max}(s):\\ &\qquad \qquad \qquad \qquad \text{Append }a \text{ to } new\_list\_of\_a(s)\\ &\qquad \qquad \text{If }new\_list\_of\_a(s) \text{ is not equal to } old\_list\_of\_a(s)\text{ :}\\ &\qquad \qquad \qquad policy\text{-}stable \leftarrow false \\ &\qquad \qquad \qquad old\_list\_of\_a(s) \leftarrow new\_list\_of\_a(s)\\ &\qquad \qquad \qquad iterator\_old\_list\_of\_a(s) \leftarrow \text{the beginning of }old\_list\_of\_a(s)\\ &\qquad \qquad \text{else :}\\ &\qquad \qquad \qquad \text{If }old\_list\_of\_a(s) \text{ is empty :}\\ &\qquad \qquad \qquad \qquad old\_list\_of\_a(s) \leftarrow new\_list\_of\_a(s)\\ &\qquad \qquad \qquad \qquad iterator\_old\_list\_of\_a(s) \leftarrow \text{the beginning of }old\_list\_of\_a(s)\\ &\qquad \qquad \qquad \qquad policy\text{-}stable \leftarrow false\\ &\qquad \qquad \qquad\text{else :}\\ &\qquad \qquad \qquad \qquad \text{If }iterator\_old\_list\_of\_a(s) \text{ is not equal to the end of }old\_list\_of\_a(s) \text{ :}\\ &\qquad \qquad \qquad \qquad \qquad \text{Move }iterator\_old\_list\_of\_a(s) \text{ to next.}\\ &\qquad \qquad \qquad \qquad \qquad policy\text{-}stable \leftarrow false\\ &\qquad \qquad \text{If }iterator\_old\_list\_of\_a(s) \text{ is not equal to the end of }old\_list\_of\_a(s) \text{ :}\\ &\qquad \qquad \qquad \pi(s) \leftarrow \text{Select }a \text{ in }old\_list\_of\_a(s) \text{ by } iterator\_old\_list\_of\_a(s)\\ &\qquad \qquad \text{else : }\\ &\qquad \qquad \qquad \pi(s) \leftarrow \text{Select }a \text{ in }old\_list\_of\_a(s) \text{ randomly}\\ &\qquad \text{If } policy\text-stable =true \text{ then stop and return } V \approx v_* \text{ and }\pi \approx \pi_* \text{ else go to 2.} \\ \end{aligned} 1 InitializationV(s)R and π(s)A(s) arbitrarily sSFor each sS create an empty list : old_list_of_a(s)For each sS create an iterator : iterator_old_list_of_a(s)2 Policy EvaluationLoop:Δ0Loop for each sS:vV(s)V(s)s,rp(s,rs,π(s))[r+γV(s)]Δmax(Δ,vV(s))until Δ<θ (a small positive number determining the accuracy of estimation) 3 Policy Improvementpolicy-stabletrueFor each sS:Vmax(s)amaxs,rp(s,rs,a)[r+γV(s)]Create an empty list: new_list_of_a(s)For each aA(s):If s,rp(s,rs,a)[r+γV(s)] is equal to Vmax(s):Append a to new_list_of_a(s)If new_list_of_a(s) is not equal to old_list_of_a(s) :policy-stablefalseold_list_of_a(s)new_list_of_a(s)iterator_old_list_of_a(s)the beginning of old_list_of_a(s)else :If old_list_of_a(s) is empty :old_list_of_a(s)new_list_of_a(s)iterator_old_list_of_a(s)the beginning of old_list_of_a(s)policy-stablefalseelse :If iterator_old_list_of_a(s) is not equal to the end of old_list_of_a(s) :Move iterator_old_list_of_a(s) to next.policy-stablefalseIf iterator_old_list_of_a(s) is not equal to the end of old_list_of_a(s) :π(s)Select a in old_list_of_a(s) by iterator_old_list_of_a(s)else : π(s)Select a in old_list_of_a(s) randomlyIf policy-stable=true then stop and return Vv and ππ else go to 2.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值