forked from Maxluli/RustyPropagation
Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| ea74e1029b |
Generated
-144
@@ -2,74 +2,18 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "atty"
|
|
||||||
version = "0.2.14"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
|
|
||||||
dependencies = [
|
|
||||||
"hermit-abi",
|
|
||||||
"libc",
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "bitflags"
|
|
||||||
version = "1.3.2"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cfg-if"
|
name = "cfg-if"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "clap"
|
|
||||||
version = "3.1.15"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "85a35a599b11c089a7f49105658d089b8f2cf0882993c17daf6de15285c2c35d"
|
|
||||||
dependencies = [
|
|
||||||
"atty",
|
|
||||||
"bitflags",
|
|
||||||
"clap_derive",
|
|
||||||
"clap_lex",
|
|
||||||
"indexmap",
|
|
||||||
"lazy_static",
|
|
||||||
"strsim",
|
|
||||||
"termcolor",
|
|
||||||
"textwrap",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "clap_derive"
|
|
||||||
version = "3.1.7"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a3aab4734e083b809aaf5794e14e756d1c798d2c69c7f7de7a09a2f5214993c1"
|
|
||||||
dependencies = [
|
|
||||||
"heck",
|
|
||||||
"proc-macro-error",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "clap_lex"
|
|
||||||
version = "0.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a37c35f1112dad5e6e0b1adaff798507497a18fceeb30cceb3bae7d1427b9213"
|
|
||||||
dependencies = [
|
|
||||||
"os_str_bytes",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "console"
|
name = "console"
|
||||||
version = "0.15.0"
|
version = "0.15.0"
|
||||||
@@ -108,21 +52,6 @@ version = "0.9.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
|
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "heck"
|
|
||||||
version = "0.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9"
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "hermit-abi"
|
|
||||||
version = "0.1.19"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
|
|
||||||
dependencies = [
|
|
||||||
"libc",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "indexmap"
|
name = "indexmap"
|
||||||
version = "1.6.2"
|
version = "1.6.2"
|
||||||
@@ -133,12 +62,6 @@ dependencies = [
|
|||||||
"hashbrown",
|
"hashbrown",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "lazy_static"
|
|
||||||
version = "1.4.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libc"
|
name = "libc"
|
||||||
version = "0.2.124"
|
version = "0.2.124"
|
||||||
@@ -151,12 +74,6 @@ version = "1.10.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
|
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "os_str_bytes"
|
|
||||||
version = "6.0.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "8e22443d1643a904602595ba1cd8f7d896afe56d26712531c5ff73a15b2fbf64"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "parameterized"
|
name = "parameterized"
|
||||||
version = "1.0.0"
|
version = "1.0.0"
|
||||||
@@ -184,30 +101,6 @@ version = "0.2.16"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro-error"
|
|
||||||
version = "1.0.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro-error-attr",
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"syn",
|
|
||||||
"version_check",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "proc-macro-error-attr"
|
|
||||||
version = "1.0.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
|
||||||
dependencies = [
|
|
||||||
"proc-macro2",
|
|
||||||
"quote",
|
|
||||||
"version_check",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "proc-macro2"
|
name = "proc-macro2"
|
||||||
version = "1.0.37"
|
version = "1.0.37"
|
||||||
@@ -275,18 +168,11 @@ checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
|
|||||||
name = "rusty_propagation"
|
name = "rusty_propagation"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
|
||||||
"console",
|
"console",
|
||||||
"parameterized",
|
"parameterized",
|
||||||
"rand",
|
"rand",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "strsim"
|
|
||||||
version = "0.10.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "syn"
|
name = "syn"
|
||||||
version = "1.0.92"
|
version = "1.0.92"
|
||||||
@@ -298,15 +184,6 @@ dependencies = [
|
|||||||
"unicode-xid",
|
"unicode-xid",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "termcolor"
|
|
||||||
version = "1.1.3"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "bab24d30b911b2376f3a13cc2cd443142f0c81dda04c118693e35b3835757755"
|
|
||||||
dependencies = [
|
|
||||||
"winapi-util",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "terminal_size"
|
name = "terminal_size"
|
||||||
version = "0.1.17"
|
version = "0.1.17"
|
||||||
@@ -317,12 +194,6 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "textwrap"
|
|
||||||
version = "0.15.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "b1141d4d61095b28419e22cb0bbf02755f5e54e0526f97f1e3d1d160e60885fb"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-width"
|
name = "unicode-width"
|
||||||
version = "0.1.9"
|
version = "0.1.9"
|
||||||
@@ -335,12 +206,6 @@ version = "0.2.3"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
|
checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "version_check"
|
|
||||||
version = "0.9.4"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "wasi"
|
name = "wasi"
|
||||||
version = "0.10.2+wasi-snapshot-preview1"
|
version = "0.10.2+wasi-snapshot-preview1"
|
||||||
@@ -363,15 +228,6 @@ version = "0.4.0"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "winapi-util"
|
|
||||||
version = "0.1.5"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
|
|
||||||
dependencies = [
|
|
||||||
"winapi",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "winapi-x86_64-pc-windows-gnu"
|
name = "winapi-x86_64-pc-windows-gnu"
|
||||||
version = "0.4.0"
|
version = "0.4.0"
|
||||||
|
|||||||
@@ -7,4 +7,3 @@ edition = "2021"
|
|||||||
console = "0.15.0"
|
console = "0.15.0"
|
||||||
rand = "0.8.5"
|
rand = "0.8.5"
|
||||||
parameterized = "1.0.0"
|
parameterized = "1.0.0"
|
||||||
clap = { version = "3.1.15", features = ["derive"] }
|
|
||||||
|
|||||||
+7
-30
@@ -12,34 +12,14 @@ mod prelude {
|
|||||||
}
|
}
|
||||||
|
|
||||||
use prelude::*;
|
use prelude::*;
|
||||||
use clap::Parser;
|
|
||||||
use std::time::Instant;
|
|
||||||
|
|
||||||
#[derive(Parser, Debug)]
|
|
||||||
struct Args {
|
|
||||||
/// Number of threads
|
|
||||||
#[clap(short, long, default_value_t = 1)]
|
|
||||||
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() {
|
||||||
let args = Args::parse();
|
|
||||||
|
|
||||||
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...");
|
||||||
|
|
||||||
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,args.width,args.height,disease);
|
let mut population = Population::new(20, 10, 5, 1000, 1000, disease);
|
||||||
//population.change_disease(disease);
|
//population.change_disease(disease);
|
||||||
println!("After Filling");
|
println!("After Filling");
|
||||||
//population.display();
|
//population.display();
|
||||||
@@ -47,21 +27,18 @@ 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();
|
||||||
//population.display();
|
//population.display();
|
||||||
if args.display {
|
println!(
|
||||||
println!(
|
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
||||||
"Normal: {} Infecteds: {} Immunes: {} Deads: {}",
|
stats[0], stats[1], stats[2], stats[3]
|
||||||
stats[0], stats[1], stats[2], stats[3]
|
);
|
||||||
);
|
|
||||||
}
|
|
||||||
if stats[1] == 0 {
|
if stats[1] == 0 {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
println!("Propagation finished in {} steps, {:?}", counter, now.elapsed());
|
println!("Propagation finished in {} steps", counter);
|
||||||
//population.display();
|
//population.display();
|
||||||
}
|
}
|
||||||
|
|||||||
+247
-173
@@ -1,14 +1,20 @@
|
|||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::sync::Mutex;
|
use std::sync::Mutex;
|
||||||
use std::thread;
|
|
||||||
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: Arc<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,
|
||||||
@@ -33,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);
|
||||||
@@ -58,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 {
|
||||||
@@ -69,137 +76,224 @@ impl Population {
|
|||||||
height: height,
|
height: height,
|
||||||
plague: plague,
|
plague: plague,
|
||||||
age: 0,
|
age: 0,
|
||||||
humans: Arc::new(the_humans),
|
humans: the_humans_arc,
|
||||||
size: size,
|
size: size,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// pub fn change_disease(&mut self, plague:Disease){
|
||||||
|
// self.plague = plague;
|
||||||
|
// }
|
||||||
|
|
||||||
|
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];
|
||||||
|
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
|
||||||
|
|
||||||
pub fn propagate_new(&mut self, num_threads: Option<usize>) -> [i32; 4] {
|
|
||||||
let humans = Arc::clone(&self.humans);
|
let humans = Arc::clone(&self.humans);
|
||||||
let len = humans.len();
|
for h in humans.lock().unwrap().iter() {
|
||||||
|
match h.present_state {
|
||||||
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(len);
|
State::Normal => {
|
||||||
let stats: [i32; 4] = [0, 0, 0, 0];
|
possible_infected.push(Point{ x: h.x, y: h.y});
|
||||||
|
stats[0] += 1;
|
||||||
let num_threads = num_threads.unwrap_or(1);
|
}
|
||||||
if num_threads > 48 {
|
State::Infected => {
|
||||||
panic!("too many threads")
|
people_to_check.push(Point { x: h.x, y: h.y });
|
||||||
|
stats[1] += 1;
|
||||||
|
}
|
||||||
|
State::Immune => {
|
||||||
|
stats[2] += 1;
|
||||||
|
}
|
||||||
|
State::Dead => {
|
||||||
|
stats[3] += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// for pos in &people_to_check {
|
||||||
|
for pos in people_to_check.iter() {
|
||||||
|
//people_to_check.iter().map(|pos|{
|
||||||
|
//get all the other people next to me and check if i die cure or infect
|
||||||
|
//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 });
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// number of items per batch
|
// for infected_position in people_to_infect.iter() {
|
||||||
let mut num_items = (len / num_threads) as usize;
|
// // println!("To infect: {:?}", infected_position);
|
||||||
if len % num_threads != 0 {
|
// //people_to_infect.iter().map(|infected_position|{
|
||||||
num_items += 1;
|
// 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 mut threads = vec![];
|
||||||
|
{
|
||||||
let stats_arc = Arc::new(Mutex::new(stats));
|
|
||||||
|
|
||||||
for x in 0..num_threads {
|
|
||||||
|
|
||||||
// find items to process
|
|
||||||
let lower_bound = x * num_items;
|
|
||||||
let mut upper_bound = (x + 1) * num_items;
|
|
||||||
if upper_bound > len {
|
|
||||||
upper_bound = len;
|
|
||||||
};
|
|
||||||
|
|
||||||
// borrow copies of data for thread
|
|
||||||
let humans = Arc::clone(&self.humans);
|
let humans = Arc::clone(&self.humans);
|
||||||
let stats = Arc::clone(&stats_arc);
|
let width = self.width;
|
||||||
let (width, height, infection_rate, curing_rate, death_rate) =
|
|
||||||
(self.width, self.height, self.plague.infection_rate, self.plague.curing_rate, self.plague.death_rate);
|
|
||||||
|
|
||||||
threads.push(thread::spawn(move || {
|
threads.push(thread::spawn(move || {
|
||||||
|
for infected_position in people_to_infect.iter() {
|
||||||
// no write on data
|
let infected_index = human_idx(infected_position.x, infected_position.y, width);
|
||||||
// let humans = humans;
|
humans.lock().unwrap()[infected_index].present_state = State::Infected;
|
||||||
let mut humans_n_plus_1: Vec<Human> = Vec::with_capacity(num_items);
|
}
|
||||||
|
}));
|
||||||
for idx in lower_bound..upper_bound {
|
}
|
||||||
let human = &humans[idx];
|
|
||||||
// let mut neighbors: Vec<&Human> = Vec::with_capacity(8);
|
{
|
||||||
let mut new_human = human.clone();
|
let humans = Arc::clone(&self.humans);
|
||||||
match human.present_state {
|
let width = self.width;
|
||||||
State::Normal => {
|
threads.push(thread::spawn(move || {
|
||||||
let possible = [
|
for cured_position in people_to_cure.iter() {
|
||||||
(human.x - 1, human.y - 1), (human.x, human.y - 1), (human.x + 1, human.y - 1),
|
//people_to_cure.iter().map(|cured_position|{
|
||||||
(human.x - 1, human.y) , (human.x + 1, human.y),
|
let cured_index = human_idx(cured_position.x, cured_position.y, width);
|
||||||
(human.x - 1, human.y + 1), (human.x, human.y + 1), (human.x + 1, human.y + 1),
|
if humans.lock().unwrap()[cured_index].present_state != State::Infected {
|
||||||
];
|
println!("not infected");
|
||||||
for neigh_coords in possible.iter() {
|
} else {
|
||||||
let neigh_idx = point_to_index(neigh_coords.0, neigh_coords.1, width, height);
|
humans.lock().unwrap()[cured_index].present_state = State::Immune;
|
||||||
match neigh_idx {
|
}
|
||||||
Some(x) => if humans[x].present_state == State::Infected {
|
//DEBUG
|
||||||
if roll(infection_rate) {
|
//println!("Cured someone");
|
||||||
new_human.present_state = State::Infected;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[0] += 1;
|
|
||||||
}
|
|
||||||
State::Infected => {
|
|
||||||
match die_or_cure(curing_rate, death_rate) {
|
|
||||||
Some(x) => new_human.present_state = x,
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[1] += 1;
|
|
||||||
}
|
|
||||||
State::Immune => {
|
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[2] += 1;
|
|
||||||
}
|
|
||||||
State::Dead => {
|
|
||||||
let mut stats = stats.lock().unwrap();
|
|
||||||
stats[3] += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
humans_n_plus_1.push(new_human);
|
|
||||||
}
|
}
|
||||||
(humans_n_plus_1,)
|
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
for t in threads {
|
for t in threads {
|
||||||
let mut res = t.join().unwrap();
|
t.join().unwrap();
|
||||||
humans_n_plus_1.append(&mut res.0);
|
|
||||||
// for x in 0..4 {
|
|
||||||
// stats[x] += res.1[x];
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.humans = Arc::new(humans_n_plus_1);
|
for dead_position in people_to_kill.iter() {
|
||||||
let stats = Arc::clone(&stats_arc);
|
let humans = Arc::clone(&self.humans);
|
||||||
let stats = *stats.lock().unwrap();
|
//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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// pub fn display(&mut self){
|
||||||
|
// let sprite = "#";
|
||||||
|
// print!("\n");
|
||||||
|
// for x in 0..self.width{
|
||||||
|
// for y in 0..self.height{
|
||||||
|
// let index = human_idx(x as i32,y as i32,self.width as i32);
|
||||||
|
// match self.humans[index].present_state {
|
||||||
|
// State::Normal => print!("{}",style(sprite).green()),
|
||||||
|
// State::Dead => print!("{}",style(sprite).black()),
|
||||||
|
// State::Infected => print!("{}",style(sprite).red()),
|
||||||
|
// State::Immune => print!("{}",style(sprite).blue()),
|
||||||
|
// _ => print!("{}",style(sprite).white()),
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// print!("\n");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn die_or_cure(curing_rate: i32, death_rate: i32) -> Option<State> {
|
pub fn roll(probability: i32) -> bool {
|
||||||
if roll(curing_rate) {
|
|
||||||
Some(State::Immune)
|
|
||||||
} else if roll(death_rate) {
|
|
||||||
Some(State::Dead)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn roll(probability: i32) -> bool {
|
|
||||||
if probability > 0 {
|
if probability > 0 {
|
||||||
let mut rng = rand::thread_rng();
|
let mut rng = rand::thread_rng();
|
||||||
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
|
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability
|
||||||
@@ -213,8 +307,6 @@ mod tests {
|
|||||||
use super::*;
|
use super::*;
|
||||||
use parameterized::parameterized;
|
use parameterized::parameterized;
|
||||||
|
|
||||||
const THREADS: Option<usize> = Some(4);
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Stats {
|
struct Stats {
|
||||||
normal: i32,
|
normal: i32,
|
||||||
@@ -303,14 +395,15 @@ mod tests {
|
|||||||
fn population_new() {
|
fn population_new() {
|
||||||
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, width, height, disease);
|
let population = Population::new(20, 10, 5, 5, 7, disease);
|
||||||
let humans = Arc::clone(&population.humans);
|
let humans = Arc::clone(&population.humans);
|
||||||
assert_eq!(humans.len(), 5 * 7);
|
assert_eq!(humans.lock().unwrap().len(), 5 * 7);
|
||||||
for h in humans.iter() {
|
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!(humans[idx].x, h.x, "coordinates should match");
|
assert_eq!(humans.lock().unwrap()[idx].x, h.x, "coordinates should match");
|
||||||
assert_eq!(humans[idx].y, h.y, "coordinates should match");
|
assert_eq!(humans.lock().unwrap()[idx].y, h.y, "coordinates should match");
|
||||||
}
|
}
|
||||||
|
assert_eq!(humans.lock().unwrap().len(), (width * height) as usize);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -319,8 +412,7 @@ 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);
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
let stats: Stats = humans_stats(&population.humans);
|
||||||
let stats: Stats = humans_stats(&humans);
|
|
||||||
println!("Stats: {:?}", stats);
|
println!("Stats: {:?}", stats);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@@ -338,29 +430,25 @@ 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);
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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);
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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);
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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);
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("should be dead: {:?}", stats);
|
println!("should be dead: {:?}", stats);
|
||||||
assert_eq!(stats.dead, width * height);
|
assert_eq!(stats.dead, width * height);
|
||||||
}
|
}
|
||||||
@@ -386,15 +474,13 @@ mod tests {
|
|||||||
let mut propagate_stats: [i32; 4];
|
let mut propagate_stats: [i32; 4];
|
||||||
|
|
||||||
// infect every one
|
// infect every one
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("stats after init: {:?}", stats);
|
println!("stats after init: {:?}", stats);
|
||||||
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(THREADS);
|
propagate_stats = population.propagate();
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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]);
|
||||||
assert_eq!(stats.normal, 0);
|
assert_eq!(stats.normal, 0);
|
||||||
@@ -403,9 +489,8 @@ 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(THREADS);
|
propagate_stats = population.propagate();
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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]);
|
||||||
assert_eq!(stats.normal, 0);
|
assert_eq!(stats.normal, 0);
|
||||||
@@ -423,7 +508,7 @@ mod tests {
|
|||||||
let mut propagate_stats: [i32; 4];
|
let mut propagate_stats: [i32; 4];
|
||||||
|
|
||||||
// start with normal population
|
// start with normal population
|
||||||
population.humans = Arc::new(vec![
|
population.humans = vec![
|
||||||
Human {
|
Human {
|
||||||
present_state: State::Normal,
|
present_state: State::Normal,
|
||||||
x: 0,
|
x: 0,
|
||||||
@@ -469,16 +554,14 @@ mod tests {
|
|||||||
x: 2,
|
x: 2,
|
||||||
y: 2,
|
y: 2,
|
||||||
},
|
},
|
||||||
]);
|
];
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("stats after init: {:?}", stats);
|
println!("stats after init: {:?}", stats);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
// kill every one
|
// kill every one
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
propagate_stats = population.propagate();
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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]);
|
||||||
assert_eq!(stats.normal, 0);
|
assert_eq!(stats.normal, 0);
|
||||||
@@ -487,9 +570,8 @@ 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(THREADS);
|
propagate_stats = population.propagate();
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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]);
|
||||||
assert_eq!(stats.normal, 0);
|
assert_eq!(stats.normal, 0);
|
||||||
@@ -507,7 +589,7 @@ mod tests {
|
|||||||
let mut propagate_stats: [i32; 4];
|
let mut propagate_stats: [i32; 4];
|
||||||
|
|
||||||
// start with normal population
|
// start with normal population
|
||||||
population.humans = Arc::new(vec![
|
population.humans = vec![
|
||||||
Human {
|
Human {
|
||||||
present_state: State::Normal,
|
present_state: State::Normal,
|
||||||
x: 0,
|
x: 0,
|
||||||
@@ -553,16 +635,14 @@ mod tests {
|
|||||||
x: 2,
|
x: 2,
|
||||||
y: 2,
|
y: 2,
|
||||||
},
|
},
|
||||||
]);
|
];
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("stats after init: {:?}", stats);
|
println!("stats after init: {:?}", stats);
|
||||||
assert_eq!(stats.normal, 8);
|
assert_eq!(stats.normal, 8);
|
||||||
|
|
||||||
// infect every one
|
// infect every one
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
propagate_stats = population.propagate();
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&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]);
|
||||||
@@ -572,9 +652,8 @@ mod tests {
|
|||||||
assert_eq!(stats.dead, 0);
|
assert_eq!(stats.dead, 0);
|
||||||
|
|
||||||
// cure every one
|
// cure every one
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
propagate_stats = population.propagate();
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
println!("population: {:?}", stats);
|
println!("population: {:?}", stats);
|
||||||
assert_eq!(propagate_stats, [0, 8, 1, 0]);
|
assert_eq!(propagate_stats, [0, 8, 1, 0]);
|
||||||
@@ -585,9 +664,8 @@ mod tests {
|
|||||||
|
|
||||||
// then
|
// then
|
||||||
for _x in 0..100 {
|
for _x in 0..100 {
|
||||||
propagate_stats = population.propagate_new(THREADS);
|
propagate_stats = population.propagate();
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("propate_stats: {:?}", propagate_stats);
|
println!("propate_stats: {:?}", propagate_stats);
|
||||||
println!("population: {:?}", stats);
|
println!("population: {:?}", stats);
|
||||||
assert_eq!(propagate_stats, [0, 0, 9, 0]);
|
assert_eq!(propagate_stats, [0, 0, 9, 0]);
|
||||||
@@ -606,18 +684,16 @@ mod tests {
|
|||||||
let stats_before: Stats;
|
let stats_before: Stats;
|
||||||
let stats_after: Stats;
|
let stats_after: Stats;
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
stats_before = humans_stats(&population.humans);
|
||||||
stats_before = humans_stats(&humans);
|
|
||||||
let should_be_infected = population.size as i32 * infection_start / 100;
|
let should_be_infected = population.size as i32 * infection_start / 100;
|
||||||
let tolerance = (should_be_infected as f32 * 0.20) as i32;
|
let tolerance = (should_be_infected as f32 * 0.20) as i32;
|
||||||
println!("{:?}", stats_before);
|
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 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(THREADS);
|
population.propagate();
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
stats_after = humans_stats(&population.humans);
|
||||||
stats_after = humans_stats(&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");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -637,8 +713,7 @@ 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);
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("Population after init: {:?}", stats);
|
println!("Population after init: {:?}", stats);
|
||||||
|
|
||||||
// total * proba - 20% < infected < total * proba + 20%
|
// total * proba - 20% < infected < total * proba + 20%
|
||||||
@@ -651,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(THREADS);
|
let propa_stats: [i32; 4] = population.propagate();
|
||||||
|
|
||||||
assert!(propa_stats[3] >= dead_at_start);
|
assert!(propa_stats[3] >= dead_at_start);
|
||||||
|
|
||||||
@@ -659,8 +734,7 @@ mod tests {
|
|||||||
assert_eq!(propa_stats[3], 0, "no human should have died");
|
assert_eq!(propa_stats[3], 0, "no human should have died");
|
||||||
}
|
}
|
||||||
|
|
||||||
let humans = Arc::clone(&population.humans);
|
stats = humans_stats(&population.humans);
|
||||||
stats = humans_stats(&humans);
|
|
||||||
println!("Population after propagate: {:?}", stats);
|
println!("Population after propagate: {:?}", stats);
|
||||||
|
|
||||||
assert!(stats.infected <= infected_at_start + width * height * infected_expected);
|
assert!(stats.infected <= infected_at_start + width * height * infected_expected);
|
||||||
|
|||||||
Reference in New Issue
Block a user