2
2
3
3
## 蒙特卡洛方法简介
4
4
5
- - 使用蒙特卡洛方法不需要像DP一样,对环境要有完整的知识,而是通过** 经验** 去学习。所谓经验就是对状态、动作、奖励的采样(sample sequence)。
6
- - 用sample的均值去近似期望。
7
- - 使用蒙特卡洛通常需要完整的episode,因此蒙特卡洛的更新方式更像是** episode-by-episode** ,而不是像DP的step-by-step。
8
- - 优点:
9
- - 1.可以从实际经验中学习;
10
- - 2.可以从模拟的经验中学习;
11
- - 3.可以直接从感兴趣的state开始采样episode。
5
+ 使用蒙特卡洛方法不需要像DP一样,对环境要有完整的知识,而是通过** 经验** 去学习。所谓经验就是对状态、动作、奖励的采样(sample sequence)。
6
+
7
+ 用sample的均值去近似期望。
8
+
9
+ 使用蒙特卡洛通常需要完整的** episode** ,因此蒙特卡洛的更新方式更像是** episode-by-episode** ,而不是像DP的** step-by-step** 。
10
+
11
+ 优点:
12
+
13
+ - 1.可以从实际经验中学习;
14
+ - 2.可以从模拟的经验中学习;
15
+ - 3.可以直接从感兴趣的state开始采样episode。
12
16
13
17
## 蒙特卡洛预测(评估)
14
18
20
24
21
25
$V(s)\leftarrow average(Return(s))$
22
26
27
+ 核心代码:
28
+
29
+ ``` python
30
+ # Monte Carlo Sample with On-Policy
31
+ def monte_carlo_on_policy (episodes ):
32
+ ...
33
+ for i in range (0 , episodes):
34
+ # play接受一个策略,然后模拟生成一个完整的轨迹和奖励
35
+ _, reward, player_trajectory = play(target_policy_player)
36
+ ...
37
+ # 返回价值函数的平均值
38
+ return states_usable_ace / states_usable_ace_count, states_no_usable_ace / states_no_usable_ace_count
39
+ ```
40
+
23
41
## 蒙特卡洛评估动作价值函数(Action Values)
24
42
25
43
- ** 注意** :如果我们的问题中,没有对环境建模,那么单纯评估状态价值函数是不够的。我们必须要评估动作价值函数。
26
- - ** 主体思想** :从评估state到评估state -action对。
27
- - ** 可能存在的问题** :某些state-action对可能不会被访问(稀疏性问题)。
44
+ - ** 主体思想** :从评估 ** state ** 到评估 ** state -action对** 。
45
+ - ** 可能存在的问题** :某些state-action对可能不会被访问(** 稀疏性问题** )。
28
46
29
47
## 蒙特卡洛控制
30
48
@@ -36,7 +54,35 @@ $V(s)\leftarrow average(Return(s))$
36
54
37
55
- ** Exploring Starts** :所有的state-action对都有可能被选为episode的开始(start)。
38
56
39
- ![ ] ( ../res/mces.png )
57
+ ![ mces] ( ../res/mces.png )
58
+
59
+ 核心代码:
60
+
61
+ ``` python
62
+ # Monte Carlo with Exploring Starts
63
+ def monte_carlo_es (episodes ):
64
+ ...
65
+ # behavior policy is greedy
66
+ def behavior_policy (usable_ace , player_sum , dealer_card ):
67
+ ...
68
+ # get argmax of the average returns(s, a)
69
+ values_ = state_action_values[player_sum, dealer_card, usable_ace, :] / \
70
+ state_action_pair_count[player_sum, dealer_card, usable_ace, :]
71
+ return np.random.choice([action_ for action_, value_ in enumerate (values_) if value_ == np.max(values_)])
72
+
73
+ # play for several episodes
74
+ for episode in range (episodes):
75
+ # for each episode, use a randomly initialized state and action
76
+ initial_state = [bool (np.random.choice([0 , 1 ])),
77
+ np.random.choice(range (12 , 22 )),
78
+ np.random.choice(range (1 , 11 ))]
79
+ initial_action = np.random.choice(ACTIONS )
80
+ current_policy = behavior_policy if episode else target_policy_player
81
+ _, reward, trajectory = play(current_policy, initial_state, initial_action)
82
+ ...
83
+
84
+ return state_action_values / state_action_pair_count
85
+ ```
40
86
41
87
## 不使用Exploring Starts
42
88
@@ -47,7 +93,7 @@ $V(s)\leftarrow average(Return(s))$
47
93
48
94
- on-policy只有** 一套policy** ,更简单,是首选。
49
95
- off-policy使用** 两套policy** ,更复杂、更难收敛;但也更通用、更强大。
50
- - on-policy和off-policy本质依然是Exploit vs Explore的权衡 。
96
+ - on-policy和off-policy ** 本质 ** 依然是 ** Exploit vs Explore ** 的权衡 。
51
97
52
98
## on-policy
53
99
@@ -56,19 +102,19 @@ $V(s)\leftarrow average(Return(s))$
56
102
## off-policy
57
103
58
104
- 所有的MC控制方法都面临一个** 困境** :它们都想找到一个最优的策略,但却** 必须采用非最优的策略去尽可能多地探索** (explore)数据。
59
- - 直接使用** 两套策略** :采样用的policy称为` behavior policy ` ,最终的目标policy:` target policy ` 。这就是off-policy。
105
+ - 直接使用** 两套策略** :采样用的policy称为` behavior policy ` ,即 ** 行为策略 ** ; 最终的目标policy:` target policy ` ,即 ** 目标策略 ** 。这就是off-policy。
60
106
- 假设目标策略是$\pi$,行为策略是$b$,那么对于所有的$\pi(a|s)>0$必然有$b(a|s)>0$,这称为“覆盖”(coverage)。一个常见的例子是:行为策略使用价值函数的greedy policy,而目标策略使用ε-greedy policy。
61
107
62
108
## 重要性采样(importance sampling)
63
109
64
110
几乎所有的off-policy都使用** 重要性采样(importance sampling)** 。
65
111
66
- 为什么要使用重要性采样?我们希望在使用目标策略 $\pi$的情况下用均值估计价值的期望,但我们获得的是在使用行为策略 $b$的情况下的均值,也就是:$\mathbb{E}[ G_t \mid S_t =s] = v_b(s)$。这二者是有差距的。因此我们希望使用重要性采样去纠正 。
112
+ ** 为什么要使用重要性采样** ?我们希望在使用 ** 目标策略 ** $\pi$的情况下用均值估计价值的期望,但我们获得的是在使用 ** 行为策略 ** $b$的情况下的均值,也就是:$\mathbb{E}[ G_t \mid S_t =s] = v_b(s)$。这二者是有差距的。因此我们希望 ** 使用重要性采样去纠正 ** 。
67
113
68
- 给定初始状态$S_t$,后续的状态-动作轨迹在使用策略 $\pi$的情况下的概率为:
114
+ 给定初始状态$S_t$,后续的状态-动作轨迹在使用目标策略 $\pi$的情况下的概率为:
69
115
$Pr\{ At,S_ {t+1}, A_ {t+1}, ... S_T \mid S_t, A_ {t: T −1} \sim \pi\} $ $=\prod_ {k=t}^{T-1}\pi(A_k\mid S_k)p(S_ {k+1}\mid S_k, A_k)$
70
116
71
- 引入** 重要性采样比例(the importancesampling ratio)** :
117
+ 引入** 重要性采样比例(the importance sampling ratio)** :
72
118
$\rho_ {t: T −1}=\frac{\prod_ {k=t}^{T-1}\pi(A_k\mid S_k)p(S_ {k+1}\mid S_k, A_k)}{\prod_ {k=t}^{T-1}b(A_k\mid S_k)p(S_ {k+1}\mid S_k, A_k)}$ $=\prod_ {k=t}^{T-1}\frac{\pi(A_k\mid S_k)}{b(A_k\mid S_k)}$
73
119
上面这个式子正好巧妙地把MDP中未知的状态转移概率约掉。
74
120
@@ -94,3 +140,44 @@ odinary importance sampling vs. weighted importance sampling:
94
140
控制:
95
141
96
142
![ off_policy_mc_control] ( ../res/off_policy_mc_control.png )
143
+
144
+ 核心代码:
145
+
146
+ ``` python
147
+ # Monte Carlo Sample with Off-Policy
148
+ def monte_carlo_off_policy (episodes ):
149
+ initial_state = [True , 13 , 2 ]
150
+
151
+ rhos = []
152
+ returns = []
153
+
154
+ for i in range (0 , episodes):
155
+ _, reward, player_trajectory = play(behavior_policy_player, initial_state = initial_state)
156
+
157
+ # get the importance ratio
158
+ numerator = 1.0
159
+ denominator = 1.0
160
+ for (usable_ace, player_sum, dealer_card), action in player_trajectory:
161
+ if action == target_policy_player(usable_ace, player_sum, dealer_card):
162
+ denominator *= 0.5
163
+ else :
164
+ numerator = 0.0
165
+ break
166
+ rho = numerator / denominator
167
+ rhos.append(rho)
168
+ returns.append(reward)
169
+
170
+ rhos = np.asarray(rhos)
171
+ returns = np.asarray(returns)
172
+ weighted_returns = rhos * returns
173
+
174
+ weighted_returns = np.add.accumulate(weighted_returns)
175
+ rhos = np.add.accumulate(rhos)
176
+
177
+ ordinary_sampling = weighted_returns / np.arange(1 , episodes + 1 )
178
+
179
+ with np.errstate(divide = ' ignore' ,invalid = ' ignore' ):
180
+ weighted_sampling = np.where(rhos != 0 , weighted_returns / rhos, 0 )
181
+
182
+ return ordinary_sampling, weighted_sampling
183
+ ```
0 commit comments