forked from Maxluli/RustyPropagation
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
b47c1fb2b6
|
@@ -24,6 +24,7 @@ fn main() {
|
||||
println!("After Filling");
|
||||
//population.display();
|
||||
//population.display();
|
||||
println!("After Propagation");
|
||||
let mut stats: [i32; 4];
|
||||
// = [0,0,0,0];
|
||||
let mut counter: u32 = 0;
|
||||
|
||||
+46
-104
@@ -1,8 +1,4 @@
|
||||
use std::sync::Arc;
|
||||
use std::sync::Mutex;
|
||||
use crate::prelude::*;
|
||||
use std::thread;
|
||||
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Point {
|
||||
@@ -14,12 +10,11 @@ pub struct Population {
|
||||
pub start_infected_ratio: i32,
|
||||
pub start_immune_ratio: i32,
|
||||
pub start_dead_ratio: i32,
|
||||
pub humans: Arc<Mutex<Vec<Human>>>,
|
||||
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 {
|
||||
@@ -37,35 +32,32 @@ impl Population {
|
||||
) -> Self {
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
let size: usize = (width * height) as usize;
|
||||
|
||||
let the_humans_arc = Arc::new(Mutex::new(vec![
|
||||
let mut the_humans: Vec<Human> = vec![
|
||||
Human {
|
||||
x: 0,
|
||||
y: 0,
|
||||
present_state: State::Normal
|
||||
};
|
||||
size
|
||||
]));
|
||||
let the_humans = Arc::clone(&the_humans_arc);
|
||||
(width * height) as usize
|
||||
];
|
||||
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)
|
||||
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)
|
||||
} 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)
|
||||
} else if start_dead_ratio > 0
|
||||
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio as i32
|
||||
{
|
||||
present_state = State::Dead;
|
||||
}
|
||||
the_humans.lock().unwrap()[idx] = Human{x: x, y: y, present_state: present_state};
|
||||
the_humans[idx] = Human{x: x, y: y, present_state: present_state};
|
||||
}
|
||||
}
|
||||
Self {
|
||||
@@ -76,8 +68,7 @@ impl Population {
|
||||
height: height,
|
||||
plague: plague,
|
||||
age: 0,
|
||||
humans: the_humans_arc,
|
||||
size: size,
|
||||
humans: the_humans,
|
||||
}
|
||||
}
|
||||
// pub fn change_disease(&mut self, plague:Disease){
|
||||
@@ -93,11 +84,9 @@ impl Population {
|
||||
}
|
||||
|
||||
fn is_inside_and_infected(&self, point: Point) -> bool {
|
||||
let the_humans_arc = Arc::clone(&self.humans);
|
||||
if self.is_inside(&point) {
|
||||
let idx = human_idx(point.x, point.y, self.width);
|
||||
let humans = the_humans_arc.lock().unwrap();
|
||||
if humans[idx].present_state == State::Infected {
|
||||
if self.humans[idx].present_state == State::Infected {
|
||||
roll(self.plague.infection_rate)
|
||||
} else {
|
||||
false
|
||||
@@ -109,20 +98,19 @@ impl Population {
|
||||
|
||||
pub fn propagate(&mut self) -> [i32; 4] {
|
||||
let mut people_to_check: Vec<Point> =
|
||||
Vec::with_capacity(self.size);
|
||||
Vec::with_capacity((self.width * self.height) as usize);
|
||||
let mut possible_infected: Vec<Point> =
|
||||
Vec::with_capacity(self.size);
|
||||
Vec::with_capacity((self.width * self.height) as usize);
|
||||
let mut people_to_infect: Vec<Point> =
|
||||
Vec::with_capacity(self.size);
|
||||
Vec::with_capacity((self.width * self.height) as usize);
|
||||
let mut people_to_cure: Vec<Point> =
|
||||
Vec::with_capacity(self.size);
|
||||
Vec::with_capacity((self.width * self.height) as usize);
|
||||
let mut people_to_kill: Vec<Point> =
|
||||
Vec::with_capacity(self.size);
|
||||
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
|
||||
|
||||
let humans = Arc::clone(&self.humans);
|
||||
for h in humans.lock().unwrap().iter() {
|
||||
for h in self.humans.iter() {
|
||||
match h.present_state {
|
||||
State::Normal => {
|
||||
possible_infected.push(Point{ x: h.x, y: h.y});
|
||||
@@ -210,66 +198,42 @@ impl Population {
|
||||
}
|
||||
}
|
||||
|
||||
// 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");
|
||||
// }
|
||||
|
||||
let mut threads = vec![];
|
||||
{
|
||||
let humans = Arc::clone(&self.humans);
|
||||
let width = self.width;
|
||||
threads.push(thread::spawn(move || {
|
||||
for infected_position in people_to_infect.iter() {
|
||||
let infected_index = human_idx(infected_position.x, infected_position.y, width);
|
||||
humans.lock().unwrap()[infected_index].present_state = State::Infected;
|
||||
}
|
||||
}));
|
||||
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");
|
||||
}
|
||||
|
||||
{
|
||||
let humans = Arc::clone(&self.humans);
|
||||
let width = self.width;
|
||||
threads.push(thread::spawn(move || {
|
||||
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, width);
|
||||
if humans.lock().unwrap()[cured_index].present_state != State::Infected {
|
||||
println!("not infected");
|
||||
} else {
|
||||
humans.lock().unwrap()[cured_index].present_state = State::Immune;
|
||||
}
|
||||
//DEBUG
|
||||
//println!("Cured someone");
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
for t in threads {
|
||||
t.join().unwrap();
|
||||
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() {
|
||||
let humans = Arc::clone(&self.humans);
|
||||
//people_to_kill.iter().map(|dead_position|{
|
||||
let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
|
||||
if humans.lock().unwrap()[dead_index].present_state == State::Dead {
|
||||
if self.humans[dead_index].present_state == State::Dead {
|
||||
// println!("Already dead");
|
||||
} else {
|
||||
humans.lock().unwrap()[dead_index].present_state = State::Dead;
|
||||
self.humans[dead_index].present_state = State::Dead;
|
||||
}
|
||||
//DEBUG
|
||||
}
|
||||
assert_eq!(
|
||||
stats[0] + stats[1] + stats[2] + stats[3],
|
||||
self.size as i32
|
||||
self.humans.len() as i32
|
||||
);
|
||||
stats
|
||||
}
|
||||
@@ -396,14 +360,13 @@ mod tests {
|
||||
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 humans = Arc::clone(&population.humans);
|
||||
assert_eq!(humans.lock().unwrap().len(), 5 * 7);
|
||||
for h in humans.lock().unwrap().iter() {
|
||||
assert_eq!(population.humans.len(), 5 * 7);
|
||||
for h in population.humans.iter() {
|
||||
let idx = human_idx(h.x, h.y, width);
|
||||
assert_eq!(humans.lock().unwrap()[idx].x, h.x, "coordinates should match");
|
||||
assert_eq!(humans.lock().unwrap()[idx].y, h.y, "coordinates should match");
|
||||
assert_eq!(population.humans[idx].x, h.x, "coordinates should match");
|
||||
assert_eq!(population.humans[idx].y, h.y, "coordinates should match");
|
||||
}
|
||||
assert_eq!(humans.lock().unwrap().len(), (width * height) as usize);
|
||||
assert_eq!(population.humans.len(), (width * height) as usize);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -676,27 +639,6 @@ 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();
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user