forked from netanelhugi/Blackjack-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBJ.py
More file actions
438 lines (348 loc) · 15.3 KB
/
Copy pathBJ.py
File metadata and controls
438 lines (348 loc) · 15.3 KB
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
import gym
import random
import numpy as np
class BlackJack(object):
def __init__(self):
# create enviroment
self.env = gym.make('Blackjack-v0')
# crate Q-table
self.action_size = self.env.action_space.n # 2
self.state_size = self.env.observation_space.spaces # [32,11,2]
self.Q_learning_table = dict()
self.init_Q_table()
# Init the Q-table with tuples(the keys) of all states:
def init_Q_table(self):
for i in range(1, 33):
for j in range(1, 12):
for k in range(1, 3):
if (k == 1):
state = (i, j, False)
else:
state = (i, j, True)
self.Q_learning_table[state] = dict((action, 0.0) for action in range(self.action_size))
# Function for manual game mode:
# games = number of games to play.
def userPlay(self, games):
# random.seed(100)
# Statistics counters:
winsUser = 0
drawsUser = 0
lossesUser = 0
bonusUser = 0
rewardsUser = []
for i in range(games):
print("NEW GAME: ")
state = self.env.reset()
self.stateDesc(state, self.env, False)
# Blackjack - ace and 10/jack/queen/king.
if (state[0] == 21):
reward = 1.5
bonusUser += 1
rewardsUser.append(reward)
print("Player won!")
print("Your reward: ", reward)
else:
action = int(input("1-hit or 0-stand:"))
while (True):
if (action != 0 and action != 1):
action = int(input("Wrong Value! - 1-hit or 0-stand:"))
else:
state, reward, done, _ = self.env.step(action)
if (action == 0):
self.stateDesc(state, self.env, True)
else:
self.stateDesc(state, self.env, False)
if (done == False):
action = int(input("good Value! - 1-hit or 0-stand:"))
else:
if (reward == -1):
lossesUser += 1
print("Dealer won!")
elif (reward == 0):
drawsUser += 1
print("Draw!")
else:
winsUser += 1
print("Player won!")
print(reward)
rewardsUser.append(reward)
print()
break
print("Total results: wind= %d, draws= %d, losses= %d, rewards= %.1f"%(winsUser,drawsUser,lossesUser,sum(rewardsUser)))
# Function for random agent game mode:
# games = number of games to play.
def randomPlay(self, games):
# Set collection of random numbers:
# random.seed(1000)
# Statistics counters:
rewardsRand = [] # Rewards counter(change to 0 every 1000 games)
avgRewardsRand = [] # Count the rewards of every 1000 games.
bonusRand = 0 # Count the games that end in blackjack.
lossesRand = 0
drawsRand = 0
winsRand = 0
totalRewardsRand = [] # Rewards counter(of all the games).
for i in range(games):
state = self.env.reset()
# Blackjack - ace and 10/jack/queen/king.
if (state[0] == 21):
reward = 1.5
bonusRand += 1
rewardsRand.append(reward)
else:
action = self.env.action_space.sample()
while (True):
state, reward, done, _ = self.env.step(action)
if (done == False):
action = self.env.action_space.sample()
else:
if (reward == -1):
lossesRand += 1
elif (reward == 0):
drawsRand += 1
else:
winsRand += 1
rewardsRand.append(reward)
totalRewardsRand.append(reward)
break
# Calculate average of every 1000 games.
if (i % 1000 == 0 and i > 999):
avgRewardsRand.append(sum(rewardsRand))
rewardsRand = []
summery = [winsRand, drawsRand, lossesRand, bonusRand, sum(totalRewardsRand)]
avgLossRand = np.mean(avgRewardsRand)
# Return - statistics, Average loss in every 1000 games:
return summery, avgLossRand
# Function for random agent game mode:
# games = number of games to play.
def autoPlay(self, games):
# Set collection of random numbers:
# random.seed(1000)
# Statistics counters:
winsAuto = 0
drawsAuto = 0
lossesAuto = 0
bonusAuto = 0 # Count the games that end in blackjack.
rewardsAuto = [] # Rewards counter(change to 0 every 1000 games)
avgRewardsAuto = [] # Count the rewards of every 1000 games.
totalRewardsAuto = [] # Rewards counter(of all the games).
for i in range(games):
state = self.env.reset()
# Blackjack - ace and 10/jack/queen/king.
if (state[0] == 21):
reward = 1.5
bonusAuto += 1
rewardsAuto.append(reward)
else:
if (sum(self.env.player) >= 18):
action = 0
else:
action = 1
while (True):
state, reward, done, _ = self.env.step(action)
if (done == False):
action = self.env.action_space.sample()
else:
if (reward == -1):
lossesAuto += 1
elif (reward == 0):
drawsAuto += 1
else:
winsAuto += 1
rewardsAuto.append(reward)
totalRewardsAuto.append(reward)
break
# Calculate average of every 1000 games.
if (i % 1000 == 0 and i > 999):
avgRewardsAuto.append(sum(rewardsAuto))
rewardsAuto = []
summery = [winsAuto, drawsAuto, lossesAuto, bonusAuto, sum(totalRewardsAuto)]
avgLossAuto = np.mean(avgRewardsAuto)
# Return - statistics, Average loss in every 1000 games:
return summery, avgLossAuto
# State description:
#
# print:
# 1. The player hand.
# 2. The dealer hand.
def stateDesc(self, state, env, stay):
print("Player:", env.player, "Sum: %d" % state[0])
if (stay == True):
if ((env.dealer[0] == 1 and env.dealer[1] == 10) or (env.dealer[1] == 1 and env.dealer[0] == 10)):
print("Dealer:", env.dealer, " Sum: 21")
else:
print("Dealer:", env.dealer, " Sum: ", sum(env.dealer))
else:
print("Dealer: [%d]" % env.dealer[0], " Sum: ", env.dealer[0])
# The learning process:
# alpha = learning rate.
# gamma = discount factor.
# printAvg = boolean, print the average loss every 100 training episodes.
def training(self, games, alpha, gamma, printAvg):
# Exploration parameters
epsilon = 1.0 # Exploration rate
max_epsilon = 1.0 # Exploration probability at start
min_epsilon = 0.01 # Minimum exploration probability
decay_rate = 0.005 # Exponential decay rate for exploration prob
# Statistics counters:
winsTrain = 0
drawsTrain = 0
lossesTrain = 0
rewardsTrain = []
for i in range(games):
state = self.env.reset()
gameover = False
while not gameover: # until the game is over.
rand = random.random()
# choice --> exploration
# at first we need more exploration
if rand < epsilon:
action = self.env.action_space.sample()
else:
# argMax
action = max(self.Q_learning_table[state], key=self.Q_learning_table[state].get)
newState, reward, gameover, _ = self.env.step(action)
self.Q_learning_table[state][action] = self.Q_learning_table[state][action] + alpha * (
reward + gamma * max(self.Q_learning_table[newState].values()) - self.Q_learning_table[state][
action])
state = newState
if (reward == -1):
lossesTrain += 1
elif (reward == 0):
drawsTrain += 1
else:
winsTrain += 1
# Reduce epsilon (because we need less and less exploration)
epsilon = min_epsilon + (max_epsilon - min_epsilon) * np.exp(-decay_rate * i)
rewardsTrain.append(reward)
# Testing the model after every 100 rounds of training:
if (i % 100 == 0 and i > 99):
# random.seed(50)
summeryTesting, avgTesting = self.testing(1001)
if (printAvg):
print("Average loss for 1000 rounds and %d training episodes: %.2f" % (i, avgTesting))
# Testing after learning:
# gamesTest = number of games to play.
def testing(self, gamesTest):
# Set collection of random numbers:
# random.seed(1000)
# Statistics counters:
winsTest = 0
drawsTest = 0
lossesTest = 0
bonusTest = 0
rewardsTest = []
avgRewardsTest = []
totalRewardTest = []
for j in range(gamesTest):
state = self.env.reset()
gameover = False
# Blackjack - ace and 10/jack/queen/king.
if (state[0] == 21):
reward = 1.5
bonusTest += 1
gameover = True
while not gameover: # until game over, or max number of steps
# argMax
action = max(self.Q_learning_table[state], key=self.Q_learning_table[state].get)
newState, reward, gameover, _ = self.env.step(action)
state = newState
if (reward == -1):
lossesTest += 1
elif (reward == 0):
drawsTest += 1
elif (reward == 1):
winsTest += 1
rewardsTest.append(reward)
totalRewardTest.append(reward)
# Calculate average of every 1000 games.
if (j % 1000 == 0 and j > 999):
avgRewardsTest.append(sum(rewardsTest))
rewardsTest = []
summery = [winsTest, drawsTest, lossesTest, bonusTest, sum(totalRewardTest)]
avgLossAuto = np.mean(avgRewardsTest)
# Return - statistics, Average loss:
return summery, avgLossAuto
# For the strategy table (in the final report):
# Convert the dictionary to 2 matrix:
# Matrix1 = the states with ace(in the player hand)
# Matrix2 = the states without ace.
def dictToTable(self):
w, h = 11, 32;
Matrix1 = [[0 for x in range(w)] for y in range(h)] # usable
Matrix2 = [[0 for x in range(w)] for y in range(h)] # no usable
for i in range(2, 32):
for j in range(1, 11):
stateTrue = (i, j, True)
stateFalse = (i, j, False)
if (max(self.Q_learning_table[stateTrue], key=self.Q_learning_table[stateTrue].get) == 1):
Matrix1[i][j] = "H"
elif (max(self.Q_learning_table[stateTrue], key=self.Q_learning_table[stateTrue].get) == 0):
Matrix1[i][j] = "S"
if (max(self.Q_learning_table[stateFalse], key=self.Q_learning_table[stateFalse].get) == 1):
Matrix2[i][j] = "H"
elif (max(self.Q_learning_table[stateFalse], key=self.Q_learning_table[stateFalse].get) == 0):
Matrix2[i][j] = "S"
return Matrix1, Matrix2
# Function for manual game mode(after learning):
# games = number of games to play.
#
# In every step, the player get advice of what action to take(according to the Q-table)
def userPlayAfterTraining(self, games):
# random.seed(100)
# Statistics counters:
winsUser = 0
drawsUser = 0
lossesUser = 0
bonusUser = 0
rewardsUser = []
for i in range(games):
print("NEW GAME: ")
state = self.env.reset()
self.stateDesc(state, self.env, False)
# Blackjack - ace and 10/jack/queen/king.
if (state[0] == 21):
reward = 1.5
bonusUser += 1
rewardsUser.append(reward)
print("Player won!")
print("Your reward: ", reward)
else:
advice = max(self.Q_learning_table[state], key=self.Q_learning_table[state].get)
if(advice==1):
action = int(input("1-hit or 0-stay(Recommended action: Hit):"))
elif(advice==0):
action = int(input("1-hit or 0-stay(Recommended action: Stand):"))
while (True):
if (action != 0 and action != 1):
if (advice == 1):
action = int(input("Wrong Value! - 1-hit or 0-stay(Recommended action: Hit):"))
elif (advice == 0):
action = int(input("Wrong Value! - 1-hit or 0-stay(Recommended action: Stand):"))
else:
state, reward, done, _ = self.env.step(action)
if (action == 0):
self.stateDesc(state, self.env, True)
else:
self.stateDesc(state, self.env, False)
if (done == False):
advice = max(self.Q_learning_table[state], key=self.Q_learning_table[state].get)
if (advice == 1):
action = int(input("1-hit or 0-stay(Recommended action: Hit):"))
elif (advice == 0):
action = int(input("1-hit or 0-stay(Recommended action: Stand):"))
else:
if (reward == -1):
lossesUser += 1
print("Dealer won!")
elif (reward == 0):
drawsUser += 1
print("Draw!")
else:
winsUser += 1
print("Player won!")
print(reward)
rewardsUser.append(reward)
print()
break
print("Total results: wind= %d, draws= %d, losses= %d, rewards= %.1f"%(winsUser,drawsUser,lossesUser,sum(rewardsUser)))