forked from Maxluli/RustyPropagation
Compare commits
2 Commits
b47c1fb2b6
...
600976cc6a
| Author | SHA1 | Date | |
|---|---|---|---|
|
600976cc6a
|
|||
|
0f2702a5c0
|
+38
-13
@@ -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 {
|
||||||
@@ -32,28 +33,30 @@ impl Population {
|
|||||||
) -> Self {
|
) -> Self {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
|
|
||||||
|
let size: usize = (width * height) as usize;
|
||||||
|
|
||||||
let mut the_humans: Vec<Human> = vec![
|
let mut the_humans: Vec<Human> = vec![
|
||||||
Human {
|
Human {
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
present_state: State::Normal
|
present_state: State::Normal
|
||||||
};
|
};
|
||||||
(width * height) as usize
|
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);
|
||||||
let mut present_state = State::Normal;
|
let mut present_state = State::Normal;
|
||||||
if (start_infected_ratio) > 0
|
if (start_infected_ratio > 0)
|
||||||
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_infected_ratio as i32)
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_infected_ratio)
|
||||||
{
|
{
|
||||||
present_state = State::Infected;
|
present_state = State::Infected;
|
||||||
} else if start_immune_ratio > 0
|
} else if (start_immune_ratio > 0)
|
||||||
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio as i32
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_immune_ratio)
|
||||||
{
|
{
|
||||||
present_state = State::Immune;
|
present_state = State::Immune;
|
||||||
} else if start_dead_ratio > 0
|
} else if (start_dead_ratio > 0)
|
||||||
&& rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio as i32
|
&& (rng.gen_range(0..CORRECTED_PERCENTAGE) <= start_dead_ratio)
|
||||||
{
|
{
|
||||||
present_state = State::Dead;
|
present_state = State::Dead;
|
||||||
}
|
}
|
||||||
@@ -69,6 +72,7 @@ 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){
|
||||||
@@ -98,15 +102,15 @@ impl Population {
|
|||||||
|
|
||||||
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 possible_infected: 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_infect: 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_cure: 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> =
|
let mut people_to_kill: Vec<Point> =
|
||||||
Vec::with_capacity((self.width * self.height) as usize);
|
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
|
||||||
|
|
||||||
@@ -233,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
|
||||||
}
|
}
|
||||||
@@ -639,6 +643,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();
|
||||||
|
|
||||||
|
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;
|
||||||
|
|||||||
Reference in New Issue
Block a user