3 Commits

Author SHA1 Message Date
herel 6aaede2208 fix population humans initialization 2022-05-03 20:46:55 +02:00
herel 1e26a78e3f main: display normals too 2022-05-03 20:45:40 +02:00
herel 87f0219b3b human: implement clone 2022-05-03 20:45:32 +02:00
3 changed files with 260 additions and 157 deletions
+2 -2
View File
@@ -1,14 +1,14 @@
// use crate::prelude::*; // use crate::prelude::*;
// #[derive(Copy, Clone, PartialEq)] // #[derive(Copy, Clone, PartialEq)]
#[derive(PartialEq)] #[derive(PartialEq, Debug, Clone)]
pub enum State { pub enum State {
Normal, Normal,
Infected, Infected,
Dead, Dead,
Immune, Immune,
} }
// #[derive(Clone)] #[derive(Clone)]
pub struct Human { pub struct Human {
pub present_state: State, pub present_state: State,
pub x: i32, pub x: i32,
+2 -2
View File
@@ -37,8 +37,8 @@ fn main() {
stats = population.propagate(); stats = population.propagate();
//population.display(); //population.display();
println!( println!(
"Infecteds: {} Immunes: {} Deads: {}", "Normal: {} Infecteds: {} Immunes: {} Deads: {}",
stats[1], stats[2], stats[3] stats[0], stats[1], stats[2], stats[3]
); );
if stats[1] == 0 { if stats[1] == 0 {
break; break;
+256 -153
View File
@@ -1,6 +1,6 @@
use crate::prelude::*; use crate::prelude::*;
// #[derive(Debug)] #[derive(Debug)]
pub struct Point { pub struct Point {
x: i32, x: i32,
y: i32, y: i32,
@@ -30,12 +30,20 @@ impl Population {
height: i32, height: i32,
plague: Disease, plague: Disease,
) -> Self { ) -> Self {
let mut the_humans: Vec<Human> = Vec::with_capacity((width * height) as usize); let mut the_humans: Vec<Human> = vec![Human{x: 0, y: 0, present_state: State::Normal}; (width * height) as usize];
for x in 0..width { for x in 0..width {
for y in 0..height { for y in 0..height {
the_humans.push(Human::new(x, y)); let idx = human_idx(x, y, width);
the_humans[idx].x = x;
the_humans[idx].y = y;
} }
} }
for h in the_humans.iter() {
let idx = human_idx(h.x, h.y, width);
assert_eq!(the_humans[idx].x, h.x);
assert_eq!(the_humans[idx].y, h.y);
}
assert_eq!(the_humans.len(), (width * height) as usize);
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,
@@ -75,14 +83,28 @@ impl Population {
} }
} }
fn is_inside(&self, pos: &Point) -> bool {
if pos.x >= 0 && pos.x < self.width && pos.y >= 0 && pos.y < self.height {
true
} else {
false
}
}
fn push_if_inside(&self, point_list: &mut Vec<Point>, point: Point) {
if self.is_inside(&point) {
point_list.push(point);
}
}
pub fn propagate(&mut self) -> [i32; 4] { pub fn propagate(&mut self) -> [i32; 4] {
let mut people_to_check: Vec<Point> = let mut people_to_check: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize); Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_infect: Vec<Point> = let mut people_to_infect: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize); Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_cure: Vec<Point> = let mut people_to_cure: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize); Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_kill: Vec<Point> = let mut people_to_kill: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize); Vec::with_capacity((self.width * self.height) as usize);
let mut stats: [i32; 4] = [0, 0, 0, 0]; let mut stats: [i32; 4] = [0, 0, 0, 0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead // stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
@@ -93,7 +115,7 @@ impl Population {
stats[0] += 1; stats[0] += 1;
} }
State::Infected => { State::Infected => {
people_to_check.push(Point { x: h.x, y: h.y }); people_to_check.push(Point{ x: h.x, y: h.y });
stats[1] += 1; stats[1] += 1;
} }
State::Immune => { State::Immune => {
@@ -104,138 +126,98 @@ impl Population {
} }
} }
} }
println!("{} people to check, death rate {}", people_to_check.len(), self.plague.death_rate);
// for pos in &people_to_check { // for pos in &people_to_check {
for pos in people_to_check.iter() { for pos in people_to_check.iter() {
//people_to_check.iter().map(|pos|{ //people_to_check.iter().map(|pos|{
//get all the other people next to me and check if i die cure or infect //get all the other people next to me and check if i die cure or infect
//now we can start to check if people would be infected or not //now we can start to check if people would be infected or not
//let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32); //let idx = human_idx(pos.x as i32, pos.y as i32, self.width as i32);
if pos.x >= 0 && pos.x <= self.width - 1 && pos.y >= 0 && pos.y <= self.height - 1 { if roll(self.plague.curing_rate) {
if roll(self.plague.curing_rate) { //checks if the man recovers
//checks if the man recovers people_to_cure.push(Point { x: pos.x, y: pos.y });
people_to_cure.push(Point { x: pos.x, y: pos.y }); } else {
if roll(self.plague.death_rate) {
//cheks if the man dies
people_to_kill.push(Point { x: pos.x, y: pos.y });
} else { } else {
let mut possible_infections: Vec<Point> = Vec::with_capacity(8);
// Vec::new();
//possible_infections.push(Point{x:pos.x,y:pos.y});
if roll(self.plague.death_rate) { self.push_if_inside(
//cheks if the man dies &mut possible_infections,
people_to_kill.push(Point { x: pos.x, y: pos.y }); Point {
} else { x: pos.x - 1,
let mut possible_infections: Vec<Point> = Vec::with_capacity(8); y: pos.y - 1,
// Vec::new(); },
//possible_infections.push(Point{x:pos.x,y:pos.y}); ); //Top Left
self.push_if_inside(
if pos.x > 0 && pos.y > 0 { &mut possible_infections,
possible_infections.push(Point { Point {
x: pos.x - 1, x: pos.x,
y: pos.y - 1, y: pos.y - 1,
}); //Top Left },
} ); //Top
if pos.y > 0 { self.push_if_inside(
possible_infections.push(Point { &mut possible_infections,
x: pos.x, Point {
y: pos.y - 1, x: pos.x + 1,
}); //Top y: pos.y - 1,
} },
if pos.y > 0 && pos.x < self.width - 1 { ); //Top Right
possible_infections.push(Point { self.push_if_inside(
x: pos.x + 1, &mut possible_infections,
y: pos.y - 1, Point {
}); //Top Right x: pos.x - 1,
} y: pos.y,
if pos.x > 0 { },
possible_infections.push(Point { ); //Left
x: pos.x - 1, self.push_if_inside(
y: pos.y, &mut possible_infections,
}); //Left Point {
} x: pos.x + 1,
if pos.x < self.width - 1{ y: pos.y,
possible_infections.push(Point { },
x: pos.x + 1, ); //Right
y: pos.y, self.push_if_inside(
}); //Right &mut possible_infections,
} Point {
if pos.x > 0 && pos.y < self.height -1 { x: pos.x - 1,
possible_infections.push(Point { y: pos.y + 1,
x: pos.x - 1, },
y: pos.y + 1, ); //Bottom Left
}); //Bottom Left self.push_if_inside(
} &mut possible_infections,
if pos.y < self.height - 1 { Point {
possible_infections.push(Point { x: pos.x,
x: pos.x, y: pos.y + 1,
y: pos.y + 1, },
}); //Bottom ); //Bottom
} self.push_if_inside(
if pos.x < self.width - 1 && pos.y < self.height - 1{ &mut possible_infections,
possible_infections.push(Point { Point {
x: pos.x + 1, x: pos.x + 1,
y: pos.y + 1, y: pos.y + 1,
}); //Bottom Right },
} ); //Bottom Right
for poss_infected_pos in possible_infections.iter() { for poss_infected_pos in possible_infections.iter() {
//possible_infections.iter().map(|poss_infected_pos|{ //possible_infections.iter().map(|poss_infected_pos|{
let inf_idx = let inf_idx =
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width); human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
if self.humans[inf_idx].present_state == State::Normal { if self.humans[inf_idx].present_state == State::Normal {
if roll(self.plague.infection_rate) { if roll(self.plague.infection_rate) {
people_to_infect.push(Point { people_to_infect.push(Point {
x: poss_infected_pos.x, x: poss_infected_pos.x,
y: poss_infected_pos.y, y: poss_infected_pos.y,
}); });
}
} }
} }
} }
} }
} else {
//TODO
//Check every special cases (corners sides etc..)
//REMOVE WHEN IMPLEMENTED
//It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate
//people_to_kill.push(Point { x: pos.x, y: pos.y });
// @##
// ###
// ###
// #@#
// ###
// ###
// ##@
// ###
// ###
// ###
// @##
// ###
// ###
// ##@
// ###
// ###
// ###
// @##
// ###
// ###
// #@#
// ###
// ###
// ##@
} }
} }
println!(
"{} to infect, {} to cure, {} to kill", for infected_position in people_to_infect.iter() {
people_to_infect.len(),
people_to_cure.len(),
people_to_kill.len()
);
for infected_position in &people_to_infect {
// println!("To infect: {:?}", infected_position); // println!("To infect: {:?}", infected_position);
//people_to_infect.iter().map(|infected_position|{ //people_to_infect.iter().map(|infected_position|{
let infected_index = human_idx(infected_position.x, infected_position.y, self.width); let infected_index = human_idx(infected_position.x, infected_position.y, self.width);
@@ -247,15 +229,18 @@ impl Population {
//println!("Infected someone"); //println!("Infected someone");
} }
for cured_position in &people_to_cure { for cured_position in people_to_cure.iter() {
//people_to_cure.iter().map(|cured_position|{ //people_to_cure.iter().map(|cured_position|{
let cured_index = human_idx(cured_position.x, cured_position.y, self.width); let cured_index = human_idx(cured_position.x, cured_position.y, self.width);
if self.humans[cured_index].present_state != State::Infected {
println!("not infected");
}
self.humans[cured_index].present_state = State::Immune; self.humans[cured_index].present_state = State::Immune;
//DEBUG //DEBUG
//println!("Cured someone"); //println!("Cured someone");
} }
for dead_position in &people_to_kill { for dead_position in people_to_kill.iter() {
//people_to_kill.iter().map(|dead_position|{ //people_to_kill.iter().map(|dead_position|{
let dead_index = human_idx(dead_position.x, dead_position.y, self.width); let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
if self.humans[dead_index].present_state == State::Dead { if self.humans[dead_index].present_state == State::Dead {
@@ -265,7 +250,10 @@ impl Population {
} }
//DEBUG //DEBUG
} }
assert_eq!(stats[0] + stats[1] + stats[2] + stats[3], self.humans.len() as i32); assert_eq!(
stats[0] + stats[1] + stats[2] + stats[3],
self.humans.len() as i32
);
stats stats
} }
@@ -291,7 +279,7 @@ impl Population {
pub fn roll(probability: i32) -> bool { pub 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 as i32..CORRECTED_PERCENTAGE) <= probability rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
} else { } else {
false false
} }
@@ -465,14 +453,131 @@ mod tests {
let disease: Disease = Disease::new(0, 0, 100, String::from("Deadly")); 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 population: Population = Population::new(100, 0, 0, 10, 10, disease);
let mut stats: Stats; let mut stats: Stats;
let mut propagate_stats: [i32; 4];
// start with normal population
stats = humans_stats(&population.humans); stats = humans_stats(&population.humans);
println!("stats after init: {:?}", stats); println!("stats after init: {:?}", stats);
assert_eq!(stats.normal, 100); assert_eq!(stats.normal, 100);
// infect every one
population.generate(); population.generate();
stats = humans_stats(&population.humans); stats = humans_stats(&population.humans);
println!("stats after generate: {:?}", stats); println!("stats after generate: {:?}", stats);
assert_eq!(stats.infected, 100); assert_eq!(stats.infected, 100);
// 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);
// kill 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, 8);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 1);
assert_eq!(stats.dead, 0);
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, [8, 0, 1, 0]);
assert_eq!(stats.normal, 8);
assert_eq!(stats.infected, 0);
assert_eq!(stats.immune, 1);
assert_eq!(stats.dead, 0);
}
} }
#[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 0})] #[parameterized(infection_rate = {0, 100, 0}, death_rate = {0, 0, 100}, infected_expected = {0, 1, 0})]
@@ -502,32 +607,30 @@ mod tests {
assert!(stats.infected >= infected_at_start_proba - infected_tolerance); assert!(stats.infected >= infected_at_start_proba - infected_tolerance);
assert_eq!(stats.dead, 0); assert_eq!(stats.dead, 0);
for _x in 0..100 { let infected_at_start = stats.infected;
let infected_at_start = stats.infected; let dead_at_start = stats.dead;
let dead_at_start = stats.dead;
let propa_stats: [i32; 4] = population.propagate(); let propa_stats: [i32; 4] = population.propagate();
assert!(propa_stats[3] >= dead_at_start); assert!(propa_stats[3] >= dead_at_start);
if death_rate == 0 { if death_rate == 0 {
assert_eq!(propa_stats[3], 0, "no human should have died"); assert_eq!(propa_stats[3], 0, "no human should have died");
}
stats = humans_stats(&population.humans);
println!("Population after propagate: {:?}", stats);
assert!(stats.normal <= 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);
} }
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);
} }
} }