diff --git a/src/main.rs b/src/main.rs index 32ae992..6386054 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,11 +21,9 @@ 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(); + //population.display(); println!("After Propagation"); let mut stats: [i32; 4]; // = [0,0,0,0]; diff --git a/src/population.rs b/src/population.rs index f16e590..c7acbb4 100644 --- a/src/population.rs +++ b/src/population.rs @@ -30,6 +30,8 @@ impl Population { height: i32, plague: Disease, ) -> Self { + let mut rng = rand::thread_rng(); + let mut the_humans: Vec = vec![ Human { x: 0, @@ -41,8 +43,21 @@ impl Population { 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 as i32) + { + present_state = State::Infected; + } else if start_immune_ratio > 0 + && rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio as i32 + { + present_state = State::Immune; + } else if start_dead_ratio > 0 + && rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio as i32 + { + present_state = State::Dead; + } + the_humans[idx] = Human{x: x, y: y, present_state: present_state}; } } Self { @@ -59,30 +74,6 @@ impl Population { // 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; - } - } - } fn is_inside(&self, pos: &Point) -> bool { if pos.x >= 0 && pos.x < self.width && pos.y >= 0 && pos.y < self.height { @@ -370,12 +361,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 +373,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 +393,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,16 +436,10 @@ 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(); @@ -681,9 +655,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;