From ee64266f16eb8ee4b42e045a7efd3bcc61b0e490 Mon Sep 17 00:00:00 2001 From: Leonhard Kost Date: Sat, 10 Jan 2026 17:46:09 +0100 Subject: [PATCH] feat(q_learning): create new methods for the policies --- src/q_learning.rs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/q_learning.rs b/src/q_learning.rs index 0f8f143..39fc3e2 100644 --- a/src/q_learning.rs +++ b/src/q_learning.rs @@ -97,6 +97,16 @@ pub struct GreedyPolicy { gamma: f32, } +impl GreedyPolicy { + pub fn new(learning_rate: f32, gamma: f32) -> Self { + GreedyPolicy { + qtable: HashMap::new(), + learning_rate, + gamma, + } + } +} + impl Policy for GreedyPolicy { fn choose_action(&self, state: E::ActionRelevantState) -> E::Action { let actions = E::actions(&state); @@ -218,6 +228,22 @@ pub struct EpsilonGreedyPolicy { } impl EpsilonGreedyPolicy { + pub fn new( + learning_rate: f32, + gamma: f32, + max_epsilon: f32, + min_epsilon: f32, + decay_rate: f32, + ) -> Self { + EpsilonGreedyPolicy { + greedy_policy: GreedyPolicy::new(learning_rate, gamma), + min_epsilon, + max_epsilon, + decay_rate, + episode: 0, + } + } + fn epsilon(&self) -> f32 { self.min_epsilon + (self.max_epsilon - self.min_epsilon) * (-self.decay_rate * self.episode as f32).exp()