diff --git a/Cargo.lock b/Cargo.lock index 439f7e8..a8b07fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,147 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "getrandom" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "899def5c37c4fd7b2664648c28120ecec138e4d395b459e5ca34f9cce2dd77fd" +dependencies = [ + "cfg-if", + "libc", + "r-efi", + "wasip2", +] + +[[package]] +name = "libc" +version = "0.2.180" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" + [[package]] name = "mankalla-rl" version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "ppv-lite86" +version = "0.2.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85eae3c4ed2f50dcfe72643da4befc30deadb458a9b590d720cde2f2b1e97da9" +dependencies = [ + "zerocopy", +] + +[[package]] +name = "proc-macro2" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "r-efi" +version = "5.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69cdb34c158ceb288df11e18b4bd39de994f6657d83847bdffdbd7f346754b0f" + +[[package]] +name = "rand" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6db2770f06117d490610c7488547d543617b21bfa07796d7a12f6f1bd53850d1" +dependencies = [ + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "99d9a13982dcf210057a8a78572b2217b667c3beacbf3a0d8b454f6f82837d38" +dependencies = [ + "getrandom", +] + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "wasip2" +version = "1.0.1+wasi-0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0562428422c63773dad2c345a1882263bbf4d65cf3f42e90921f787ef5ad58e7" +dependencies = [ + "wit-bindgen", +] + +[[package]] +name = "wit-bindgen" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f17a85883d4e6d00e8a97c586de764dabcc06133f7f1d55dce5cdc070ad7fe59" + +[[package]] +name = "zerocopy" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "668f5168d10b9ee831de31933dc111a459c97ec93225beb307aed970d1372dfd" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.8.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2c7962b26b0a8685668b671ee4b54d007a67d4eaf05fda79ac0ecf41e32270f1" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] diff --git a/Cargo.toml b/Cargo.toml index de4b975..8f10a2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,4 @@ edition = "2024" name = "mankalla_rl" [dependencies] +rand = "0.9.2" diff --git a/src/q_learning.rs b/src/q_learning.rs index e654c25..02718ef 100644 --- a/src/q_learning.rs +++ b/src/q_learning.rs @@ -1,6 +1,8 @@ use std::collections::HashMap; use std::hash::Hash; +use rand::seq::IndexedRandom; + pub trait Environment { type State: Copy + Eq + Hash; type Action: Copy + Eq + Hash; @@ -18,6 +20,7 @@ pub trait Policy { reward: f32, next_state: Option<&E::State>, ); + fn on_episode_increment(&mut self) {} } pub struct QLearning; @@ -29,29 +32,34 @@ impl QLearning { max_steps: Option, ) { for _ in 0..num_training_episodes { - let mut state = E::new(); + QLearning::one_episode(policy, max_steps); + policy.on_episode_increment(); + } + } - if let Some(m) = max_steps { - for _ in 0..m { - if let Some(next_state) = QLearning::one_iteration(policy, state) { - state = next_state; - } else { - break; - } + fn one_episode(policy: &mut impl Policy, max_steps: Option) { + let mut state = E::new(); + + if let Some(m) = max_steps { + for _ in 0..m { + if let Some(next_state) = QLearning::choose_and_improve(policy, state) { + state = next_state; + } else { + break; } - } else { - loop { - if let Some(next_state) = QLearning::one_iteration(policy, state) { - state = next_state; - } else { - break; - } + } + } else { + loop { + if let Some(next_state) = QLearning::choose_and_improve(policy, state) { + state = next_state; + } else { + break; } } } } - fn one_iteration( + fn choose_and_improve( policy: &mut impl Policy, state: E::State, ) -> Option { @@ -97,7 +105,7 @@ impl Policy for GreedyPolicy { + self.gamma * self .qtable - .get(&(*next_state, self.choose_action(&next_state))) + .get(&(*next_state, self.choose_action(next_state))) .unwrap_or(&0f32) } None => 0f32, @@ -110,8 +118,46 @@ impl Policy for GreedyPolicy { } pub struct EpsilonGreedyPolicy { - qtable: HashMap<(E::State, E::Action), f32>, - learning_rate: f32, - gamma: f32, - epsilon: f32, + greedy_policy: GreedyPolicy, + min_epsilon: f32, + max_epsilon: f32, + decay_rate: f32, + episode: usize, +} + +impl EpsilonGreedyPolicy { + fn epsilon(&self) -> f32 { + self.min_epsilon + + (self.max_epsilon - self.min_epsilon) * (-self.decay_rate * self.episode as f32).exp() + } +} + +impl Policy for EpsilonGreedyPolicy { + fn choose_action(&self, state: &::State) -> ::Action { + let action: E::Action; + if rand::random_range(0f32..1f32) < self.epsilon() { + action = *E::actions(state).choose(&mut rand::rng()).expect( + "The way it is implemented now, there should always be possible actions (might be bad)", + ); + } else { + action = self.greedy_policy.choose_action(state); + } + + action + } + + fn improve( + &mut self, + state: &::State, + action: ::Action, + reward: f32, + next_state: Option<&::State>, + ) { + self.greedy_policy + .improve(state, action, reward, next_state); + } + + fn on_episode_increment(&mut self) { + self.episode += 1; + } }