forked from Maxluli/RustyPropagation
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ea74e1029b |
+1
-1
@@ -29,7 +29,7 @@ fn main() {
|
|||||||
let mut counter: u32 = 0;
|
let mut counter: u32 = 0;
|
||||||
loop {
|
loop {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
stats = population.propagate_new();
|
stats = population.propagate();
|
||||||
//population.display();
|
//population.display();
|
||||||
println!(
|
println!(
|
||||||
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
||||||
|
|||||||
+231
-86
@@ -1,10 +1,20 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
use std::sync::Mutex;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
use std::thread;
|
||||||
|
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Point {
|
||||||
|
x: i32,
|
||||||
|
y: i32,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Population {
|
pub struct Population {
|
||||||
pub start_infected_ratio: i32,
|
pub start_infected_ratio: i32,
|
||||||
pub start_immune_ratio: i32,
|
pub start_immune_ratio: i32,
|
||||||
pub start_dead_ratio: i32,
|
pub start_dead_ratio: i32,
|
||||||
pub humans: Vec<Human>,
|
pub humans: Arc<Mutex<Vec<Human>>>,
|
||||||
pub width: i32,
|
pub width: i32,
|
||||||
pub height: i32,
|
pub height: i32,
|
||||||
pub age: i32,
|
pub age: i32,
|
||||||
@@ -29,14 +39,15 @@ impl Population {
|
|||||||
|
|
||||||
let size: usize = (width * height) as usize;
|
let size: usize = (width * height) as usize;
|
||||||
|
|
||||||
let mut the_humans: Vec<Human> = vec![
|
let the_humans_arc = Arc::new(Mutex::new(vec![
|
||||||
Human {
|
Human {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
present_state: State::Normal
|
present_state: State::Normal
|
||||||
};
|
};
|
||||||
size
|
size
|
||||||
];
|
]));
|
||||||
|
let the_humans = Arc::clone(&the_humans_arc);
|
||||||
for x in 0..width {
|
for x in 0..width {
|
||||||
for y in 0..height {
|
for y in 0..height {
|
||||||
let idx = human_idx(x, y, width);
|
let idx = human_idx(x, y, width);
|
||||||
@@ -54,7 +65,7 @@ impl Population {
|
|||||||
{
|
{
|
||||||
present_state = State::Dead;
|
present_state = State::Dead;
|
||||||
}
|
}
|
||||||
the_humans[idx] = Human{x: x, y: y, present_state: present_state};
|
the_humans.lock().unwrap()[idx] = Human{x: x, y: y, present_state: present_state};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Self {
|
Self {
|
||||||
@@ -65,88 +76,221 @@ impl Population {
|
|||||||
height: height,
|
height: height,
|
||||||
plague: plague,
|
plague: plague,
|
||||||
age: 0,
|
age: 0,
|
||||||
humans: the_humans,
|
humans: the_humans_arc,
|
||||||
size: size,
|
size: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// pub fn change_disease(&mut self, plague:Disease){
|
||||||
|
// self.plague = plague;
|
||||||
|
// }
|
||||||
|
|
||||||
pub fn propagate_new(&mut self) -> [i32; 4] {
|
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 {
|
||||||
|
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 {
|
||||||
|
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.size);
|
||||||
|
let mut possible_infected: Vec<Point> =
|
||||||
|
Vec::with_capacity(self.size);
|
||||||
|
let mut people_to_infect: Vec<Point> =
|
||||||
|
Vec::with_capacity(self.size);
|
||||||
|
let mut people_to_cure: Vec<Point> =
|
||||||
|
Vec::with_capacity(self.size);
|
||||||
|
let mut people_to_kill: Vec<Point> =
|
||||||
|
Vec::with_capacity(self.size);
|
||||||
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
||||||
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(self.humans.len());
|
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
||||||
|
|
||||||
for human in self.humans.iter() {
|
let humans = Arc::clone(&self.humans);
|
||||||
let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
for h in humans.lock().unwrap().iter() {
|
||||||
if human.present_state == State::Normal {
|
match h.present_state {
|
||||||
let possible = [
|
State::Normal => {
|
||||||
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
|
possible_infected.push(Point{ x: h.x, y: h.y});
|
||||||
(human.x - 1, human.y) , (human.x + 1, human.y),
|
stats[0] += 1;
|
||||||
(human.x - 1, human.y + 1), (human.x, human.y + 1), (human.x + 1, human.y + 1),
|
}
|
||||||
];
|
State::Infected => {
|
||||||
for neigh_coords in possible.iter() {
|
people_to_check.push(Point { x: h.x, y: h.y });
|
||||||
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, self.width, self.height);
|
stats[1] += 1;
|
||||||
match neigh_idx {
|
}
|
||||||
Some(x) => neighbors.push(&self.humans[x]),
|
State::Immune => {
|
||||||
None => {},
|
stats[2] += 1;
|
||||||
}
|
}
|
||||||
|
State::Dead => {
|
||||||
|
stats[3] += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let new_human = evolve(human, neighbors, self.plague.infection_rate, self.plague.curing_rate, self.plague.death_rate);
|
}
|
||||||
match human.present_state {
|
// for pos in &people_to_check {
|
||||||
State::Normal => { stats[0] += 1; }
|
for pos in people_to_check.iter() {
|
||||||
State::Infected => { stats[1] += 1; }
|
//people_to_check.iter().map(|pos|{
|
||||||
State::Immune => { stats[2] += 1; }
|
//get all the other people next to me and check if i die cure or infect
|
||||||
State::Dead => { stats[3] += 1; }
|
//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 });
|
||||||
}
|
}
|
||||||
humans_n_plus_1.push(new_human);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.humans = humans_n_plus_1;
|
// 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;
|
||||||
|
}
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
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 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 {
|
||||||
|
// println!("Already dead");
|
||||||
|
} else {
|
||||||
|
humans.lock().unwrap()[dead_index].present_state = State::Dead;
|
||||||
|
}
|
||||||
|
//DEBUG
|
||||||
|
}
|
||||||
|
assert_eq!(
|
||||||
|
stats[0] + stats[1] + stats[2] + stats[3],
|
||||||
|
self.size as i32
|
||||||
|
);
|
||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn evolve(human: &Human, neighbors: Vec<&Human>, infection_rate: i32, curing_rate: i32, death_rate: i32) -> Human {
|
// pub fn display(&mut self){
|
||||||
let mut new_human = human.clone();
|
// let sprite = "#";
|
||||||
match human.present_state {
|
// print!("\n");
|
||||||
State::Normal => {
|
// for x in 0..self.width{
|
||||||
new_human.present_state = infect_by_neighbors(neighbors, infection_rate);
|
// for y in 0..self.height{
|
||||||
}
|
// let index = human_idx(x as i32,y as i32,self.width as i32);
|
||||||
State::Infected => {
|
// match self.humans[index].present_state {
|
||||||
new_human.present_state = die_or_cure(curing_rate, death_rate);
|
// State::Normal => print!("{}",style(sprite).green()),
|
||||||
}
|
// State::Dead => print!("{}",style(sprite).black()),
|
||||||
State::Immune => {}
|
// State::Infected => print!("{}",style(sprite).red()),
|
||||||
State::Dead => {}
|
// State::Immune => print!("{}",style(sprite).blue()),
|
||||||
}
|
// _ => print!("{}",style(sprite).white()),
|
||||||
new_human
|
// }
|
||||||
}
|
// }
|
||||||
|
// print!("\n");
|
||||||
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 {
|
pub fn roll(probability: i32) -> bool {
|
||||||
@@ -252,13 +396,14 @@ mod tests {
|
|||||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||||
let (width, height) = (5, 7);
|
let (width, height) = (5, 7);
|
||||||
let population = Population::new(20, 10, 5, 5, 7, disease);
|
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||||
assert_eq!(population.humans.len(), 5 * 7);
|
let humans = Arc::clone(&population.humans);
|
||||||
for h in population.humans.iter() {
|
assert_eq!(humans.lock().unwrap().len(), 5 * 7);
|
||||||
|
for h in humans.lock().unwrap().iter() {
|
||||||
let idx = human_idx(h.x, h.y, width);
|
let idx = human_idx(h.x, h.y, width);
|
||||||
assert_eq!(population.humans[idx].x, h.x, "coordinates should match");
|
assert_eq!(humans.lock().unwrap()[idx].x, h.x, "coordinates should match");
|
||||||
assert_eq!(population.humans[idx].y, h.y, "coordinates should match");
|
assert_eq!(humans.lock().unwrap()[idx].y, h.y, "coordinates should match");
|
||||||
}
|
}
|
||||||
assert_eq!(population.humans.len(), (width * height) as usize);
|
assert_eq!(humans.lock().unwrap().len(), (width * height) as usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -334,7 +479,7 @@ mod tests {
|
|||||||
assert_eq!(stats.infected, 100, "everybody should be infected");
|
assert_eq!(stats.infected, 100, "everybody should be infected");
|
||||||
|
|
||||||
// kill every one
|
// kill every one
|
||||||
propagate_stats = population.propagate_new();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
assert_eq!(propagate_stats, [0, 100, 0, 0]);
|
assert_eq!(propagate_stats, [0, 100, 0, 0]);
|
||||||
@@ -344,7 +489,7 @@ mod tests {
|
|||||||
assert_eq!(stats.dead, 100);
|
assert_eq!(stats.dead, 100);
|
||||||
|
|
||||||
for _x in 0..100 {
|
for _x in 0..100 {
|
||||||
propagate_stats = population.propagate_new();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
assert_eq!(propagate_stats, [0, 0, 0, 100]);
|
assert_eq!(propagate_stats, [0, 0, 0, 100]);
|
||||||
@@ -415,7 +560,7 @@ mod tests {
|
|||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
// kill every one
|
// kill every one
|
||||||
propagate_stats = population.propagate_new();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
||||||
@@ -425,7 +570,7 @@ mod tests {
|
|||||||
assert_eq!(stats.dead, 0);
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
for _x in 0..100 {
|
for _x in 0..100 {
|
||||||
propagate_stats = population.propagate_new();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
assert_eq!(propagate_stats, [0, 9, 0, 0]);
|
assert_eq!(propagate_stats, [0, 9, 0, 0]);
|
||||||
@@ -496,7 +641,7 @@ mod tests {
|
|||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
// infect every one
|
// infect every one
|
||||||
propagate_stats = population.propagate_new();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
println!("population: {:?}", stats);
|
println!("population: {:?}", stats);
|
||||||
@@ -507,7 +652,7 @@ mod tests {
|
|||||||
assert_eq!(stats.dead, 0);
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
// cure every one
|
// cure every one
|
||||||
propagate_stats = population.propagate_new();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
println!("population: {:?}", stats);
|
println!("population: {:?}", stats);
|
||||||
@@ -519,7 +664,7 @@ mod tests {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
for _x in 0..100 {
|
for _x in 0..100 {
|
||||||
propagate_stats = population.propagate_new();
|
propagate_stats = population.propagate();
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
println!("population: {:?}", stats);
|
println!("population: {:?}", stats);
|
||||||
@@ -546,7 +691,7 @@ mod tests {
|
|||||||
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 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);
|
assert!(stats_before.infected >= should_be_infected - tolerance, "{} infected, should be more than {}", stats_before.infected, should_be_infected - tolerance);
|
||||||
|
|
||||||
population.propagate_new();
|
population.propagate();
|
||||||
|
|
||||||
stats_after = humans_stats(&population.humans);
|
stats_after = humans_stats(&population.humans);
|
||||||
assert_eq!(stats_before.infected, stats_after.infected, "no one should have been infected");
|
assert_eq!(stats_before.infected, stats_after.infected, "no one should have been infected");
|
||||||
@@ -581,7 +726,7 @@ mod tests {
|
|||||||
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_new();
|
let propa_stats: [i32; 4] = population.propagate();
|
||||||
|
|
||||||
assert!(propa_stats[3] >= dead_at_start);
|
assert!(propa_stats[3] >= dead_at_start);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user