-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmonte_carlo.py
349 lines (284 loc) · 14.7 KB
/
monte_carlo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
class MCAgent:
def __init__(self, env, gamma=1.0):
import numpy as np
#------------------------------------------------------------
# Store environment and gamma
#------------------------------------------------------------
self.env = env
self.gamma = gamma
#------------------------------------------------------------
# Create empty V, Q, and policy
#------------------------------------------------------------
self.V = {}
self.Q = {}
self.policy = {}
#------------------------------------------------------------
# Create dict for tracking V values
#------------------------------------------------------------
self.V_history = []
def select_action(self, state):
from rltools.utils import encode_state
state = encode_state(state)
if state in self.policy.keys():
return self.policy[state]
return self.env.action_space.sample()
def prediction(self, policy, episodes, max_steps=None, alpha=None, alpha_decay=0.0,
epsilon=0.0, epsilon_decay=0.0, seed=None, show_progress=False):
'''
Evaluates the policy currenty attached to the agent.
'''
import numpy as np
from tqdm.auto import tqdm
from rltools.utils import DictAgent, generate_episode, set_seed, unset_seed
agent=DictAgent(self.env, policy)
#------------------------------------------------------------
# Set the seed
#------------------------------------------------------------
np_state = set_seed(seed)
#------------------------------------------------------------
# Create dict to count number of visits to each state
# Used only if alpha == None
#------------------------------------------------------------
visit_counts = {}
#------------------------------------------------------------
# Loop for specified number of episodes
#------------------------------------------------------------
if max_steps is None:
max_steps = float('inf')
rng = tqdm(range(episodes)) if show_progress else range(episodes)
for n in rng:
#------------------------------------------------------------
# Generate episode using policy.
# Obtain history and determine number of steps in episode
# Generate episde needs a seed to be used with gym env
#------------------------------------------------------------
gym_seed = np.random.choice(100*episodes)
history = generate_episode(
self.env, agent=agent, max_steps=max_steps,
epsilon=epsilon, seed=gym_seed, verbose=False)
num_steps = len(history['actions'])
#------------------------------------------------------------
# Calcuate returns
#------------------------------------------------------------
returns = []
Gt = 0
for t in reversed(range(num_steps)):
Gt = history['rewards'][t] + self.gamma * Gt
returns.insert(0, Gt)
#------------------------------------------------------------
# Loop over episode, updating V est for each visited state
#------------------------------------------------------------
visited = set()
for t in range(num_steps):
#------------------------------------------------------------
# Get state and exit if already visited
#------------------------------------------------------------
st = history['states'][t]
if st in visited:
continue
#------------------------------------------------------------
# Update visit information
#------------------------------------------------------------
visited.add(st)
visit_counts[st] = visit_counts.get(st, 0) + 1
#------------------------------------------------------------
# Perform update
#------------------------------------------------------------
Gt = returns[t]
V_st = self.V.get(st, 0)
if alpha is None:
self.V[st] = V_st + (Gt - V_st) / visit_counts[st]
else:
self.V[st] = V_st + alpha * (Gt - V_st)
#------------------------------------------------------------
# Decay alpha and epsilon
#------------------------------------------------------------
epsilon = epsilon * (1 - epsilon_decay)
if alpha is not None:
alpha = alpha * (1 - alpha_decay)
#------------------------------------------------------------
# Restore the global seed
#------------------------------------------------------------
unset_seed(np_state)
def control(self, episodes, max_steps=None, alpha=None, alpha_decay=0.0,
epsilon=0.0, epsilon_decay=0.0, seed=None, show_progress=False,
updates=None, eval_eps=100, check_success=False, track_V=False,
restore_best=True, return_best=False, alphas=None, epsilons=None,
exploring_starts=False, verbose=True):
import numpy as np
from tqdm.auto import tqdm
from rltools.utils import generate_episode, evaluate, encode_state, decode_state
from rltools.utils import set_seed, unset_seed
#------------------------------------------------------------
# Set the seed
#------------------------------------------------------------
np_state = set_seed(seed)
#------------------------------------------------------------
# Create dict to count number of visits to each state
# Used only if alpha == None
#------------------------------------------------------------
visit_counts = {}
#------------------------------------------------------------
# Number of actions needed for creating entries in Q
#------------------------------------------------------------
num_actions = self.env.action_space.n
#------------------------------------------------------------
# Create objects for storing best results
#------------------------------------------------------------
best_score = -float('inf')
best_pi = self.policy.copy()
best_Q = self.Q.copy()
best_V = self.V.copy()
#------------------------------------------------------------
# Loop for specified number of episodes
#------------------------------------------------------------
if max_steps is None:
max_steps = float('inf')
rng = tqdm(range(episodes)) if show_progress else range(episodes)
for n in rng:
if alphas is not None: alpha = alphas[n]
if epsilons is not None: epsilon = epsilons[n]
#------------------------------------------------------------
# Check if using exploring starts.
#------------------------------------------------------------
random_init_action = True if exploring_starts else False
init_state = None
if exploring_starts and len(self.Q) > 0:
init_state = np.random.choice(list(self.Q.keys()))
init_state = decode_state(self.env, init_state)
#------------------------------------------------------------
# Generate episode using policy.
#------------------------------------------------------------
episode_seed = np.random.choice(10**6)
history = generate_episode(
self.env, agent=self, max_steps=max_steps,
init_state=init_state, random_init_action=random_init_action,
epsilon=epsilon, seed=episode_seed, verbose=False)
#------------------------------------------------------------
# Calcuate returns
#------------------------------------------------------------
num_steps = len(history['actions'])
returns = []
Gt = 0
for t in reversed(range(num_steps)):
Gt = history['rewards'][t] + self.gamma * Gt
returns.insert(0, Gt)
#------------------------------------------------------------
# Loop over episode, updating Q estimate
#------------------------------------------------------------
visited = set()
updated = set()
for t in range(num_steps):
#------------------------------------------------------------
# Get state/action pair and exit if already visited
#------------------------------------------------------------
st, at = history['states'][t], history['actions'][t]
# Encode state
st = encode_state(st)
if (st,at) in visited:
continue
updated.add(st)
#------------------------------------------------------------
# Update visit information
#------------------------------------------------------------
visited.add((st,at))
visit_counts[(st,at)] = visit_counts.get((st,at), 0) + 1
#------------------------------------------------------------
# Perform update
#------------------------------------------------------------
Gt = returns[t]
if st not in self.Q.keys():
self.Q[st] = np.zeros(num_actions)
Qsa = self.Q[st][at]
if alpha is None:
self.Q[st][at] = Qsa + (Gt - Qsa) / visit_counts[(st,at)]
else:
self.Q[st][at] = Qsa + alpha * (Gt - Qsa)
#------------------------------------------------------------
# Decay alpha and epsilon
#------------------------------------------------------------
epsilon = epsilon * (1 - epsilon_decay)
if alpha is not None:
alpha = alpha * (1 - alpha_decay)
#------------------------------------------------------------
# Update V and policy
#------------------------------------------------------------
#for s in self.Q.keys():
for s in updated:
self.V[s] = np.max(self.Q[s])
self.policy[s] = np.argmax(self.Q[s])
#------------------------------------------------------------
# Report Results
#------------------------------------------------------------
if updates is not None and n == 0:
col_names = 'Episode Mean[Return] SD[Return] Mean[Length] SD[Length]'
if check_success: col_names += ' Success_Rate'
if verbose:
print(col_names, '\n', '-' * len(col_names), sep='')
if updates is not None and (n+1) % updates == 0:
#------------------------------------------------------------
# Evaluate Model
#------------------------------------------------------------
stats = evaluate(
self.env, self, self.gamma, episodes=eval_eps,
max_steps=max_steps, seed=episode_seed,
check_success=check_success, show_report=False
)
#------------------------------------------------------------
# Check for new best model
#------------------------------------------------------------
save_msg = ''
score = stats['mean_return'] - stats['stdev_return']
if score > best_score:
best_score = score
best_pi = self.policy.copy()
best_Q = self.Q.copy()
best_V = self.V.copy()
save_msg = '(Saving new best model)'
#------------------------------------------------------------
# Construct output
#------------------------------------------------------------
out = f'{n+1:<9}{stats["mean_return"]:>13.4f}{stats["stdev_return"]:>12.4f}'
out += f'{stats["mean_length"]:>14.4f}{stats["stdev_length"]:>12.4f}'
if check_success:
out += f'{stats["sr"]:>14.4f}'
out += f' {save_msg}'
if verbose:
print(out)
np.set_printoptions(suppress=True)
continue # skip stuff below for now
if track_V:
self.V_history.append(self.V.copy())
# Check Success Rate
if check_success is not None:
if i % check_success == 0:
sr, info = success_rate(
self.env, self.policy, n=100, max_steps=max_steps)
print(f'{i}: Success Rate: {sr:.4f}')
#------------------------------------------------------------
# Unset the seed
#------------------------------------------------------------
unset_seed(np_state)
#------------------------------------------------------------
# Restore and/or return best model
#------------------------------------------------------------
if restore_best:
self.policy = best_pi
self.Q = best_Q
self.V = best_V
if return_best:
return best_pi, best_Q
if __name__ == '__main__':
import gymnasium as gym
from gymnasium.envs.toy_text.frozen_lake import generate_random_map
from rltools.envs import FrozenLakeMod
from rltools.utils import generate_episode
env = gym.make(
'FrozenLake-v1',
desc=generate_random_map(size=10, p=0.9, seed=1),
max_episode_steps=1000,
render_mode='ansi'
)
mod_env = FrozenLakeMod(env, rew=[-1,-100, 100])
env.reset()
print(env.render())