forked from altf4/SmashBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RL.py
375 lines (279 loc) · 11.4 KB
/
RL.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import tensorflow as tf
import random
import ssbm
import ctypes
import tf_lib as tfl
import util
import ctype_util as ct
with tf.name_scope('input'):
input_states = ct.inputCType(ssbm.GameMemory, [None], "states")
# player 2's controls
input_controls = ct.inputCType(ssbm.SimpleControllerState, [None], "controls")
def feedStateActions(states, actions, feed_dict = None):
if feed_dict is None:
feed_dict = {}
ct.feedCTypes(ssbm.GameMemory, 'input/states', states, feed_dict)
ct.feedCTypes(ssbm.SimpleControllerState, 'input/controls', actions, feed_dict)
return feed_dict
embedFloat = lambda t: tf.reshape(t, [-1, 1])
castFloat = lambda t: embedFloat(tf.cast(t, tf.float32))
def one_hot(size):
return lambda t: tf.one_hot(tf.cast(t, tf.int64), size, 1.0, 0.0)
maxAction = 512 # altf4 says 0x017E
actionSpace = 32
maxCharacter = 32 # should be large enough?
maxJumps = 8 # unused
with tf.variable_scope("embed_action"):
actionHelper = tfl.makeAffineLayer(maxAction, actionSpace)
def embedAction(t):
return actionHelper(one_hot(maxAction)(t))
def rescale(a):
return lambda x: a * x
playerEmbedding = [
("percent", util.compose(rescale(0.01), castFloat)),
("facing", embedFloat),
("x", util.compose(rescale(0.01), embedFloat)),
("y", util.compose(rescale(0.01), embedFloat)),
("action_state", embedAction),
# ("action_counter", castFloat),
("action_frame", util.compose(rescale(0.02), castFloat)),
("character", one_hot(maxCharacter)),
("invulnerable", castFloat),
("hitlag_frames_left", castFloat),
("hitstun_frames_left", castFloat),
("jumps_used", castFloat),
("charging_smash", castFloat),
("in_air", castFloat),
('speed_air_x_self', embedFloat),
('speed_ground_x_self', embedFloat),
('speed_y_self', embedFloat),
('speed_x_attack', embedFloat),
('speed_y_attack', embedFloat)
]
# TODO: give the tensors some names/scopes
def embedStruct(embedding):
def f(struct):
embed = [op(struct[field]) for field, op in embedding]
return tf.concat(1, embed)
return f
embedPlayer = embedStruct(playerEmbedding)
def embedArray(embed, indices=None):
def f(array):
return tf.concat(1, [embed(array[i]) for i in indices])
return f
maxStage = 64 # overestimate
stageSpace = 32
with tf.variable_scope("embed_stage"):
stageHelper = tfl.makeAffineLayer(maxStage, stageSpace)
def embedStage(stage):
return stageHelper(one_hot(maxStage)(stage))
gameEmbedding = [
('players', embedArray(embedPlayer, [0, 1])),
#('frame', c_uint),
('stage', embedStage)
]
embedGame = embedStruct(gameEmbedding)
embedded_states = embedGame(input_states)
state_size = embedded_states.get_shape()[-1].value
stickEmbedding = [
('x', embedFloat),
('y', embedFloat)
]
embedStick = embedStruct(stickEmbedding)
controllerEmbedding = [
('button_A', castFloat),
('button_B', castFloat),
('button_X', castFloat),
('button_Y', castFloat),
('button_L', castFloat),
('button_R', castFloat),
('trigger_L', embedFloat),
('trigger_R', embedFloat),
('stick_MAIN', embedStick),
('stick_C', embedStick),
]
embedController = embedStruct(controllerEmbedding)
def embedEnum(enum):
return one_hot(len(enum))
simpleControllerEmbedding = [
('button', embedEnum(ssbm.SimpleButton)),
('stick_MAIN', embedEnum(ssbm.SimpleStick)),
]
embedSimpleController = embedStruct(simpleControllerEmbedding)
#embedded_controls = embedController(train_controls)
embedded_controls = embedSimpleController(input_controls)
control_size = embedded_controls.get_shape()[-1].value
assert(control_size == 7)
with tf.variable_scope("q_net"):
q1 = tfl.makeAffineLayer(state_size + control_size, 512, tf.tanh)
q2 = tfl.makeAffineLayer(512, 1)
def q(states, controls):
state_actions = tf.concat(1, [states, controls])
with tf.name_scope('q1'):
q1_layer = q1(state_actions)
return tf.squeeze(q2(q1_layer), name='q'), q1_layer
# pre-computed long-term rewards
rewards = tf.placeholder(tf.float32, [None], name='rewards')
global_step = tf.Variable(0, name='global_step', trainable=False)
with tf.name_scope('train_q'):
qPredictions, q1_layer = q(embedded_states, embedded_controls)
qLosses = tf.squared_difference(qPredictions, rewards)
qLoss = tf.reduce_mean(qLosses)
#trainQ = tf.train.RMSPropOptimizer(0.0001).minimize(qLoss)
train_q = tf.train.AdamOptimizer().minimize(qLoss, global_step=global_step)
# opt = tf.train.AdamOptimizer()
# opt = tf.train.GradientDescentOptimizer(0.0)
# grads_and_vars = opt.compute_gradients(qLoss)
# train_q = opt.apply_gradients(grads_and_vars)
with tf.name_scope('epsilon'):
epsilon = tf.maximum(0.05, 0.7 - tf.cast(global_step, tf.float32) / 5000.0)
def getEpsilon():
return sess.run(epsilon)
#with tf.name_scope('temperature'):
# temperature = 20.0 * 0.5 ** (tf.cast(global_step, tf.float32) / 1000.0) + 1.0
""" no more actor
with tf.variable_scope("actor"):
layers = [state_size, 64]
nls = [tf.tanh] * (len(layers) - 1)
zip_layers = zip(layers[:-1], layers[1:])
applyLayers = [tfl.makeAffineLayer(prev, next, nl) for (prev, next), nl in zip(zip_layers, nls)]
button_layer = tfl.makeAffineLayer(layers[-1], len(ssbm.SimpleButton._fields_), util.compose(tf.nn.softmax, lambda x: x / temperature))
stick_layer = tfl.makeAffineLayer(layers[-1], len(ssbm.SimpleStick._fields_), util.compose(tf.nn.softmax, lambda x: x / temperature))
def applyActor(state):
for f in applyLayers:
state = f(state)
button_state = button_layer(state)
stick_state = stick_layer(state)
return tf.concat(1, [button_state, stick_state])
actor_variables = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='actor')
#print(actor_variables)
with tf.name_scope("actorQ"):
actions = applyActor(embedded_states)
actorQ = tf.reduce_mean(q(embedded_states, actions)[0])
#trainActor = tf.train.RMSPropOptimizer(0.001).minimize(-actorQ)
# FIXME: is this the right sign?
#trainActor = tf.train.RMSPropOptimizer(0.0001).minimize(-actorQ, var_list=actor_variables)
# trainActor = tf.train.AdamOptimizer().minimize(-actorQ, var_list=actor_variables)
actor_opt = tf.train.AdamOptimizer()
actor_grads_and_vars = opt.compute_gradients(-actorQ, var_list=actor_variables)
trainActor = opt.apply_gradients(actor_grads_and_vars, global_step=global_step)
def deepMap(f, obj):
if isinstance(obj, dict):
return {k : deepMap(f, v) for k, v in obj.items()}
if isinstance(obj, list):
return [deepMap(f, x) for x in obj]
return f(obj)
with tf.name_scope('predict'):
predict_state = inputCType(ssbm.GameMemory, [None], 'state')
#reshaped = deepMap(lambda t: tf.reshape(t, [1]), predict_input_state)
embedded_state = embedGame(predict_state)
predict_action = inputCType(ssbm.SimpleControllerState, [None], 'action')
e
"""
sess = tf.Session()
summaryWriter = tf.train.SummaryWriter('logs/', sess.graph)
summaryWriter.flush()
saver = tf.train.Saver(tf.all_variables())
#def predictAction(state):
# feed_dict = tfl.feedCType(ssbm.GameMemory, 'predict/state', state)
# return sess.run('predict/action:0', feed_dict)
def predictQ(states, controls):
return sess.run(qPredictions, feedStateActions(states, controls))
# TODO: do this within the tf model?
# if we do, then we can add in softmax and temperature
def scoreActions(state):
return predictQ([state] * len(ssbm.simpleControllerStates), ssbm.simpleControllerStates)
# see https://docs.google.com/spreadsheets/d/1JX2w-r2fuvWuNgGb6D3Cs4wHQKLFegZe2jhbBuIhCG8/edit#gid=13
dyingActions = set(range(0xA))
def isDying(player):
return player.action_state in dyingActions
# players tend to be dead for many frames in a row
# here we prune all but the first frame of the death
def processDeaths(deaths):
return util.zipWith(lambda prev, next: (not prev) and next, [False] + deaths[:-1] , deaths)
# from player 2's perspective
def computeRewards(states, reward_halflife = 2.0):
# reward_halflife is measured in seconds
fps = 60.0
discount = 0.5 ** ( 1.0 / (fps*reward_halflife) )
kills = [isDying(state.players[0]) for state in states]
deaths = [isDying(state.players[1]) for state in states]
# print(states[random.randint(0, len(states))].players[0])
kills = processDeaths(kills)
deaths = processDeaths(deaths)
# print("Deaths for current memory: ", sum(deaths))
# print("Kills for current memory: ", sum(kills))
damage_dealt = [max(states[i+1].players[0].percent - states[i].players[0].percent, 0) for i in range(len(states)-1)]
# damage_dealt = util.zipWith(lambda prev, next: max(next.players[0].percent - prev.players[0].percent, 0), states[:-1], states[1:])
scores = util.zipWith(lambda x, y: x - y, kills[1:], deaths[1:])
final_scores = util.zipWith(lambda x, y: x + y / 100, scores, damage_dealt)
# print("Damage for current memory: ", sum(damage_dealt))
# print("Scores for current memory: ", final_scores[:1000])
# use last action taken?
lastQ = max(scoreActions(states[-1]))
discounted_rewards = util.scanr(lambda r1, r2: r1 + discount * r2, lastQ, final_scores)[:-1]
# print("discounted_rewards for current memory: ", discounted_rewards[:])
return discounted_rewards
# return util.scanr(lambda r1, r2: r1 + discount * r2, lastQ, damage_dealt)[:-1]
def readFile(filename, states=None, controls=None):
if states is None:
states = []
if controls is None:
controls = []
with open(filename, 'rb') as f:
for i in range(60 * 60):
states.append(ssbm.GameMemory())
f.readinto(states[-1])
controls.append(ssbm.SimpleControllerState())
f.readinto(controls[-1])
# should be zero
# print(len(f.read()))
return states, controls
def train(filename, steps=1):
states, controls = readFile(filename)
feed_dict = {rewards : computeRewards(states)}
feedStateActions(states[:-1], controls[:-1], feed_dict)
# FIXME: we feed the inputs in on each iteration, which might be inefficient.
for step_index in range(steps):
# for _ in range(1):
# gs = sess.run([gv[0] for gv in actor_grads_and_vars], feed_dict)
# vs = sess.run([gv[1] for gv in actor_grads_and_vars], feed_dict)
# loss = sess.run(qLoss, feed_dict)
# act_4 = sess.run(q1_layer, feed_dict)
#t = sess.run(temperature)
#print("Temperature: ", t)
# import numpy as np
# print("act_4", act_4)
# print("grad/param(0)", np.mean(np.abs(gs[0] / vs[0])))
# print("grad/param(2)", np.mean(np.abs(gs[2] / vs[2])))
# print("grad/param(4)", np.mean(np.abs(gs[4] / vs[4])))
# print("grad", np.mean(np.abs(gs[4])))
# print("param", np.mean(np.abs(vs[0])))
# if step_index == 10:
# import pdb; pdb.set_trace()
# print(sum(gvs))
#sess.run([train_q, trainActor], feed_dict)
sess.run(train_q, feed_dict)
# sess.run(trainQ, feed_dict)
#sess.run(trainActor, feed_dict)
#print(sess.run([qLoss, actorQ], feed_dict))
print(sess.run(qLoss, feed_dict))
def save(filename='saves/simpleDQN'):
print("Saving to", filename)
saver.save(sess, filename)
def restore(filename='saves/simpleDQN'):
saver.restore(sess, filename)
def writeGraph():
import os
graph_def = tf.python.client.graph_util.convert_variables_to_constants(sess, sess.graph_def, ['predict/action'])
tf.train.write_graph(graph_def, 'models/', 'simpleDQN.pb.temp', as_text=False)
try:
os.remove('models/simpleDQN.pb')
except:
print("No previous model file.")
os.rename('models/simpleDQN.pb.temp', 'models/simpleDQN.pb')
def init():
sess.run(tf.initialize_all_variables())
#train('testRecord0')
#saver.restore(sess, 'saves/simpleDQN')
#writeGraph()