Skip to main content

PayoffCache

Struct PayoffCache 

Source
pub struct PayoffCache {
    pub eval_count: usize,
    /* private fields */
}
Expand description

Cached N-tensor empirical-payoff cache for an N-agent symmetric game.

Stores per-agent payoffs at every joint pure strategy s ∈ [0, k^N) where k is the per-agent population size (assumed identical across agents under the symmetric posture) and N is the number of agents.

§Index convention

The flat joint-strategy index decomposes into per-agent indices (s_0, s_1, ..., s_{N-1}) via little-endian mixed-radix: s = Σ_i s_i · k^i. Agent 0 is the fastest-varying index. This convention matches AlphaRankMetaSolver::solve_n_player_impl — the cache feeds its cells buffer directly into α-rank with no transpose.

§Storage

cells[s] is a Vec<f32> of length num_agents containing each agent’s mean per-episode return at joint strategy s. The per-cell allocation matches α-rank’s payoffs[s][a] input shape. For N=2 with k populations, this collapses to k² cells × 2-element vectors — identical information to the pre-refactor Vec<Vec<f32>> row-major matrix but with the per-cell agent payoffs co-located.

§Growth

PSRO grows each agent’s population by one policy per outer iteration. When agent a’s population grows from k to k+1, the cache needs to evaluate the new boundary slab: all joint strategies where agent a plays index k (its new policy). PayoffCache::resize_for_boundary grows the storage to the new (k+1)^N size; PayoffCache::set_cell writes individual cell payoffs. The trainer is responsible for iterating over the agent-a-newest-strategy boundary and calling set_cell for each new joint strategy.

Memory is O(k^N · N · f32), bounded by PsroConfig::max_population_size cubed (or higher for N>3); the PsroConfig::max_population_size cap should be tuned downward for large N to keep memory reasonable.

Fields§

§eval_count: usize

Counter incremented on every payoff evaluation (not every query). Used by unit tests to assert the cache is hit.

Implementations§

Source§

impl PayoffCache

Source

pub fn new() -> Self

Construct an empty cache.

Source

pub fn with_num_agents(num_agents: usize) -> Self

Construct a cache sized for num_agents agents with per_role_k = 0 (empty). Use PayoffCache::resize_for_boundary to grow.

Source

pub fn per_role_k(&self) -> usize

Current per-role population size k.

Source

pub fn num_agents(&self) -> usize

Number of agents N.

Source

pub fn num_cells(&self) -> usize

Total number of joint-strategy cells k^N.

Source

pub fn get_joint(&self, joint: &[usize]) -> Option<&[f32]>

Read the per-agent payoffs at joint strategy joint. Returns None if any per-agent index is out of bounds. The returned slice has length num_agents.

Source

pub fn payoff_tensor(&self) -> &[Vec<f32>]

View the full per-cell payoff tensor in the (k^N, N) flat layout consumed by AlphaRankMetaSolver::solve_n_player. The outer length is k^N; each inner Vec<f32> has length num_agents.

Source

pub fn set_cell(&mut self, joint: &[usize], payoffs: Vec<f32>)

Set the per-agent payoffs at joint strategy joint. Bumps eval_count by 1. Panics if the cache isn’t sized for joint (call PayoffCache::resize_for_boundary first) or if the payoff length doesn’t equal num_agents.

Source

pub fn set_cell_no_count(&mut self, joint: &[usize], payoffs: Vec<f32>)

Set the per-agent payoffs at joint strategy joint without bumping eval_count.

Used by the issue-#212 boundary-subsampling path to fill an un-sampled boundary cell with a reused payoff (copied from an already-evaluated sampled neighbour). Such a fill performs no fresh rollout, so it must not be counted as an evaluation — eval_count continues to reflect only the cells that were actually rolled out. Same bounds/asserts as Self::set_cell.

Source

pub fn resize_for_boundary(&mut self, new_per_role_k: usize)

Grow storage from (per_role_k)^N to (new_per_role_k)^N in-place, preserving the cached payoffs at all joint strategies that map to the same little-endian flat index in the new storage.

Newly-introduced cells are zero-initialized; the caller is responsible for evaluating them via the trainer’s evaluate_payoff_joint and writing the result with PayoffCache::set_cell.

§Why we can’t just Vec::resize

Under little-endian mixed-radix, joint index s = Σ_i s_i · k^i changes when the radix k grows: the same per-agent indices (s_0, ..., s_{N-1}) map to a different flat s' in the (k+1)^N storage. We rebuild the buffer by iterating over the old joint strategies and re-keying.

Source

pub fn boundary_joint_strategies(&self, agent_index: usize) -> Vec<Vec<usize>>

Iterate over every joint strategy s in the boundary slab where agent agent_index plays its newest pure strategy (per_role_k - 1) — the cells whose payoffs must be evaluated after agent agent_index’s population just grew by one.

Returns the joint-strategy index vectors (per-agent indices), suitable for passing to evaluate_payoff_joint and set_cell.

Trait Implementations§

Source§

impl Clone for PayoffCache

Source§

fn clone(&self) -> PayoffCache

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PayoffCache

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for PayoffCache

Source§

fn default() -> PayoffCache

Returns the “default value” for a type. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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