pub struct SnakeEnv {
pub width: i32,
pub height: i32,
pub snakes: Vec<Snake>,
pub num_agents: usize,
pub food: Position,
pub episode: usize,
pub steps: usize,
pub max_steps: usize,
pub done: bool,
pub rng: StdRng,
}Expand description
Multi-agent Snake environment.
§Snapshot semantics
clone_state / restore_state capture every visible env field (grid,
snake bodies, food location, score, step count, done flag) and the
env’s owned RNG. Because the RNG is part of the snapshot, restoring and
re-stepping replays the identical random stream: a food respawn after
restore_state lands in the same cell as the original trajectory, so
round-trips are fully reproducible (including trajectories that eat food).
Fields§
§width: i32Grid width
height: i32Grid height
snakes: Vec<Snake>All snakes
num_agents: usizeNumber of agents
food: PositionFood position
episode: usizeEpisode counter
steps: usizeStep counter
max_steps: usizeMaximum steps per episode
done: boolDone flag
rng: StdRngOwned RNG for food spawning. Captured by clone_state so that
restore_state + step reproduces the exact same trajectory.
Implementations§
Source§impl SnakeEnv
impl SnakeEnv
Sourcepub fn new_multi(width: i32, height: i32, num_agents: usize) -> Self
pub fn new_multi(width: i32, height: i32, num_agents: usize) -> Self
Create new snake environment with specified number of agents
Sourcepub fn new_multi_with_seed(
width: i32,
height: i32,
num_agents: usize,
seed: u64,
) -> Self
pub fn new_multi_with_seed( width: i32, height: i32, num_agents: usize, seed: u64, ) -> Self
Create new snake environment with a specific RNG seed.
The seed controls food placement. Two envs constructed with the same seed and stepped with the same actions produce identical trajectories.
Sourcepub fn new(width: i32, height: i32) -> Self
pub fn new(width: i32, height: i32) -> Self
Create new single-agent snake environment (for backward compatibility)
Sourcepub fn step_multi(&mut self, actions: &[i64]) -> StepResult
pub fn step_multi(&mut self, actions: &[i64]) -> StepResult
Execute multi-agent step with actions for all snakes Returns a single StepResult with summed rewards (for backward compatibility) Use step_multi_agents for per-agent rewards
Sourcepub fn step_multi_agents(&mut self, actions: &[i64]) -> (Vec<f32>, bool, bool)
pub fn step_multi_agents(&mut self, actions: &[i64]) -> (Vec<f32>, bool, bool)
Execute multi-agent step with per-agent rewards Returns individual rewards for each snake
Sourcepub fn step(&mut self, action: i64) -> StepResult
pub fn step(&mut self, action: i64) -> StepResult
Execute single-agent step (for backward compatibility)
Sourcepub fn get_grid_observation(&self, snake_id: usize) -> Vec<f32>
pub fn get_grid_observation(&self, snake_id: usize) -> Vec<f32>
Get grid-based observation for a specific snake
Returns a multi-channel grid representation:
- Channel 0: Own snake body (1.0 where body is)
- Channel 1: Own snake head (1.0 at head position)
- Channel 2: Other snakes (1.0 where other snakes are)
- Channel 3: Food (1.0 at food position)
- Channel 4: Walls (1.0 at boundaries)
Flattened as [C0_pixels…, C1_pixels…, C2_pixels…, …]
Sourcepub fn get_observation(&self) -> Vec<f32>
pub fn get_observation(&self) -> Vec<f32>
Get current observation (for first snake, backward compatibility)
Trait Implementations§
Source§impl Environment for SnakeEnv
impl Environment for SnakeEnv
Source§type State = SnakeEnvState
type State = SnakeEnvState
restore_state
reproduces every subsequent step exactly. For envs that consume an
internal RNG (e.g. ball serve direction, food placement), the snapshot
captures the simulation step but not the RNG; see the env-level
documentation for which fields are preserved. Read moreSource§fn get_observation(&self) -> Vec<f32>
fn get_observation(&self) -> Vec<f32>
Source§fn step(&mut self, action: i64) -> StepResult
fn step(&mut self, action: i64) -> StepResult
Source§fn observation_space(&self) -> SpaceInfo
fn observation_space(&self) -> SpaceInfo
Source§fn action_space(&self) -> SpaceInfo
fn action_space(&self) -> SpaceInfo
Source§fn clone_state(&self) -> SnakeEnvState
fn clone_state(&self) -> SnakeEnvState
Source§fn restore_state(&mut self, state: &SnakeEnvState)
fn restore_state(&mut self, state: &SnakeEnvState)
Auto Trait Implementations§
impl Freeze for SnakeEnv
impl RefUnwindSafe for SnakeEnv
impl Send for SnakeEnv
impl Sync for SnakeEnv
impl Unpin for SnakeEnv
impl UnsafeUnpin for SnakeEnv
impl UnwindSafe for SnakeEnv
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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