pub struct QLearner<V, E, const STATES: usize, const ACTIONS: usize, Q = QTable<V, STATES, ACTIONS>> { /* private fields */ }Expand description
A Q-learning agent over STATES states and ACTIONS actions
(tinymind (C++)’s QLearner). It owns its Environment and its Q values (a
QTable unless built with with_q_values).
Implementations§
Source§impl<V: Scalar, E: Environment<ACTIONS>, const STATES: usize, const ACTIONS: usize> QLearner<V, E, STATES, ACTIONS>
impl<V: Scalar, E: Environment<ACTIONS>, const STATES: usize, const ACTIONS: usize> QLearner<V, E, STATES, ACTIONS>
Sourcepub fn new(
environment: E,
learning_rate: V,
discount_factor: V,
random_action_decision_point: usize,
) -> Self
pub fn new( environment: E, learning_rate: V, discount_factor: V, random_action_decision_point: usize, ) -> Self
A learner with a zeroed QTable. learning_rate and
discount_factor must be in [0, 1] and random_action_decision_point
(the exploration percentage) at most 100; a value outside that range
is ignored, leaving 0, as in the C++.
Source§impl<V: Scalar, E: Environment<ACTIONS>, const STATES: usize, const ACTIONS: usize, Q: QValues<V>> QLearner<V, E, STATES, ACTIONS, Q>
impl<V: Scalar, E: Environment<ACTIONS>, const STATES: usize, const ACTIONS: usize, Q: QValues<V>> QLearner<V, E, STATES, ACTIONS, Q>
Sourcepub fn with_q_values(
environment: E,
q_values: Q,
learning_rate: V,
discount_factor: V,
random_action_decision_point: usize,
) -> Self
pub fn with_q_values( environment: E, q_values: Q, learning_rate: V, discount_factor: V, random_action_decision_point: usize, ) -> Self
A learner over the given Q values; otherwise as new.
Sourcepub fn environment(&self) -> &E
pub fn environment(&self) -> &E
The environment.
Sourcepub fn environment_mut(&mut self) -> &mut E
pub fn environment_mut(&mut self) -> &mut E
The environment, mutably (for example to draw from its generator).
Sourcepub fn q_values_mut(&mut self) -> &mut Q
pub fn q_values_mut(&mut self) -> &mut Q
The Q values, mutably.
Sourcepub fn set_q_value(&mut self, state: usize, action: usize, value: V)
pub fn set_q_value(&mut self, state: usize, action: usize, value: V)
Sets the Q value of taking action in state.
Sourcepub fn learning_rate(&self) -> V
pub fn learning_rate(&self) -> V
The learning rate.
Sourcepub fn set_learning_rate(&mut self, rate: V)
pub fn set_learning_rate(&mut self, rate: V)
Sets the learning rate if it is in [0, 1]; otherwise keeps the old one.
Sourcepub fn discount_factor(&self) -> V
pub fn discount_factor(&self) -> V
The discount factor.
Sourcepub fn set_discount_factor(&mut self, factor: V)
pub fn set_discount_factor(&mut self, factor: V)
Sets the discount factor if it is in [0, 1]; otherwise keeps the old one.
Sourcepub fn random_action_decision_point(&self) -> usize
pub fn random_action_decision_point(&self) -> usize
How often the learner explores, in percent: 0 never, 100 always.
Sourcepub fn set_random_action_decision_point(&mut self, percent: usize)
pub fn set_random_action_decision_point(&mut self, percent: usize)
Sets the exploration percentage if it is at most 100; otherwise keeps the old one.
Sourcepub fn start_new_episode(&mut self)
pub fn start_new_episode(&mut self)
Forgets the current state (the C++ invalid state), as at the start of an episode.
Sourcepub fn take_action(&mut self, state: usize) -> Option<usize>
pub fn take_action(&mut self, state: usize) -> Option<usize>
Chooses an action in state (exploring or exploiting), makes state
the current state and returns the action, or None if state has no
valid action.
Sourcepub fn update_from_experience(&mut self, experience: &Experience<V>)
pub fn update_from_experience(&mut self, experience: &Experience<V>)
Learns from a step: updates the Q value of experience.action in
experience.state (unless the learning rate is 0 or the state is out of
range), whatever the learner’s current state, and moves to
experience.new_state.