use super::chips::Chips;
use super::tree::{Action, Node, Terminal};
use crate::card::{bit, Card};
use crate::eval::score_7;
use std::collections::{HashMap, HashSet};
use std::fmt;
use crate::range::Combo;
type Policy = HashMap<usize, Vec<Vec<f64>>>;
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct StrategyQuality {
pub profile_ev: [f64; 2],
pub best_response_ev: [f64; 2],
pub nash_conv: f64,
pub exploitability: f64,
pub joint_mass: f64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum QualityError {
InvalidBoard(String),
InvalidRange {
player: usize,
reason: String,
},
NoCompatibleDeals,
InvalidTree(String),
UnsupportedChanceReduction,
InvalidStrategy {
node_id: usize,
reason: String,
},
NonFiniteResult,
}
impl fmt::Display for QualityError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidBoard(reason) => write!(f, "invalid initial board: {reason}"),
Self::InvalidRange { player, reason } => write!(f, "invalid player {player} range: {reason}"),
Self::NoCompatibleDeals => write!(f, "legal private deals must have positive finite joint mass"),
Self::InvalidTree(reason) => write!(f, "invalid reference tree: {reason}"),
Self::UnsupportedChanceReduction => write!(f, "reference evaluation requires singleton chance classes; expand suit-isomorphism reduction first"),
Self::InvalidStrategy { node_id, reason } => write!(f, "invalid strategy at node {node_id}: {reason}"),
Self::NonFiniteResult => write!(f, "reference evaluation produced a nonfinite value"),
}
}
}
impl std::error::Error for QualityError {}
pub struct ReferenceEvaluator<'a> {
root: &'a Node,
initial_board: &'a [Card],
combos: [&'a [Combo]; 2],
masks: [Vec<u64>; 2],
prior: Vec<f64>,
joint_mass: f64,
decisions: Vec<(usize, usize, usize)>,
outcomes: HashMap<u64, Vec<i8>>,
}
impl std::fmt::Debug for ReferenceEvaluator<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReferenceEvaluator")
.field("initial_board", &self.initial_board)
.field("num_combos", &[self.combos[0].len(), self.combos[1].len()])
.field("num_decisions", &self.decisions.len())
.field("joint_mass", &self.joint_mass)
.finish_non_exhaustive()
}
}
impl<'a> ReferenceEvaluator<'a> {
pub fn new(
root: &'a Node,
initial_board: &'a [Card],
combos: [&'a [Combo]; 2],
) -> Result<Self, QualityError> {
if !(3..=5).contains(&initial_board.len()) {
return Err(QualityError::InvalidBoard("expected 3 to 5 cards".into()));
}
let mut board_mask = 0u64;
for &card in initial_board {
if board_mask & bit(card) != 0 {
return Err(QualityError::InvalidBoard("cards must be distinct".into()));
}
board_mask |= bit(card);
}
let mut masks = [Vec::new(), Vec::new()];
for player in 0..2 {
let mut seen = HashSet::new();
for &Combo {
cards: [a, b],
weight,
} in combos[player]
{
let reason = if a == b {
Some("a combo must contain two distinct cards")
} else if !weight.is_finite() || weight < 0.0 {
Some("range weights must be finite and nonnegative")
} else if !seen.insert((a.min(b), a.max(b))) {
Some("duplicate private combo")
} else {
None
};
if let Some(reason) = reason {
return Err(QualityError::InvalidRange {
player,
reason: reason.into(),
});
}
masks[player].push((bit(a)) | (bit(b)));
}
}
let mut prior = Vec::new();
for (i, a) in combos[0].iter().enumerate() {
for (j, b) in combos[1].iter().enumerate() {
let legal =
(masks[0][i] | masks[1][j]) & board_mask == 0 && masks[0][i] & masks[1][j] == 0;
prior.push(if legal { a.weight * b.weight } else { 0.0 });
}
}
let joint_mass: f64 = prior.iter().sum();
if !joint_mass.is_finite() || joint_mass <= 0.0 {
return Err(QualityError::NoCompatibleDeals);
}
for probability in &mut prior {
*probability /= joint_mass;
}
let mut evaluator = Self {
root,
initial_board,
combos,
masks,
prior,
joint_mass,
decisions: Vec::new(),
outcomes: HashMap::new(),
};
evaluator.validate_tree(root, &mut initial_board.to_vec(), &mut HashSet::new(), 0)?;
Ok(evaluator)
}
pub fn evaluate<F>(&self, mut strategy: F) -> Result<StrategyQuality, QualityError>
where
F: FnMut(usize) -> Vec<Vec<f64>>,
{
let mut policies = HashMap::new();
for &(id, player, actions) in &self.decisions {
let mut policy = strategy(id);
let invalid = |reason: &str| QualityError::InvalidStrategy {
node_id: id,
reason: reason.into(),
};
if policy.len() != actions
|| policy
.iter()
.any(|row| row.len() != self.combos[player].len())
{
return Err(invalid("expected [action][combo] dimensions"));
}
for combo in 0..self.combos[player].len() {
let sum: f64 = policy.iter().map(|row| row[combo]).sum();
if policy
.iter()
.any(|row| !row[combo].is_finite() || !(0.0..=1.0).contains(&row[combo]))
|| !sum.is_finite()
|| (sum - 1.0).abs() > 1e-10
{
return Err(invalid(
"probability columns must be finite, nonnegative, and sum to one",
));
}
for row in &mut policy {
row[combo] /= sum;
}
}
policies.insert(id, policy);
}
let mut profile_ev = [0.0; 2];
let mut best_response_ev = [0.0; 2];
let board_mask = mask(self.initial_board);
for player in 0..2 {
for own in 0..self.combos[player].len() {
let reach: Vec<f64> = (0..self.combos[1 - player].len())
.map(|opp| self.prior[self.pair_index(player, own, opp)])
.collect();
if reach.iter().all(|&weight| weight == 0.0) {
continue;
}
let problem = PrivateProblem {
evaluator: self,
policies: &policies,
player,
own,
};
profile_ev[player] += problem.value(self.root, board_mask, &reach, false);
best_response_ev[player] += problem.value(self.root, board_mask, &reach, true);
}
}
let nash_conv =
(best_response_ev[0] - profile_ev[0]) + (best_response_ev[1] - profile_ev[1]);
let result = StrategyQuality {
profile_ev,
best_response_ev,
nash_conv,
exploitability: nash_conv / 2.0,
joint_mass: self.joint_mass,
};
if profile_ev
.iter()
.chain(&best_response_ev)
.chain([&nash_conv])
.any(|x| !x.is_finite())
{
return Err(QualityError::NonFiniteResult);
}
Ok(result)
}
fn pair_index(&self, player: usize, own: usize, opp: usize) -> usize {
if player == 0 {
own * self.combos[1].len() + opp
} else {
opp * self.combos[1].len() + own
}
}
fn validate_tree(
&mut self,
node: &Node,
board: &mut Vec<Card>,
ids: &mut HashSet<usize>,
depth: usize,
) -> Result<(), QualityError> {
let invalid = |reason: &str| QualityError::InvalidTree(reason.into());
if depth > 256 {
return Err(invalid("reference traversal depth exceeds 256"));
}
match node {
Node::Decision {
id,
player,
actions,
children,
} => {
if actions.is_empty() || actions.len() != children.len() {
return Err(invalid(
"decisions need matching nonempty actions and children",
));
}
if !ids.insert(*id) {
return Err(invalid("decision IDs must be unique public histories"));
}
if actions.iter().any(|action| matches!(action, Action::Bet(amount) | Action::Raise(amount) if *amount <= Chips::ZERO)) {
return Err(invalid("bet and raise amounts must be positive"));
}
self.decisions.push((*id, player.index(), actions.len()));
for child in children {
self.validate_tree(child, board, ids, depth + 1)?;
}
}
Node::Chance { children } => {
if board.len() >= 5 {
return Err(invalid("cannot deal past a five-card board"));
}
if children.iter().any(|(members, _)| members.len() > 1) {
return Err(QualityError::UnsupportedChanceReduction);
}
if children.len() != 52 - board.len() {
return Err(invalid(
"chance must enumerate every card outside the public board",
));
}
let mut seen = mask(board);
for (members, child) in children {
if members.len() != 1 || seen & bit(members[0]) != 0 {
return Err(invalid(
"chance cards must be distinct singletons outside the public board",
));
}
seen |= bit(members[0]);
board.push(members[0]);
self.validate_tree(child, board, ids, depth + 1)?;
board.pop();
}
}
Node::Terminal(terminal) => {
let invested = match terminal {
Terminal::Fold { invested, .. } | Terminal::Showdown { invested } => invested,
};
if invested
.iter()
.any(|amount| !amount.is_finite() || *amount < 0.0)
{
return Err(invalid(
"terminal investments must be finite and nonnegative",
));
}
if matches!(terminal, Terminal::Showdown { .. }) {
if board.len() != 5 {
return Err(invalid("showdown requires a complete five-card board"));
}
self.cache_outcomes(board);
}
}
}
Ok(())
}
fn cache_outcomes(&mut self, board: &[Card]) {
let board_mask = mask(board);
self.outcomes.entry(board_mask).or_insert_with(|| {
let scores: [Vec<Option<u32>>; 2] = std::array::from_fn(|player| {
self.combos[player]
.iter()
.enumerate()
.map(|(i, &Combo { cards: [a, b], .. })| {
(self.masks[player][i] & board_mask == 0).then(|| {
score_7([a, b, board[0], board[1], board[2], board[3], board[4]])
})
})
.collect()
});
let mut outcomes = Vec::new();
for (i, a) in scores[0].iter().enumerate() {
for (j, b) in scores[1].iter().enumerate() {
outcomes.push(match (a, b) {
(Some(a), Some(b)) if self.masks[0][i] & self.masks[1][j] == 0 => {
match a.cmp(b) {
std::cmp::Ordering::Less => -1,
std::cmp::Ordering::Equal => 0,
std::cmp::Ordering::Greater => 1,
}
}
_ => 0, });
}
}
outcomes
});
}
}
struct PrivateProblem<'a, 'b> {
evaluator: &'b ReferenceEvaluator<'a>,
policies: &'b Policy,
player: usize,
own: usize,
}
impl PrivateProblem<'_, '_> {
fn value(&self, node: &Node, board: u64, reach: &[f64], best_response: bool) -> f64 {
if reach.iter().all(|&weight| weight == 0.0) {
return 0.0;
}
match node {
Node::Terminal(terminal) => reach
.iter()
.enumerate()
.map(|(opp, &weight)| {
if weight == 0.0 {
return 0.0;
}
let payoff = match terminal {
Terminal::Fold { winner, invested } => {
if winner.index() == self.player {
invested[1 - self.player]
} else {
-invested[self.player]
}
}
Terminal::Showdown { invested } => {
let oop_order = self.evaluator.outcomes[&board]
[self.evaluator.pair_index(self.player, self.own, opp)];
let own_order = if self.player == 0 {
oop_order
} else {
-oop_order
};
match own_order {
1 => invested[1 - self.player],
-1 => -invested[self.player],
_ => (invested[1 - self.player] - invested[self.player]) / 2.0,
}
}
};
weight * payoff
})
.sum(),
Node::Chance { children } => {
let probability = 1.0 / (52 - board.count_ones() - 4) as f64;
children
.iter()
.map(|(members, child)| {
let card = bit(members[0]);
if self.evaluator.masks[self.player][self.own] & card != 0 {
return 0.0;
}
let next: Vec<f64> = reach
.iter()
.enumerate()
.map(|(opp, &weight)| {
if self.evaluator.masks[1 - self.player][opp] & card == 0 {
weight * probability
} else {
0.0
}
})
.collect();
self.value(child, board | card, &next, best_response)
})
.sum()
}
Node::Decision {
id,
player,
children,
..
} => {
let policy = &self.policies[id];
if player.index() == self.player {
let values = children
.iter()
.map(|child| self.value(child, board, reach, best_response));
if best_response {
values.fold(f64::NEG_INFINITY, f64::max)
} else {
values
.enumerate()
.map(|(action, value)| policy[action][self.own] * value)
.sum()
}
} else {
children
.iter()
.enumerate()
.map(|(action, child)| {
let next: Vec<f64> = reach
.iter()
.enumerate()
.map(|(opp, &weight)| weight * policy[action][opp])
.collect();
self.value(child, board, &next, best_response)
})
.sum()
}
}
}
}
}
fn mask(cards: &[Card]) -> u64 {
cards.iter().fold(0, |mask, &card| mask | (bit(card)))
}