-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq-learn.py
328 lines (280 loc) · 7.65 KB
/
q-learn.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
import random
from copy import deepcopy, copy
from math import inf
import tensorflow as tf
from tensorflow import keras
import numpy as np
# Learning rate
ALPHA = 0.001
# Size of the gameboard
SIZE = 4
# Value function approximaters
try:
# Load models from disk if possible
THETA = [keras.models.load_model("tf_model0.h5"), keras.models.load_model("tf_model1.h5"),
keras.models.load_model("tf_model2.h5"), keras.models.load_model("tf_model3.h5")]
except BaseException as e:
print("\n\n!======== UNABLE TO LOAD MODELS - CREATING MODELS... ==========!\n\n")
model1 = keras.Sequential([
keras.layers.Dense(4, activation='relu', input_shape=(16,)),
keras.layers.Dense(1)
])
model2 = keras.Sequential([
keras.layers.Dense(4, activation='relu', input_shape=(16,)),
keras.layers.Dense(1)
])
model3 = keras.Sequential([
keras.layers.Dense(4, activation='relu', input_shape=(16,)),
keras.layers.Dense(1)
])
model4 = keras.Sequential([
keras.layers.Dense(4, activation='relu', input_shape=(16,)),
keras.layers.Dense(1)
])
THETA = np.array([model1, model2, model3, model4])
for model in THETA:
model.compile(optimizer='SGD', loss=tf.keras.losses.Hinge(), metrics=['accuracy'])
def is_terminal(state) -> bool:
"""
Checks if game has ended. Pure function.
Returns: bool indicating whether or not game has ended.
"""
end = True
for i in range(SIZE ** 2):
end = not ((state[i] == 0) or (
(i + 1) % SIZE != 0 and state[i] == state[i + 1]) or (
i + SIZE < SIZE ** 2 and state[i] == state[i + SIZE]))
if end is False:
break
return end
def spawn_piece(state):
"""
Spawns a piece in the state. A 2 with probability 0.9 and 4 with probability 0.1. Pure function.
Returns: State with new piece spawned in it.
"""
arange = range(len(state))
n_state = deepcopy(state)
avail = [x for x in arange if state[x] == 0]
if not avail:
return state
n_state[random.choice(avail)] = np.random.choice([1, 2], p=[0.9, 0.1])
return n_state
def combiner(arange, state):
"""
Combines numbers along range, propagates out from start. This is a support method for actions. Non-pure function.
Args:
arange: Range to combine along
state: Game state
Returns: Tuple of reward and whether or not any tiles moved
"""
prev = 0
reward = 0
moved = False
for i in range(1, len(arange)):
cur_val = state[arange[i]]
if cur_val != 0:
prev_val = state[arange[prev]]
# Can combine in direction of swipe
if prev_val == state[arange[i]]:
state[arange[prev]] += 1
reward += 2 ** prev_val
state[arange[i]] = 0
prev += 1
moved = True
# Can't combine (or zero)
elif prev_val != cur_val:
# Zero
if prev_val != 0:
prev += 1
if prev == i:
continue
state[arange[prev]] = cur_val
state[arange[i]] = 0
moved = True
return reward, moved
def swipe_left(state):
"""
Moves pieces to the left. Non-pure function.
Returns: Tuple of reward and whether or not any tiles moved
"""
reward = 0
moved = False
for i in range(0, SIZE ** 2, SIZE):
arange = range(i, SIZE + i)
r, m = combiner(arange, state)
reward += r
moved = m or moved
return reward, moved
def swipe_right(state):
"""
Moves pieces to the right. Non-pure function.
Returns: Tuple of reward and whether or not any tiles moved
"""
reward = 0
moved = False
for i in range(SIZE - 1, SIZE ** 2, SIZE):
arange = range(i, i - SIZE, -1)
r, m = combiner(arange, state)
reward += r
moved = m or moved
return reward, moved
def swipe_up(state):
"""
Moves pieces up. Non-pure function.
Returns: Tuple of reward and whether or not any tiles moved
"""
reward = 0
moved = False
for i in range(0, SIZE, 1):
arange = range(i, SIZE ** 2, SIZE)
r, m = combiner(arange, state)
reward += r
moved = m or moved
return reward, moved
def swipe_down(state):
"""
Moves pieces down. Non-pure function.
Returns: Tuple of reward and whether or not any tiles moved
"""
reward = 0
moved = False
for i in range((SIZE - 1) * SIZE, SIZE ** 2, 1):
arange = range(i, -1, -SIZE)
r, m = combiner(arange, state)
reward += r
moved = m or moved
return reward, moved
def evaluate(state, action):
"""
Returns the estimated value of a state-action pair
Args:
state: Game state
action: Action to be taken from the state
Returns: Estimated value
"""
# Prediction is nested list
return THETA[action].predict(tf.convert_to_tensor([state]))[0][0]
def compute_afterstate(state, action):
"""
Computes the afterstate (before random piece spawn). Pure function.
Args:
state: Game state
action: Action to execute (integer)
Returns: Tuple of state after executing action and reward gained by action
"""
s_prime = deepcopy(state)
if action == 0:
reward, m = swipe_left(s_prime)
elif action == 1:
reward, m = swipe_right(s_prime)
elif action == 2:
reward, m = swipe_up(s_prime)
elif action == 3:
reward, m = swipe_down(s_prime)
else:
raise ValueError("Incorrect move")
return s_prime, reward
def make_move(state, action):
"""
Makes a move.
Args:
state: State of the game
action: Action [0-3]
Returns: Reward, state after action, state after action and generating tile
"""
s_prime, reward = compute_afterstate(state, action)
s_dprime = spawn_piece(s_prime)
return reward, s_prime, s_dprime
def learn_evaluation(state, action, reward, s_prime, s_dprime):
"""
Trains the value approximation function
Args:
state: Game state
action: Action
reward: Reward for taking action from state
s_prime: State after action but before random tile generation
s_dprime: Final state after action
Returns: None
"""
v_next = np.max([evaluate(s_dprime, i) for i in range(3)])
THETA[action].fit(tf.convert_to_tensor([s_prime]), tf.convert_to_tensor([reward + v_next]), verbose=0)
def get_moves(state):
"""
Checks which moves are available
Args:
state: Game state
Returns: List of legal moves
"""
s1 = copy(state)
s2 = copy(state)
s3 = copy(state)
s4 = copy(state)
actions = []
if swipe_left(s1)[1] * 1 == 1:
actions.append(0)
if swipe_right(s2)[1] * 1 == 1:
actions.append(1)
if swipe_up(s3)[1] * 1 == 1:
actions.append(2)
if swipe_down(s4)[1] * 1 == 1:
actions.append(3)
return actions
def play_game():
"""
Plays the game. Can optionally enable training of the value function approximater
Returns: Final score
"""
learning_enabled = True
score = 0
# Init
state = np.zeros(16)
state = spawn_piece(state)
# While not terminal
while not is_terminal(state):
# argmax
max_val = -inf
action = -1
moves = get_moves(state)
assert moves != []
for i in moves:
ret = evaluate(state, i)
if ret > max_val:
max_val = ret
action = i
# Make move
assert action != -1
reward, s_prime, s_dprime = make_move(state, action)
# Train value approximater
if learning_enabled:
learn_evaluation(state, action, reward, s_prime, s_dprime)
score += reward
# Advance state
state = s_dprime
return score
def main():
"""
This setup isn't exact to what was used to generate empirical results. Running this in its current state
will take FOREVER (days or weeks).
Returns: None
"""
learning = True
scores = np.zeros(10000)
for x in range(10000):
try:
ret_score = play_game()
print("Game: ", x, " Score: ", ret_score)
scores[x] = ret_score
except BaseException as er:
print(er)
if learning:
for i in range(len(THETA)):
THETA[i].save("tf_model" + str(i) + ".h5")
return
print("Max: ", np.max(scores), " Min: ", np.min(scores), " Avg: ", np.average(scores))
print("STDEV: ", np.std(scores), " Median: ", np.median(scores))
if learning:
# print("Saving model at game "+str(x))
for i in range(len(THETA)):
THETA[i].save("tf_model" + str(i) + ".h5")
if __name__ == "__main__":
main()