forked from Maxluli/RustyPropagation
Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| fbf3b376c6 | |||
| 13fc7e9bd5 | |||
| b6d07d4d3c | |||
| 8a6b686490 |
+19
-6
@@ -13,12 +13,22 @@ mod prelude {
|
|||||||
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
|
use std::time::Instant;
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
struct Args {
|
struct Args {
|
||||||
/// Number of threads
|
/// Number of threads
|
||||||
#[clap(short, long, default_value_t = 1)]
|
#[clap(short, long, default_value_t = 1)]
|
||||||
threads: usize,
|
threads: usize,
|
||||||
|
/// Display stats after each propagation
|
||||||
|
#[clap(short, long)]
|
||||||
|
display: bool,
|
||||||
|
/// Width of humans grid in population
|
||||||
|
#[clap(short, long, default_value_t = 1000)]
|
||||||
|
width: i32,
|
||||||
|
/// Height of humans grid in population
|
||||||
|
#[clap(short, long, default_value_t = 1000)]
|
||||||
|
height: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -29,7 +39,7 @@ fn main() {
|
|||||||
.expect("Oops Looks like we have a problem here...");
|
.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,args.width,args.height,disease);
|
||||||
//population.change_disease(disease);
|
//population.change_disease(disease);
|
||||||
println!("After Filling");
|
println!("After Filling");
|
||||||
//population.display();
|
//population.display();
|
||||||
@@ -37,18 +47,21 @@ fn main() {
|
|||||||
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;
|
||||||
|
let now = Instant::now();
|
||||||
loop {
|
loop {
|
||||||
counter += 1;
|
counter += 1;
|
||||||
stats = population.propagate_new(Some(args.threads));
|
stats = population.propagate_new(Some(args.threads));
|
||||||
//population.display();
|
//population.display();
|
||||||
println!(
|
if args.display {
|
||||||
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
println!(
|
||||||
stats[0], stats[1], stats[2], stats[3]
|
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
||||||
);
|
stats[0], stats[1], stats[2], stats[3]
|
||||||
|
);
|
||||||
|
}
|
||||||
if stats[1] == 0 {
|
if stats[1] == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("Propagation finished in {} steps", counter);
|
println!("Propagation finished in {} steps, {:?}", counter, now.elapsed());
|
||||||
//population.display();
|
//population.display();
|
||||||
}
|
}
|
||||||
|
|||||||
+33
-48
@@ -81,7 +81,7 @@ impl Population {
|
|||||||
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
let mut stats: [i32; 4] = [0, 0, 0, 0];
|
||||||
|
|
||||||
let num_threads = num_threads.unwrap_or(1);
|
let num_threads = num_threads.unwrap_or(1);
|
||||||
if num_threads > 8 {
|
if num_threads > 48 {
|
||||||
panic!("too many threads")
|
panic!("too many threads")
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,25 +116,36 @@ impl Population {
|
|||||||
|
|
||||||
for idx in lower_bound..upper_bound {
|
for idx in lower_bound..upper_bound {
|
||||||
let human = &humans[idx];
|
let human = &humans[idx];
|
||||||
let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
// let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
||||||
if human.present_state == State::Normal {
|
let mut new_human = human.clone();
|
||||||
let possible = [
|
match human.present_state {
|
||||||
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
|
State::Normal => {
|
||||||
(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),
|
let possible = [
|
||||||
];
|
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
|
||||||
for neigh_coords in possible.iter() {
|
(human.x - 1, human.y) , (human.x + 1, human.y),
|
||||||
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
|
(human.x - 1, human.y + 1), (human.x, human.y + 1), (human.x + 1, human.y + 1),
|
||||||
match neigh_idx {
|
];
|
||||||
Some(x) => neighbors.push(&humans[x]),
|
for neigh_coords in possible.iter() {
|
||||||
None => {},
|
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
|
||||||
|
match neigh_idx {
|
||||||
|
Some(x) => if humans[x].present_state == State::Infected {
|
||||||
|
if roll(infection_rate) {
|
||||||
|
new_human.present_state = State::Infected;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
State::Infected => {
|
||||||
|
stats[1] += 1;
|
||||||
|
match die_or_cure(curing_rate, death_rate) {
|
||||||
|
Some(x) => new_human.present_state = x,
|
||||||
|
None => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
let new_human = evolve(human, neighbors, infection_rate, curing_rate, death_rate);
|
|
||||||
match human.present_state {
|
|
||||||
State::Normal => { stats[0] += 1; }
|
|
||||||
State::Infected => { stats[1] += 1; }
|
|
||||||
State::Immune => { stats[2] += 1; }
|
State::Immune => { stats[2] += 1; }
|
||||||
State::Dead => { stats[3] += 1; }
|
State::Dead => { stats[3] += 1; }
|
||||||
}
|
}
|
||||||
@@ -157,39 +168,13 @@ impl Population {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evolve(human: &Human, neighbors: Vec<&Human>, infection_rate: i32, curing_rate: i32, death_rate: i32) -> Human {
|
fn die_or_cure(curing_rate: i32, death_rate: i32) -> Option<State> {
|
||||||
let mut new_human = human.clone();
|
|
||||||
match human.present_state {
|
|
||||||
State::Normal => {
|
|
||||||
new_human.present_state = infect_by_neighbors(neighbors, infection_rate);
|
|
||||||
}
|
|
||||||
State::Infected => {
|
|
||||||
new_human.present_state = die_or_cure(curing_rate, death_rate);
|
|
||||||
}
|
|
||||||
State::Immune => {}
|
|
||||||
State::Dead => {}
|
|
||||||
}
|
|
||||||
new_human
|
|
||||||
}
|
|
||||||
|
|
||||||
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) {
|
if roll(curing_rate) {
|
||||||
State::Immune
|
Some(State::Immune)
|
||||||
} else if roll(death_rate) {
|
} else if roll(death_rate) {
|
||||||
State::Dead
|
Some(State::Dead)
|
||||||
} else {
|
} else {
|
||||||
State::Infected
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user