Skip to main content

QLearner

Struct QLearner 

Source
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>

Source

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>

Source

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.

Source

pub fn environment(&self) -> &E

The environment.

Source

pub fn environment_mut(&mut self) -> &mut E

The environment, mutably (for example to draw from its generator).

Source

pub fn q_values(&self) -> &Q

The Q values.

Source

pub fn q_values_mut(&mut self) -> &mut Q

The Q values, mutably.

Source

pub fn q_value(&self, state: usize, action: usize) -> V

The Q value of taking action in state.

Source

pub fn set_q_value(&mut self, state: usize, action: usize, value: V)

Sets the Q value of taking action in state.

Source

pub fn learning_rate(&self) -> V

The learning rate.

Source

pub fn set_learning_rate(&mut self, rate: V)

Sets the learning rate if it is in [0, 1]; otherwise keeps the old one.

Source

pub fn discount_factor(&self) -> V

The discount factor.

Source

pub fn set_discount_factor(&mut self, factor: V)

Sets the discount factor if it is in [0, 1]; otherwise keeps the old one.

Source

pub fn random_action_decision_point(&self) -> usize

How often the learner explores, in percent: 0 never, 100 always.

Source

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.

Source

pub fn state(&self) -> Option<usize>

The current state, None at the start of an episode.

Source

pub fn set_state(&mut self, state: usize)

Sets the current state if it is below STATES.

Source

pub fn start_new_episode(&mut self)

Forgets the current state (the C++ invalid state), as at the start of an episode.

Source

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.

Source

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.

Trait Implementations§

Source§

impl<V: Clone, E: Clone, const STATES: usize, const ACTIONS: usize, Q: Clone> Clone for QLearner<V, E, STATES, ACTIONS, Q>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<V: Debug, E: Debug, const STATES: usize, const ACTIONS: usize, Q: Debug> Debug for QLearner<V, E, STATES, ACTIONS, Q>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<V, E, const STATES: usize, const ACTIONS: usize, Q> Freeze for QLearner<V, E, STATES, ACTIONS, Q>
where E: Freeze, Q: Freeze, V: Freeze,

§

impl<V, E, const STATES: usize, const ACTIONS: usize, Q> RefUnwindSafe for QLearner<V, E, STATES, ACTIONS, Q>

§

impl<V, E, const STATES: usize, const ACTIONS: usize, Q> Send for QLearner<V, E, STATES, ACTIONS, Q>
where E: Send, Q: Send, V: Send,

§

impl<V, E, const STATES: usize, const ACTIONS: usize, Q> Sync for QLearner<V, E, STATES, ACTIONS, Q>
where E: Sync, Q: Sync, V: Sync,

§

impl<V, E, const STATES: usize, const ACTIONS: usize, Q> Unpin for QLearner<V, E, STATES, ACTIONS, Q>
where E: Unpin, Q: Unpin, V: Unpin,

§

impl<V, E, const STATES: usize, const ACTIONS: usize, Q> UnsafeUnpin for QLearner<V, E, STATES, ACTIONS, Q>

§

impl<V, E, const STATES: usize, const ACTIONS: usize, Q> UnwindSafe for QLearner<V, E, STATES, ACTIONS, Q>
where E: UnwindSafe, Q: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.