-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack_solver.py
executable file
·340 lines (286 loc) · 10.4 KB
/
blackjack_solver.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
#!/usr/bin/python
from collections import deque, namedtuple
import matplotlib.pyplot as plt
import numpy as np
import tabulate
BASEDECK = [4] * 9 + [16]
BLACKJACK = 21
ACE_PLUS = 10
PLAYER_OPTS = {
"split": False,
"double": False,
"surrender": True,
}
DEALER_OPTS = {
"target": 17,
"peeks": False, # NotImplemented
"hit_soft_target": False, # NotImplemented
}
LOG_OPTS = {
"totals": (0, 2),
"dealer_stand_prob": (False, 21),
"stand_win_prob": False,
"optimal_policy": True,
"floatfmt": ".4f",
"plot": True,
}
class Solver:
"""Optimal policy solver."""
def __init__(self) -> None:
self.policy = None
self.values = None
self.exp_val = None
def optimal_policy(self):
"""Solve for optimal policy."""
self.policy = np.zeros((HANDS.len, DECK.len), dtype=np.uint8)
self.exp_val = np.zeros(self.policy.shape)
for h in range(HANDS.len - 1, -1, -1):
hand = HANDS[h]
exp_val_stand = (
PLAYER.prob_if_stand["win"][h] - PLAYER.prob_if_stand["lose"][h]
)
if not DRAWS[h]:
self.exp_val[h] = exp_val_stand
continue
for dealer_card in range(DECK.len):
cards_left = DECK - hand
if cards_left[dealer_card]:
cards_left[dealer_card] -= 1
num_cards = DECK.sum - HANDS.sum[h] - 1
exp_val_hit = 0
total_prob = 0
for nxt, card_drawn in DRAWS[h]:
if cards_left[card_drawn]:
prob = cards_left[card_drawn] / num_cards
total_prob += prob
exp_val_hit += self.exp_val[nxt][dealer_card] * prob
exp_val_hit -= 1 - total_prob
if exp_val_hit > exp_val_stand[dealer_card]:
self.policy[h, dealer_card] = 1
self.exp_val[h, dealer_card] = exp_val_hit
else:
self.exp_val[h, dealer_card] = exp_val_stand[dealer_card]
if HANDS.sum[h] == 2:
# Surrender
if (
PLAYER.options.surrender
and self.exp_val[h, dealer_card] < -0.5
):
self.policy[h, dealer_card] = 2
self.exp_val[h, dealer_card] = -0.5
else:
self.exp_val[h, dealer_card] = np.nan
class Deck(np.ndarray):
"""Deck of cards"""
def __new__(cls):
return np.array(BASEDECK, dtype=np.uint8).view(cls)
def __init__(self):
self.htoi = None
self.len = len(self)
self.sum = np.sum(self)
def get_hands_and_draws(self):
"""Get all possible hands and draws"""
draws = dict()
q = deque()
empty_hand = np.zeros(self.len, dtype=np.uint8)
q.append((empty_hand, 0))
while q:
hand, min_score = q.popleft()
t_hand = tuple(hand)
if t_hand in draws:
continue
draws[t_hand] = []
card = 0
min_score += 1
while card < self.len and min_score <= BLACKJACK:
if hand[card] < self[card]:
hand[card] += 1
draws[t_hand].append((tuple(hand), card))
q.append((hand.copy(), min_score))
hand[card] -= 1
card += 1
min_score += 1
hands = Hands(*sorted_hands(draws.keys()))
draws = [
[(hands.htoi[dest], card) for dest, card in draws[tuple(hand)]]
for hand in hands
]
return hands, draws
def sorted_hands(hands):
hands = sorted(hands, key=lambda x: sum(x))
htoi = {h: i for i, h in enumerate(hands)}
return hands, htoi
class Hands(np.ndarray):
def __new__(cls, hands, _):
return np.array(hands, dtype=np.uint8).view(cls)
def __init__(self, _, htoi):
self.htoi = htoi
self.len = len(self)
self.sum = np.sum(self, axis=-1, dtype=np.uint8)
def get_scores(self):
card_vals = np.arange(1, len(self[0]) + 1, dtype=np.uint8)
scores = self @ card_vals
scores_plus = scores + ACE_PLUS
self.scores = np.where(
np.logical_and(self[:, 0], scores_plus <= BLACKJACK),
scores_plus,
scores,
)
PlayerOptions = namedtuple("PlayerOptions", ["split", "double", "surrender"])
class Player:
def __init__(self, **options):
self.options = PlayerOptions(**options)
self.prob_if_stand = None
def get_stand_value(self):
self.prob_if_stand = np.zeros(
(HANDS.len, DECK.len),
dtype=[
("win", float),
("lose", float),
],
)
def get_total_prob(inds):
v = np.full(self.prob_if_stand.shape, np.nan)
for i in range(HANDS.len):
for j in range(DECK.len):
if not any(np.isnan(DEALER.prob_stand[i, j])):
k = DEALER.prob_stand[i, j, inds[i] :]
v[i][j] = np.sum(k, axis=-1)
return v
val = HANDS.scores - DEALER.target
self.prob_if_stand["win"] = 1 - get_total_prob(
np.where(HANDS.scores > DEALER.target, val, 0)
)
self.prob_if_stand["lose"] = get_total_prob(
np.where(HANDS.scores < DEALER.target, 0, val + 1)
)
class Dealer:
def __init__(self, target, peeks, hit_soft_target):
self.target = target
self.peeks = peeks
self.hit_soft = hit_soft_target
self.paths = None
self.inds_stand_hands = None
self.prob_stand = None
def get_paths(self):
self.paths = np.zeros_like(HANDS, dtype=np.uint8)
for nxt, card in DRAWS[0]:
self.paths[nxt, card] = 1
for h in range(1, HANDS.len):
if HANDS.scores[h] >= self.target:
continue
for nxt, card in DRAWS[h]:
self.paths[nxt] += self.paths[h]
def get_stand_hands(self):
self.inds_stand_hands = np.logical_and(
np.any(self.paths, axis=-1),
HANDS.scores >= self.target,
).nonzero()[0]
def get_stand_probability(self):
deck_left = DECK - HANDS
stand_scores = BLACKJACK - self.target + 1
self.prob_stand = np.zeros((HANDS.len, DECK.len, stand_scores))
for h in self.inds_stand_hands:
num_cards_left = DECK.sum - HANDS.sum - 1
cards_to_draw = np.nonzero(HANDS[h])[0]
denominator = np.where(
np.all(deck_left >= HANDS[h], axis=-1),
1
/ np.prod(
[num_cards_left - i for i in range(HANDS.sum[h] - 1)],
axis=0,
dtype=float,
),
0,
)
prob = np.tile(
self.paths[h].astype(float),
(HANDS.len, 1),
)
for fc in cards_to_draw:
prob[:, fc] *= (
np.prod(
[
deck_left[:, c] - i
for c in cards_to_draw
for i in np.arange(c == fc, HANDS[h, c])
],
axis=0,
dtype=float,
)
* denominator
)
self.prob_stand[:, :, HANDS.scores[h] - self.target] += prob
def log():
ops = LOG_OPTS
cond1 = np.logical_not(np.all(SOLVER.policy, axis=-1))
cond2 = False
for t in ops["totals"]:
cond2 = np.logical_or(cond2, HANDS.sum == t)
inds = np.nonzero(np.logical_and(cond1, cond2))
charmap = {i: str(i + 1) for i in range(DECK.len)}
charmap[9] = "K"
charmap[0] = "A"
hands = HANDS[inds]
hands = [
["".join([charmap[i] * c for i, c in enumerate(hand) if c])] for hand in hands
]
headers = ["hand"] + [charmap[i] for i in range(DECK.len)]
def Tabulate(arr):
return tabulate.tabulate(
[hands[i] + k for i, k in enumerate(arr)],
headers=headers,
floatfmt=ops["floatfmt"],
)
if ops["dealer_stand_prob"][0]:
target = ops["dealer_stand_prob"][1]
prob_dealer = DEALER.prob_stand[inds]
prob_dealer = prob_dealer[:, :, target - DEALER.target].astype(float).tolist()
print(f"Dealer prob to stand at {target} (PLAYER hand vs. dealer card)")
print(Tabulate(prob_dealer))
if ops["stand_win_prob"]:
prob = PLAYER.prob_if_stand["win"][inds].astype(float).tolist()
print("\nWin Probability if stand (PLAYER hand vs. dealer card)")
print(Tabulate(prob))
if ops["optimal_policy"]:
pol = SOLVER.policy[inds].astype(int).tolist()
print("\nOptimal Policy (PLAYER hand vs. dealer card)")
print("1=Hit 0=Stand")
print(Tabulate(pol))
game_exp_val = (SOLVER.exp_val[0] @ DECK) / DECK.sum
print("\nGame EV:", game_exp_val)
if ops["plot"]:
strmap = {0: "S", 1: "H", 2: "U"}
clrmap = {0: "black", 1: "white", 2: "white"}
fig, ax = plt.subplots(1, 1, figsize=(3.5, 8))
ax.imshow(SOLVER.policy[inds], cmap="binary")
ax.set_title("Optimal Policy")
ax.set_xlabel("Dealer Card")
ax.set_ylabel("Player Hand")
ax.set_xticks(np.arange(DECK.len))
ax.set_yticks(np.arange(len(hands)))
ax.set_xticklabels(headers[1:])
ax.set_yticklabels([hand[0] for hand in hands])
for i in range(DECK.len):
for j in range(len(hands)):
ax.text(
i,
j,
strmap[SOLVER.policy[inds][j, i]],
ha="center",
va="center",
color=clrmap[SOLVER.policy[inds][j, i]],
)
fig.savefig("optimal_policy.png")
SOLVER = Solver()
PLAYER = Player(**PLAYER_OPTS)
DEALER = Dealer(**DEALER_OPTS)
DECK = Deck()
HANDS, DRAWS = DECK.get_hands_and_draws()
HANDS.get_scores()
DEALER.get_paths()
DEALER.get_stand_hands()
DEALER.get_stand_probability()
PLAYER.get_stand_value()
SOLVER.optimal_policy()
log()