Skip to main content

Trainer

Struct Trainer 

Source
pub struct Trainer { /* private fields */ }
Expand description

Trains a strategy for one game with counterfactual regret minimization.

A trainer owns the regret and strategy tables. The game itself (SolverGame) is immutable and shared through Trainer::game, so a Policy snapshot can be read while training continues.

§Examples

use native_solver::{build_tree, Card, CardSet, Chips, GameConfig, Player, Range, Trainer, TrainerOptions};

let board = ["Ks", "Th", "7s", "4d", "2s"]
    .iter()
    .map(|card| card.parse())
    .collect::<Result<Vec<Card>, _>>()?;
let dead = CardSet::from_cards(&board);
let oop = "AA,KQs".parse::<Range>()?.combos(dead);
let ip = "QQ,JJ".parse::<Range>()?.combos(dead);
let config = GameConfig::new(board, Chips::whole(100), Chips::whole(100)).with_bet_sizes([1.0]);

let mut trainer = Trainer::new(build_tree(&config)?, [oop, ip], TrainerOptions::default())?;
trainer.train(200);
assert_eq!(trainer.iterations_completed(), 200);

// Node 0 is OOP's first decision: one row per action, one column per OOP combo.
let strategy = trainer.average_strategy(0);
assert_eq!(strategy[0].len(), trainer.game().combos(Player::Oop).len());

// How much a best response could still gain, in chips.
assert!(trainer.exploitability().total < 1.0);

Implementations§

Source§

impl Trainer

Source

pub fn new( tree: Tree, combos: [Vec<Combo>; 2], options: TrainerOptions, ) -> Result<Self, TrainerError>

Builds a trainer for tree and both players’ combos.

The combo order is kept and is the combo axis of every per-combo result. options selects the update rule, the update order (fixed for this trainer’s lifetime; an alternating iteration is an OOP pass followed by an IP pass), and per-street card abstraction.

Trees with grouped chance cards (suit isomorphism) need ranges closed under every suit swap that fixes the board, with equal weights for swapped combos; flop and turn buckets need the swapped combos to exist. Ranges from Range::combos satisfy both when dead is exactly the tree’s board.

§Errors

Returns a TrainerError if the tree’s board is illegal, the tree does not continue from it or is malformed, a range is empty or holds invalid, repeated, or board-blocked combos, no deal is possible, an abstraction asks for zero buckets, or a range is not suit-symmetric as required above.

Source

pub fn algorithm(&self) -> SolverAlgorithm

The fixed update policy selected at construction.

Source

pub fn update_schedule(&self) -> UpdateSchedule

The fixed player-update order selected at construction.

Source

pub fn iterations_completed(&self) -> u64

Iterations completed across all calls to train.

An alternating iteration counts once, though it makes two passes. DCFR uses this count as its discounting clock.

Source

pub fn train(&mut self, iterations: u32) -> [f64; 2]

Runs iterations more training iterations.

Returns the root [OOP, IP] raw range-weighted values under the last iteration’s current strategy. They are a progress signal, not EVs, and say nothing about convergence; use exploitability for that and average_strategy for the result. With alternating updates the values come from the final IP pass.

§Panics

Panics if the total iteration count would overflow u64.

Source

pub fn game(&self) -> &Arc<SolverGame> ⓘ

The shared, immutable game definition.

Source

pub fn policy(&self) -> Policy

Snapshot of the average strategy at this iteration boundary.

Source

pub fn evaluate(&self) -> Evaluation

SolverGame::evaluate under the current average strategy.

Source

pub fn exploitability(&self) -> Exploitability

SolverGame::exploitability under the current average strategy.

Source

pub fn average_strategy(&self, node_id: usize) -> Vec<Vec<f64>>

The trained average strategy for one decision node, as [action][combo] (see SolverGame::average_strategy).

§Panics

Panics if node_id is not below SolverGame::num_decision_nodes.

Trait Implementations§

Source§

impl Debug for Trainer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

A summary: the update rule, progress, and the game, not the tables.

Auto Trait Implementations§

§

impl Forget for Trainer

§

impl Freeze for Trainer
where *const ArcInner<SolverGame> is !null: Freeze, PhantomData<ArcInner<SolverGame>>: Freeze, *const u8 is !null: Freeze, PhantomData<Mutex<NodeData>>: Freeze,

§

impl Move for Trainer

§

impl RefUnwindSafe for Trainer

§

impl Send for Trainer
where Arc<SolverGame>: Send, RawVecInner: Send, Mutex<NodeData>: Send,

§

impl Sync for Trainer
where Arc<SolverGame>: Sync, RawVecInner: Sync, Mutex<NodeData>: Sync,

§

impl Unpin for Trainer
where Arc<SolverGame>: Unpin, *const u8 is !null: Unpin, Align4<u32>: Unpin, Align1<u8>: Unpin,

§

impl UnsafeUnpin for Trainer
where *const ArcInner<SolverGame> is !null: UnsafeUnpin, PhantomData<ArcInner<SolverGame>>: UnsafeUnpin, *const u8 is !null: UnsafeUnpin, PhantomData<Mutex<NodeData>>: UnsafeUnpin,

§

impl UnwindSafe for Trainer
where Arc<SolverGame>: UnwindSafe, RawVecInner: UnwindSafe, Mutex<NodeData>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.