CFR Poker Agent
Goal
A poker agent for two-player Limit Texas Hold'em that learns robust, mixed strategies through offline Counterfactual Regret Minimization (CFR+) and executes them at runtime via a precomputed lookup table for near-instant decision-making.
The Problem
Poker is a fundamentally challenging AI domain because it involves imperfect information, stochastic outcomes, and adversarial opponents—unlike games like chess, where all states are visible. Building a competitive agent requires reasoning about hidden cards, balancing aggressive and conservative play, and avoiding predictable, exploitable patterns. A hard-coded rule-based agent is brittle, while a full game-tree search is computationally infeasible under the tournament's strict per-action time limit (~0.38 seconds). We needed an agent that could learn strategic depth offline and deploy lightning-fast decisions online.
Methodology
We broke down the project into five stages:
1. State Abstraction
- Compressed the full poker game tree into a compact information set to make learning tractable.
- Each state defined by four components:
- The current betting street (preflop, flop, turn, or river)
- A discretized hand strength bucket (1-5, ranging from very weak to very strong)
- A discretized pot size bucket (1-3, representing small, medium, or large relative to remaining chips)
- The current number of raises in the round (capped at 4)
- This abstraction reduced the state space from billions of possible card configurations to just 352 strategically meaningful information states, preserving the most important variables—stage of hand, hand strength, pot pressure, and betting aggression—while discarding irrelevant card-level details.
2. Hand Strength Estimation
- Mapped each player's hole cards to one of five hand strength buckets based on approximate equity values ranging from 0.15 to 0.85, rather than enumerating all possible card combinations.
- During terminal evaluation, the model compared the equity of both players' hand buckets and converted the difference into expected utility.
- This simplified showdown evaluation enabled rapid training iterations while still capturing the relative strength ordering critical for strategic decision-making.
3. CFR+ Training
- Trained the agent using Counterfactual Regret Minimization (CFR+), an iterative policy optimization algorithm specifically designed for imperfect-information games.
- Over 200,000 self-play iterations, the algorithm recursively traversed the abstracted game tree, tracking regret for every action at every information state.
- At each node, the agent computed counterfactual values—what the expected payoff would have been under each alternative action, holding the opponent's strategy and chance events fixed.
- Updated regret for an action as the difference between that action's counterfactual value and the average counterfactual value across all actions.
- Used a CFR+ variant that clips cumulative regrets to zero (element-wise maximum), which stabilizes training and promotes smoother convergence compared to vanilla regret accumulation.
- The next iteration's strategy was derived via regret matching, where action probabilities were proportional to positive regrets; if all regrets were non-positive, the policy defaulted to uniform distribution over legal actions.
4. Strategy Extraction and Deployment
- After training, extracted the average strategy (rather than the current strategy) at each information set for improved robustness.
- These learned policies were saved to a JSON lookup table, creating a lightweight runtime agent.
- During live play, the agent simply computes its current information set from observed game state, performs a dictionary lookup, and retrieves precomputed action probabilities—enabling millisecond decision times well within the tournament's 0.38-second per-action limit.
5. Baseline Comparison with MCTS
- Implemented a separate Monte Carlo Tree Search (MCTS) agent as an alternative approach and comparison baseline.
- At each decision point, MCTS performed root sampling:
- Repeatedly generating hypothetical complete game states by randomly sampling opponent hole cards and future community cards
- Evaluating each sampled world with the PyPokerEngine hand evaluator
- Averaging expected chip values for each legal action.
- The search ran within the same 0.38-second time budget, selecting the action with the highest average value.
- This approach required no offline training but was computationally expensive at runtime and provided no theoretical convergence guarantees.
Results
- The CFR+ agent achieved a 90% win rate against a random baseline and a 90% win rate against an aggressive raise-always opponent, showing the learned strategy captures meaningful poker decision-making beyond chance or simple heuristics.
- In a 100-match head-to-head against an MCTS baseline, CFR+ won 49% of matches while MCTS won 51%, but CFR+ maintained a positive average profit of +3.6 chips per game versus MCTS at −3.6—demonstrating superior chip extraction and loss minimization.
- The agent progressed from an ultra-passive early version (95% call rate) to a refined 352-state context-aware strategy, placing 7th in a class tournament of 20+ agents.
- The MCTS baseline's best version (V10) achieved 79.6% win rate in controlled testing but overfitted against unseen opponents, confirming CFR+'s equilibrium-seeking approach produces more robust play.
Visual Results