608 lines
20 KiB
Rust
608 lines
20 KiB
Rust
use crate::prelude::*;
|
|
|
|
pub struct Population {
|
|
pub start_infected_ratio: i32,
|
|
pub start_immune_ratio: i32,
|
|
pub start_dead_ratio: i32,
|
|
pub humans: Vec<Human>,
|
|
pub width: i32,
|
|
pub height: i32,
|
|
pub age: i32,
|
|
pub plague: Disease,
|
|
pub size: usize,
|
|
}
|
|
|
|
pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
|
|
((y * width) + x) as usize
|
|
}
|
|
|
|
impl Population {
|
|
pub fn new(
|
|
start_infected_ratio: i32,
|
|
start_immune_ratio: i32,
|
|
start_dead_ratio: i32,
|
|
width: i32,
|
|
height: i32,
|
|
plague: Disease,
|
|
) -> Self {
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let size: usize = (width * height) as usize;
|
|
|
|
let mut the_humans: Vec<Human> = vec![
|
|
Human {
|
|
x: 0,
|
|
y: 0,
|
|
present_state: State::Normal
|
|
};
|
|
size
|
|
];
|
|
for x in 0..width {
|
|
for y in 0..height {
|
|
let idx = human_idx(x, y, width);
|
|
let mut present_state = State::Normal;
|
|
if (start_infected_ratio > 0)
|
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_infected_ratio)
|
|
{
|
|
present_state = State::Infected;
|
|
} else if (start_immune_ratio > 0)
|
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio)
|
|
{
|
|
present_state = State::Immune;
|
|
} else if (start_dead_ratio > 0)
|
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio)
|
|
{
|
|
present_state = State::Dead;
|
|
}
|
|
the_humans[idx] = Human{x: x, y: y, present_state: present_state};
|
|
}
|
|
}
|
|
Self {
|
|
start_infected_ratio: start_infected_ratio,
|
|
start_immune_ratio: start_immune_ratio,
|
|
start_dead_ratio: start_dead_ratio,
|
|
width: width,
|
|
height: height,
|
|
plague: plague,
|
|
age: 0,
|
|
humans: the_humans,
|
|
size: size,
|
|
}
|
|
}
|
|
|
|
pub fn propagate_new(&mut self) -> [i32; 4] {
|
|
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
|
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(self.humans.len());
|
|
|
|
for human in self.humans.iter() {
|
|
let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
|
if human.present_state == State::Normal {
|
|
let possible = [
|
|
(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, self.width, self.height);
|
|
match neigh_idx {
|
|
Some(x) => neighbors.push(&self.humans[x]),
|
|
None => {},
|
|
}
|
|
}
|
|
}
|
|
let new_human = evolve(human, neighbors, self.plague.infection_rate, self.plague.curing_rate, self.plague.death_rate);
|
|
match human.present_state {
|
|
State::Normal => { stats[0] += 1; }
|
|
State::Infected => { stats[1] += 1; }
|
|
State::Immune => { stats[2] += 1; }
|
|
State::Dead => { stats[3] += 1; }
|
|
}
|
|
humans_n_plus_1.push(new_human);
|
|
}
|
|
|
|
self.humans = humans_n_plus_1;
|
|
stats
|
|
}
|
|
}
|
|
|
|
fn evolve(human: &Human, neighbors: Vec<&Human>, infection_rate: i32, curing_rate: i32, death_rate: i32) -> Human {
|
|
let mut new_human = human.clone();
|
|
match human.present_state {
|
|
State::Normal => {
|
|
new_human.present_state = infect_by_neighbors(neighbors, infection_rate);
|
|
}
|
|
State::Infected => {
|
|
new_human.present_state = die_or_cure(curing_rate, death_rate);
|
|
}
|
|
State::Immune => {}
|
|
State::Dead => {}
|
|
}
|
|
new_human
|
|
}
|
|
|
|
fn infect_by_neighbors(neighbors: Vec<&Human>, infection_rate: i32) -> State {
|
|
for neighbor in neighbors.iter() {
|
|
if neighbor.present_state == State::Infected {
|
|
if roll(infection_rate) {
|
|
return State::Infected;
|
|
}
|
|
}
|
|
}
|
|
State::Normal
|
|
}
|
|
|
|
fn die_or_cure(curing_rate: i32, death_rate: i32) -> State {
|
|
if roll(curing_rate) {
|
|
State::Immune
|
|
} else if roll(death_rate) {
|
|
State::Dead
|
|
} else {
|
|
State::Infected
|
|
}
|
|
}
|
|
|
|
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
|
|
}
|
|
}
|
|
|
|
pub fn roll(probability: i32) -> bool {
|
|
if probability > 0 {
|
|
let mut rng = rand::thread_rng();
|
|
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
|
|
} else {
|
|
false
|
|
}
|
|
}
|
|
|
|
#[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);
|
|
assert_eq!(population.humans.len(), 5 * 7);
|
|
for h in population.humans.iter() {
|
|
let idx = human_idx(h.x, h.y, width);
|
|
assert_eq!(population.humans[idx].x, h.x, "coordinates should match");
|
|
assert_eq!(population.humans[idx].y, h.y, "coordinates should match");
|
|
}
|
|
assert_eq!(population.humans.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_new();
|
|
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_new();
|
|
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_new();
|
|
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_new();
|
|
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_new();
|
|
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_new();
|
|
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_new();
|
|
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_new();
|
|
|
|
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_new();
|
|
|
|
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);
|
|
}
|
|
}
|