pub struct ReinforcementLearningAgent { /* private fields */ }Expand description
Production-quality tabular RL agent supporting multiple algorithms and policies.
Register states and their valid actions, then call update
after each environment step to learn Q-values. Call
run_episode to process a complete episode at once.
Implementations§
Source§impl ReinforcementLearningAgent
impl ReinforcementLearningAgent
Sourcepub fn new(config: AgentConfig) -> Self
pub fn new(config: AgentConfig) -> Self
Create a new agent with the given configuration.
Returns RlAgentError::InvalidConfig if hyperparameters are out of range.
Sourcepub fn register_state(
&mut self,
state: RlState,
actions: Vec<RlAction>,
) -> Result<(), RlAgentError>
pub fn register_state( &mut self, state: RlState, actions: Vec<RlAction>, ) -> Result<(), RlAgentError>
Register state as a valid environment state with actions as its
legal action set. Can be called multiple times to add more actions;
duplicate actions are ignored.
§Errors
Returns RlAgentError::InvalidConfig when actions is empty.
Sourcepub fn select_action(
&self,
state: &RlState,
rng_seed: &mut u64,
) -> Result<RlAction, RlAgentError>
pub fn select_action( &self, state: &RlState, rng_seed: &mut u64, ) -> Result<RlAction, RlAgentError>
Select an action for state according to the configured policy.
§Errors
RlAgentError::StateNotFoundif the state is not registered.
Sourcepub fn best_action(&self, state: &RlState) -> Result<RlAction, RlAgentError>
pub fn best_action(&self, state: &RlState) -> Result<RlAction, RlAgentError>
Return the greedy (argmax Q) action for state.
§Errors
RlAgentError::StateNotFoundif the state is not registered.
Sourcepub fn value(&self, state: &RlState) -> f64
pub fn value(&self, state: &RlState) -> f64
V(s) = max_a Q(s, a). Returns 0.0 if the state is unregistered.
Sourcepub fn update(&mut self, transition: &Transition) -> Result<f64, RlAgentError>
pub fn update(&mut self, transition: &Transition) -> Result<f64, RlAgentError>
Apply a single TD update from transition.
Returns the absolute TD error |δ| so callers can track convergence.
§Errors
RlAgentError::StateNotFound— state or next_state not registered.RlAgentError::ActionNotFound— action not valid for state.
Sourcepub fn run_episode(
&mut self,
transitions: Vec<Transition>,
_rng_seed: u64,
) -> Result<EpisodeStats, RlAgentError>
pub fn run_episode( &mut self, transitions: Vec<Transition>, _rng_seed: u64, ) -> Result<EpisodeStats, RlAgentError>
Process a complete sequence of transitions as one episode.
Epsilon is decayed once after all transitions are processed.
Episode statistics are accumulated into AgentStats.
§Errors
Propagates any error from update.
Sourcepub fn decay_epsilon(&mut self)
pub fn decay_epsilon(&mut self)
Decay epsilon for EpsilonGreedy policies: ε ← max(min_ε, ε × decay). No-op for other policies.
Sourcepub fn add_experience(&mut self, t: Transition)
pub fn add_experience(&mut self, t: Transition)
Push t into the experience replay buffer.
Sourcepub fn sample_experience(
&self,
n: usize,
rng_seed: u64,
) -> Result<Vec<Transition>, RlAgentError>
pub fn sample_experience( &self, n: usize, rng_seed: u64, ) -> Result<Vec<Transition>, RlAgentError>
Draw n transitions uniformly at random from the replay buffer.
§Errors
RlAgentError::InsufficientExperiencewhen the buffer has fewer thannentries.
Sourcepub fn stats(&self) -> AgentStats
pub fn stats(&self) -> AgentStats
Return a snapshot of aggregate statistics.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ReinforcementLearningAgent
impl RefUnwindSafe for ReinforcementLearningAgent
impl Send for ReinforcementLearningAgent
impl Sync for ReinforcementLearningAgent
impl Unpin for ReinforcementLearningAgent
impl UnsafeUnpin for ReinforcementLearningAgent
impl UnwindSafe for ReinforcementLearningAgent
Blanket Implementations§
impl<T> Allocation for T
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more