diff --git a/simulator/Cargo.lock b/simulator/Cargo.lock index 4963fbb..1f2381e 100644 --- a/simulator/Cargo.lock +++ b/simulator/Cargo.lock @@ -14,6 +14,56 @@ version = "0.2.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923" +[[package]] +name = "anstream" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824a212faf96e9acacdbd09febd34438f8f711fb84e09a8916013cd7815ca28d" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "940b3a0ca603d1eade50a4846a2afffd5ef57a9feac2c0e2ec2e14f9ead76000" + +[[package]] +name = "anstyle-parse" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52ce7f38b242319f7cabaa6813055467063ecdc9d355bbb4ce0c68908cd8130e" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + [[package]] name = "approx" version = "0.5.1" @@ -76,12 +126,58 @@ dependencies = [ "rand_core", ] +[[package]] +name = "clap" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa8876b300ab35ba921adea3dfd70157a46249b33f95c9084ae5709785478946" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0797fb7aeb1406c84efac526901f7ec3ead2124f946b494e72879d4b54704d" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f9c751b79415d4e559e3d1fcf128e09e720eb673a06d26cf6f392d37d75b66e0" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1c133bc6a41be0d194c306b5506d15e6feeea7b1d6604bd3f8310dfb2ca96486" + [[package]] name = "color_quant" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" +[[package]] +name = "colorchoice" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" + [[package]] name = "core_maths" version = "0.1.1" @@ -109,6 +205,31 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "622f3fc73690be383c7214310406f28a90e6edeadc3cea882f9d71e495b9711a" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74980687109a3b14c72fd458107bf0baa1da1a1a805e178d15501ba9b86d9d" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a31eee39dddec8330830986fcd7625edb5a24ec90ea038215273bbc3adb08ac6" + [[package]] name = "downcast-rs" version = "2.0.2" @@ -255,6 +376,12 @@ dependencies = [ "foldhash 0.2.0", ] +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + [[package]] name = "image" version = "0.24.9" @@ -268,6 +395,12 @@ dependencies = [ "png", ] +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + [[package]] name = "libc" version = "0.2.191" @@ -392,6 +525,12 @@ dependencies = [ "malloc_buf", ] +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + [[package]] name = "ordered-float" version = "5.5.1" @@ -487,6 +626,26 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" +[[package]] +name = "rayon" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fb39b166781f92d482534ef4b4b1b2568f42613b53e5b6c160e24cfbfa30926d" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e18b0f0062d30d4230b2e85ff77fdfe4326feb054b9783a3460d8435c8ab91" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "robust" version = "1.2.0" @@ -524,10 +683,12 @@ checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea" name = "simulator" version = "0.1.0" dependencies = [ + "clap", "glam 0.34.3", "macroquad", "parry2d", "rand", + "rayon", ] [[package]] @@ -554,6 +715,12 @@ dependencies = [ "smallvec", ] +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + [[package]] name = "syn" version = "3.0.9" @@ -600,6 +767,12 @@ version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563" +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + [[package]] name = "wide" version = "1.7.1" @@ -632,6 +805,21 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link", +] + [[package]] name = "zlib-rs" version = "0.6.8" diff --git a/simulator/Cargo.toml b/simulator/Cargo.toml index 0e52bd6..afa3347 100644 --- a/simulator/Cargo.toml +++ b/simulator/Cargo.toml @@ -4,7 +4,9 @@ version = "0.1.0" edition = "2024" [dependencies] +clap = { version = "4.6.7", features = ["derive"] } glam = "0.34.3" macroquad = "0.4.16" parry2d = "0.31.1" rand_gen = { package = "rand", version = "0.10.3" } +rayon = "1.12.0" diff --git a/simulator/src/main.rs b/simulator/src/main.rs index 551eaa7..4ea0295 100644 --- a/simulator/src/main.rs +++ b/simulator/src/main.rs @@ -20,6 +20,10 @@ use rand_gen::SeedableRng; use rand_gen::distr::Distribution; use rand_gen::distr::Uniform; use rand_gen::rngs::SmallRng; +use rayon::iter::IntoParallelIterator; +use rayon::iter::ParallelIterator; + +use clap::Parser; const VIEW_DIST: f32 = 180.0; const BOT_RADIUS: f32 = 16.0; @@ -326,7 +330,7 @@ async fn run_simulation(seed: u64) -> (i32, i32) { } } -async fn run_headless_simulation(seed: u64) -> (i32, i32) { +fn run_headless_simulation(seed: u64) -> (i32, i32) { let mut sim = Simulation::new(seed); loop { @@ -337,24 +341,47 @@ async fn run_headless_simulation(seed: u64) -> (i32, i32) { } +#[derive(Parser)] +#[command(version, about, long_about = None)] +struct Cli { + /// run headless, allows multithreading + #[arg(short, long)] + no_gui: bool, + + /// number of samples + #[arg(short, long)] + run_count: Option, + + ///// Sets a custom config file + //#[arg(short, long, value_name = "FILE")] + //config: Option, + + /// Turn debugging information on + #[arg(short, long, action = clap::ArgAction::Count)] + debug: u8, +} #[macroquad::main("Adversarial Foraging Simulator")] async fn main() { + let cli = Cli::parse(); + let iters = cli.run_count.unwrap_or(1); + + let (red, blu) = if cli.no_gui { + (0..iters).into_par_iter().map(run_headless_simulation).reduce(|| (0,0), |a,b| (a.0 + b.0, a.1 + b.1)) + } else { + let (mut rtot, mut btot) = (0, 0); + for i in 0..iters { + let (red, blu) = run_simulation(i).await; + rtot += red; + btot += blu; - let mut rtot = 0; - let mut btot = 0; - for i in 0..100 { + println!("{} wins! RED: {red}, BLU: {blu}", if red > blu {"red"} else {"blue"}); + } + (rtot,btot) + }; - let (red, blu) = run_headless_simulation(i).await; - - rtot += red; - btot += blu; - - println!("{} wins! RED: {red}, BLU: {blu}", if red > blu {"red"} else {"blue"}); - } - - let red = rtot / 100; - let blu = btot / 100; + let red = red as f32 / iters as f32; + let blu = blu as f32 / iters as f32; println!("{} wins on average! RED: {red}, BLU: {blu}", if red > blu {"red"} else {"blue"}); }