-
Notifications
You must be signed in to change notification settings - Fork 0
/
policy_gradient.py
295 lines (211 loc) · 7.76 KB
/
policy_gradient.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
import numpy as np
import matplotlib.pyplot as plt
import gym
import random
# TODO: explain following
ENV_NAME = "CarRacing-v0"
EPISODE_DURATION = 15000
EPISODE_DURATION_AUGM = 25
ALPHA_INIT = 0.01 # (can also decay this over time..)
SCORE = 300
TEST_TIME = 0
TEST_TIME_AUGM = 1
LEFT = np.array([-1, 0, 0])
RIGHT = np.array([1, 0, 0])
GAMMA = 0.99
EPSILON = 0.05
VERBOSE = True
#########################################################################################
# First: we accelerate once every two frames and choose left or right each time
DISCRETE_ACTION_SPACE = [np.array([-1, 0, 0]), np.array([1, 0, 0])]
# [np.array([-1, 0, 0]), np.array([1, 0, 0]), np.array([0, 1, 0]), np.array([0, 0, 0]), np.array([0, 0, 1])]
#########################################################################################
# from complex to simple image (observation)
def preprocess(rgb):
'''
Simplifie l'image. PAsse de RGB à gray et enlève le base inutile.
'''
end_img = 84
gray = np.dot(rgb[...,:3], [0.0, 0.5, 0.5])
gray[gray>150] = 180
return gray[:84]
# get useful informations from an observation
def capteur(observation):
'''
Position du nez de la voiture codé en dur pour l'instant.
Cette fonction renvoie les quatres distances d'intérêt !
'''
i_nose = 67
j_left = 46
j_right = 49
grass_color = 180
road_color = 102
# informations horizontales
horizontal = np.where(observation[67] == grass_color)[0]
try:
hori_gauche = 46 - horizontal[horizontal < 46][-1]
except:
hori_gauche = 46
try:
hori_droite = horizontal[horizontal > 49][0] - 49
except:
hori_droite = 46
# informations verticales
vertical_gauche = np.where(observation[:, 46] == grass_color)[0]
try:
verti_gauche = 67 - vertical_gauche[vertical_gauche < 67][-1]
except:
verti_gauche = 67
vertical_droite = np.where(observation[:, 49] == grass_color)[0]
try:
verti_droite = 67 - vertical_droite[vertical_droite < 67][-1]
except:
verti_droite = 67
res = np.array([hori_gauche, hori_droite, verti_gauche, verti_droite])
res[res == 1] == 0
return res/20
# a kind of complete preprocessing of the images observed
def useful_from_observation(rgb):
'''
From an observation (or a state), which is a 96x96 RGB image, we return 4 distances, which will be used by our classifier.
'''
gray = preprocess(rgb)
return capteur(gray)
#################################################################################
# transparent
def sigmoid(x):
x = np.clip(x, -100, 100)
return 1.0 / (1.0 + np.exp(-x))
# Return policy
def get_policy(s, theta):
p_right = sigmoid(np.dot(s, np.transpose(theta)))
# pi = [1-p_right, p_right]
# return pi
return 2*p_right - 1
# Draw an action according to current policy
def act_with_policy(s, theta):
# p_right = get_policy(s, theta)[1]
p_right = get_policy(s, theta)
r = np.random.rand()
# if r < EPSILON:
# return np.array([1, 0, 0])
#else:
# return np.array([-1, 0, 0])
turn = np.clip(np.array([p_right + r*EPSILON]), a_min = -1, a_max = 1)[0][0]
return np.array([turn, 0.0, 0.0])
# Generate an episode
def gen_rollout(env, theta, max_episode_length=EPISODE_DURATION, render=False):
s_t = env.reset()
s_t = useful_from_observation(s_t)
s_t = np.concatenate((s_t, np.array([0, 0, 0, 0])))
episode_states = []
episode_actions = []
episode_rewards = []
episode_states.append(s_t)
to_stop = 15
for t in range(max_episode_length):
if render:
env.render()
a_t = act_with_policy(s_t, theta)
if t%6 == 0:
a_t += np.array([0.0, 1.0, 0.0])
s_t, r_t, done, info = env.step(a_t)
# transform the state to a usable one
s_t = useful_from_observation(s_t)
old_s_t = episode_states[-1][:4]
s_t = np.concatenate((s_t, s_t-old_s_t))
# working on the reward
# r_t += -(s_t[0]**2 + s_t[1]**2)*100
if ((s_t[:4]==np.array([0.05, 0.05, 0.05, 0.05])).all()):
r_t = r_t - 1
to_stop = to_stop - 1
elif (s_t[0]==0.05 or s_t[1]==0.05):
r_t = r_t - 0.5
else:
r_t += 0.2-(s_t[0]**2 + s_t[1]**2)*100
episode_states.append(s_t)
episode_actions.append(a_t)
episode_rewards.append(r_t)
if done or to_stop==0:
break
return episode_states, episode_actions, episode_rewards
def test_policy(env, theta, score = SCORE, num_episodes = TEST_TIME , max_episode_length=EPISODE_DURATION, render=False):
num_success = 0
average_return = 0
for i_episode in range(num_episodes):
_, _, episode_rewards = gen_rollout(env, theta, max_episode_length, render)
total_rewards = sum(episode_rewards)
if total_rewards > score:
num_success+=1
average_return += (1.0 / num_episodes) * total_rewards
if render:
print("Test Episode {0}: Total Reward = {1} - Success = {2}".format(i_episode,total_rewards,total_rewards>score))
if average_return > score:
success = True
else:
success = False
return success, num_success, average_return
# Returns policy gradient for a given episode
def compute_PG(episode_states, episode_actions, episode_rewards, theta):
H = len(episode_rewards)
PG = 0
H2 = 50
for t in range(H):
if H == 1000 and H - t < 100:
break
pi = get_policy(episode_states[t], theta)
a_t = episode_actions[t]
# R_t = sum(np.array(episode_rewards[t::])*np.array([GAMMA**i for i in range(0, H-t)]))
R_t = sum(episode_rewards[t::t+H2])
#if (a_t[0] == 1):
# g_theta_log_pi = - pi[1] * episode_states[t] * R_t
#else:
# g_theta_log_pi = pi[0] * episode_states[t] * R_t
g_theta_log_pi = pi * episode_states[t] * R_t
PG += g_theta_log_pi
return PG
# Train until average_return is larger than SCORE
def train(env, theta_init, max_episode_length = EPISODE_DURATION, alpha_init = ALPHA_INIT):
theta = theta_init
i_episode = 0
average_returns = []
success, _, R = test_policy(env, theta, render=True)
average_returns.append(R)
new_test_time = TEST_TIME
# Train until success
while (not success):
# Rollout
episode_states, episode_actions, episode_rewards = gen_rollout(env, theta, max_episode_length, render=True)
# Schedule step size
alpha = alpha_init / (1 + i_episode)
# Compute gradient
PG = compute_PG(episode_states, episode_actions, episode_rewards, theta)
# Do gradient ascent
theta += alpha * PG
# Test new policy
# new_test_time += TEST_TIME_AUGM
success, _, R = test_policy(env, theta, score=SCORE, num_episodes=new_test_time, max_episode_length= max_episode_length, render=True)
# Monitor
average_returns.append(R)
i_episode += 1
if VERBOSE:
print("Episode {0}, average return: {1}".format(i_episode, R))
return theta, i_episode, average_returns
def main():
env = gym.make(ENV_NAME)
# dim = env.observation_space.shape[0]
# Init parameters to random
theta_init = np.random.randn(1, 8)
# Train agent
theta, i, average_returns = train(env, theta_init)
print("Solved after {} iterations".format(i))
# Test final policy
test_policy(env,theta, num_episodes=10, render=True)
# Show training curve
plt.plot(range(len(average_returns)),average_returns)
plt.title("Average reward on 100 episodes")
plt.xlabel("Training Steps")
plt.ylabel("Reward")
plt.show()
if __name__ == "__main__":
main()