feat(q_learning): finalize EpsilonGreedyPolicy

This commit is contained in:
leonhard committed 2026-01-09 15:34:45 +01:00
1 parent e65b826714
commit dd614085e7
3 files changed
+209 -21

No files matched your search

Generated
+141
View File
@@ -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",
]
+1
View File
@@ -7,3 +7,4 @@ edition = "2024"
name = "mankalla_rl"
[dependencies]
rand = "0.9.2"
+67 -21
View File
@@ -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<E: Environment> {
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<usize>,
) {
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<E: Environment>(policy: &mut impl Policy<E>, max_steps: Option<usize>) {
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<E: Environment>(
fn choose_and_improve<E: Environment>(
policy: &mut impl Policy<E>,
state: E::State,
) -> Option<E::State> {
@@ -97,7 +105,7 @@ impl<E: Environment> Policy<E> for GreedyPolicy<E> {
+ 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<E: Environment> Policy<E> for GreedyPolicy<E> {
}
pub struct EpsilonGreedyPolicy<E: Environment> {
qtable: HashMap<(E::State, E::Action), f32>,
learning_rate: f32,
gamma: f32,
epsilon: f32,
greedy_policy: GreedyPolicy<E>,
min_epsilon: f32,
max_epsilon: f32,
decay_rate: f32,
episode: usize,
}
impl<E: Environment> EpsilonGreedyPolicy<E> {
fn epsilon(&self) -> f32 {
self.min_epsilon
+ (self.max_epsilon - self.min_epsilon) * (-self.decay_rate * self.episode as f32).exp()
}
}
impl<E: Environment> Policy<E> for EpsilonGreedyPolicy<E> {
fn choose_action(&self, state: &<E as Environment>::State) -> <E as Environment>::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: &<E as Environment>::State,
action: <E as Environment>::Action,
reward: f32,
next_state: Option<&<E as Environment>::State>,
) {
self.greedy_policy
.improve(state, action, reward, next_state);
}
fn on_episode_increment(&mut self) {
self.episode += 1;
}
}