forked from Maxluli/RustyPropagation
Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a6d56af613 | |||
| 1abf452ce5 | |||
| ea74e1029b |
+1
-1
@@ -7,4 +7,4 @@ edition = "2021"
|
|||||||
console = "0.15.0"
|
console = "0.15.0"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
parameterized = "1.0.0"
|
parameterized = "1.0.0"
|
||||||
clap = { version = "3.1.15", features = ["derive"] }
|
clap = { version = "3.0", features = ["derive"] }
|
||||||
|
|||||||
+13
-27
@@ -1,3 +1,5 @@
|
|||||||
|
use clap::Parser;
|
||||||
|
|
||||||
mod disease;
|
mod disease;
|
||||||
mod human;
|
mod human;
|
||||||
mod population;
|
mod population;
|
||||||
@@ -12,34 +14,21 @@ mod prelude {
|
|||||||
}
|
}
|
||||||
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
use clap::Parser;
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser)]
|
||||||
struct Args {
|
struct Cli {
|
||||||
/// Number of threads
|
/// Number of threads to use
|
||||||
#[clap(short, long, default_value_t = 1)]
|
|
||||||
threads: usize,
|
threads: usize,
|
||||||
/// Display stats after each propagation
|
|
||||||
#[clap(short, long)]
|
|
||||||
display: bool,
|
|
||||||
/// Width of humans grid in population
|
|
||||||
#[clap(short, long, default_value_t = 1000)]
|
|
||||||
width: i32,
|
|
||||||
/// Height of humans grid in population
|
|
||||||
#[clap(short, long, default_value_t = 1000)]
|
|
||||||
height: i32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Cli::parse();
|
||||||
|
|
||||||
let term = Term::stdout();
|
let term = Term::stdout();
|
||||||
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
||||||
.expect("Oops Looks like we have a problem here...");
|
.expect("Oops Looks like we have a problem here...");
|
||||||
|
|
||||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||||
let mut population = Population::new(20,10,5,args.width,args.height,disease);
|
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
|
||||||
//population.change_disease(disease);
|
//population.change_disease(disease);
|
||||||
println!("After Filling");
|
println!("After Filling");
|
||||||
//population.display();
|
//population.display();
|
||||||
@@ -47,21 +36,18 @@ fn main() {
|
|||||||
let mut stats: [i32; 4];
|
let mut stats: [i32; 4];
|
||||||
// = [0,0,0,0];
|
// = [0,0,0,0];
|
||||||
let mut counter: u32 = 0;
|
let mut counter: u32 = 0;
|
||||||
let now = Instant::now();
|
|
||||||
loop {
|
loop {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
stats = population.propagate_new(Some(args.threads));
|
stats = population.propagate(args.threads);
|
||||||
//population.display();
|
//population.display();
|
||||||
if args.display {
|
println!(
|
||||||
println!(
|
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
||||||
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
stats[0], stats[1], stats[2], stats[3]
|
||||||
stats[0], stats[1], stats[2], stats[3]
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
if stats[1] == 0 {
|
if stats[1] == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("Propagation finished in {} steps, {:?}", counter, now.elapsed());
|
println!("Propagation finished in {} steps", counter);
|
||||||
//population.display();
|
//population.display();
|
||||||
}
|
}
|
||||||
|
|||||||
+325
-559
@@ -1,14 +1,20 @@
|
|||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::{Arc, RwLock};
|
||||||
use std::sync::Mutex;
|
|
||||||
use std::thread;
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use std::thread;
|
||||||
|
use std::time::{Instant};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Population {
|
pub struct Population {
|
||||||
pub start_infected_ratio: i32,
|
pub start_infected_ratio: i32,
|
||||||
pub start_immune_ratio: i32,
|
pub start_immune_ratio: i32,
|
||||||
pub start_dead_ratio: i32,
|
pub start_dead_ratio: i32,
|
||||||
pub humans: Arc<Vec<Human>>,
|
pub humans: Arc<RwLock<Vec<Human>>>,
|
||||||
pub width: i32,
|
pub width: i32,
|
||||||
pub height: i32,
|
pub height: i32,
|
||||||
pub age: i32,
|
pub age: i32,
|
||||||
@@ -33,7 +39,7 @@ impl Population {
|
|||||||
|
|
||||||
let size: usize = (width * height) as usize;
|
let size: usize = (width * height) as usize;
|
||||||
|
|
||||||
let mut the_humans: Vec<Human> = vec![
|
let mut the_humans = vec![
|
||||||
Human {
|
Human {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
@@ -41,6 +47,7 @@ impl Population {
|
|||||||
};
|
};
|
||||||
size
|
size
|
||||||
];
|
];
|
||||||
|
|
||||||
for x in 0..width {
|
for x in 0..width {
|
||||||
for y in 0..height {
|
for y in 0..height {
|
||||||
let idx = human_idx(x, y, width);
|
let idx = human_idx(x, y, width);
|
||||||
@@ -61,6 +68,7 @@ impl Population {
|
|||||||
the_humans[idx] = Human{x: x, y: y, present_state: present_state};
|
the_humans[idx] = Human{x: x, y: y, present_state: present_state};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
start_infected_ratio: start_infected_ratio,
|
start_infected_ratio: start_infected_ratio,
|
||||||
start_immune_ratio: start_immune_ratio,
|
start_immune_ratio: start_immune_ratio,
|
||||||
@@ -69,137 +77,342 @@ impl Population {
|
|||||||
height: height,
|
height: height,
|
||||||
plague: plague,
|
plague: plague,
|
||||||
age: 0,
|
age: 0,
|
||||||
humans: Arc::new(the_humans),
|
humans: Arc::new(RwLock::new(the_humans)),
|
||||||
size: size,
|
size: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// pub fn change_disease(&mut self, plague:Disease){
|
||||||
|
// self.plague = plague;
|
||||||
|
// }
|
||||||
|
|
||||||
pub fn propagate_new(&mut self, num_threads: Option<usize>) -> [i32; 4] {
|
// fn is_inside(&self, pos: &Point) -> bool {
|
||||||
let humans = Arc::clone(&self.humans);
|
// if pos.x >= 0 && pos.x < self.width && pos.y >= 0 && pos.y < self.height {
|
||||||
let len = humans.len();
|
// true
|
||||||
|
// } else {
|
||||||
|
// false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(len);
|
// fn is_inside_and_infected(&self, point: Point) -> bool {
|
||||||
let stats: [i32; 4] = [0, 0, 0, 0];
|
// let humans = Arc::clone(&self.humans);
|
||||||
|
// if self.is_inside(&point) {
|
||||||
|
// let idx = human_idx(point.x, point.y, self.width);
|
||||||
|
// let humans = humans.read().unwrap();
|
||||||
|
// if humans[idx].present_state == State::Infected {
|
||||||
|
// roll(self.plague.infection_rate)
|
||||||
|
// } else {
|
||||||
|
// false
|
||||||
|
// }
|
||||||
|
// } else {
|
||||||
|
// false
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
let num_threads = num_threads.unwrap_or(1);
|
pub fn propagate(&mut self, num_threads: usize) -> [i32; 4] {
|
||||||
if num_threads > 48 {
|
let timer = Instant::now();
|
||||||
panic!("too many threads")
|
let mut people_to_check: Vec<Point> =
|
||||||
}
|
Vec::with_capacity(self.size);
|
||||||
|
let mut possible_infected: Vec<Point> =
|
||||||
// number of items per batch
|
Vec::with_capacity(self.size);
|
||||||
let mut num_items = (len / num_threads) as usize;
|
let mut people_to_infect: Vec<Point> =
|
||||||
if len % num_threads != 0 {
|
Vec::with_capacity(self.size);
|
||||||
num_items += 1;
|
let mut people_to_cure: Vec<Point> =
|
||||||
}
|
Vec::with_capacity(self.size);
|
||||||
|
let mut people_to_kill: Vec<Point> =
|
||||||
|
Vec::with_capacity(self.size);
|
||||||
|
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
||||||
|
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
||||||
|
|
||||||
let mut threads = vec![];
|
let mut threads = vec![];
|
||||||
|
|
||||||
let stats_arc = Arc::new(Mutex::new(stats));
|
let len: usize = (self.width * self.height) as usize;
|
||||||
|
let mut lines = (len / num_threads) as usize;
|
||||||
|
if len % num_threads != 0 {
|
||||||
|
lines += 1;
|
||||||
|
}
|
||||||
for x in 0..num_threads {
|
for x in 0..num_threads {
|
||||||
|
let lower_bound = x * lines;
|
||||||
// find items to process
|
let mut upper_bound = (x + 1) * lines;
|
||||||
let lower_bound = x * num_items;
|
|
||||||
let mut upper_bound = (x + 1) * num_items;
|
|
||||||
if upper_bound > len {
|
if upper_bound > len {
|
||||||
upper_bound = len;
|
upper_bound = len;
|
||||||
};
|
};
|
||||||
|
|
||||||
// borrow copies of data for thread
|
|
||||||
let humans = Arc::clone(&self.humans);
|
let humans = Arc::clone(&self.humans);
|
||||||
let stats = Arc::clone(&stats_arc);
|
|
||||||
let (width, height, infection_rate, curing_rate, death_rate) =
|
|
||||||
(self.width, self.height, self.plague.infection_rate, self.plague.curing_rate, self.plague.death_rate);
|
|
||||||
|
|
||||||
threads.push(thread::spawn(move || {
|
threads.push(thread::spawn(move || {
|
||||||
|
let mut possible_infected: Vec<Point> =Vec::with_capacity(lines);
|
||||||
// no write on data
|
let mut people_to_check: Vec<Point> =Vec::with_capacity(lines);
|
||||||
// let humans = humans;
|
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
||||||
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(num_items);
|
let humans = humans.read().unwrap();
|
||||||
|
|
||||||
for idx in lower_bound..upper_bound {
|
for idx in lower_bound..upper_bound {
|
||||||
let human = &humans[idx];
|
let h = &humans[idx];
|
||||||
// let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
match h.present_state {
|
||||||
let mut new_human = human.clone();
|
|
||||||
match human.present_state {
|
|
||||||
State::Normal => {
|
State::Normal => {
|
||||||
let possible = [
|
possible_infected.push(Point{ x: h.x, y: h.y});
|
||||||
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
|
|
||||||
(human.x - 1, human.y) , (human.x + 1, human.y),
|
|
||||||
(human.x - 1, human.y + 1), (human.x, human.y + 1), (human.x + 1, human.y + 1),
|
|
||||||
];
|
|
||||||
for neigh_coords in possible.iter() {
|
|
||||||
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
|
|
||||||
match neigh_idx {
|
|
||||||
Some(x) => if humans[x].present_state == State::Infected {
|
|
||||||
if roll(infection_rate) {
|
|
||||||
new_human.present_state = State::Infected;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[0] += 1;
|
stats[0] += 1;
|
||||||
}
|
}
|
||||||
State::Infected => {
|
State::Infected => {
|
||||||
match die_or_cure(curing_rate, death_rate) {
|
people_to_check.push(Point { x: h.x, y: h.y });
|
||||||
Some(x) => new_human.present_state = x,
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[1] += 1;
|
stats[1] += 1;
|
||||||
}
|
}
|
||||||
State::Immune => {
|
State::Immune => {
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[2] += 1;
|
stats[2] += 1;
|
||||||
}
|
}
|
||||||
State::Dead => {
|
State::Dead => {
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[3] += 1;
|
stats[3] += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
humans_n_plus_1.push(new_human);
|
|
||||||
}
|
}
|
||||||
(humans_n_plus_1,)
|
(possible_infected, people_to_check, stats)
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
for t in threads {
|
for t in threads {
|
||||||
let mut res = t.join().unwrap();
|
let mut res = t.join().unwrap();
|
||||||
humans_n_plus_1.append(&mut res.0);
|
possible_infected.append(&mut res.0);
|
||||||
// for x in 0..4 {
|
people_to_check.append(&mut res.1);
|
||||||
// stats[x] += res.1[x];
|
for x in 0..4 {
|
||||||
// }
|
stats[x] += res.2[x];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.humans = Arc::new(humans_n_plus_1);
|
#[cfg(debug_assertions)]
|
||||||
let stats = Arc::clone(&stats_arc);
|
println!("after first pass {:?}", timer.elapsed());
|
||||||
let stats = *stats.lock().unwrap();
|
|
||||||
|
debug_assert_eq!(
|
||||||
|
stats[0] + stats[1] + stats[2] + stats[3],
|
||||||
|
self.size as i32
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
|
let mut threads = vec![];
|
||||||
|
|
||||||
|
let len: usize = people_to_check.len();
|
||||||
|
let mut lines = (len / num_threads) as usize;
|
||||||
|
if len % num_threads != 0 {
|
||||||
|
lines += 1;
|
||||||
|
}
|
||||||
|
let people_to_check_arc = Arc::new(people_to_check);
|
||||||
|
for x in 0..num_threads {
|
||||||
|
let lower_bound = x * lines;
|
||||||
|
let mut upper_bound = (x + 1) * lines;
|
||||||
|
if upper_bound > len {
|
||||||
|
upper_bound = len;
|
||||||
|
};
|
||||||
|
let curing_rate = self.plague.curing_rate;
|
||||||
|
let death_rate = self.plague.death_rate;
|
||||||
|
let people_to_check = Arc::clone(&people_to_check_arc);
|
||||||
|
threads.push(thread::spawn(move || {
|
||||||
|
let mut people_to_cure: Vec<Point> =Vec::with_capacity(lines);
|
||||||
|
let mut people_to_kill: Vec<Point> =Vec::with_capacity(lines);
|
||||||
|
for idx in lower_bound..upper_bound {
|
||||||
|
let pos = &people_to_check[idx];
|
||||||
|
if roll(curing_rate) {
|
||||||
|
//checks if the man recovers
|
||||||
|
people_to_cure.push(Point { x: pos.x, y: pos.y });
|
||||||
|
} else {
|
||||||
|
if roll(death_rate) {
|
||||||
|
//cheks if the man dies
|
||||||
|
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
(people_to_cure, people_to_kill)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
for t in threads {
|
||||||
|
let mut res = t.join().unwrap();
|
||||||
|
people_to_cure.append(&mut res.0);
|
||||||
|
people_to_kill.append(&mut res.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
println!("after people_to_check {:?}", timer.elapsed());
|
||||||
|
|
||||||
|
// check normal people to see if someone near is infected
|
||||||
|
|
||||||
|
let mut threads = vec![];
|
||||||
|
|
||||||
|
let len: usize = possible_infected.len();
|
||||||
|
let mut lines = (len / num_threads) as usize;
|
||||||
|
if len % num_threads != 0 {
|
||||||
|
lines += 1;
|
||||||
|
}
|
||||||
|
let possible_infected_arc = Arc::new(possible_infected);
|
||||||
|
|
||||||
|
{
|
||||||
|
let (width, height) = (self.width, self.height);
|
||||||
|
let infection_rate = self.plague.infection_rate;
|
||||||
|
|
||||||
|
for x in 0..num_threads {
|
||||||
|
let humans = Arc::clone(&self.humans);
|
||||||
|
|
||||||
|
|
||||||
|
let lower_bound = x * lines;
|
||||||
|
let mut upper_bound = (x + 1) * lines;
|
||||||
|
if upper_bound > len {
|
||||||
|
upper_bound = len;
|
||||||
|
};
|
||||||
|
let possible_infected = Arc::clone(&possible_infected_arc);
|
||||||
|
threads.push(thread::spawn(move || {
|
||||||
|
let humans = &humans.read().unwrap().to_vec();
|
||||||
|
let mut people_to_infect: Vec<Point> =Vec::with_capacity(lines);
|
||||||
|
for idx in lower_bound..upper_bound {
|
||||||
|
let pos = &possible_infected[idx];
|
||||||
|
|
||||||
|
let infected: bool = fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x - 1,
|
||||||
|
y: pos.y - 1,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
) || //Top Left
|
||||||
|
fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x,
|
||||||
|
y: pos.y - 1,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
) || //Top
|
||||||
|
fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x + 1,
|
||||||
|
y: pos.y - 1,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
) || //Top Right
|
||||||
|
fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x - 1,
|
||||||
|
y: pos.y,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
) || //Left
|
||||||
|
fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x + 1,
|
||||||
|
y: pos.y,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
) || //Right
|
||||||
|
fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x - 1,
|
||||||
|
y: pos.y + 1,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
) || //Bottom Left
|
||||||
|
fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x,
|
||||||
|
y: pos.y + 1,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
) || //Bottom
|
||||||
|
fn_is_inside_and_infected(
|
||||||
|
Point {
|
||||||
|
x: pos.x + 1,
|
||||||
|
y: pos.y + 1,
|
||||||
|
}, width, height, humans, infection_rate
|
||||||
|
); //Bottom Right
|
||||||
|
if infected {
|
||||||
|
people_to_infect.push(Point { x: pos.x, y: pos.y });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
people_to_infect
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
for t in threads {
|
||||||
|
let mut res = t.join().unwrap();
|
||||||
|
people_to_infect.append(&mut res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
println!("after possible_infected {:?}", timer.elapsed());
|
||||||
|
|
||||||
|
let mut threads = vec![];
|
||||||
|
|
||||||
|
{
|
||||||
|
let humans = Arc::clone(&self.humans);
|
||||||
|
let width = self.width;
|
||||||
|
threads.push(thread::spawn(move || {
|
||||||
|
for infected_position in people_to_infect.iter() {
|
||||||
|
let infected_index = human_idx(infected_position.x, infected_position.y, width);
|
||||||
|
{
|
||||||
|
// eprint!("i");
|
||||||
|
let mut humans = humans.write().unwrap();
|
||||||
|
humans[infected_index].present_state = State::Infected;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let humans = Arc::clone(&self.humans);
|
||||||
|
let width = self.width;
|
||||||
|
threads.push(thread::spawn(move || {
|
||||||
|
for cured_position in people_to_cure.iter() {
|
||||||
|
//people_to_cure.iter().map(|cured_position|{
|
||||||
|
let cured_index = human_idx(cured_position.x, cured_position.y, width);
|
||||||
|
{
|
||||||
|
let humans = humans.read().unwrap();
|
||||||
|
debug_assert_eq!(humans[cured_index].present_state, State::Infected, "This human should be infected: {:?}", humans[cured_index].present_state);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// eprint!("c");
|
||||||
|
let mut humans = humans.write().unwrap();
|
||||||
|
humans[cured_index].present_state = State::Immune;
|
||||||
|
}
|
||||||
|
//DEBUG
|
||||||
|
//println!("Cured someone");
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let humans = Arc::clone(&self.humans);
|
||||||
|
let width = self.width;
|
||||||
|
threads.push(thread::spawn(move || {
|
||||||
|
for dead_position in people_to_kill.iter() {
|
||||||
|
//people_to_kill.iter().map(|dead_position|{
|
||||||
|
let dead_index = human_idx(dead_position.x, dead_position.y, width);
|
||||||
|
{
|
||||||
|
let humans = humans.read().unwrap();
|
||||||
|
debug_assert!(humans[dead_index].present_state != State::Dead);
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// eprint!("k");
|
||||||
|
let mut humans = humans.write().unwrap();
|
||||||
|
humans[dead_index].present_state = State::Dead;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
for t in threads {
|
||||||
|
t.join().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(debug_assertions)]
|
||||||
|
println!("after kills and co {:?}", timer.elapsed());
|
||||||
|
|
||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub fn display(&mut self){
|
||||||
|
// let sprite = "#";
|
||||||
|
// print!("\n");
|
||||||
|
// for x in 0..self.width{
|
||||||
|
// for y in 0..self.height{
|
||||||
|
// let index = human_idx(x as i32,y as i32,self.width as i32);
|
||||||
|
// match self.humans[index].present_state {
|
||||||
|
// State::Normal => print!("{}",style(sprite).green()),
|
||||||
|
// State::Dead => print!("{}",style(sprite).black()),
|
||||||
|
// State::Infected => print!("{}",style(sprite).red()),
|
||||||
|
// State::Immune => print!("{}",style(sprite).blue()),
|
||||||
|
// _ => print!("{}",style(sprite).white()),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// print!("\n");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn die_or_cure(curing_rate: i32, death_rate: i32) -> Option<State> {
|
pub fn roll(probability: i32) -> bool {
|
||||||
if roll(curing_rate) {
|
|
||||||
Some(State::Immune)
|
|
||||||
} else if roll(death_rate) {
|
|
||||||
Some(State::Dead)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn point_to_index(x: i32, y: i32, width: i32, height: i32) -> Option<usize> {
|
|
||||||
if (x >= 0) && (x < width) && (y >= 0) && (y < height) {
|
|
||||||
Some(human_idx(x, y, width))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn roll(probability: i32) -> bool {
|
|
||||||
if probability > 0 {
|
if probability > 0 {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
|
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
|
||||||
@@ -208,471 +421,24 @@ fn roll(probability: i32) -> bool {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
fn fn_is_inside(pos: &Point, width: i32, height: i32) -> bool {
|
||||||
mod tests {
|
if (pos.x >= 0) && (pos.x < width) && (pos.y >= 0) && (pos.y < height) {
|
||||||
use super::*;
|
true
|
||||||
use parameterized::parameterized;
|
} else {
|
||||||
|
false
|
||||||
const THREADS: Option<usize> = Some(4);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct Stats {
|
|
||||||
normal: i32,
|
|
||||||
infected: i32,
|
|
||||||
immune: i32,
|
|
||||||
dead: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Stats {
|
|
||||||
fn new() -> Stats {
|
|
||||||
Stats {
|
|
||||||
normal: 0,
|
|
||||||
infected: 0,
|
|
||||||
immune: 0,
|
|
||||||
dead: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn humans_stats(humans: &Vec<Human>) -> Stats {
|
|
||||||
let mut stats: Stats = Stats::new();
|
|
||||||
for human in humans.iter() {
|
|
||||||
match human.present_state {
|
|
||||||
State::Normal => {
|
|
||||||
stats.normal += 1;
|
|
||||||
}
|
|
||||||
State::Infected => {
|
|
||||||
stats.infected += 1;
|
|
||||||
}
|
|
||||||
State::Immune => {
|
|
||||||
stats.immune += 1;
|
|
||||||
}
|
|
||||||
State::Dead => {
|
|
||||||
stats.dead += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
stats
|
|
||||||
}
|
|
||||||
|
|
||||||
#[parameterized(x = {
|
|
||||||
2, 3, 5
|
|
||||||
}, y = {
|
|
||||||
1, 4, 0
|
|
||||||
}, width = {
|
|
||||||
3, 5, 7
|
|
||||||
}, res = {
|
|
||||||
5, 23, 5
|
|
||||||
})]
|
|
||||||
fn test_human_idx(x: i32, y: i32, width: i32, res: usize) {
|
|
||||||
assert_eq!(human_idx(x, y, width), res);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_human_stats() {
|
|
||||||
let mut humans: Vec<Human> = Vec::with_capacity(10);
|
|
||||||
let mut stats: Stats;
|
|
||||||
|
|
||||||
for _ in 0..10 {
|
|
||||||
humans.push(Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
assert_eq!(stats.normal, 10);
|
|
||||||
|
|
||||||
for x in 0..2 {
|
|
||||||
humans[x].present_state = State::Infected;
|
|
||||||
}
|
|
||||||
for x in 2..5 {
|
|
||||||
humans[x].present_state = State::Immune;
|
|
||||||
}
|
|
||||||
for x in 5..9 {
|
|
||||||
humans[x].present_state = State::Dead;
|
|
||||||
}
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
assert_eq!(stats.normal, 1);
|
|
||||||
assert_eq!(stats.infected, 2);
|
|
||||||
assert_eq!(stats.immune, 3);
|
|
||||||
assert_eq!(stats.dead, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn population_new() {
|
|
||||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
|
||||||
let (width, height) = (5, 7);
|
|
||||||
let population = Population::new(20, 10, 5, width, height, disease);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
assert_eq!(humans.len(), 5 * 7);
|
|
||||||
for h in humans.iter() {
|
|
||||||
let idx = human_idx(h.x, h.y, width);
|
|
||||||
assert_eq!(humans[idx].x, h.x, "coordinates should match");
|
|
||||||
assert_eq!(humans[idx].y, h.y, "coordinates should match");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn population_gen() {
|
|
||||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
|
||||||
let (width, height) = (5, 7);
|
|
||||||
let population = Population::new(20, 10, 5, 5, 7, disease);
|
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
let stats: Stats = humans_stats(&humans);
|
|
||||||
println!("Stats: {:?}", stats);
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
stats.normal + stats.infected + stats.immune + stats.dead,
|
|
||||||
width * height
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn plague_init_stats() {
|
|
||||||
let mut disease: Disease;
|
|
||||||
let mut population: Population;
|
|
||||||
let mut stats: Stats;
|
|
||||||
let (width, height) = (5, 7);
|
|
||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
|
||||||
population = Population::new(0, 0, 0, width, height, disease);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("should be normal: {:?}", stats);
|
|
||||||
assert_eq!(stats.normal, width * height);
|
|
||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
|
||||||
population = Population::new(100, 0, 0, width, height, disease);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("should be infected: {:?}", stats);
|
|
||||||
assert_eq!(stats.infected, width * height);
|
|
||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
|
||||||
population = Population::new(0, 100, 0, width, height, disease);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("should be immune: {:?}", stats);
|
|
||||||
assert_eq!(stats.immune, width * height);
|
|
||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
|
||||||
population = Population::new(0, 0, 100, width, height, disease);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("should be dead: {:?}", stats);
|
|
||||||
assert_eq!(stats.dead, width * height);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[parameterized(rate = {0, 100}, expected = {false, true})]
|
|
||||||
fn roll_test(rate: i32, expected: bool) {
|
|
||||||
let tries = 100000;
|
|
||||||
let mut result = 0;
|
|
||||||
println!("Testing roll, rate {}, expected {}", rate, expected);
|
|
||||||
for _x in 0..tries {
|
|
||||||
if roll(rate) == expected {
|
|
||||||
result += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
assert_eq!(result, tries);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn propagate_simple() {
|
|
||||||
let disease: Disease = Disease::new(0, 0, 100, String::from("Deadly"));
|
|
||||||
let mut population: Population = Population::new(100, 0, 0, 10, 10, disease);
|
|
||||||
let mut stats: Stats;
|
|
||||||
let mut propagate_stats: [i32; 4];
|
|
||||||
|
|
||||||
// infect every one
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("stats after init: {:?}", stats);
|
|
||||||
assert_eq!(stats.infected, 100, "everybody should be infected");
|
|
||||||
|
|
||||||
// kill every one
|
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
|
||||||
assert_eq!(propagate_stats, [0, 100, 0, 0]);
|
|
||||||
assert_eq!(stats.normal, 0);
|
|
||||||
assert_eq!(stats.infected, 0);
|
|
||||||
assert_eq!(stats.immune, 0);
|
|
||||||
assert_eq!(stats.dead, 100);
|
|
||||||
|
|
||||||
for _x in 0..100 {
|
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
|
||||||
assert_eq!(propagate_stats, [0, 0, 0, 100]);
|
|
||||||
assert_eq!(stats.normal, 0);
|
|
||||||
assert_eq!(stats.infected, 0);
|
|
||||||
assert_eq!(stats.immune, 0);
|
|
||||||
assert_eq!(stats.dead, 100);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn propagate_infect_all() {
|
|
||||||
let disease: Disease = Disease::new(100, 0, 0, String::from("Deadly"));
|
|
||||||
let mut population: Population = Population::new(0, 0, 0, 3, 3, disease);
|
|
||||||
let mut stats: Stats;
|
|
||||||
let mut propagate_stats: [i32; 4];
|
|
||||||
|
|
||||||
// start with normal population
|
|
||||||
population.humans = Arc::new(vec![
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 1,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 2,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 0,
|
|
||||||
y: 1,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Infected,
|
|
||||||
x: 1,
|
|
||||||
y: 1,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 2,
|
|
||||||
y: 1,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 0,
|
|
||||||
y: 2,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 1,
|
|
||||||
y: 2,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 2,
|
|
||||||
y: 2,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("stats after init: {:?}", stats);
|
|
||||||
assert_eq!(stats.normal, 8);
|
|
||||||
|
|
||||||
// kill every one
|
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
|
||||||
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
|
||||||
assert_eq!(stats.normal, 0);
|
|
||||||
assert_eq!(stats.infected, 9);
|
|
||||||
assert_eq!(stats.immune, 0);
|
|
||||||
assert_eq!(stats.dead, 0);
|
|
||||||
|
|
||||||
for _x in 0..100 {
|
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
|
||||||
assert_eq!(propagate_stats, [0, 9, 0, 0]);
|
|
||||||
assert_eq!(stats.normal, 0);
|
|
||||||
assert_eq!(stats.infected, 9);
|
|
||||||
assert_eq!(stats.immune, 0);
|
|
||||||
assert_eq!(stats.dead, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn propagate_infect_cure_all() {
|
|
||||||
let disease: Disease = Disease::new(100, 100, 0, String::from("Deadly"));
|
|
||||||
let mut population: Population = Population::new(0, 0, 0, 3, 3, disease);
|
|
||||||
let mut stats: Stats;
|
|
||||||
let mut propagate_stats: [i32; 4];
|
|
||||||
|
|
||||||
// start with normal population
|
|
||||||
population.humans = Arc::new(vec![
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 1,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 2,
|
|
||||||
y: 0,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 0,
|
|
||||||
y: 1,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Infected,
|
|
||||||
x: 1,
|
|
||||||
y: 1,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 2,
|
|
||||||
y: 1,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 0,
|
|
||||||
y: 2,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 1,
|
|
||||||
y: 2,
|
|
||||||
},
|
|
||||||
Human {
|
|
||||||
present_state: State::Normal,
|
|
||||||
x: 2,
|
|
||||||
y: 2,
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("stats after init: {:?}", stats);
|
|
||||||
assert_eq!(stats.normal, 8);
|
|
||||||
|
|
||||||
// infect every one
|
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
|
||||||
println!("population: {:?}", stats);
|
|
||||||
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
|
||||||
assert_eq!(stats.normal, 0);
|
|
||||||
assert_eq!(stats.infected, 8);
|
|
||||||
assert_eq!(stats.immune, 1);
|
|
||||||
assert_eq!(stats.dead, 0);
|
|
||||||
|
|
||||||
// cure every one
|
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
|
||||||
println!("population: {:?}", stats);
|
|
||||||
assert_eq!(propagate_stats, [0, 8, 1, 0]);
|
|
||||||
assert_eq!(stats.normal, 0);
|
|
||||||
assert_eq!(stats.infected, 0);
|
|
||||||
assert_eq!(stats.immune, 9);
|
|
||||||
assert_eq!(stats.dead, 0);
|
|
||||||
|
|
||||||
// then
|
|
||||||
for _x in 0..100 {
|
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
|
||||||
println!("population: {:?}", stats);
|
|
||||||
assert_eq!(propagate_stats, [0, 0, 9, 0]);
|
|
||||||
assert_eq!(stats.normal, 0);
|
|
||||||
assert_eq!(stats.infected, 0);
|
|
||||||
assert_eq!(stats.immune, 9);
|
|
||||||
assert_eq!(stats.dead, 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[parameterized(infection_start = {0, 50, 100})]
|
|
||||||
fn propagate_harmless(infection_start: i32) {
|
|
||||||
let disease: Disease = Disease::new(0, 0, 0, String::from("Harmless"));
|
|
||||||
let (width, height) = (100, 100);
|
|
||||||
let mut population: Population = Population::new(infection_start, 0, 0, width, height, disease);
|
|
||||||
let stats_before: Stats;
|
|
||||||
let stats_after: Stats;
|
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats_before = humans_stats(&humans);
|
|
||||||
let should_be_infected = population.size as i32 * infection_start / 100;
|
|
||||||
let tolerance = (should_be_infected as f32 * 0.20) as i32;
|
|
||||||
println!("{:?}", stats_before);
|
|
||||||
assert!(stats_before.infected <= should_be_infected + tolerance, "{} infected, should be less than {}", stats_before.infected, should_be_infected + tolerance);
|
|
||||||
assert!(stats_before.infected >= should_be_infected - tolerance, "{} infected, should be more than {}", stats_before.infected, should_be_infected - tolerance);
|
|
||||||
|
|
||||||
population.propagate_new(THREADS);
|
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats_after = humans_stats(&humans);
|
|
||||||
assert_eq!(stats_before.infected, stats_after.infected, "no one should have been infected");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 0})]
|
|
||||||
fn propagate_test(infection_rate: i32, death_rate: i32, infected_expected: i32) {
|
|
||||||
let disease: Disease;
|
|
||||||
let mut population: Population;
|
|
||||||
let mut stats: Stats;
|
|
||||||
let (width, height) = (100, 100);
|
|
||||||
let start_infected = 50;
|
|
||||||
|
|
||||||
println!(
|
|
||||||
"infection rate: {}, death_rate: {}",
|
|
||||||
infection_rate, death_rate
|
|
||||||
);
|
|
||||||
|
|
||||||
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
|
|
||||||
population = Population::new(start_infected, 0, 0, width, height, disease);
|
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("Population after init: {:?}", stats);
|
|
||||||
|
|
||||||
// total * proba - 20% < infected < total * proba + 20%
|
|
||||||
let infected_at_start_proba = width * height * start_infected / 100;
|
|
||||||
let infected_tolerance = ((width * height) as f32 * 0.2) as i32;
|
|
||||||
assert!(stats.infected <= infected_at_start_proba + infected_tolerance);
|
|
||||||
assert!(stats.infected >= infected_at_start_proba - infected_tolerance);
|
|
||||||
assert_eq!(stats.dead, 0);
|
|
||||||
|
|
||||||
let infected_at_start = stats.infected;
|
|
||||||
let dead_at_start = stats.dead;
|
|
||||||
|
|
||||||
let propa_stats: [i32; 4] = population.propagate_new(THREADS);
|
|
||||||
|
|
||||||
assert!(propa_stats[3] >= dead_at_start);
|
|
||||||
|
|
||||||
if death_rate == 0 {
|
|
||||||
assert_eq!(propa_stats[3], 0, "no human should have died");
|
|
||||||
}
|
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("Population after propagate: {:?}", stats);
|
|
||||||
|
|
||||||
assert!(stats.infected <= infected_at_start + width * height * infected_expected);
|
|
||||||
|
|
||||||
let should_be_dead = infected_at_start * death_rate / 100;
|
|
||||||
let dead_tolerance = (should_be_dead as f32 * 0.20) as i32;
|
|
||||||
|
|
||||||
assert!(
|
|
||||||
stats.dead <= should_be_dead + dead_tolerance,
|
|
||||||
"death count should be less or equal than {}",
|
|
||||||
should_be_dead + dead_tolerance
|
|
||||||
);
|
|
||||||
assert!(stats.dead >= should_be_dead - dead_tolerance);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn fn_is_inside_and_infected(point: Point, width: i32, height: i32, humans: &Vec<Human>, infection_rate: i32) -> bool {
|
||||||
|
if fn_is_inside(&point, width, height) {
|
||||||
|
let idx = human_idx(point.x, point.y, width);
|
||||||
|
if humans[idx].present_state == State::Infected {
|
||||||
|
roll(infection_rate)
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
+448
@@ -0,0 +1,448 @@
|
|||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
use parameterized::parameterized;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct Stats {
|
||||||
|
normal: i32,
|
||||||
|
infected: i32,
|
||||||
|
immune: i32,
|
||||||
|
dead: i32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Stats {
|
||||||
|
fn new() -> Stats {
|
||||||
|
Stats {
|
||||||
|
normal: 0,
|
||||||
|
infected: 0,
|
||||||
|
immune: 0,
|
||||||
|
dead: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn humans_stats(humans: &Vec<Human>) -> Stats {
|
||||||
|
let mut stats: Stats = Stats::new();
|
||||||
|
for human in humans.iter() {
|
||||||
|
match human.present_state {
|
||||||
|
State::Normal => {
|
||||||
|
stats.normal += 1;
|
||||||
|
}
|
||||||
|
State::Infected => {
|
||||||
|
stats.infected += 1;
|
||||||
|
}
|
||||||
|
State::Immune => {
|
||||||
|
stats.immune += 1;
|
||||||
|
}
|
||||||
|
State::Dead => {
|
||||||
|
stats.dead += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
stats
|
||||||
|
}
|
||||||
|
|
||||||
|
#[parameterized(x = {
|
||||||
|
2, 3, 5
|
||||||
|
}, y = {
|
||||||
|
1, 4, 0
|
||||||
|
}, width = {
|
||||||
|
3, 5, 7
|
||||||
|
}, res = {
|
||||||
|
5, 23, 5
|
||||||
|
})]
|
||||||
|
fn test_human_idx(x: i32, y: i32, width: i32, res: usize) {
|
||||||
|
assert_eq!(human_idx(x, y, width), res);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_human_stats() {
|
||||||
|
let mut humans: Vec<Human> = Vec::with_capacity(10);
|
||||||
|
let mut stats: Stats;
|
||||||
|
|
||||||
|
for _ in 0..10 {
|
||||||
|
humans.push(Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
stats = humans_stats(&humans);
|
||||||
|
assert_eq!(stats.normal, 10);
|
||||||
|
|
||||||
|
for x in 0..2 {
|
||||||
|
humans[x].present_state = State::Infected;
|
||||||
|
}
|
||||||
|
for x in 2..5 {
|
||||||
|
humans[x].present_state = State::Immune;
|
||||||
|
}
|
||||||
|
for x in 5..9 {
|
||||||
|
humans[x].present_state = State::Dead;
|
||||||
|
}
|
||||||
|
stats = humans_stats(&humans);
|
||||||
|
assert_eq!(stats.normal, 1);
|
||||||
|
assert_eq!(stats.infected, 2);
|
||||||
|
assert_eq!(stats.immune, 3);
|
||||||
|
assert_eq!(stats.dead, 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn population_new() {
|
||||||
|
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||||
|
let (width, height) = (5, 7);
|
||||||
|
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||||
|
let humans = Arc::clone(&population.humans);
|
||||||
|
assert_eq!(humans.lock().unwrap().len(), 5 * 7);
|
||||||
|
for h in humans.lock().unwrap().iter() {
|
||||||
|
let idx = human_idx(h.x, h.y, width);
|
||||||
|
assert_eq!(humans.lock().unwrap()[idx].x, h.x, "coordinates should match");
|
||||||
|
assert_eq!(humans.lock().unwrap()[idx].y, h.y, "coordinates should match");
|
||||||
|
}
|
||||||
|
assert_eq!(humans.lock().unwrap().len(), (width * height) as usize);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn population_gen() {
|
||||||
|
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||||
|
let (width, height) = (5, 7);
|
||||||
|
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||||
|
|
||||||
|
let stats: Stats = humans_stats(&population.humans);
|
||||||
|
println!("Stats: {:?}", stats);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
stats.normal + stats.infected + stats.immune + stats.dead,
|
||||||
|
width * height
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn plague_init_stats() {
|
||||||
|
let mut disease: Disease;
|
||||||
|
let mut population: Population;
|
||||||
|
let mut stats: Stats;
|
||||||
|
let (width, height) = (5, 7);
|
||||||
|
|
||||||
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
|
population = Population::new(0, 0, 0, width, height, disease);
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("should be normal: {:?}", stats);
|
||||||
|
assert_eq!(stats.normal, width * height);
|
||||||
|
|
||||||
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
|
population = Population::new(100, 0, 0, width, height, disease);
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("should be infected: {:?}", stats);
|
||||||
|
assert_eq!(stats.infected, width * height);
|
||||||
|
|
||||||
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
|
population = Population::new(0, 100, 0, width, height, disease);
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("should be immune: {:?}", stats);
|
||||||
|
assert_eq!(stats.immune, width * height);
|
||||||
|
|
||||||
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
|
population = Population::new(0, 0, 100, width, height, disease);
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("should be dead: {:?}", stats);
|
||||||
|
assert_eq!(stats.dead, width * height);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[parameterized(rate = {0, 100}, expected = {false, true})]
|
||||||
|
fn roll_test(rate: i32, expected: bool) {
|
||||||
|
let tries = 100000;
|
||||||
|
let mut result = 0;
|
||||||
|
println!("Testing roll, rate {}, expected {}", rate, expected);
|
||||||
|
for _x in 0..tries {
|
||||||
|
if roll(rate) == expected {
|
||||||
|
result += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assert_eq!(result, tries);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn propagate_simple() {
|
||||||
|
let disease: Disease = Disease::new(0, 0, 100, String::from("Deadly"));
|
||||||
|
let mut population: Population = Population::new(100, 0, 0, 10, 10, disease);
|
||||||
|
let mut stats: Stats;
|
||||||
|
let mut propagate_stats: [i32; 4];
|
||||||
|
|
||||||
|
// infect every one
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("stats after init: {:?}", stats);
|
||||||
|
assert_eq!(stats.infected, 100, "everybody should be infected");
|
||||||
|
|
||||||
|
// kill every one
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
assert_eq!(propagate_stats, [0, 100, 0, 0]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 0);
|
||||||
|
assert_eq!(stats.immune, 0);
|
||||||
|
assert_eq!(stats.dead, 100);
|
||||||
|
|
||||||
|
for _x in 0..100 {
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
assert_eq!(propagate_stats, [0, 0, 0, 100]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 0);
|
||||||
|
assert_eq!(stats.immune, 0);
|
||||||
|
assert_eq!(stats.dead, 100);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn propagate_infect_all() {
|
||||||
|
let disease: Disease = Disease::new(100, 0, 0, String::from("Deadly"));
|
||||||
|
let mut population: Population = Population::new(0, 0, 0, 3, 3, disease);
|
||||||
|
let mut stats: Stats;
|
||||||
|
let mut propagate_stats: [i32; 4];
|
||||||
|
|
||||||
|
// start with normal population
|
||||||
|
population.humans = vec![
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 1,
|
||||||
|
y: 0,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 2,
|
||||||
|
y: 0,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 1,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Infected,
|
||||||
|
x: 1,
|
||||||
|
y: 1,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 2,
|
||||||
|
y: 1,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 2,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 1,
|
||||||
|
y: 2,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 2,
|
||||||
|
y: 2,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("stats after init: {:?}", stats);
|
||||||
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
|
// kill every one
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 9);
|
||||||
|
assert_eq!(stats.immune, 0);
|
||||||
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
|
for _x in 0..100 {
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
assert_eq!(propagate_stats, [0, 9, 0, 0]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 9);
|
||||||
|
assert_eq!(stats.immune, 0);
|
||||||
|
assert_eq!(stats.dead, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn propagate_infect_cure_all() {
|
||||||
|
let disease: Disease = Disease::new(100, 100, 0, String::from("Deadly"));
|
||||||
|
let mut population: Population = Population::new(0, 0, 0, 3, 3, disease);
|
||||||
|
let mut stats: Stats;
|
||||||
|
let mut propagate_stats: [i32; 4];
|
||||||
|
|
||||||
|
// start with normal population
|
||||||
|
population.humans = vec![
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 1,
|
||||||
|
y: 0,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 2,
|
||||||
|
y: 0,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 1,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Infected,
|
||||||
|
x: 1,
|
||||||
|
y: 1,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 2,
|
||||||
|
y: 1,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 2,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 1,
|
||||||
|
y: 2,
|
||||||
|
},
|
||||||
|
Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 2,
|
||||||
|
y: 2,
|
||||||
|
},
|
||||||
|
];
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("stats after init: {:?}", stats);
|
||||||
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
|
// infect every one
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
println!("population: {:?}", stats);
|
||||||
|
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 8);
|
||||||
|
assert_eq!(stats.immune, 1);
|
||||||
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
|
// cure every one
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
println!("population: {:?}", stats);
|
||||||
|
assert_eq!(propagate_stats, [0, 8, 1, 0]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 0);
|
||||||
|
assert_eq!(stats.immune, 9);
|
||||||
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
|
// then
|
||||||
|
for _x in 0..100 {
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
println!("population: {:?}", stats);
|
||||||
|
assert_eq!(propagate_stats, [0, 0, 9, 0]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 0);
|
||||||
|
assert_eq!(stats.immune, 9);
|
||||||
|
assert_eq!(stats.dead, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[parameterized(infection_start = {0, 50, 100})]
|
||||||
|
fn propagate_harmless(infection_start: i32) {
|
||||||
|
let disease: Disease = Disease::new(0, 0, 0, String::from("Harmless"));
|
||||||
|
let (width, height) = (100, 100);
|
||||||
|
let mut population: Population = Population::new(infection_start, 0, 0, width, height, disease);
|
||||||
|
let stats_before: Stats;
|
||||||
|
let stats_after: Stats;
|
||||||
|
|
||||||
|
stats_before = humans_stats(&population.humans);
|
||||||
|
let should_be_infected = population.size as i32 * infection_start / 100;
|
||||||
|
let tolerance = (should_be_infected as f32 * 0.20) as i32;
|
||||||
|
println!("{:?}", stats_before);
|
||||||
|
assert!(stats_before.infected <= should_be_infected + tolerance, "{} infected, should be less than {}", stats_before.infected, should_be_infected + tolerance);
|
||||||
|
assert!(stats_before.infected >= should_be_infected - tolerance, "{} infected, should be more than {}", stats_before.infected, should_be_infected - tolerance);
|
||||||
|
|
||||||
|
population.propagate();
|
||||||
|
|
||||||
|
stats_after = humans_stats(&population.humans);
|
||||||
|
assert_eq!(stats_before.infected, stats_after.infected, "no one should have been infected");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 0})]
|
||||||
|
fn propagate_test(infection_rate: i32, death_rate: i32, infected_expected: i32) {
|
||||||
|
let disease: Disease;
|
||||||
|
let mut population: Population;
|
||||||
|
let mut stats: Stats;
|
||||||
|
let (width, height) = (100, 100);
|
||||||
|
let start_infected = 50;
|
||||||
|
|
||||||
|
println!(
|
||||||
|
"infection rate: {}, death_rate: {}",
|
||||||
|
infection_rate, death_rate
|
||||||
|
);
|
||||||
|
|
||||||
|
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
|
||||||
|
population = Population::new(start_infected, 0, 0, width, height, disease);
|
||||||
|
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("Population after init: {:?}", stats);
|
||||||
|
|
||||||
|
// total * proba - 20% < infected < total * proba + 20%
|
||||||
|
let infected_at_start_proba = width * height * start_infected / 100;
|
||||||
|
let infected_tolerance = ((width * height) as f32 * 0.2) as i32;
|
||||||
|
assert!(stats.infected <= infected_at_start_proba + infected_tolerance);
|
||||||
|
assert!(stats.infected >= infected_at_start_proba - infected_tolerance);
|
||||||
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
|
let infected_at_start = stats.infected;
|
||||||
|
let dead_at_start = stats.dead;
|
||||||
|
|
||||||
|
let propa_stats: [i32; 4] = population.propagate();
|
||||||
|
|
||||||
|
assert!(propa_stats[3] >= dead_at_start);
|
||||||
|
|
||||||
|
if death_rate == 0 {
|
||||||
|
assert_eq!(propa_stats[3], 0, "no human should have died");
|
||||||
|
}
|
||||||
|
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("Population after propagate: {:?}", stats);
|
||||||
|
|
||||||
|
assert!(stats.infected <= infected_at_start + width * height * infected_expected);
|
||||||
|
|
||||||
|
let should_be_dead = infected_at_start * death_rate / 100;
|
||||||
|
let dead_tolerance = (should_be_dead as f32 * 0.20) as i32;
|
||||||
|
|
||||||
|
assert!(
|
||||||
|
stats.dead <= should_be_dead + dead_tolerance,
|
||||||
|
"death count should be less or equal than {}",
|
||||||
|
should_be_dead + dead_tolerance
|
||||||
|
);
|
||||||
|
assert!(stats.dead >= should_be_dead - dead_tolerance);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user