-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmountain_car.py
327 lines (281 loc) · 9.51 KB
/
mountain_car.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
import numpy as np
import matplotlib.pyplot as plt
import gym
from tqdm import trange
import sys
from os.path import dirname, join, realpath
dir_path = dirname(dirname(realpath(__file__)))
sys.path.insert(1, join(dir_path, 'utils'))
from tile_coding import IHT, tiles
import math
class ValueFunction:
def __init__(self, n_tilings, env):
'''
n_tilings: int
number of tilings used for tile coding
env: OpenAI's MountainCar env
'''
self.n_tilings = n_tilings
self.position_scale = n_tilings / (env.high[0] - env.low[0])
self.velocity_scale = n_tilings / (env.high[1] - env.low[1])
# size = math.ceil((self.position_scale + 1) * (self.velocity_scale + 1) * n_tilings)
size = 4096
self.iht = IHT(size)
self.w = np.zeros(size)
self.env = env
def get_active_tiles(self, position, velocity, action):
'''
Get (indices of) active tiles corresponding to
state-action pair [[@position, @velocity], @action]
(i.e., index of the tile in each tilings where the value = 1)
Params
------
position: float
current position of the car
velocity: float
current velocity of the car
action: int
action taken at the current state
Return
------
active_tiles: list
'''
active_tiles = tiles(self.iht, self.n_tilings, [position *
self.position_scale, self.velocity_scale * velocity], [action])
return active_tiles
def get_value(self, position, velocity, action):
'''
Get action-value of state-action pair [[@position, @velocity], @action]
Since the feature vector is one-hot and
we are using linear function approx
=> value at [[@position, @velocity], @action] is exactly the
total of weight corresponding to [[@position, @velocity], @action]
Params
------
position: float
current position of the car
velocity: float
current velocity of the car
action: int
action taken at the current state
'''
active_tiles = self.get_active_tiles(position, velocity, action)
return np.sum(self.w[active_tiles])
def learn(self, position, velocity, action, target, alpha):
'''
Update weight vector
Params
------
position: float
current position of the car
velocity: float
current velocity of the car
action: int
action taken at the current state
alpha: float
step size param
'''
active_tiles = self.get_active_tiles(position, velocity, action)
estimate = np.sum(self.w[active_tiles])
error = target - estimate
for tile in active_tiles:
self.w[tile] += alpha / self.n_tilings * error
def cost_to_go(self, position, velocity, n_actions):
'''
Get cost-to-go at the current state [@position, @velocity]
Params
------
position: float
current position of the car
velocity: float
current velocity of the car
n_actions: int
number of actions
'''
costs = np.array([self.get_value(position, velocity, action_)
for action_ in range(n_actions)])
return -np.max(costs)
def epsilon_greedy(epsilon, value_function, position, velocity, n_actions):
'''
Epsilon-greedy policy
Params:
-------
epsilon: float
value_function: ValueFunction
action-value function
position: float
current position of the car
velocity: float
current velocity of the car
n_actions: int
number of actions
Return
------
action: int
'''
if not np.random.binomial(1, epsilon):
values = np.array([value_function.get_value(position, velocity, action_)
for action_ in range(n_actions)])
action = np.argmax(values)
else:
action = np.random.randint(n_actions)
return action
def episodic_semi_gradient_sarsa(value_function, env, alpha, gamma, epsilon,
current_ep, n_eps):
'''
Episodic Semi-gradient Sarsa algorithm
Params
------
value_function: ValueFunction
action-value function
env: OpenAI's MountainCar
alpha: float
step size
gamma: float
discount factor
epsilon: float
epsilon greedy param
current_ep: int
current epside
n_eps: int
total number of episodes
'''
n_actions = env.action_space.n
state = env.reset()
action = epsilon_greedy(epsilon, value_function,
state[0], state[1], n_actions)
while True:
if current_ep + 10 >= n_eps:
env.render()
next_state, reward, terminated, _ = env.step(action)
position, velocity = state
next_position, next_velocity = next_state
if terminated:
value_function.learn(position, velocity, action, reward, alpha)
break
next_action = epsilon_greedy(epsilon,
value_function, next_position, next_velocity, n_actions)
value_function.learn(position, velocity, action,
reward + gamma * value_function.get_value(
next_position, next_velocity, next_action), alpha)
state = next_state
action = next_action
def episodic_semi_gradient_n_step_sarsa(value_function, env, n,
alpha, gamma, epsilon):
'''
Episodic Semi-gradient n-step Sarsa algorithm
Params
------
value_function: ValueFunction
action-value function
env: OpenAI's MountainCar
n: int
n-step
alpha: float
step size
gamma: float
discount factor
epsilon: float
epsilon greedy param
'''
n_actions = env.action_space.n
state = env.reset()
action = epsilon_greedy(epsilon, value_function,
state[0], state[1], n_actions)
states = [state]
actions = [action]
rewards = [0] # 0 is a dummy reward
T = float('inf')
t = 0
while True:
if t < T:
next_state, reward, terminated, _ = env.step(actions[t])
states.append(next_state)
rewards.append(reward)
if terminated:
T = t + 1
else:
next_action = epsilon_greedy(epsilon, value_function,
next_state[0], next_state[1], n_actions)
actions.append(next_action)
tau = t - n + 1
if tau >= 0:
G = 0
for i in range(tau + 1, min(tau + n, T) + 1):
G += np.power(gamma, i - tau - 1) * rewards[i]
if tau + n < T:
G += np.power(gamma, n) * value_function.get_value(
states[tau + n][0], states[tau + n][1], actions[tau + n])
value_function.learn(states[tau][0], states[tau][1],
actions[tau], G, alpha)
t += 1
if tau == T - 1:
break
return t
def episodic_semi_gradient_sarsa_plot():
n_eps = 9000
alpha = 0.3
gamma = 1
epsilon = 0.1
n_tilings = 8
plot_eps = [0, 99, 399, 999, 3999, n_eps - 1]
fig = plt.figure(figsize=(24, 16))
fig.subplots_adjust(wspace=0.25, hspace=0.25)
plot_count = 0
env = gym.make('MountainCar-v0')
env.reset()
value_function = ValueFunction(n_tilings, env)
for ep in trange(n_eps):
episodic_semi_gradient_sarsa(value_function, env, alpha,
gamma, epsilon, ep, n_eps)
if ep in plot_eps:
ax = fig.add_subplot(2, 3, plot_count + 1, projection='3d')
plot_count += 1
positions = np.linspace(env.low[0], env.high[0])
velocities = np.linspace(env.low[1], env.high[1])
axis_x = []
axis_y = []
axis_z = []
for position in positions:
for velocity in velocities:
axis_x.append(position)
axis_y.append(velocity)
axis_z.append(value_function.cost_to_go(position,
velocity, env.action_space.n))
ax.scatter(axis_x, axis_y, axis_z)
ax.set_xlabel('Position')
ax.set_ylabel('Velocity')
ax.set_zlabel('Cost to go')
ax.set_title('Episode %d' % (ep + 1))
plt.savefig('./mountain-car-ep-semi-grad-sarsa.png')
plt.close()
def episodic_semi_gradient_n_step_sarsa_plot():
runs = 100
n_eps = 500
n_tilings = 8
ns = [1, 8]
alphas = [0.5, 0.3]
gamma = 1
epsilon = 0
env = gym.make('MountainCar-v0')
env.reset()
steps = np.zeros((len(alphas), n_eps))
for run in range(runs):
value_functions = [ValueFunction(n_tilings, env) for _ in alphas]
for alpha_idx in range(len(alphas)):
for ep in trange(n_eps):
step = episodic_semi_gradient_n_step_sarsa(value_functions[alpha_idx],
env, ns[alpha_idx], alphas[alpha_idx], gamma, epsilon)
steps[alpha_idx, ep] += step
steps /= runs
for i in range(0, len(alphas)):
plt.plot(steps[i], label='n = %d' % (ns[i]))
plt.xlabel('Episode')
plt.ylabel('Steps per episode')
plt.yscale('log')
plt.legend()
plt.savefig('./mountain-car-ep-semi-grad-n-step-sarsa.png')
plt.close()
if __name__ == '__main__':
# episodic_semi_gradient_sarsa_plot()
episodic_semi_gradient_n_step_sarsa_plot()