QLearningRule#

class QLearningRule(alpha=0.5, gamma=0.1, values=None, reward=None, maximum=None, *args, **kwargs)[source]#

Bases: object

Q-learning rule (Watkins, 1989) for a one-dimensional array of Q-values.

Parameters:
alphafloat

The learning rate. Default is 0.5.

gammafloat

The discount factor. Default is 0.1.

valuesndarray

The values matrix. It is a 1D array of Q-values active for the current state, where each element corresponds to an action.

rewardfloat

The reward received on the current state.

maximumfloat

The maximum estimated reward for the next state.

Notes

The Q-learning rule is a model-free reinforcement learning algorithm that is used to learn the value of an action in a given state. It is defined as

\[\Delta \mathcal{Q}(s, a) = \alpha \cdot (r + \gamma \cdot \max_{a'} \mathcal{Q}(s', a') - \mathcal{Q}(s, a))\]

where \(\Delta \mathcal{Q}(s, a)\) is the change in value of action \(a\) in state \(s\), \(r\) is the reward received on the current state, \(\gamma\) is the discount factor, and \(\max_{a'} \mathcal{Q}(s', a')\) is the maximum estimated reward for the next state.

References

Watkins, C. J. C. H. (1989). Learning from delayed rewards.

Watkins, C. J., & Dayan, P. (1992). Q-learning. Machine learning, 8, 279-292.

Examples

>>> import numpy as np
>>> from cpm.models.learning import QLearningRule
>>> values = np.array([1, 0.5, 0.99])
>>> component = QLearningRule(alpha=0.1, gamma=0.8, values=values, reward=1, maximum=10)
>>> component.compute()
array([1.8  , 1.35 , 1.791])
compute()[source]#

Compute the change in values based on the given values, reward, and parameters, and return the updated values.

Returns:
output: numpy.ndarray:

The computed output values.