fix population humans initialization

This commit is contained in:
2022-05-03 20:46:55 +02:00
parent 1e26a78e3f
commit 6aaede2208
+256 -153
View File
@@ -1,6 +1,6 @@
use crate::prelude::*;
// #[derive(Debug)]
#[derive(Debug)]
pub struct Point {
x: i32,
y: i32,
@@ -30,12 +30,20 @@ impl Population {
height: i32,
plague: Disease,
) -> 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 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 {
start_infected_ratio: start_infected_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] {
let mut people_to_check: Vec<Point> =
let mut people_to_check: Vec<Point> =
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);
let mut people_to_cure: Vec<Point> =
let mut people_to_cure: Vec<Point> =
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);
let mut stats: [i32; 4] = [0, 0, 0, 0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
@@ -93,7 +115,7 @@ impl Population {
stats[0] += 1;
}
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;
}
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.iter() {
//people_to_check.iter().map(|pos|{
//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
//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) {
//checks if the man recovers
people_to_cure.push(Point { x: pos.x, y: pos.y });
if roll(self.plague.curing_rate) {
//checks if the man recovers
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 {
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) {
//cheks if the man dies
people_to_kill.push(Point { x: pos.x, y: pos.y });
} else {
let mut possible_infections: Vec<Point> = Vec::with_capacity(8);
// Vec::new();
//possible_infections.push(Point{x:pos.x,y:pos.y});
if pos.x > 0 && pos.y > 0 {
possible_infections.push(Point {
x: pos.x - 1,
y: pos.y - 1,
}); //Top Left
}
if pos.y > 0 {
possible_infections.push(Point {
x: pos.x,
y: pos.y - 1,
}); //Top
}
if pos.y > 0 && pos.x < self.width - 1 {
possible_infections.push(Point {
x: pos.x + 1,
y: pos.y - 1,
}); //Top Right
}
if pos.x > 0 {
possible_infections.push(Point {
x: pos.x - 1,
y: pos.y,
}); //Left
}
if pos.x < self.width - 1{
possible_infections.push(Point {
x: pos.x + 1,
y: pos.y,
}); //Right
}
if pos.x > 0 && pos.y < self.height -1 {
possible_infections.push(Point {
x: pos.x - 1,
y: pos.y + 1,
}); //Bottom Left
}
if pos.y < self.height - 1 {
possible_infections.push(Point {
x: pos.x,
y: pos.y + 1,
}); //Bottom
}
if pos.x < self.width - 1 && pos.y < self.height - 1{
possible_infections.push(Point {
x: pos.x + 1,
y: pos.y + 1,
}); //Bottom Right
}
for poss_infected_pos in possible_infections.iter() {
//possible_infections.iter().map(|poss_infected_pos|{
let inf_idx =
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
if self.humans[inf_idx].present_state == State::Normal {
if roll(self.plague.infection_rate) {
people_to_infect.push(Point {
x: poss_infected_pos.x,
y: poss_infected_pos.y,
});
}
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x - 1,
y: pos.y - 1,
},
); //Top Left
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x,
y: pos.y - 1,
},
); //Top
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x + 1,
y: pos.y - 1,
},
); //Top Right
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x - 1,
y: pos.y,
},
); //Left
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x + 1,
y: pos.y,
},
); //Right
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x - 1,
y: pos.y + 1,
},
); //Bottom Left
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x,
y: pos.y + 1,
},
); //Bottom
self.push_if_inside(
&mut possible_infections,
Point {
x: pos.x + 1,
y: pos.y + 1,
},
); //Bottom Right
for poss_infected_pos in possible_infections.iter() {
//possible_infections.iter().map(|poss_infected_pos|{
let inf_idx =
human_idx(poss_infected_pos.x, poss_infected_pos.y, self.width);
if self.humans[inf_idx].present_state == State::Normal {
if roll(self.plague.infection_rate) {
people_to_infect.push(Point {
x: poss_infected_pos.x,
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",
people_to_infect.len(),
people_to_cure.len(),
people_to_kill.len()
);
for infected_position in &people_to_infect {
for infected_position in people_to_infect.iter() {
// println!("To infect: {:?}", infected_position);
//people_to_infect.iter().map(|infected_position|{
let infected_index = human_idx(infected_position.x, infected_position.y, self.width);
@@ -247,15 +229,18 @@ impl Population {
//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|{
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;
//DEBUG
//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|{
let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
if self.humans[dead_index].present_state == State::Dead {
@@ -265,7 +250,10 @@ impl Population {
}
//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
}
@@ -291,7 +279,7 @@ impl Population {
pub fn roll(probability: i32) -> bool {
if probability > 0 {
let mut rng = rand::thread_rng();
rng.gen_range(0 as i32..CORRECTED_PERCENTAGE) <= probability
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
} else {
false
}
@@ -465,14 +453,131 @@ mod tests {
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];
// start with normal population
stats = humans_stats(&population.humans);
println!("stats after init: {:?}", stats);
assert_eq!(stats.normal, 100);
// infect every one
population.generate();
stats = humans_stats(&population.humans);
println!("stats after generate: {:?}", stats);
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})]
@@ -502,32 +607,30 @@ mod tests {
assert!(stats.infected >= infected_at_start_proba - infected_tolerance);
assert_eq!(stats.dead, 0);
for _x in 0..100 {
let infected_at_start = stats.infected;
let dead_at_start = stats.dead;
let infected_at_start = stats.infected;
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 {
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);
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);
}
}