3 Commits

Author SHA1 Message Date
Maxluli bc13c48302 Ready to compare with C# 2022-05-03 14:47:57 +02:00
Maxluli ced3a93412 Release V1.0 2022-05-03 14:46:23 +02:00
Maxluli 3fe4ddc2ee Fixed bug appeared in the last merge 2022-05-03 14:36:18 +02:00
7 changed files with 175 additions and 548 deletions
Generated
-79
View File
@@ -2,12 +2,6 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "cfg-if"
version = "1.0.0"
@@ -46,22 +40,6 @@ dependencies = [
"wasi",
]
[[package]]
name = "hashbrown"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04"
[[package]]
name = "indexmap"
version = "1.6.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3"
dependencies = [
"autocfg",
"hashbrown",
]
[[package]]
name = "libc"
version = "0.2.124"
@@ -74,51 +52,12 @@ version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
[[package]]
name = "parameterized"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33543d17fe882d6d8835b193d2c7f0fdc3317645ca7510aaaf6103848498381c"
dependencies = [
"parameterized-macro",
]
[[package]]
name = "parameterized-macro"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24792407b50d07456abc531c1bc629a642a005d3b292c97dc23bf5bff1ad9cea"
dependencies = [
"indexmap",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "ppv-lite86"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eb9f9e6e233e5c4a35559a617bf40a4ec447db2e84c20b55a6f83167b7e57872"
[[package]]
name = "proc-macro2"
version = "1.0.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec757218438d5fda206afc041538b2f6d889286160d649a86a24d37e1235afd1"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1feb54ed693b93a84e14094943b84b7c4eae204c512b7ccb95ab0c66d278ad1"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rand"
version = "0.8.5"
@@ -169,21 +108,9 @@ name = "rusty_propagation"
version = "0.1.0"
dependencies = [
"console",
"parameterized",
"rand",
]
[[package]]
name = "syn"
version = "1.0.92"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ff7c592601f11445996a06f8ad0c27f094a58857c2f89e97974ab9235b92c52"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "terminal_size"
version = "0.1.17"
@@ -200,12 +127,6 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ed742d4ea2bd1176e236172c8429aaf54486e7ac098db29ffe6529e0ce50973"
[[package]]
name = "unicode-xid"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "957e51f3646910546462e67d5f7599b9e4fb8acdd304b087a6494730f9eebf04"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
-1
View File
@@ -6,4 +6,3 @@ edition = "2021"
[dependencies]
console = "0.15.0"
rand = "0.8.5"
parameterized = "1.0.0"
+9
View File
@@ -0,0 +1,9 @@
# Rusty Propagation
Run :
```
cargo run -q
```
Speed run :
```
cargo run -q --release
```
+7 -6
View File
@@ -1,18 +1,19 @@
// use crate::prelude::*;
// pub const MUTATION_TRAIT_INCREASE_PROBABILITY:i32 = 50;
// pub const MUTATION_TRAIT_CHANGE_AMOUNT:i32 = 20;
// #[derive(Debug)]
#[derive(Debug)]
pub struct Disease {
pub infection_rate: i32,
pub curing_rate: i32,
pub death_rate: i32,
pub traits: Vec<i32>,
pub infection_rate:u32,
pub curing_rate:u32,
pub death_rate:u32,
pub traits: Vec<u32>,
pub name: String,
}
impl Disease{
pub fn new(infection_r: i32, curing_r: i32, death_r: i32, the_name: String) -> Self {
pub fn new(infection_r:u32,curing_r:u32,death_r:u32,the_name:String) -> Self{
Self{
infection_rate : infection_r,
curing_rate : curing_r,
+1 -1
View File
@@ -20,7 +20,7 @@ impl Human {
Self{
present_state : State::Normal,
x : pos_x,
y: pos_y,
y : pos_y
}
}
// pub fn new_empty() -> Self{
+11 -16
View File
@@ -1,13 +1,13 @@
mod disease;
mod human;
mod disease;
mod population;
mod prelude {
pub use crate::disease::*;
pub use crate::human::*;
pub use crate::disease::*;
pub use crate::population::*;
pub use console::style;
pub use console::Term;
pub use rand::Rng;
pub use console::Term;
pub use console::style;
pub const CORRECTED_PERCENTAGE:i32 = 101;
}
@@ -15,13 +15,12 @@ use prelude::*;
fn main() {
let term = Term::stdout();
term.write_line("********** Rusty Propagation (Console) 2022 **********")
.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...");
term.write_line("********** Rusty Propagation (Console) 2022 **********").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 mut population = Population::new(20, 10, 5, 300, 300, disease);
let mut population = Population::new(20,10,5,1000,1000,disease);
//population.change_disease(disease);
println!("Before Filling");
//population.display();
@@ -36,14 +35,10 @@ fn main() {
counter += 1;
stats=population.propagate();
//population.display();
println!(
"Infecteds: {} Immunes: {} Deads: {}",
stats[1], stats[2], stats[3]
);
if stats[1] == 0 {
break;
}
println!("Infecteds: {} Immunes: {} Deads: {}",stats[1],stats[2],stats[3]);
if stats[1] == 0 {break;}
}
println!("Propagation finished in {} steps",counter);
//population.display();
}
+75 -373
View File
@@ -1,15 +1,15 @@
use crate::prelude::*;
// #[derive(Debug)]
#[derive(Debug)]
pub struct Point{
x:i32,
y:i32,
}
pub struct Population{
pub start_infected_ratio: i32,
pub start_immune_ratio: i32,
pub start_dead_ratio: i32,
pub start_infected_ratio:u32,
pub start_immune_ratio:u32,
pub start_dead_ratio:u32,
pub humans:Vec<Human>,
pub width:i32,
pub height:i32,
@@ -22,14 +22,7 @@ pub fn human_idx(x: i32, y: i32, width: i32) -> usize {
}
impl Population{
pub fn new(
start_infected_ratio: i32,
start_immune_ratio: i32,
start_dead_ratio: i32,
width: i32,
height: i32,
plague: Disease,
) -> Self {
pub fn new(start_infected_ratio:u32,start_immune_ratio:u32,start_dead_ratio:u32,width:i32,height:i32,plague:Disease)->Self{
let mut the_humans: Vec<Human> = Vec::with_capacity((width*height) as usize);
for x in 0..width{
for y in 0..height{
@@ -57,67 +50,67 @@ impl Population {
//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();
let mut i: i32 = 0;
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)
if 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
}
if 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
}
if rng.gen_range(0..CORRECTED_PERCENTAGE) < self.start_dead_ratio as i32
{
x.present_state = State::Dead;
}
i += 1;
}
println!("generate for {} humans", i);
}
pub fn propagate(&mut self)->[i32;4]{
let mut people_to_check: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_infect: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_cure: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_kill: Vec<Point> =
Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_check:Vec<Point> = Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_infect:Vec<Point> = Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_cure:Vec<Point> = Vec::with_capacity((self.width * self.height) as usize);
let mut people_to_kill:Vec<Point> = Vec::with_capacity((self.width * self.height) as usize);
let mut stats: [i32;4] = [0,0,0,0];
// stats[0] Normal stats[1] Infected stats[2] Immune stats[3] Dead
/*
for h in self.humans.iter() {
match h.present_state{
State::Normal => {
stats[0] += 1;
State::Normal => {stats[0] += 1;}
State::Infected => {people_to_check.push(Point{x:h.x,y:h.y});stats[1]+=1;}
State::Immune => {stats[2] += 1;}
State::Dead => {stats[3] += 1;}
}
State::Infected => {
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 x in 0..self.width{
for y in 0..self.height{
let idx = human_idx(x as i32, y as i32, self.width as i32);
match self.humans[idx].present_state{
State::Normal => {stats[0] += 1;}
State::Infected => {people_to_check.push(Point{x:x,y:y});stats[1]+=1;}
State::Immune => {stats[2] += 1;}
State::Dead => {stats[3] += 1;}
}
}
}
println!("{} people to check, death rate {}", people_to_check.len(), self.plague.death_rate);
//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 pos.x >= 0 && pos.x <= self.width - 1 && pos.y >= 0 && pos.y <= self.height - 1 {
if roll(self.plague.curing_rate) {
//checks if the man recovers
if pos.x > 0 && pos.x < self.width - 1 && pos.y > 0 && pos.y < self.height -1 {
if self.roll(self.plague.curing_rate){
//checks if the man dies
people_to_cure.push(Point{x:pos.x,y:pos.y});
}else{
if roll(self.plague.death_rate) {
if self.roll(self.plague.death_rate){
//cheks if the man dies
people_to_kill.push(Point{x:pos.x,y:pos.y});
}else{
@@ -125,67 +118,26 @@ impl Population {
// Vec::new();
//possible_infections.push(Point{x:pos.x,y:pos.y});
if pos.x > 0 && pos.y > 0 {
possible_infections.push(Point {
x: pos.x - 1,
y: pos.y - 1,
}); //Top Left
}
if pos.y > 0 {
possible_infections.push(Point {
x: pos.x,
y: pos.y - 1,
}); //Top
}
if pos.y > 0 && pos.x < self.width - 1 {
possible_infections.push(Point {
x: pos.x + 1,
y: pos.y - 1,
}); //Top Right
}
if pos.x > 0 {
possible_infections.push(Point {
x: pos.x - 1,
y: pos.y,
}); //Left
}
if pos.x < self.width - 1{
possible_infections.push(Point {
x: pos.x + 1,
y: pos.y,
}); //Right
}
if pos.x > 0 && pos.y < self.height -1 {
possible_infections.push(Point {
x: pos.x - 1,
y: pos.y + 1,
}); //Bottom Left
}
if pos.y < self.height - 1 {
possible_infections.push(Point {
x: pos.x,
y: pos.y + 1,
}); //Bottom
}
if pos.x < self.width - 1 && pos.y < self.height - 1{
possible_infections.push(Point {
x: pos.x + 1,
y: pos.y + 1,
}); //Bottom Right
}
possible_infections.push(Point{x:pos.x -1,y:pos.y -1}); //Top Left
possible_infections.push(Point{x:pos.x,y:pos.y-1}); //Top
possible_infections.push(Point{x:pos.x +1,y:pos.y -1}); //Top Right
possible_infections.push(Point{x:pos.x -1,y:pos.y}); //Left
possible_infections.push(Point{x:pos.x +1,y:pos.y}); //Right
possible_infections.push(Point{x:pos.x -1,y:pos.y + 1}); //Bottom Left
possible_infections.push(Point{x:pos.x,y:pos.y + 1}); //Bottom
possible_infections.push(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);
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,
});
}
if self.roll(self.plague.infection_rate){
people_to_infect.push(Point{x:poss_infected_pos.x,y:poss_infected_pos.y});
}
}
};
}
}
}else{
@@ -194,7 +146,7 @@ impl Population {
//REMOVE WHEN IMPLEMENTED
//It is here to prevent an infected in the borders to keep the programm running as he thinks there are still people to simulate
//people_to_kill.push(Point { x: pos.x, y: pos.y });
people_to_kill.push(Point{x:pos.x,y:pos.y});
// @##
// ###
@@ -228,13 +180,7 @@ impl Population {
// ###
// ##@
}
}
println!(
"{} to infect, {} to cure, {} to kill",
people_to_infect.len(),
people_to_cure.len(),
people_to_kill.len()
);
};
for infected_position in &people_to_infect {
// println!("To infect: {:?}", infected_position);
//people_to_infect.iter().map(|infected_position|{
@@ -245,7 +191,7 @@ impl Population {
self.humans[infected_index].present_state = State::Infected;
//DEBUG
//println!("Infected someone");
}
};
for cured_position in &people_to_cure {
//people_to_cure.iter().map(|cured_position|{
@@ -253,281 +199,37 @@ impl Population {
self.humans[cured_index].present_state = State::Immune;
//DEBUG
//println!("Cured someone");
}
};
for dead_position in &people_to_kill {
//people_to_kill.iter().map(|dead_position|{
let dead_index = human_idx(dead_position.x, dead_position.y, self.width);
if self.humans[dead_index].present_state == State::Dead {
// println!("Already dead");
} else {
self.humans[dead_index].present_state = State::Dead;
}
//DEBUG
}
assert_eq!(stats[0] + stats[1] + stats[2] + stats[3], self.humans.len() as i32);
//println!("Killed someone");
};
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");
// }
// }
}
pub fn roll(probability: i32) -> bool {
if probability > 0 {
pub fn roll(&self,probability:u32)->bool{
let mut rng = rand::thread_rng();
rng.gen_range(0 as i32..CORRECTED_PERCENTAGE) <= probability
} else {
false
rng.gen_range(0..CORRECTED_PERCENTAGE) <= probability as i32
}
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()),
}
}
#[cfg(test)]
mod tests {
use super::*;
use parameterized::parameterized;
#[derive(Debug)]
struct Stats {
normal: i32,
infected: i32,
immune: i32,
dead: i32,
}
impl Stats {
fn new() -> Stats {
Stats {
normal: 0,
infected: 0,
immune: 0,
dead: 0,
}
}
}
fn humans_stats(humans: &Vec<Human>) -> Stats {
let mut stats: Stats = Stats::new();
for human in humans.iter() {
match human.present_state {
State::Normal => {
stats.normal += 1;
}
State::Infected => {
stats.infected += 1;
}
State::Immune => {
stats.immune += 1;
}
State::Dead => {
stats.dead += 1;
}
}
}
stats
}
#[parameterized(x = {
2, 3, 5
}, y = {
1, 4, 0
}, width = {
3, 5, 7
}, res = {
5, 23, 5
})]
fn test_human_idx(x: i32, y: i32, width: i32, res: usize) {
assert_eq!(human_idx(x, y, width), res);
}
#[test]
fn test_human_stats() {
let mut humans: Vec<Human> = Vec::with_capacity(10);
let mut stats: Stats;
for _ in 0..10 {
humans.push(Human::new(0, 0));
}
stats = humans_stats(&humans);
assert_eq!(stats.normal, 10);
for x in 0..2 {
humans[x].present_state = State::Infected;
}
for x in 2..5 {
humans[x].present_state = State::Immune;
}
for x in 5..9 {
humans[x].present_state = State::Dead;
}
stats = humans_stats(&humans);
assert_eq!(stats.normal, 1);
assert_eq!(stats.infected, 2);
assert_eq!(stats.immune, 3);
assert_eq!(stats.dead, 4);
}
#[test]
fn population_new() {
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let population = Population::new(20, 10, 5, 5, 7, disease);
assert_eq!(population.humans.len(), 5 * 7);
for human in population.humans.iter() {
assert!(
human.present_state == State::Normal,
"all humans should be normal"
);
}
}
#[test]
fn population_gen() {
let disease = Disease::new(20, 10, 5, String::from("Covid 44"));
let (width, height) = (5, 7);
let mut population = Population::new(20, 10, 5, 5, 7, disease);
population.generate();
let stats: Stats = humans_stats(&population.humans);
println!("Stats: {:?}", stats);
assert_eq!(
stats.normal + stats.infected + stats.immune + stats.dead,
width * height
);
}
#[test]
fn plague_init_stats() {
let mut disease: Disease;
let mut population: Population;
let mut stats: Stats;
let (width, height) = (5, 7);
disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(0, 0, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans);
println!("should be normal: {:?}", stats);
assert_eq!(stats.normal, width * height);
disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(100, 0, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans);
println!("should be infected: {:?}", stats);
assert_eq!(stats.infected, width * height);
disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(0, 100, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans);
println!("should be immune: {:?}", stats);
assert_eq!(stats.immune, width * height);
disease = Disease::new(0, 0, 0, String::from("Test"));
population = Population::new(0, 0, 100, width, height, disease);
population.generate();
stats = humans_stats(&population.humans);
println!("should be dead: {:?}", stats);
assert_eq!(stats.dead, width * height);
}
#[parameterized(rate = {0, 100}, expected = {false, true})]
fn roll_test(rate: i32, expected: bool) {
let tries = 100000;
let mut result = 0;
println!("Testing roll, rate {}, expected {}", rate, expected);
for _x in 0..tries {
if roll(rate) == expected {
result += 1;
}
}
assert_eq!(result, tries);
}
#[test]
fn propagate_simple() {
let disease: Disease = Disease::new(0, 0, 100, String::from("Deadly"));
let mut population: Population = Population::new(100, 0, 0, 10, 10, disease);
let mut stats: Stats;
stats = humans_stats(&population.humans);
println!("stats after init: {:?}", stats);
assert_eq!(stats.normal, 100);
population.generate();
stats = humans_stats(&population.humans);
println!("stats after generate: {:?}", stats);
assert_eq!(stats.infected, 100);
}
#[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) {
let disease: Disease;
let mut population: Population;
let mut stats: Stats;
let (width, height) = (100, 100);
let start_infected = 50;
println!(
"infection rate: {}, death_rate: {}",
infection_rate, death_rate
);
disease = Disease::new(infection_rate, 0, death_rate, String::from("Test"));
population = Population::new(start_infected, 0, 0, width, height, disease);
population.generate();
stats = humans_stats(&population.humans);
println!("Population after generate: {:?}", stats);
// total * proba - 20% < infected < total * proba + 20%
let infected_at_start_proba = width * height * start_infected / 100;
let infected_tolerance = ((width * height) as f32 * 0.2) as i32;
assert!(stats.infected <= infected_at_start_proba + infected_tolerance);
assert!(stats.infected >= infected_at_start_proba - infected_tolerance);
assert_eq!(stats.dead, 0);
for _x in 0..100 {
let infected_at_start = stats.infected;
let dead_at_start = stats.dead;
let propa_stats: [i32; 4] = population.propagate();
assert!(propa_stats[3] >= dead_at_start);
if death_rate == 0 {
assert_eq!(propa_stats[3], 0, "no human should have died");
}
stats = humans_stats(&population.humans);
println!("Population after propagate: {:?}", stats);
assert!(stats.normal <= infected_at_start + width * height * infected_expected);
let should_be_dead = infected_at_start * death_rate / 100;
let dead_tolerance = (should_be_dead as f32 * 0.20) as i32;
assert!(
stats.dead <= should_be_dead + dead_tolerance,
"death count should be less or equal than {}",
should_be_dead + dead_tolerance
);
assert!(stats.dead >= should_be_dead - dead_tolerance);
print!("\n");
}
}
}