-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_free.py
314 lines (229 loc) · 9.12 KB
/
model_free.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
import gym # 0.24.0
from gym.core import ObservationWrapper
import numpy as np
import time
from collections import defaultdict
class QLearningAgent:
def __init__(self, alpha: float, gamma: float, epsilon: float, get_legal_actions: callable):
self.get_legal_actions = get_legal_actions
self.q_values = defaultdict(lambda: defaultdict(lambda: 0))
self.alpha = alpha
self.gamma = gamma
self.epsilon = epsilon
def get_q_value(self, state, action):
""" Returns Q(state,action) """
return self.q_values[state][action]
def set_q_value(self, state, action, value):
""" Sets the Q_value for [state,action] to the given value """
self.q_values[state][action] = value
def get_value(self, state):
"""
Compute your agent's estimate of V(s) using current q_values
V(s) = max_over_action Q(state,action) over possible actions.
"""
possible_actions = self.get_legal_actions(state)
# If there are no legal actions, return 0.0
if len(possible_actions) == 0:
return 0.0
value = max([
self.get_q_value(state, action) for action in possible_actions
])
return value
def update(self, state, action, reward, next_state):
"""
Q-Value update:
Q(s,a) := (1 - alpha) * Q(s,a) + alpha * (r + gamma * V(s'))
"""
# agent parameters
gamma = self.gamma
lr = self.alpha
new_value = (1 - lr) * self.get_q_value(state, action) + lr * (reward + gamma * self.get_value(next_state))
self.set_q_value(state, action, new_value)
def get_best_action(self, state):
"""
Compute the best action to take in a state (using current q-values).
"""
possible_actions = self.get_legal_actions(state)
# If there are no legal actions, return None
if len(possible_actions) == 0:
return None
best_action = max(
possible_actions,
key=lambda action: self.get_q_value(state, action)
)
return best_action
def get_action(self, state):
"""
Compute the action to take in the current state, including exploration.
With probability self.epsilon, we should take a random action.
otherwise - the best policy action (self.get_best_action).
"""
# Pick Action
possible_actions = self.get_legal_actions(state)
# If there are no legal actions, return None
if len(possible_actions) == 0:
return None
if np.random.random() < self.epsilon:
chosen_action = np.random.choice(possible_actions)
else:
chosen_action = self.get_best_action(state)
return chosen_action
class SARSAAgent(QLearningAgent):
def update(self, state, action, reward, next_state):
"""
Q-Value update:
Q(s,a) := (1 - alpha) * Q(s,a) + alpha * (r + gamma * V(s'))
"""
# agent parameters
gamma = self.gamma
lr = self.alpha
next_action = self.get_action(next_state)
new_value = (1 - lr) * self.get_q_value(state, action) + lr * (reward + gamma * self.get_q_value(next_state, next_action))
self.set_q_value(state, action, new_value)
class MonteCarloAgent:
def __init__(self, gamma: float, epsilon: float, get_legal_actions: callable):
self.get_legal_actions = get_legal_actions
self.q_values = defaultdict(lambda: defaultdict(lambda: 0))
self.gamma = gamma
self.epsilon = epsilon
self.n_values = defaultdict(lambda: defaultdict(lambda: 0))
def get_q_value(self, state, action):
""" Returns Q(state,action) """
return self.q_values[state][action]
def get_n_value(self, state, action):
""" Returns Q(state,action) """
return self.n_values[state][action]
def update(self, states, actions, rewards):
"""
Q-Value update:
Q(s,a) := (1 - alpha) * Q(s,a) + alpha * (r + gamma * V(s'))
"""
# agent parameters
gamma = self.gamma
G = np.zeros(len(rewards))
G[-1] = rewards[-1]
for t in range(len(rewards) - 2, -1, -1):
G[t] = rewards[t] + gamma * G[t + 1]
for t in range(len(rewards)):
self.q_values[states[t]][actions[t]] += (G[t] - self.q_values[states[t]][actions[t]]) / (self.n_values[states[t]][actions[t]] + 1)
self.n_values[states[t]][actions[t]] += 1
def get_best_action(self, state):
"""
Compute the best action to take in a state (using current q-values).
"""
possible_actions = self.get_legal_actions(state)
# If there are no legal actions, return None
if len(possible_actions) == 0:
return None
best_action = max(
possible_actions,
key=lambda action: self.get_q_value(state, action)
)
return best_action
def get_action(self, state):
"""
Compute the action to take in the current state, including exploration.
With probability self.epsilon, we should take a random action.
otherwise - the best policy action (self.get_best_action).
"""
# Pick Action
possible_actions = self.get_legal_actions(state)
# If there are no legal actions, return None
if len(possible_actions) == 0:
return None
if np.random.random() < self.epsilon:
chosen_action = np.random.choice(possible_actions)
else:
chosen_action = self.get_best_action(state)
return chosen_action
class Binarizer(ObservationWrapper):
def observation(self, state):
n_digits = [1, 1, 1, 1]
state = [round(x, n) for x, n in zip(state, n_digits)]
return tuple(state)
def fit_q_learning_agent(env, agent, episode_n: int, session_len: int):
total_rewards = []
for episode in range(episode_n):
total_reward = 0.0
state = env.reset()
for _ in range(session_len):
# get agent to pick action given state
action = agent.get_action(state)
next_state, reward, done, _ = env.step(action)
# train (update) agent for state
agent.update(state, action, reward, next_state)
state = next_state
total_reward += reward
if done:
break
total_rewards.append(total_reward)
if episode % 100 == 0:
print(f'iteration: {episode}, eps: {agent.epsilon:.5f}, mean reward: {np.mean(total_rewards[-10:]):.1f}')
agent.epsilon = 1 - (episode / episode_n)
return total_rewards
def fit_monte_carlo_agent(env, agent, episode_n: int, session_len: int):
total_rewards = []
for episode in range(episode_n):
state = env.reset()
states, actions, rewards = [], [], []
for _ in range(session_len):
states.append(state)
# get agent to pick action given state
action = agent.get_action(state)
actions.append(action)
next_state, reward, done, _ = env.step(action)
rewards.append(reward)
state = next_state
if done:
break
total_rewards.append(sum(rewards))
# train (update) agent for state
agent.update(states, actions, rewards)
if episode % 100 == 0:
print(f'iteration: {episode}, eps: {agent.epsilon:.5f}, mean reward: {np.mean(total_rewards[-10:]):.1f}')
agent.epsilon = 1 - (episode / episode_n)
return total_rewards
def visualize(env, agent, max_len=1000):
trajectory = {'states': [], 'actions': [], 'rewards': []}
obs = env.reset()
state = obs
for _ in range(max_len):
trajectory['states'].append(state)
action = agent.get_action(state)
trajectory['actions'].append(action)
obs, reward, done, _ = env.step(action)
trajectory['rewards'].append(reward)
state = obs
time.sleep(0.03)
env.render()
if done:
break
return trajectory
if __name__ == '__main__':
env = Binarizer(gym.make('CartPole-v1'))
env.reset()
n_states = env.observation_space.shape
n_actions = env.action_space.n
print(f"n_states: {n_states}, n_actions: {n_actions}")
q_learning_agent = QLearningAgent(
alpha=0.5,
gamma=0.99999,
epsilon=1,
get_legal_actions=lambda s: range(n_actions)
)
q_learning_rewards = fit_q_learning_agent(env, q_learning_agent, episode_n=10_000, session_len=500)
sarsa_agent = SARSAAgent(
alpha=0.5,
gamma=0.99999,
epsilon=1,
get_legal_actions=lambda s: range(n_actions)
)
sarsa_rewards = fit_q_learning_agent(env, sarsa_agent, episode_n=10_000, session_len=500)
monte_carlo_agent = MonteCarloAgent(
gamma=0.999999,
epsilon=1,
get_legal_actions=lambda s: range(n_actions)
)
monte_carlo_rewards = fit_monte_carlo_agent(env, monte_carlo_agent, episode_n=10_000, session_len=10_000)
sarsa_agent.epsilon = 0
visualize(env, sarsa_agent)