native_solver/lib.rs
1//! Find near-equilibrium strategies for heads-up postflop No-Limit Hold'em
2//! spots, and measure how exploitable they are.
3//!
4//! Give it a board, both players' ranges, the pot, the stacks, and the bet
5//! sizes to consider; it trains a strategy with counterfactual regret
6//! minimization (CFR, CFR+, or DCFR) and reports per-hand strategies, EVs,
7//! and exploitability: how much a perfect opponent could still win against
8//! the result. Chip amounts are exact integers, invalid inputs are reported
9//! as errors, and an independent reference evaluator can check results on
10//! small trees.
11//!
12//! - [`card`]: [`Card`]s and [`CardSet`]s.
13//! - [`eval`]: five- and seven-card hand evaluation.
14//! - [`range`]: weighted preflop ranges and their expansion into [`Combo`]s.
15//! - [`solver`]: the betting tree ([`GameConfig`], [`build_tree`]) and the
16//! combo-vectorized CFR [`Trainer`], with exploitability and node EVs.
17//!
18//! Only heads-up postflop play is modeled: no preflop, no side pots, no
19//! multiway pots. All values are in input chips unless a doc says otherwise.
20//!
21//! # Example
22//!
23//! Solve a river spot and check how close the result is to equilibrium:
24//!
25//! ```
26//! use native_solver::{build_tree, Card, CardSet, Chips, GameConfig, Range, Trainer, TrainerOptions};
27//!
28//! let board = ["Ks", "Th", "7s", "4d", "2s"]
29//! .iter()
30//! .map(|card| card.parse())
31//! .collect::<Result<Vec<Card>, _>>()?;
32//! let config = GameConfig::new(board.clone(), Chips::whole(100), Chips::whole(100))
33//! .with_bet_sizes([0.5, 1.0]);
34//!
35//! // Remove combos that share a card with the board.
36//! let dead = CardSet::from_cards(&board);
37//! let oop = "AA,KK,KQs,QJs,JTs:0.5".parse::<Range>()?.combos(dead);
38//! let ip = "AKs,KJs,TT,99,A5s:0.5".parse::<Range>()?.combos(dead);
39//!
40//! let mut trainer = Trainer::new(build_tree(&config)?, [oop, ip], TrainerOptions::default())?;
41//! trainer.train(500);
42//!
43//! // How much a best response could still gain, in chips.
44//! assert!(trainer.exploitability().total < 1.0);
45//! # Ok::<(), Box<dyn std::error::Error>>(())
46//! ```
47//!
48//! See [`conventions`] for units and value normalization, and
49//! [`algorithms`] for the update rules.
50
51#![warn(missing_docs)]
52
53// Compile and run the README example as a doctest.
54#[cfg(doctest)]
55#[doc = include_str!("../README.md")]
56pub struct ReadmeDoctests;
57
58pub mod card;
59pub mod eval;
60// Kuhn poker: a separate toy CFR with a known closed-form equilibrium, kept
61// only to cross-check the update rules in tests.
62#[cfg(test)]
63mod games;
64pub mod range;
65pub mod solver;
66
67#[doc = include_str!("../docs/conventions.md")]
68pub mod conventions {}
69
70#[doc = include_str!("../docs/algorithms.md")]
71pub mod algorithms {}
72
73pub use card::{Card, CardParseError, CardSet};
74pub use eval::{evaluate_5, evaluate_7, HandCategory, HandStrength};
75pub use range::{all_canonical_hands, Combo, Range, RangeError};
76pub use solver::{
77 build_tree, build_tree_bounded, Action, BoardError, CardAbstraction, Chips, Evaluation,
78 Exploitability, GameConfig, Node, NodeEvaluation, Player, Policy, PolicyError, SolverAlgorithm,
79 SolverGame, Terminal, Trainer, TrainerError, TrainerOptions, Tree, TreeError, UpdateSchedule,
80 CHIP_SCALE,
81};