Skip to main content

NfspTrainer

Struct NfspTrainer 

Source
pub struct NfspTrainer<B, P, O, E, FP, FO, FE>
where B: AutodiffBackend, P: JointPolicy<B>, O: Optimizer<P, B>, E: JointEnv, FP: Fn(&B::Device, u64) -> P, FO: Fn() -> BurnOptimizer<B, P, O>, FE: Fn() -> E,
{ /* private fields */ }
Expand description

NFSP outer-loop trainer.

Generic over the Burn backend B, policy module P, optimizer type O, env E, and the user-supplied policy/optimizer/env factories. The trainer owns:

  • A JointMultiAgentTrainer holding the N best-response (BR) policies. BR updates go through JointMultiAgentTrainer::update_with_active_agents — the freeze-N-1 primitive PSRO also uses.
  • N average-policy (AP) Burn modules and their independent optimizers. AP updates are plain supervised cross-entropy on reservoir samples.
  • N reservoir buffers (one per agent) carrying (obs, action) pairs harvested from BR-sampled rollout steps.
  • An internal StdRng (seeded from NfspConfig::seed) used for η-anticipatory coin flips, reservoir eviction, and AP minibatch sampling.

As of #119, joint_config.num_agents may be any value ≥ 2. The formal ε-Nash convergence guarantee from Heinrich & Silver 2016 §3 holds only for N = 2 zero-sum games; for N > 2 this trainer ships the same per-agent BR/AP/reservoir machinery as an “approximate NFSP” — see the module docstring for the caveat.

§Single-policy-class assumption

All 2N policies (N BR + N AP) share the same module type P. For symmetric matching-pennies-style games this is what we want.

§See also

Implementations§

Source§

impl<B, P, O, E, FP, FO, FE> NfspTrainer<B, P, O, E, FP, FO, FE>
where B: AutodiffBackend, P: JointPolicy<B>, O: Optimizer<P, B>, E: JointEnv, FP: Fn(&B::Device, u64) -> P, FO: Fn() -> BurnOptimizer<B, P, O>, FE: Fn() -> E,

Source

pub fn new( config: NfspConfig, joint_config: JointTrainerConfig, device: B::Device, policy_factory: FP, optimizer_factory: FO, env_factory: FE, ) -> Result<Self>

Construct an NFSP trainer with fresh BR and AP policies.

joint_config.num_agents must be ≥ 2. For N = 2 the trainer retains Heinrich & Silver §3’s ε-Nash convergence guarantee on zero-sum games; for N > 2 it ships as “approximate NFSP” with no formal guarantee beyond per-agent reservoir uniformity (see the module docstring).

Source

pub fn br_policy(&self, i: usize) -> &P

Borrow agent i’s best-response (BR) policy.

Source

pub fn avg_policy(&self, i: usize) -> &P

Borrow agent i’s average (AP) policy.

Source

pub fn reservoir(&self, i: usize) -> &ReservoirBuffer<(Vec<f32>, Vec<i64>)>

Borrow agent i’s reservoir buffer.

Source

pub fn cumulative_br_pushes(&self) -> usize

Cumulative BR-sampled push count across all reservoirs.

Source

pub fn cumulative_rollout_steps(&self) -> usize

Cumulative rollout-step count across all agents.

Source

pub fn config(&self) -> &NfspConfig

Trainer configuration.

Source

pub fn run<F>(&mut self, on_iteration: F) -> Result<NfspStats>
where F: FnMut(&NfspIterationStats), P: Send + Sync, O: Send, B::Device: Sync,

Run the NFSP outer loop and return the per-iteration history.

Source

pub fn run_silent(&mut self) -> Result<NfspStats>
where P: Send + Sync, O: Send, B::Device: Sync,

Convenience entry point: drives Self::run with a no-op iteration callback.

Source

pub fn action_marginal_for(&mut self, policy: &P) -> Option<Vec<f32>>

Average-policy action distribution under a constant observation [0.0; obs_dim]. Returns None if the policy’s action_dims is unsupported (e.g. multi-dim multi-discrete). Used as a diagnostic on matching-pennies and as the load-bearing convergence assertion in tests/test_nfsp_matching_pennies.rs.

Takes &mut self because the 128-probe sampling loop consumes the trainer-owned StdRng via JointPolicy::get_action_host_seeded. This makes the diagnostic reproducible under NfspConfig::seed (issue #114). Callers that hold a &P from Self::avg_policy / Self::br_policy must .clone() the policy before calling this method to avoid a &self / &mut self aliasing conflict — the policy clone is cheap (Burn modules are Clone by design).

Auto Trait Implementations§

§

impl<B, P, O, E, FP, FO, FE> Freeze for NfspTrainer<B, P, O, E, FP, FO, FE>
where <B as BackendTypes>::Device: Freeze, FP: Freeze, FO: Freeze, FE: Freeze,

§

impl<B, P, O, E, FP, FO, FE> RefUnwindSafe for NfspTrainer<B, P, O, E, FP, FO, FE>

§

impl<B, P, O, E, FP, FO, FE> Send for NfspTrainer<B, P, O, E, FP, FO, FE>
where FP: Send, FO: Send, FE: Send,

§

impl<B, P, O, E, FP, FO, FE> Sync for NfspTrainer<B, P, O, E, FP, FO, FE>
where FP: Sync, FO: Sync, FE: Sync, P: Sync, O: Sync,

§

impl<B, P, O, E, FP, FO, FE> Unpin for NfspTrainer<B, P, O, E, FP, FO, FE>
where <B as BackendTypes>::Device: Unpin, FP: Unpin, FO: Unpin, FE: Unpin, P: Unpin, O: Unpin, B: Unpin,

§

impl<B, P, O, E, FP, FO, FE> UnsafeUnpin for NfspTrainer<B, P, O, E, FP, FO, FE>

§

impl<B, P, O, E, FP, FO, FE> UnwindSafe for NfspTrainer<B, P, O, E, FP, FO, FE>

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> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more