Skip to content

Commit

Permalink
feat: swap rng with ChaCha8 to make the simulation deterministic + ou…
Browse files Browse the repository at this point in the history
…tput agent position at step 200 to compare them with the ecs-experiment branch
  • Loading branch information
Carbonhell committed Feb 26, 2024
1 parent 5f67039 commit 1767fbb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
3 changes: 2 additions & 1 deletion flockers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ readme = "README.md"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
krabmaga = "0.4.*"
krabmaga = {path = "../../krABMaga"}
rand_chacha = "0.3.1"

[features]
parallel = ["krabmaga/parallel"]
Expand Down
8 changes: 5 additions & 3 deletions flockers/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,18 @@ pub static MOMENTUM: f32 = 1.0;
pub static JUMP: f32 = 0.7;
pub static DISCRETIZATION: f32 = 10.0 / 1.5;
pub static TOROIDAL: bool = true;
pub static SEED: u64 = 1337;

// Main used when only the simulation should run, without any visualization.
#[cfg(not(any(feature = "visualization", feature = "visualization_wasm")))]
fn main() {
let step = 200;

let dim = (800., 800.);
let num_agents = 64000;
let state = Flocker::new(dim, num_agents);
let _ = simulate_old!(state, step, 1, Info::Normal);
let num_agents = 200;
let mut state = Flocker::new(dim, num_agents);
// Outputs the positions of the 200 agents at step 200. Compare those with the ecs-experiment branch to verify the ecs-experiment branch is correct.
let _ = simulate_old!(&mut state, step, 1, Info::Verbose);
}

// Main used when a visualization feature is applied.
Expand Down
33 changes: 23 additions & 10 deletions flockers/src/model/bird.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use krabmaga::engine::agent::Agent;
use krabmaga::engine::fields::field_2d::{toroidal_distance, toroidal_transform, Location2D};
use krabmaga::engine::location::Real2D;
use krabmaga::engine::state::State;
use krabmaga::rand;
use krabmaga::{rand, Uniform};
use krabmaga::rand::Rng;
use std::hash::{Hash, Hasher};
use rand_chacha::ChaCha8Rng;
use rand_chacha::rand_core::SeedableRng;

use crate::model::state::Flocker;
use crate::{AVOIDANCE, COHESION, CONSISTENCY, JUMP, MOMENTUM, RANDOMNESS};
use crate::{AVOIDANCE, COHESION, CONSISTENCY, JUMP, MOMENTUM, RANDOMNESS, SEED};
use krabmaga::Distribution;

#[derive(Clone, Copy)]
pub struct Bird {
Expand Down Expand Up @@ -99,12 +102,19 @@ impl Agent for Bird {
y: -y_cohe / 10.0,
};

//randomness
let mut rng = rand::thread_rng();
let r1: f32 = rng.gen();
let x_rand = r1 * 2.0 - 1.0;
let r2: f32 = rng.gen();
let y_rand = r2 * 2.0 - 1.0;
//randomness - uses the same rng of the ecs-experiment branch
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
rng.set_stream(self.id as u64 + state.step);
let mut range = Uniform::new(0.0f32, 1.0);
let r1: f32 = range.sample(&mut rng);
let x_rand = r1 * 2. - 1.;
let r2: f32 = range.sample(&mut rng);
let y_rand = r2 * 2. - 1.;
// let mut rng = rand::thread_rng();
// let r1: f32 = rng.gen();
// let x_rand = r1 * 2.0 - 1.0;
// let r2: f32 = rng.gen();
// let y_rand = r2 * 2.0 - 1.0;

let square = (x_rand * x_rand + y_rand * y_rand).sqrt();
randomness = Real2D {
Expand Down Expand Up @@ -138,6 +148,9 @@ impl Agent for Bird {
let loc_y = toroidal_transform(self.loc.y + dy, height);

self.loc = Real2D { x: loc_x, y: loc_y };
if state.step == 200 {
println!("bird {} step {} - cohesion {}, avoidance {}, consistency {}, randomness {}, mom {}, loc_x {} loc_y {}", self.id, state.step, cohesion, avoidance, consistency, randomness, mom, loc_x, loc_y);
}
drop(vec);
state
.field1
Expand All @@ -147,8 +160,8 @@ impl Agent for Bird {

impl Hash for Bird {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
where
H: Hasher,
{
self.id.hash(state);
}
Expand Down
16 changes: 11 additions & 5 deletions flockers/src/model/state.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::model::bird::Bird;
use crate::{DISCRETIZATION, TOROIDAL};
use crate::{DISCRETIZATION, SEED, TOROIDAL};
use krabmaga::engine::fields::field::Field;
use krabmaga::engine::fields::field_2d::Field2D;
use krabmaga::engine::location::Real2D;
use krabmaga::engine::schedule::Schedule;
use krabmaga::engine::state::State;
use krabmaga::rand;
use krabmaga::{rand, Uniform};
use krabmaga::Distribution;
use krabmaga::rand::Rng;
use std::any::Any;
use rand_chacha::ChaCha8Rng;
use rand_chacha::rand_core::SeedableRng;

pub struct Flocker {
pub step: u64,
Expand Down Expand Up @@ -35,11 +38,13 @@ impl State for Flocker {
}

fn init(&mut self, schedule: &mut Schedule) {
let mut rng = rand::thread_rng();
// Should be moved in the init method on the model exploration changes
for bird_id in 0..self.initial_flockers {
let r1: f32 = rng.gen();
let r2: f32 = rng.gen();
let mut rng = ChaCha8Rng::seed_from_u64(SEED);
let range = Uniform::new(0.0f32, 1.0);
rng.set_stream(bird_id as u64);
let r1: f32 = range.sample(&mut rng);
let r2: f32 = range.sample(&mut rng);
let last_d = Real2D { x: 0., y: 0. };
let loc = Real2D {
x: self.dim.0 * r1,
Expand All @@ -53,6 +58,7 @@ impl State for Flocker {

fn update(&mut self, _step: u64) {
self.field1.lazy_update();
self.step += 1;
}

fn as_any(&self) -> &dyn Any {
Expand Down

0 comments on commit 1767fbb

Please sign in to comment.