-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcfr.py
More file actions
577 lines (446 loc) · 20.7 KB
/
Copy pathcfr.py
File metadata and controls
577 lines (446 loc) · 20.7 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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
import random
import itertools
from collections import Counter
from src import evaluate_best_hand,Card
#from src import _score_5,evaluate_best_hand
#from src import Table as table
import sys
import json
import copy
from collections import Counter
from src import Table, Player, evaluate_best_hand, Card
# A global dictionary to hold all encountered InfoSets:
INFOSETS = {} # key (string) → InfoSet object
_RANK_TO_INT = {
"2": 2, "3": 3, "4": 4, "5": 5, "6": 6,
"7": 7, "8": 8, "9": 9, "10": 10,
"J": 11, "Q": 12, "K": 13, "A": 14
}
_INT_TO_RANK = {v: k for k, v in _RANK_TO_INT.items()}
PREFLOP_EQ_TABLE = {}
_crand = random.SystemRandom()
def monte_carlo_equity(table, hole_cards, community_cards, trials):
wins = 0
ties = 0
# 1) Build the "remaining deck" by removing Hero's and board cards from table.deck
known = set(hole_cards) | set(community_cards)
# We do NOT modify table.deck in place (so we copy it)
full_deck = [card for card in table.deck if card not in known]
# Decide how often to update (every N steps)
update_every = 1000
# Length of the bar itself (number of characters)
bar_length = 30
print("calculating equity")
for i in range(trials):
# ─── 2A) Sample 2 random cards for Villain from full_deck ───
villian_hole = _crand.sample(full_deck, 2)
# ─── 2B) Remove villain_hole from a fresh copy of full_deck ───
# We need a new "deck minus villain" for dealing remaining board cards.
remaining = [c for c in full_deck if c not in villian_hole]
# ─── 2C) "Complete" the board if fewer than 5 cards are known ───
needed = 5 - len(community_cards)
if needed > 0:
board_extra = _crand.sample(remaining, needed)
final_board = list(community_cards) + board_extra
else:
final_board = list(community_cards)
# ─── 2D) Build Hero's full 7 cards and Villain's full 7 cards ───
hero_7 = list(hole_cards) + final_board
villain_7 = list(villian_hole) + final_board
# ─── 2E) Evaluate both hands using your engine's evaluator ───
hero_score = evaluate_best_hand(hero_7)
villain_score = evaluate_best_hand(villain_7)
# ─── 2F) Increment wins/ties appropriately ───
if hero_score > villain_score:
wins += 1
elif hero_score == villain_score:
ties += 1
# else: villain wins → do nothing
##_____PROGRESS BAR_____
if (i % update_every == 0) or (i == trials - 1):
# percent complete (use i+1 so that at the final iteration it shows 100%)
percent = 100 * (i + 1) / trials
# how many “filled” characters in the bar?
filled_len = int(bar_length * (i + 1) // trials)
bar = "█" * filled_len + "-" * (bar_length - filled_len)
# \r returns cursor to the start of the line so we overwrite it
sys.stdout.write(f"\r|{bar}| {percent:6.2f}%")
sys.stdout.flush()
print()
# ─── 3) Final equity estimate ───
return ((wins + 0.5 * ties) / trials)
class Normalization:
#This function does **not** look at the board or community cards—its only job
#is to canonicalize any two hole cards into the standard preflop bucket
def normalize_hand_suits(self,hole_cards):
if len(hole_cards) != 2:
raise ValueError("normalize_hand_suits: expected exactly two hole cards, got "f"{len(hole_cards)} cards")
c1, c2 = hole_cards
r1 = _RANK_TO_INT[c1.rank]
r2 = _RANK_TO_INT[c2.rank]
if r1 > r2:
rank_high, rank_low = r1, r2
elif r2 > r1:
rank_high, rank_low = r2, r1
else:
rank_high = rank_low = r1
# 5) Determine if the two cards are suited
suited_flag = (c1.suit == c2.suit)
# 6) Return the canonical 3‐tuple
return (rank_high, rank_low, suited_flag)
def get_preflop_equity(self,table, hole_cards, trials=2000):
# 1) Canonicalize to one of 169 keys
key = self.normalize_hand_suits(hole_cards)
if key in PREFLOP_EQ_TABLE:
return PREFLOP_EQ_TABLE[key]
equity = monte_carlo_equity(table, hole_cards, community_cards=(), trials=trials)
# 4) Store in cache and return
PREFLOP_EQ_TABLE[key] = equity
return equity
def build_full_preflop_table(table, trials=2000):
#Enumerate all 169 (rank_high,rank_low,suited) combinations, run Monte Carlo
#once for each
ranks_int = list(range(2, 15))
for r_high in ranks_int[::-1]:
for r_low in ranks_int[::-1]:
if r_low > r_high:
continue
# Case 1: Pair (r_high==r_low)
if r_high == r_low:
key = (r_high, r_low, False)
if key not in PREFLOP_EQ_TABLE:
# Create any two cards of that rank; suit choice doesn’t matter for a pair
c1 = Card(_INT_TO_RANK[r_high], "Spades")
c2 = Card(_INT_TO_RANK[r_low], "Hearts")
eq = monte_carlo_equity(table, (c1, c2), community_cards=(), trials=trials)
PREFLOP_EQ_TABLE[key] = eq
# Case 2: Suited (r_high > r_low, suited=True)
else:
key_s = (r_high, r_low, True)
if key_s not in PREFLOP_EQ_TABLE:
c1 = Card(_INT_TO_RANK[r_high], "Spades")
c2 = Card(_INT_TO_RANK[r_low], "Spades")
eq_s = monte_carlo_equity(table, (c1, c2), community_cards=(), trials=trials)
PREFLOP_EQ_TABLE[key_s] = eq_s
key_o = (r_high, r_low, False)
if key_o not in PREFLOP_EQ_TABLE:
# Offsuit: pick two different suits arbitrarily
c1 = Card(_INT_TO_RANK[r_high], "Spades")
c2 = Card(_INT_TO_RANK[r_low], "Hearts")
eq_o = monte_carlo_equity(table, (c1, c2), community_cards=(), trials=trials)
PREFLOP_EQ_TABLE[key_o] = eq_o
print("Built PREFLOP_EQ_TABLE: total entries =", len(PREFLOP_EQ_TABLE))
def postflop_equity_vs_random(table, hole_cards, community_cards, trials=2000):
"""
Approximate the equity of `hole_cards` vs. a
single random opponent, **given** the existing community_cards
(which must be length 3 or 4).
Returns a float in [0,1].
- If len(community_cards)=3 (flop), we sample Villain’s 2 hole cards
from "deck minus (hole_cards ∪ community_cards)" and finish with random
turn+river.
- If len(community_cards)=4 (turn), we sample Villain’s 2 hole cards
and finish with random river.
- If len(community_cards)=5 (river), we simply do one showdown (no randomness).
"""
n_board = len(community_cards)
if n_board > 5 or n_board < 3:
raise ValueError("postflop_equity_vs_random requires 3≤len(board)≤5")
wins = 0
ties = 0
# Build set of “known” cards: Hero’s hole + current board
known = set(hole_cards) | set(community_cards)
for _ in range(trials):
# 1) Build “deck minus known”
full_deck = [c for c in table.deck if c not in known]
# 2) Sample Villain’s hole from that
villain_hole = random.sample(full_deck, 2)
# 3) Build a fresh “remaining deck” after giving villain hole
rem = [c for c in full_deck if c not in villain_hole]
# 4) Complete the board if needed:
if n_board == 3:
# on the flop: deal 2 more cards (turn+river)
extra = random.sample(rem, 2)
final_board = list(community_cards) + extra # 5 cards
elif n_board == 4:
# on the turn: deal 1 more card (river)
extra = random.sample(rem, 1)
final_board = list(community_cards) + extra # 5 cards
else:
# river: board is already length‐5
final_board = list(community_cards)
# 5) Build 7‐card hands
hero_7 = list(hole_cards) + final_board
villain_7 = list(villain_hole) + final_board
# 6) Evaluate both
h_score = evaluate_best_hand(hero_7)
v_score = evaluate_best_hand(villain_7)
if h_score > v_score:
wins += 1
elif h_score == v_score:
ties += 1
# else villain wins → nothing to add
return (wins + 0.5 * ties) / trials
def get_preflop_bucket(self,hole_cards, num_buckets=20):
#Requires that PREFLOP_EQ_TABLE is already populated for this shape
key = self.normalize_hand_suits(hole_cards)
if key not in PREFLOP_EQ_TABLE:
raise KeyError(
f"Missing preflop equity for shape {key}. "
"Did you call get_preflop_equity(...) or build_full_preflop_table(...) first?"
)
eq = PREFLOP_EQ_TABLE[key] #gives float value in [0.0, 1.0]
#Multiply by num_buckets and floor to get an integer index
idx = int(eq * num_buckets)
if idx >= num_buckets:
idx = num_buckets - 1
if idx < 0:
idx = 0
return idx
class InformationNode:
def __init__(self,Norm : Normalization):
self.norm = Norm
def create_infoset_key(self,table, player_idx, num_buckets=20, postflop_trials=1000):
player = table.players[player_idx]
hole = player.hole_cards # tuple of 2 Card objects
board = list(table.community) # list of 0..5 Card objects
stage = table.stage # one of "pre-flop", "flop", "turn", "river"
if stage == "pre-flop":
hole_bucket = self.norm.get_preflop_bucket(hole, num_buckets)
elif stage in ("flop", "turn"):
# Postflop: bucket by equity given board
hole_bucket = self.norm.get_postflop_bucket(table, hole, board,
num_buckets=num_buckets,
trials=postflop_trials)
else:
hole_bucket = self.norm.get_postflop_bucket(table, hole, board,
num_buckets=num_buckets,
trials=postflop_trials // 2)
#Build betting‐history string
history = ""
for p in table.players:
if p.action_taken:
history += p.action_taken[0]
to_call = table.amount_to_call()
if(to_call <0):
to_call = 0
#creating a return keyed structure
key = f"stage={stage}|hb={hole_bucket}|hist={history}|tocall={to_call}"
return key
class InfoSet:
#it is a container of the node properties
def __init__(self,key,legal_actions):
self.key = key
self.legal_actions = legal_actions[:]#safed the legal actions in the local class leagal_actions(self)
#setting up strategy and regret sum values for each node container
self.regret_sum = {action: 0.0 for action in legal_actions}
self.strategy_sum = {action: 0.0 for action in legal_actions}
def get_strategy(self,realization_weight):
"""
Computes a mixed strategy from current regret sums via regret‐matching,
then accumulates (strategy × realization_weight) into strategy_sum.
Input:
- realization_weight: probability weight reaching this node for THIS player
Returns:
- strategy (dict action→probability)
"""
strategy = {}
Z = 0.0
# 1) For each action:
# if regret_sum[action] > 0 → strategy[action] = regret_sum[action];
# else strategy[action] = 0.0
for action in self.legal_actions:
r = self.regret_sum[action]
strategy[action] = r if r > 0 else 0.0
Z += strategy[action]
# 2) If all regrets ≤ 0, use uniform strategy
if Z <= 0:
for action in self.legal_actions:
strategy[action] = 1.0 / len(self.legal_actions)
else:
# Normalize positive regrets to probabilities
for action in self.legal_actions:
strategy[action] /= Z
# 3) Accumulate into strategy_sum for average later
for action in self.legal_actions:
self.strategy_sum[action] += realization_weight * strategy[action]
return strategy
def get_average_strategy(self):
"""
After many CFR iterations, the average strategy at this infoset is:
avg_strat[action] = strategy_sum[action] / (sum of all strategy_sum entries)
Returns:
- avg_strategy (dict action→float)
"""
avg_strategy = {}
total = sum(self.strategy_sum.values())
if total > 0:
for action in self.legal_actions:
avg_strategy[action] = self.strategy_sum[action] / total
else:
# If we never visited this infoset, return uniform
for action in self.legal_actions:
avg_strategy[action] = 1.0 / len(self.legal_actions)
return avg_strategy
class CFRGameState:
"""
A thin wrapper around your Table instance to allow safe copying
for recursion. Each CFR node gets its own CFRGameState, which
holds a deep copy of the entire Table and players.
- table.to_act tells us whose turn it is (0 or 1)
- table.stage in {"pre-flop", "flop", "turn", "river", "showdown"}
- table._settle_showdown() will split the pot (but in CFR, we want
payoff without modifying the original, so we'll handle payoff manually)
"""
def __init__(self, table):
# Make a deep copy of the entire Table (including Player states)
self.table = copy.deepcopy(table)
# Stub out chance‐node handling so cfr_ex() won’t blow up:
def is_chance_node(self):
return False
def chance_outcomes(self):
# CFR will never sample a chance branch
return []
def with_deal(self, outcome):
# No board‐dealing, so just return self
return self
def is_terminal(self):
"""
Returns True if this is a terminal node (someone won, or showdown).
Two cases:
1) Only one player remains in_hand (everyone else folded).
2) stage == "river" and the betting round is over → showdown.
"""
alive = [p for p in self.table.players if p.in_hand]
if len(alive) == 1:
return True # folded-to-one‐player
if self.table.stage == "river":
# If no more actions possible on river, it is showdown.
# We can check if betting round is over:
# a) Everyone who can match has matched, or
# b) Everyone checked.
# Reuse your existing is_betting_round_over logic if you wrote it.
# For simplicity, we assume the CFR driver only calls is_terminal()
# once the betting round on river is complete.
is_bets_over = self.table.is_betting_round_over(self.table)
return is_bets_over
return False
def get_payoff(self, player_idx):
"""
Returns the terminal *utility* for player_idx (zero‐sum, normalized to +1/–1).
Two cases:
1) One player folded: that player wins the pot. We return +1 for the winner,
−1 for loser.
2) River showdown: compute best 7‐card hands for both, compare:
If hero > villain → +1, equal → 0, hero < villain → −1.
For simplicity, we ignore the actual pot size and just give +1 or −1.
"""
# 1) Fold‐to‐one‐player
alive = [p for p in self.table.players if p.in_hand]
if len(alive) == 1:
winner = alive[0]
if self.table.players[player_idx] == winner: #if the arg player_idx is winner or not
return +1.0
else:
return -1.0
# 2) Showdown (river)
# Build each player’s 7‐card list:
final_board = list(self.table.community) # length == 5 on river
hands = []
for p in self.table.players:
hole = list(p.hole_cards)
full7 = hole + final_board
score = evaluate_best_hand(full7)
hands.append(score)
#___here the hand is just appended with score i.e. the empty list so how is it according to the player_idx ??
# Compare
if hands[player_idx] > hands[1 - player_idx]:
return +1.0
elif hands[player_idx] == hands[1 - player_idx]:
return 0.0
else:
return -1.0
def cfr_ex(state, reach_probs, target_player):
"""
Perform a CFR recursion on the given state.
state: Table instance representing current game state
reach_probs: tuple (pi0, pi1)
target_player: 0 or 1 indicating which player's regrets to update
Returns: utility for the current player to act
"""
# Terminal node
if state.is_terminal():
return state.get_payoff(target_player)
# Chance node: deal next card(s)
if state.is_chance_node():
util = 0.0
# Enumerate possible chance outcomes and their probability
for card_outcome, prob in state.chance_outcomes():
next_state = state.with_deal(card_outcome)
util += prob * cfr_ex(next_state, reach_probs, target_player)
return util
# Decision node
player = state.table.to_act
#infoset_key = InformationNode.create_infoset_key(state, player)
#infoset_key = InformationNode.create_infoset_key(state.table, player)
infoset_key = KEY_GEN.create_infoset_key(state.table, player)
if infoset_key not in INFOSETS:
INFOSETS[infoset_key] = InformationNode(state.get_available_actions(), infoset_key)
node = INFOSETS[infoset_key]
# Get current strategy for this infoset
strategy = node.get_strategy()
actions = state.table.get_available_actions()
# Store counterfactual values
util_per_action = {}
node_util = 0.0
for a in actions:
#next_state = state.copy()
#next_state.apply_action(a)
next_table = copy.deepcopy(state.table)
next_table.apply_action(a)
next_state = CFRGameState(next_table)
new_reach = list(reach_probs)
new_reach[player] *= strategy[a]
util_per_action[a] = cfr_ex(next_state, tuple(new_reach), target_player)
node_util += strategy[a] * util_per_action[a]
# Regret and strategy updates
opp = 1 - player
if player == target_player:
for a in actions:
regret = util_per_action[a] - node_util
node.regret_sum[a] += reach_probs[opp] * regret
node.strategy_sum[a] += reach_probs[player] * strategy[a]
return node_util
NORM = Normalization()
KEY_GEN = InformationNode(NORM)
def train(iterations=5000, starting_stack=1000, sb=5, bb=10):
"""
Train CFR by running `iterations` loops of cfr_ex on fresh hands,
then export the average strategy to JSON.
"""
# Template players for consistent stack sizes
players_template = [Player("P1", starting_stack), Player("P2", starting_stack)]
# Build full preflop equity table once to avoid KeyError
temp_players = [Player(p.name, p.chips) for p in players_template]
temp_table = Table(temp_players, sb=sb, bb=bb)
NORM.build_full_preflop_table(temp_table)
for t in range(1, iterations + 1):
# Create fresh table and deal a new hand
players = [Player(p.name, p.chips) for p in players_template]
table = Table(players, sb=sb, bb=bb)
table.start_hand()
# Wrap in CFRGameState to isolate copies
state = CFRGameState(table)
# Run CFR recursion for both players
cfr_ex(state, (1.0, 1.0), target_player=0)
cfr_ex(state, (1.0, 1.0), target_player=1)
# After training, export the average strategy
avg_strategy = {key: node.get_average_strategy() for key, node in INFOSETS.items()}
with open('poker_strategy.json', 'w') as f:
json.dump(avg_strategy, f, indent=2)
print(f"Training complete. Exported strategy for {len(avg_strategy)} infosets.")
if __name__ == '__main__':
# Adjust parameters as needed
train(iterations=500, starting_stack=1000, sb=5, bb=10)
# Adjust `iterations` for faster testing or deeper convergence
train(iterations=5000, starting_stack=1000, sb=5, bb=10)