Implementation of Multi Armed Bandit algorithms from "Reinforcement Learning - An Introduction" by Richard S. Sutton and Andrew G. Barto
Project description
Multi-Armed Bandits
Implementation of Multi Armed Bandit algorithms from "Reinforcement Learning - An Introduction" by Richard S. Sutton and Andrew G. Barto
This library suggests the next actions based on the rewards received for each action.
Algorithms
This library covers the following algorithms:
- Epsilon Greedy
- Upper Confidence Bound
- Gradient Bandit
Epsilon Greedy
This algorithm selects an action based on an epsilon variable.
| Probability | Action |
|---|---|
| 1 - epsilon | action with highest estimated reward |
| epsilon | random action |
Note 0 <=
epsilon<= 1
Upper Confidence Bound
This algorithm selects the next action based on the confidence in the estimated values.
This uses an exploration parameter to control the balance between exploration and exploitation.
A larger exploration value means the algorithm explores more.
Note
exploration> 0
Gradient Bandit Algorithm
This algorithm selects an action based on a preference.
Internally it uses a soft-max distribution to convert preferences to probabilities.
It has a step_size parameter, which influences how much preferences are changed at each step.
A larger step_size will learn faster from the most recent reward.
Note
step_size> 0
Averaging Functions
The algorithms could be used with any averaging function:
- Simple Average (a.k.a running average)
- Weighted Average
- Exponential Moving Average
Simple Average
A simple average is the same as a running average. As time steps are advanced, the average is adjusted to account for the reward.
A higher reward, increases the average and a lower reward decreases the average.
Weighted Average
A weighted average operates much like a running average but it applies a step_size parameter.
The step_size parameter controls how much weighting is given to new rewards.
A higher step_size gives more weighting to the most recent reward.
Note
step_size> 0
Exponential Moving Average
An exponential moving average applies a discount multiplier to previous rewards and (1 - discount) multiplier to recent rewards.
A higher discount means more weight is given to previous average, making recent rewards less influential.
A lower discount means more weight is given to the most recent reward, making older rewards less influential.
Usage
To use this algorithm first import this library:
from mab_algo import *
Declare a set of actions:
actions = ["A", "B", "C"]
Select any averaging function:
averager = SimpleAverage()
averager = WeightedAverage(step_size = 0.2)
averager = ExponentialMovingAverage(discount = 0.8)
Note This uses example
step_sizeanddiscountvalues, please refer to the sections above to learn more about them.
Select any algorithm:
algo = EpsilonGreedy(
actions = actions,
averager = averager,
epsilon = 0.5
)
algo = UCB(
actions = actions,
averager = averager,
exploration = 0.35
)
algo = GradientBandit(
actions = actions,
averager = averager,
step_size = 0.9
)
Note This uses example
epsilon,explorationandstep_sizevalues, please refer to the sections above to learn more about them.
Step through the algorithms to get the next actions:
action = algo.step()
action = algo.step(reward = 0.5)
action = algo.step(reward = 5)
# ... continue as needed
Verbose
action = algo.step() # get initial action
reward = 0.5 # for taking 'action' - reward must be supplied by you
next_action = algo.step(reward)
reward = 5 # for taking 'next_action' - reward must be supplied by you
next_action = algo.step(reward)
# ... continue stepping through and providing reward values to get new actions...
example.py file contains an example of the above in action!
Testing
Unit tests are provided in the 'tests' directory.
Tests can be executed using this command:
python3 -m unittest discover tests
Check Out My Other Projects:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mab_algo-1.0.0.tar.gz.
File metadata
- Download URL: mab_algo-1.0.0.tar.gz
- Upload date:
- Size: 9.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
16627ffb4cbf540568eef551d62903c1e2c3739ba0bbaaf68303e3b3ed2a332e
|
|
| MD5 |
4443b94253cc7e3e4b13fe68c378199f
|
|
| BLAKE2b-256 |
9322f86cd653cf7cc4b36d630708ec89faba04f1b535d85332ccb6194bcb09b6
|
File details
Details for the file mab_algo-1.0.0-py3-none-any.whl.
File metadata
- Download URL: mab_algo-1.0.0-py3-none-any.whl
- Upload date:
- Size: 15.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ba4e3961510f7cf9b4d13537cca3c7cb3600494452c0cc442ed5da465af276f
|
|
| MD5 |
c2d052945e8fce68fe66ea1d879bace1
|
|
| BLAKE2b-256 |
b1e2aa29ee8e81784740367d110e88f9d0c739cfe5928bf554fc9b4e3d9f053f
|