use native_solver::solver::{
build_tree, build_tree_bounded, Action, BoardError, CardAbstraction, Chips, GameConfig, Node,
Player, Terminal, Trainer, TrainerError, TrainerOptions, Tree, TreeError,
};
use native_solver::{Card, CardSet, Combo, Range};
fn board(names: &[&str]) -> Vec<Card> {
names.iter().map(|s| s.parse::<Card>().unwrap()).collect()
}
fn river() -> Vec<Card> {
board(&["Ks", "Th", "7s", "4d", "2s"])
}
fn config(board: Vec<Card>) -> GameConfig {
config_with(board, Chips::whole(100), Chips::whole(100))
}
fn config_with(board: Vec<Card>, pot: Chips, effective_stack: Chips) -> GameConfig {
GameConfig::new(board, pot, effective_stack)
.with_bet_sizes([0.5, 1.0])
.with_max_raises(1)
}
fn combos(oop: &str, ip: &str, board: &[Card]) -> [Vec<Combo>; 2] {
let dead = CardSet::from_cards(board);
[
Range::parse(oop).unwrap().combos(dead),
Range::parse(ip).unwrap().combos(dead),
]
}
fn combo(a: &str, b: &str, weight: f64) -> Combo {
Combo::new(
a.parse::<Card>().unwrap(),
b.parse::<Card>().unwrap(),
weight,
)
}
fn river_trainer(combos: [Vec<Combo>; 2]) -> Result<Trainer, TrainerError> {
let board = river();
Trainer::new(
build_tree(&config(board.clone())).unwrap(),
combos,
TrainerOptions::default(),
)
}
fn showdown() -> Node {
Node::Terminal(Terminal::Showdown {
invested: [50.0, 50.0],
})
}
#[test]
fn build_tree_rejects_illegal_boards() {
let cases = [
(board(&["Ks", "Th"]), BoardError::WrongSize { len: 2 }),
(
board(&["Ks", "Th", "7s", "4d", "2s", "3c"]),
BoardError::WrongSize { len: 6 },
),
(
board(&["Ks", "Th", "Ks"]),
BoardError::DuplicateCard {
card: "Ks".parse::<Card>().unwrap(),
},
),
];
for (cards, expected) in cases {
let error = build_tree(&config(cards)).err();
assert_eq!(error, Some(TreeError::Board(expected)));
}
}
#[test]
fn build_tree_rejects_illegal_chip_amounts_and_bet_sizes() {
for pot in [Chips::ZERO, Chips::whole(-10)] {
let cfg = config_with(river(), pot, Chips::whole(100));
assert_eq!(build_tree(&cfg).err(), Some(TreeError::NonPositivePot));
}
let cfg = config_with(river(), Chips::whole(100), Chips::whole(-1));
assert_eq!(build_tree(&cfg).err(), Some(TreeError::NegativeStack));
for size in [0.0, -0.5, f64::NAN, f64::INFINITY] {
let cfg = config(river()).with_bet_sizes([0.5, size]);
assert_eq!(
build_tree(&cfg).err(),
Some(TreeError::InvalidBetSize { index: 1 }),
"bet size {size}"
);
}
let cfg = config_with(river(), Chips::ZERO, Chips::whole(100));
assert_eq!(
build_tree_bounded(&cfg, 0).err(),
Some(TreeError::NonPositivePot)
);
}
#[test]
fn build_tree_accepts_degenerate_but_legal_configs() {
let cfg = GameConfig::new(river(), Chips::whole(100), Chips::whole(100));
assert_eq!(build_tree(&cfg).unwrap().num_decision_nodes, 2);
let cfg = config_with(river(), Chips::whole(100), Chips::ZERO);
assert_eq!(build_tree(&cfg).unwrap().num_decision_nodes, 0);
}
#[test]
fn trainer_rejects_an_illegal_board_on_a_hand_edited_tree() {
let mut tree = build_tree(&config(river())).unwrap();
tree.board = board(&["Ks", "Ks", "7s", "4d", "2s"]);
let error = Trainer::new(
tree,
combos("AA", "QQ", &river()),
TrainerOptions::default(),
)
.err();
assert_eq!(
error,
Some(TrainerError::Board(BoardError::DuplicateCard {
card: "Ks".parse::<Card>().unwrap()
}))
);
}
#[test]
fn trainer_rejects_a_tree_built_for_another_street() {
let turn = board(&["Ks", "Th", "7s", "4d"]);
let mut tree = build_tree(&config(river())).unwrap();
tree.board = turn.clone();
let error = Trainer::new(tree, combos("AA", "QQ", &turn), TrainerOptions::default()).err();
assert_eq!(error, Some(TrainerError::TreeBoardMismatch));
let mut tree = build_tree(&config(turn)).unwrap();
tree.board = river();
let error = Trainer::new(
tree,
combos("AA", "QQ", &river()),
TrainerOptions::default(),
)
.err();
assert_eq!(error, Some(TrainerError::TreeBoardMismatch));
}
#[test]
fn trainer_rejects_malformed_hand_built_trees() {
let decision = |id, actions: Vec<Action>, children: Vec<Node>| Node::Decision {
id,
player: Player::Oop,
actions,
children,
};
let trees = [
Tree {
board: river(),
root: decision(
0,
vec![Action::Check, Action::Bet(Chips::whole(10))],
vec![
showdown(),
decision(0, vec![Action::Check], vec![showdown()]),
],
),
num_decision_nodes: 2,
},
Tree {
board: river(),
root: decision(3, vec![Action::Check], vec![showdown()]),
num_decision_nodes: 1,
},
Tree {
board: river(),
root: decision(0, vec![Action::Check], vec![showdown()]),
num_decision_nodes: 2,
},
Tree {
board: river(),
root: decision(
0,
vec![Action::Check, Action::Bet(Chips::whole(10))],
vec![showdown()],
),
num_decision_nodes: 1,
},
Tree {
board: river(),
root: Node::Terminal(Terminal::Showdown {
invested: [-1.0, 50.0],
}),
num_decision_nodes: 0,
},
];
for tree in trees {
let board = river();
let error = Trainer::new(tree, combos("AA", "QQ", &board), TrainerOptions::default()).err();
assert!(
matches!(error, Some(TrainerError::InvalidTree { .. })),
"{error:?}"
);
}
let turn = board(&["Ks", "Th", "7s", "4d"]);
let dead = CardSet::from_cards(&turn);
let children = Card::all()
.filter(|&c| !dead.contains(c))
.skip(1)
.map(|c| (vec![c], showdown()))
.collect();
let tree = Tree {
board: turn.clone(),
root: Node::Chance { children },
num_decision_nodes: 0,
};
let error = Trainer::new(tree, combos("AA", "QQ", &turn), TrainerOptions::default()).err();
assert!(
matches!(error, Some(TrainerError::InvalidTree { .. })),
"{error:?}"
);
}
#[test]
fn trainer_rejects_empty_ranges() {
let [oop, ip] = combos("AA", "QQ", &river());
assert_eq!(
river_trainer([Vec::new(), ip.clone()]).err(),
Some(TrainerError::EmptyRange {
player: Player::Oop
})
);
let zero_weight = oop.iter().map(|c| Combo { weight: 0.0, ..*c }).collect();
assert_eq!(
river_trainer([oop, zero_weight]).err(),
Some(TrainerError::EmptyRange { player: Player::Ip })
);
}
#[test]
fn trainer_rejects_invalid_combos_and_weights() {
let ip = vec![combo("Qc", "Qd", 1.0)];
assert_eq!(
river_trainer([
vec![combo("Ad", "Ah", 1.0), combo("Ac", "Ac", 1.0)],
ip.clone()
])
.err(),
Some(TrainerError::InvalidCombo {
player: Player::Oop,
index: 1
})
);
for weight in [-1.0, f64::NAN, f64::INFINITY] {
assert_eq!(
river_trainer([vec![combo("Ad", "Ah", weight)], ip.clone()]).err(),
Some(TrainerError::InvalidWeight {
player: Player::Oop,
index: 0
}),
"weight {weight}"
);
}
assert_eq!(
river_trainer([
vec![combo("Ad", "Ah", 1.0), combo("Ah", "Ad", 1.0)],
ip.clone()
])
.err(),
Some(TrainerError::DuplicateCombo {
player: Player::Oop,
index: 1
})
);
}
#[test]
fn trainer_rejects_combos_that_use_board_cards() {
let oop = Range::parse("KK").unwrap().combos(CardSet::EMPTY);
let ip = Range::parse("AA")
.unwrap()
.combos(CardSet::from_cards(&river()));
let error = river_trainer([oop, ip]).err();
assert!(
matches!(
error,
Some(TrainerError::ComboConflictsWithBoard {
player: Player::Oop,
..
})
),
"{error:?}"
);
}
#[test]
fn trainer_rejects_ranges_that_can_never_be_dealt_together() {
let error = river_trainer([vec![combo("Ac", "Ad", 1.0)], vec![combo("Ac", "Ah", 1.0)]]).err();
assert_eq!(error, Some(TrainerError::NoCompatibleDeals));
let error = river_trainer([
vec![combo("Ac", "Ad", 1.0), combo("Qc", "Qd", 0.0)],
vec![combo("Ac", "Ah", 1.0)],
])
.err();
assert_eq!(error, Some(TrainerError::NoCompatibleDeals));
}
#[test]
fn trainer_rejects_zero_bucket_abstraction() {
let board = river();
let error = Trainer::new(
build_tree(&config(board.clone())).unwrap(),
combos("AA,KQs", "QQ,JJ", &board),
TrainerOptions {
abstraction: CardAbstraction {
river: Some(0),
..CardAbstraction::default()
},
..TrainerOptions::default()
},
)
.err();
assert_eq!(error, Some(TrainerError::ZeroBuckets));
}
#[test]
fn trainer_rejects_asymmetric_ranges_on_suit_reduced_trees() {
let turn = board(&["Js", "9s", "4d", "2d"]);
let cfg = config(turn.clone()).with_suit_isomorphism(true);
let [mut oop, ip] = combos("AKs,AQs", "AKs,AQs", &turn);
oop[0].weight = 0.25;
let error = Trainer::new(
build_tree(&cfg).unwrap(),
[oop, ip],
TrainerOptions::default(),
)
.err();
assert_eq!(
error,
Some(TrainerError::AsymmetricRange {
player: Player::Oop
})
);
}
#[test]
fn errors_explain_what_is_wrong() {
let tree_error = build_tree(&config(board(&["Ks", "Th"]))).err().unwrap();
assert_eq!(
tree_error.to_string(),
"invalid board: board has 2 cards; expected 3, 4, or 5"
);
assert!(std::error::Error::source(&tree_error).is_some());
let trainer_error = river_trainer([Vec::new(), vec![combo("Qc", "Qd", 1.0)]])
.err()
.unwrap();
assert_eq!(
trainer_error.to_string(),
"Oop range has no combo with positive weight"
);
}
#[test]
fn flop_and_turn_buckets_need_suit_closed_ranges() {
let flop = board(&["2h", "7h", "9h"]);
let cfg = config(flop.clone());
let new = |oop: Vec<Combo>, abstraction: CardAbstraction| {
let ip = combos("AA", "QQ", &flop)[1].clone();
Trainer::new(
build_tree(&cfg).unwrap(),
[oop, ip],
TrainerOptions {
abstraction,
..TrainerOptions::default()
},
)
};
let flop_buckets = CardAbstraction {
flop: Some(2),
..CardAbstraction::default()
};
let turn_buckets = CardAbstraction {
turn: Some(2),
..CardAbstraction::default()
};
for abstraction in [flop_buckets, turn_buckets] {
assert_eq!(
new(vec![combo("As", "Ks", 1.0)], abstraction).err(),
Some(TrainerError::AsymmetricRange {
player: Player::Oop
})
);
}
let mut oop = combos("AKs", "QQ", &flop)[0].clone();
oop[0].weight = 0.25;
let mut trainer = new(oop.clone(), flop_buckets).unwrap();
trainer.train(1);
assert!(new(vec![combo("As", "Ks", 1.0)], CardAbstraction::default()).is_ok());
}
#[test]
fn trainer_rejects_chance_groups_that_are_not_suit_swaps() {
let turn = board(&["2h", "7c", "9d", "Jc"]);
let c = |s: &str| s.parse::<Card>().unwrap();
let chance_tree = |groups: &[&[&str]]| {
let grouped: Vec<Card> = groups.iter().flat_map(|g| g.iter().map(|s| c(s))).collect();
let mut children: Vec<(Vec<Card>, Node)> = groups
.iter()
.map(|g| (g.iter().map(|s| c(s)).collect(), showdown()))
.collect();
let dead = CardSet::from_cards(&turn) | CardSet::from_cards(&grouped);
children.extend(
Card::all()
.filter(|&card| !dead.contains(card))
.map(|card| (vec![card], showdown())),
);
Tree {
board: turn.clone(),
root: Node::Chance { children },
num_decision_nodes: 0,
}
};
let new = |tree| {
Trainer::new(
tree,
combos("77,88,AKs", "77,88,AKs", &turn),
TrainerOptions::default(),
)
};
for bad in [&[&["Ks", "Kc"][..]][..], &[&["Ks", "Qs"][..]][..]] {
let error = new(chance_tree(bad)).err();
assert!(
matches!(error, Some(TrainerError::InvalidTree { .. })),
"{bad:?}: {error:?}"
);
}
let flop = board(&["2h", "7h", "9h"]);
let flop_tree = {
let dead = CardSet::from_cards(&flop);
let mut children = vec![(vec![c("Ks"), c("Kd"), c("Kc")], showdown())];
children.extend(
Card::all()
.filter(|&card| {
!dead.contains(card) && ![c("Ks"), c("Kd"), c("Kc")].contains(&card)
})
.map(|card| (vec![card], showdown())),
);
Tree {
board: flop.clone(),
root: Node::Chance { children },
num_decision_nodes: 0,
}
};
let error = Trainer::new(
flop_tree,
combos("77,88,AKs", "77,88,AKs", &flop),
TrainerOptions::default(),
)
.err();
assert_eq!(error, Some(TrainerError::TreeBoardMismatch));
}