From 83d5eb9ad7c285142e6b2e8ac6684f953b7b202e Mon Sep 17 00:00:00 2001 From: Wojciech Janota Date: Fri, 10 Nov 2023 23:20:47 +0100 Subject: [PATCH] First --- longinus/.idea/.gitignore | 8 ++++ longinus/.idea/longinus.iml | 13 ++++++ longinus/.idea/modules.xml | 8 ++++ longinus/.idea/vcs.xml | 6 +++ longinus/Cargo.toml | 9 ++++ longinus/helpers/Cargo.toml | 9 ++++ longinus/helpers/src/lib.rs | 91 +++++++++++++++++++++++++++++++++++++ longinus/src/main.rs | 3 ++ 8 files changed, 147 insertions(+) create mode 100644 longinus/.idea/.gitignore create mode 100644 longinus/.idea/longinus.iml create mode 100644 longinus/.idea/modules.xml create mode 100644 longinus/.idea/vcs.xml create mode 100644 longinus/Cargo.toml create mode 100644 longinus/helpers/Cargo.toml create mode 100644 longinus/helpers/src/lib.rs create mode 100644 longinus/src/main.rs diff --git a/longinus/.idea/.gitignore b/longinus/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/longinus/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/longinus/.idea/longinus.iml b/longinus/.idea/longinus.iml new file mode 100644 index 0000000..1f554fc --- /dev/null +++ b/longinus/.idea/longinus.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/longinus/.idea/modules.xml b/longinus/.idea/modules.xml new file mode 100644 index 0000000..11bf7b2 --- /dev/null +++ b/longinus/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/longinus/.idea/vcs.xml b/longinus/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/longinus/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/longinus/Cargo.toml b/longinus/Cargo.toml new file mode 100644 index 0000000..d09d166 --- /dev/null +++ b/longinus/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "longinus" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +helpers = { path = "helpers" } \ No newline at end of file diff --git a/longinus/helpers/Cargo.toml b/longinus/helpers/Cargo.toml new file mode 100644 index 0000000..b25aa7a --- /dev/null +++ b/longinus/helpers/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "helpers" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +num-traits = "0.2.17" \ No newline at end of file diff --git a/longinus/helpers/src/lib.rs b/longinus/helpers/src/lib.rs new file mode 100644 index 0000000..4e3305c --- /dev/null +++ b/longinus/helpers/src/lib.rs @@ -0,0 +1,91 @@ +mod min_max_stats { + use std::f64; + + #[derive(Debug)] + #[derive(PartialEq)] + pub struct MinMaxStats { + pub(crate) maximum: f64, + pub(crate) minimum: f64 + } + + pub struct KnownBounds { + pub(crate) maximum: f64, + pub(crate) minimum: f64 + } + + pub fn initialize(known_bounds: Option) -> MinMaxStats { + let f64_max: f64 = f64::MAX; + //let default_known_bounds: KnownBounds = KnownBounds {maximum: - f64_max, minimum: f64_max }; + let int_known_bounds: KnownBounds = known_bounds.unwrap_or(KnownBounds {maximum: -f64_max, minimum: f64_max} ); + let min_max_stats: MinMaxStats = MinMaxStats {maximum: int_known_bounds.maximum, minimum: int_known_bounds.minimum}; + return min_max_stats; + } + + pub fn normalize(min_max_stats_obj: MinMaxStats, value: Option) -> Option { + if min_max_stats_obj.maximum > min_max_stats_obj.minimum { + if value.is_none(){ + return None; + } + else { + let result: Option = Some((value.unwrap() - min_max_stats_obj.minimum) / (min_max_stats_obj.maximum - min_max_stats_obj.minimum)); + return result; + } + } + else { + return None; + } + } +} + +mod config{ + use crate::min_max_stats; + + pub struct MuZeroConfig { + pub(crate) action_space_size: i64, + pub(crate) observation_space_size: i64, + pub(crate) max_moves: i64, + pub(crate) discount: f64, + pub(crate) dirichlet_alpha_value: f64, + pub(crate) simulations_count: i64, + pub(crate) batch_size: i64, + pub(crate) td_steps: i64, + pub(crate) actors_count: i64, + pub(crate) lr_init: i64, + pub(crate) lr_decay_steps: i64, + pub(crate) training_episodes: i64, + pub(crate) hidden_layer_size: i64, + pub(crate) visit_softmax_temp_fn: i64, + pub(crate) known_bounds: min_max_stats::KnownBounds + } +} + +#[cfg(test)] +mod tests { + use crate::min_max_stats::KnownBounds; + use super::*; + + #[test] + fn initialize_min_max_stats_no_args() { + let result: min_max_stats::MinMaxStats = min_max_stats::initialize(None); + let test: min_max_stats::MinMaxStats = min_max_stats::MinMaxStats {minimum: f64::MAX, maximum: -f64::MAX}; + assert_eq!(test, result); + } + + #[test] + fn initialize_min_max_stats_bounds_args() { + let bounds: min_max_stats::KnownBounds = KnownBounds {minimum: 12.0, maximum: 80.0}; + let result: min_max_stats::MinMaxStats = min_max_stats::initialize(Some(bounds)); + let test: min_max_stats::MinMaxStats = min_max_stats::MinMaxStats {minimum: 12.0, maximum: 80.0}; + assert_eq!(test, result); + } + + #[test] + fn min_max_stats_normalize() { + let bounds: min_max_stats::KnownBounds = KnownBounds {minimum: 12.0, maximum: 80.0}; + let min_max_stats_obj: min_max_stats::MinMaxStats = min_max_stats::initialize(Some(bounds)); + let value: f64 = 30.0; + let value_normalized: f64 = min_max_stats::normalize(min_max_stats_obj, Some(value)).unwrap_or(0.0); + let test: f64 = (30.0 - 12.0) / (80.0 - 12.0); + assert_eq!(test, value_normalized); + } +} diff --git a/longinus/src/main.rs b/longinus/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/longinus/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +}