use burn::{
Tensor,
config::Config,
prelude::{
Backend,
Bool,
Int,
},
tensor::Distribution,
};
use crate::kits::sims::conway::{
ops::next_state_wrapped_3d,
util::{
ConwayRules,
ConwaySim,
},
};
#[derive(Config, Debug)]
pub struct ConwayLife3DConfig {
pub shape: [usize; 3],
#[config(default_value = "Default::default()")]
pub rules: ConwayRules,
}
impl ConwayLife3DConfig {
pub fn init<B: Backend>(
&self,
device: &B::Device,
) -> ConwayLife3DState<B> {
ConwayLife3DState {
state: Tensor::<B, 3, Int>::zeros(&self.shape, device).bool(),
rules: self.rules.clone(),
}
}
}
pub struct ConwayLife3DState<B: Backend> {
pub state: Tensor<B, 3, Bool>,
pub rules: ConwayRules,
}
impl<B: Backend> ConwayLife3DState<B> {
fn shape(&self) -> [usize; 3] {
self.state.shape().dims()
}
}
impl<B: Backend> ConwaySim<B> for ConwayLife3DState<B> {
fn device(&self) -> B::Device {
self.state.device()
}
fn fuzz(
&mut self,
density: f64,
) {
if density == 0.0 {
return;
}
let noise: Tensor<B, 3, Bool> = Tensor::<B, 3>::random(
self.shape(),
Distribution::Bernoulli(density),
&self.device(),
)
.equal_elem(1.0);
self.state.inplace(|s| s.bool_or(noise));
}
fn step(&mut self) {
self.state
.inplace(|s| next_state_wrapped_3d(s, &self.rules));
}
}
#[cfg(test)]
mod tests {
use serial_test::serial;
use super::*;
use crate::support::testing::{
DeviceMemoryGuard,
PerformanceBackend,
default_device,
};
#[test]
#[serial]
fn test_smoke() {
type B = PerformanceBackend;
let device = default_device();
let _memory = DeviceMemoryGuard::<B>::new(&device);
let steps = 100;
let grid_size = 20;
let config = ConwayLife3DConfig {
shape: [grid_size, grid_size, grid_size],
rules: ConwayRules {
spawn: 3..5,
keep: 2..4,
},
};
let mut game: ConwayLife3DState<B> = config.init(&device);
game.fuzz(0.05);
for _ in 0..steps {
game.step();
}
}
}