3 Commits

Author SHA1 Message Date
66bded5763 remove dead code 2022-05-05 15:25:33 +02:00
94466c7f1b propagate each human consecutively 2022-05-05 14:33:52 +02:00
6684ad2675 remove generate and add some testing 2022-05-04 14:57:55 +02:00
2 changed files with 124 additions and 241 deletions

View File

@@ -21,18 +21,15 @@ fn main() {
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
//population.change_disease(disease);
println!("Before Filling");
//population.display();
population.generate();
println!("After Filling");
//population.display();
println!("After Propagation");
//population.display();
let mut stats: [i32; 4];
// = [0,0,0,0];
let mut counter: u32 = 0;
loop {
counter += 1;
stats = population.propagate();
stats = population.propagate_new();
//population.display();
println!(
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",

View File

@@ -1,11 +1,5 @@
use crate::prelude::*;
#[derive(Debug)]
pub struct Point {
x: i32,
y: i32,
}
pub struct Population {
pub start_infected_ratio: i32,
pub start_immune_ratio: i32,
@@ -15,6 +9,7 @@ pub struct Population {
pub height: i32,
pub age: i32,
pub plague: Disease,
pub size: usize,
}
pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
@@ -30,19 +25,36 @@ impl Population {
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
};
(width * height) as usize
size
];
for x in 0..width {
for y in 0..height {
let idx = human_idx(x, y, width);
the_humans[idx].x = x;
the_humans[idx].y = y;
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 {
@@ -54,216 +66,87 @@ impl Population {
plague: plague,
age: 0,
humans: the_humans,
}
}
// pub fn change_disease(&mut self, plague:Disease){
// self.plague = plague;
// }
pub fn generate(&mut self) {
//The ratios will not be exact, for example someone who wants 100% infected 100% immune and 100% dead, he will have 100% dead because they are overwriting each others
//Maybe consider limiting the total to not exceed 100 in the view
//Other thing, there will always be more of the last one because someone who is already infected for example could be then put to immune or dead
//One solution to this issue would be to have if else else statements but in this case for example 20% would be lower with the last because its 20% on the remaining population and not of the all
//In other words I did it that way but it can be changed just its not the right method to have perfect ratios
let mut rng = rand::thread_rng();
for x in self.humans.iter_mut() {
if (self.start_infected_ratio) > 0
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_infected_ratio as i32)
{
x.present_state = State::Infected;
} else if self.start_immune_ratio > 0
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_immune_ratio as i32
{
x.present_state = State::Immune;
} else if self.start_dead_ratio > 0
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= self.start_dead_ratio as i32
{
x.present_state = State::Dead;
}
size: size,
}
}
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 is_inside_and_infected(&self, point: Point) -> bool {
if self.is_inside(&point) {
let idx = human_idx(point.x, point.y, self.width);
if self.humans[idx].present_state == State::Infected {
roll(self.plague.infection_rate)
} else {
false
}
} else {
false
}
}
pub fn propagate(&mut self) -> [i32; 4] {
let mut people_to_check: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut possible_infected: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_infect: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_cure: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_kill: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
pub fn propagate_new(&mut self) -> [i32; 4] {
let mut stats: [i32; 4] = [0, 0, 0, 0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(self.humans.len());
for h in self.humans.iter() {
match h.present_state {
State::Normal => {
possible_infected.push(Point{ x: h.x, y: h.y});
stats[0] += 1;
}
State::Infected => {
people_to_check.push(Point { x: h.x, y: h.y });
stats[1] += 1;
}
State::Immune => {
stats[2] += 1;
}
State::Dead => {
stats[3] += 1;
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 => {},
}
}
}
}
// 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 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 });
}
}
}
for pos in possible_infected.iter() {
let infected: bool = self.is_inside_and_infected(
Point {
x: pos.x - 1,
y: pos.y - 1,
},
) || //Top Left
self.is_inside_and_infected(
Point {
x: pos.x,
y: pos.y - 1,
},
) || //Top
self.is_inside_and_infected(
Point {
x: pos.x + 1,
y: pos.y - 1,
},
) || //Top Right
self.is_inside_and_infected(
Point {
x: pos.x - 1,
y: pos.y,
},
) || //Left
self.is_inside_and_infected(
Point {
x: pos.x + 1,
y: pos.y,
},
) || //Right
self.is_inside_and_infected(
Point {
x: pos.x - 1,
y: pos.y + 1,
},
) || //Bottom Left
self.is_inside_and_infected(
Point {
x: pos.x,
y: pos.y + 1,
},
) || //Bottom
self.is_inside_and_infected(
Point {
x: pos.x + 1,
y: pos.y + 1,
},
); //Bottom Right
if infected {
people_to_infect.push(Point { x: pos.x, y: pos.y });
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);
}
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);
// let _ = infected_position.x;
//DEBUG
//println!("x: {} y: {} index: {}",infected_position.x,infected_position.y,infected_index);
self.humans[infected_index].present_state = State::Infected;
//DEBUG
//println!("Infected someone");
}
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.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 {
// println!("Already dead");
} else {
self.humans[dead_index].present_state = State::Dead;
}
//DEBUG
}
assert_eq!(
stats[0] + stats[1] + stats[2] + stats[3],
self.humans.len() as i32
);
self.humans = humans_n_plus_1;
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 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 {
@@ -370,12 +253,6 @@ mod tests {
let (width, height) = (5, 7);
let population = Population::new(20, 10, 5, 5, 7, disease);
assert_eq!(population.humans.len(), 5 * 7);
for human in population.humans.iter() {
assert!(
human.present_state == State::Normal,
"all humans should be normal"
);
}
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");
@@ -388,9 +265,8 @@ mod tests {
fn population_gen() {
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let (width, height) = (5, 7);
let mut population = Population::new(20, 10, 5, 5, 7, disease);
let population = Population::new(20, 10, 5, 5, 7, disease);
population.generate();
let stats: Stats = humans_stats(&population.humans);
println!("Stats: {:?}", stats);
@@ -409,28 +285,24 @@ mod tests {
disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(0, 0, 0, width, height, disease);
population.generate();
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);
population.generate();
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);
population.generate();
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);
population.generate();
stats = humans_stats(&population.humans);
println!("should be dead: {:?}", stats);
assert_eq!(stats.dead, width * height);
@@ -456,19 +328,13 @@ mod tests {
let mut stats: Stats;
let mut propagate_stats: [i32; 4];
// start with normal population
// infect every one
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);
assert_eq!(stats.infected, 100, "everybody should be infected");
// kill every one
propagate_stats = population.propagate();
propagate_stats = population.propagate_new();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [0, 100, 0, 0]);
@@ -478,7 +344,7 @@ mod tests {
assert_eq!(stats.dead, 100);
for _x in 0..100 {
propagate_stats = population.propagate();
propagate_stats = population.propagate_new();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [0, 0, 0, 100]);
@@ -549,7 +415,7 @@ mod tests {
assert_eq!(stats.normal, 8);
// kill every one
propagate_stats = population.propagate();
propagate_stats = population.propagate_new();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [8, 1, 0, 0]);
@@ -559,7 +425,7 @@ mod tests {
assert_eq!(stats.dead, 0);
for _x in 0..100 {
propagate_stats = population.propagate();
propagate_stats = population.propagate_new();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
assert_eq!(propagate_stats, [0, 9, 0, 0]);
@@ -630,7 +496,7 @@ mod tests {
assert_eq!(stats.normal, 8);
// infect every one
propagate_stats = population.propagate();
propagate_stats = population.propagate_new();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
@@ -641,7 +507,7 @@ mod tests {
assert_eq!(stats.dead, 0);
// cure every one
propagate_stats = population.propagate();
propagate_stats = population.propagate_new();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
@@ -653,7 +519,7 @@ mod tests {
// then
for _x in 0..100 {
propagate_stats = population.propagate();
propagate_stats = population.propagate_new();
stats = humans_stats(&population.humans);
println!("propate_stats: {:?}", propagate_stats);
println!("population: {:?}", stats);
@@ -665,6 +531,27 @@ mod tests {
}
}
#[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;
@@ -681,9 +568,8 @@ mod tests {
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
population = Population::new(start_infected, 0, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans);
println!("Population after generate: {:?}", stats);
println!("Population after init: {:?}", stats);
// total * proba - 20% < infected < total * proba + 20%
let infected_at_start_proba = width * height * start_infected / 100;
@@ -695,7 +581,7 @@ mod tests {
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_new();
assert!(propa_stats[3] >= dead_at_start);