forked from Maxluli/RustyPropagation
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
d528ae76a0
|
|||
|
600976cc6a
|
|||
|
0f2702a5c0
|
|||
|
b47c1fb2b6
|
|||
|
3a41670fea
|
|||
|
fb6e9e82b3
|
|||
|
bdb6e60e98
|
|||
|
a2bbd1d431
|
|||
|
b456a9fb63
|
|||
|
89020eeb21
|
|||
|
5affe83fdc
|
+17
-17
@@ -14,20 +14,20 @@ pub struct Human {
|
|||||||
pub x: i32,
|
pub x: i32,
|
||||||
pub y: i32,
|
pub y: i32,
|
||||||
}
|
}
|
||||||
impl Human {
|
// impl Human {
|
||||||
// humans with state "Normal"
|
// // humans with state "Normal"
|
||||||
pub fn new(pos_x: i32, pos_y: i32) -> Self {
|
// pub fn new(pos_x: i32, pos_y: i32) -> Self {
|
||||||
Self {
|
// Self {
|
||||||
present_state: State::Normal,
|
// present_state: State::Normal,
|
||||||
x: pos_x,
|
// x: pos_x,
|
||||||
y: pos_y,
|
// y: pos_y,
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
// pub fn new_empty() -> Self{
|
// // pub fn new_empty() -> Self{
|
||||||
// Self{
|
// // Self{
|
||||||
// present_state:State::Normal,
|
// // present_state:State::Normal,
|
||||||
// x : 0,
|
// // x : 0,
|
||||||
// y : 0,
|
// // y : 0,
|
||||||
// }
|
// // }
|
||||||
// }
|
// // }
|
||||||
}
|
// }
|
||||||
|
|||||||
+1
-6
@@ -17,18 +17,13 @@ fn main() {
|
|||||||
let term = Term::stdout();
|
let term = Term::stdout();
|
||||||
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
term.write_line("********** Rusty Propagation (Console) 2022 **********")
|
||||||
.expect("Oops Looks like we have a problem here...");
|
.expect("Oops Looks like we have a problem here...");
|
||||||
term.write_line("Press any key to start the propagation")
|
|
||||||
.expect("Oops Looks like we have a problem here...");
|
|
||||||
|
|
||||||
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
|
||||||
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
|
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
|
||||||
//population.change_disease(disease);
|
//population.change_disease(disease);
|
||||||
println!("Before Filling");
|
|
||||||
//population.display();
|
|
||||||
population.generate();
|
|
||||||
println!("After Filling");
|
println!("After Filling");
|
||||||
//population.display();
|
//population.display();
|
||||||
println!("After Propagation");
|
//population.display();
|
||||||
let mut stats: [i32; 4];
|
let mut stats: [i32; 4];
|
||||||
// = [0,0,0,0];
|
// = [0,0,0,0];
|
||||||
let mut counter: u32 = 0;
|
let mut counter: u32 = 0;
|
||||||
|
|||||||
+242
-161
@@ -15,6 +15,7 @@ pub struct Population {
|
|||||||
pub height: i32,
|
pub height: i32,
|
||||||
pub age: i32,
|
pub age: i32,
|
||||||
pub plague: Disease,
|
pub plague: Disease,
|
||||||
|
pub size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
|
pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
|
||||||
@@ -30,12 +31,36 @@ impl Population {
|
|||||||
height: i32,
|
height: i32,
|
||||||
plague: Disease,
|
plague: Disease,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut the_humans: Vec<Human> = vec![Human{x: 0, y: 0, present_state: State::Normal}; (width * height) as usize];
|
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
|
||||||
|
};
|
||||||
|
size
|
||||||
|
];
|
||||||
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);
|
||||||
the_humans[idx].x = x;
|
let mut present_state = State::Normal;
|
||||||
the_humans[idx].y = y;
|
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 {
|
Self {
|
||||||
@@ -47,35 +72,12 @@ impl Population {
|
|||||||
plague: plague,
|
plague: plague,
|
||||||
age: 0,
|
age: 0,
|
||||||
humans: the_humans,
|
humans: the_humans,
|
||||||
|
size: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// pub fn change_disease(&mut self, plague:Disease){
|
// pub fn change_disease(&mut self, plague:Disease){
|
||||||
// self.plague = plague;
|
// 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 {
|
fn is_inside(&self, pos: &Point) -> bool {
|
||||||
if pos.x >= 0 && pos.x < self.width && pos.y >= 0 && pos.y < self.height {
|
if pos.x >= 0 && pos.x < self.width && pos.y >= 0 && pos.y < self.height {
|
||||||
@@ -85,31 +87,41 @@ impl Population {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_if_inside(&self, point_list: &mut Vec<Point>, point: Point) {
|
fn is_inside_and_infected(&self, point: Point) -> bool {
|
||||||
if self.is_inside(&point) {
|
if self.is_inside(&point) {
|
||||||
point_list.push(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] {
|
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);
|
Vec::with_capacity(self.size);
|
||||||
let mut people_to_infect: Vec<Point> =
|
let mut possible_infected: Vec<Point> =
|
||||||
Vec::with_capacity((self.width * self.height) as usize);
|
Vec::with_capacity(self.size);
|
||||||
let mut people_to_cure: Vec<Point> =
|
let mut people_to_infect: Vec<Point> =
|
||||||
Vec::with_capacity((self.width * self.height) as usize);
|
Vec::with_capacity(self.size);
|
||||||
let mut people_to_kill: Vec<Point> =
|
let mut people_to_cure: Vec<Point> =
|
||||||
Vec::with_capacity((self.width * self.height) as usize);
|
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];
|
||||||
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
||||||
|
|
||||||
for h in self.humans.iter() {
|
for h in self.humans.iter() {
|
||||||
match h.present_state {
|
match h.present_state {
|
||||||
State::Normal => {
|
State::Normal => {
|
||||||
|
possible_infected.push(Point{ x: h.x, y: h.y});
|
||||||
stats[0] += 1;
|
stats[0] += 1;
|
||||||
}
|
}
|
||||||
State::Infected => {
|
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;
|
stats[1] += 1;
|
||||||
}
|
}
|
||||||
State::Immune => {
|
State::Immune => {
|
||||||
@@ -133,83 +145,62 @@ impl Population {
|
|||||||
if roll(self.plague.death_rate) {
|
if roll(self.plague.death_rate) {
|
||||||
//cheks if the man dies
|
//cheks if the man dies
|
||||||
people_to_kill.push(Point { x: pos.x, y: pos.y });
|
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});
|
|
||||||
|
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
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 });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for infected_position in people_to_infect.iter() {
|
for infected_position in people_to_infect.iter() {
|
||||||
// println!("To infect: {:?}", infected_position);
|
// println!("To infect: {:?}", infected_position);
|
||||||
@@ -246,7 +237,7 @@ impl Population {
|
|||||||
}
|
}
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
stats[0] + stats[1] + stats[2] + stats[3],
|
stats[0] + stats[1] + stats[2] + stats[3],
|
||||||
self.humans.len() as i32
|
self.size as i32
|
||||||
);
|
);
|
||||||
stats
|
stats
|
||||||
}
|
}
|
||||||
@@ -343,7 +334,11 @@ mod tests {
|
|||||||
let mut stats: Stats;
|
let mut stats: Stats;
|
||||||
|
|
||||||
for _ in 0..10 {
|
for _ in 0..10 {
|
||||||
humans.push(Human::new(0, 0));
|
humans.push(Human {
|
||||||
|
present_state: State::Normal,
|
||||||
|
x: 0,
|
||||||
|
y: 0,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
stats = humans_stats(&humans);
|
stats = humans_stats(&humans);
|
||||||
assert_eq!(stats.normal, 10);
|
assert_eq!(stats.normal, 10);
|
||||||
@@ -370,28 +365,20 @@ mod tests {
|
|||||||
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);
|
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() {
|
for h in population.humans.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!(population.humans[idx].x, h.x, "coordinates should match");
|
||||||
assert_eq!(population.humans[idx].y, h.y, "coordinates should match");
|
assert_eq!(population.humans[idx].y, h.y, "coordinates should match");
|
||||||
}
|
}
|
||||||
assert_eq!(population.humans.len(), (width * height) as usize);
|
assert_eq!(population.humans.len(), (width * height) as usize);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn population_gen() {
|
fn population_gen() {
|
||||||
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 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);
|
let stats: Stats = humans_stats(&population.humans);
|
||||||
println!("Stats: {:?}", stats);
|
println!("Stats: {:?}", stats);
|
||||||
|
|
||||||
@@ -410,28 +397,24 @@ mod tests {
|
|||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
population = Population::new(0, 0, 0, width, height, disease);
|
population = Population::new(0, 0, 0, width, height, disease);
|
||||||
population.generate();
|
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("should be normal: {:?}", stats);
|
println!("should be normal: {:?}", stats);
|
||||||
assert_eq!(stats.normal, width * height);
|
assert_eq!(stats.normal, width * height);
|
||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
population = Population::new(100, 0, 0, width, height, disease);
|
population = Population::new(100, 0, 0, width, height, disease);
|
||||||
population.generate();
|
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("should be infected: {:?}", stats);
|
println!("should be infected: {:?}", stats);
|
||||||
assert_eq!(stats.infected, width * height);
|
assert_eq!(stats.infected, width * height);
|
||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
population = Population::new(0, 100, 0, width, height, disease);
|
population = Population::new(0, 100, 0, width, height, disease);
|
||||||
population.generate();
|
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("should be immune: {:?}", stats);
|
println!("should be immune: {:?}", stats);
|
||||||
assert_eq!(stats.immune, width * height);
|
assert_eq!(stats.immune, width * height);
|
||||||
|
|
||||||
disease = Disease::new(0, 0, 0, String::from("Test"));
|
disease = Disease::new(0, 0, 0, String::from("Test"));
|
||||||
population = Population::new(0, 0, 100, width, height, disease);
|
population = Population::new(0, 0, 100, width, height, disease);
|
||||||
population.generate();
|
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("should be dead: {:?}", stats);
|
println!("should be dead: {:?}", stats);
|
||||||
assert_eq!(stats.dead, width * height);
|
assert_eq!(stats.dead, width * height);
|
||||||
@@ -457,16 +440,10 @@ mod tests {
|
|||||||
let mut stats: Stats;
|
let mut stats: Stats;
|
||||||
let mut propagate_stats: [i32; 4];
|
let mut propagate_stats: [i32; 4];
|
||||||
|
|
||||||
// start with normal population
|
// infect every one
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("stats after init: {:?}", stats);
|
println!("stats after init: {:?}", stats);
|
||||||
assert_eq!(stats.normal, 100);
|
assert_eq!(stats.infected, 100, "everybody should be infected");
|
||||||
|
|
||||||
// infect every one
|
|
||||||
population.generate();
|
|
||||||
stats = humans_stats(&population.humans);
|
|
||||||
println!("stats after generate: {:?}", stats);
|
|
||||||
assert_eq!(stats.infected, 100);
|
|
||||||
|
|
||||||
// kill every one
|
// kill every one
|
||||||
propagate_stats = population.propagate();
|
propagate_stats = population.propagate();
|
||||||
@@ -499,15 +476,51 @@ mod tests {
|
|||||||
|
|
||||||
// start with normal population
|
// start with normal population
|
||||||
population.humans = vec![
|
population.humans = vec![
|
||||||
Human{present_state: State::Normal, x: 0, y: 0},
|
Human {
|
||||||
Human{present_state: State::Normal, x: 1, y: 0},
|
present_state: State::Normal,
|
||||||
Human{present_state: State::Normal, x: 2, y: 0},
|
x: 0,
|
||||||
Human{present_state: State::Normal, x: 0, y: 1},
|
y: 0,
|
||||||
Human{present_state: State::Infected, x: 1, y: 1},
|
},
|
||||||
Human{present_state: State::Normal, x: 2, y: 1},
|
Human {
|
||||||
Human{present_state: State::Normal, x: 0, y: 2},
|
present_state: State::Normal,
|
||||||
Human{present_state: State::Normal, x: 1, y: 2},
|
x: 1,
|
||||||
Human{present_state: State::Normal, x: 2, y: 2},
|
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);
|
stats = humans_stats(&population.humans);
|
||||||
println!("stats after init: {:?}", stats);
|
println!("stats after init: {:?}", stats);
|
||||||
@@ -544,44 +557,113 @@ mod tests {
|
|||||||
|
|
||||||
// start with normal population
|
// start with normal population
|
||||||
population.humans = vec![
|
population.humans = vec![
|
||||||
Human{present_state: State::Normal, x: 0, y: 0},
|
Human {
|
||||||
Human{present_state: State::Normal, x: 1, y: 0},
|
present_state: State::Normal,
|
||||||
Human{present_state: State::Normal, x: 2, y: 0},
|
x: 0,
|
||||||
Human{present_state: State::Normal, x: 0, y: 1},
|
y: 0,
|
||||||
Human{present_state: State::Infected, x: 1, y: 1},
|
},
|
||||||
Human{present_state: State::Normal, x: 2, y: 1},
|
Human {
|
||||||
Human{present_state: State::Normal, x: 0, y: 2},
|
present_state: State::Normal,
|
||||||
Human{present_state: State::Normal, x: 1, y: 2},
|
x: 1,
|
||||||
Human{present_state: State::Normal, x: 2, y: 2},
|
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);
|
stats = humans_stats(&population.humans);
|
||||||
println!("stats after init: {:?}", stats);
|
println!("stats after init: {:?}", stats);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
// kill every one
|
// infect every one
|
||||||
propagate_stats = population.propagate();
|
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);
|
||||||
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
assert_eq!(propagate_stats, [8, 1, 0, 0]);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 0);
|
||||||
assert_eq!(stats.infected, 0);
|
assert_eq!(stats.infected, 8);
|
||||||
assert_eq!(stats.immune, 1);
|
assert_eq!(stats.immune, 1);
|
||||||
assert_eq!(stats.dead, 0);
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
|
// cure every one
|
||||||
|
propagate_stats = population.propagate();
|
||||||
|
stats = humans_stats(&population.humans);
|
||||||
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
|
println!("population: {:?}", stats);
|
||||||
|
assert_eq!(propagate_stats, [0, 8, 1, 0]);
|
||||||
|
assert_eq!(stats.normal, 0);
|
||||||
|
assert_eq!(stats.infected, 0);
|
||||||
|
assert_eq!(stats.immune, 9);
|
||||||
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
|
// then
|
||||||
for _x in 0..100 {
|
for _x in 0..100 {
|
||||||
propagate_stats = population.propagate();
|
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);
|
||||||
assert_eq!(propagate_stats, [8, 0, 1, 0]);
|
assert_eq!(propagate_stats, [0, 0, 9, 0]);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 0);
|
||||||
assert_eq!(stats.infected, 0);
|
assert_eq!(stats.infected, 0);
|
||||||
assert_eq!(stats.immune, 1);
|
assert_eq!(stats.immune, 9);
|
||||||
assert_eq!(stats.dead, 0);
|
assert_eq!(stats.dead, 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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})]
|
#[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) {
|
fn propagate_test(infection_rate: i32, death_rate: i32, infected_expected: i32) {
|
||||||
let disease: Disease;
|
let disease: Disease;
|
||||||
@@ -598,9 +680,8 @@ mod tests {
|
|||||||
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
|
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
|
||||||
population = Population::new(start_infected, 0, 0, width, height, disease);
|
population = Population::new(start_infected, 0, 0, width, height, disease);
|
||||||
|
|
||||||
population.generate();
|
|
||||||
stats = humans_stats(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
println!("Population after generate: {:?}", stats);
|
println!("Population after init: {:?}", stats);
|
||||||
|
|
||||||
// total * proba - 20% < infected < total * proba + 20%
|
// total * proba - 20% < infected < total * proba + 20%
|
||||||
let infected_at_start_proba = width * height * start_infected / 100;
|
let infected_at_start_proba = width * height * start_infected / 100;
|
||||||
|
|||||||
Reference in New Issue
Block a user