Skip to main content

Library to solve k-armed bandit problems

Project description

Bandit Agents Library (A concise and easy to use library to help with k-armed bandit problems)

This library is an attempt at creating a library that would streamline the resolution of k-armed bandit problems

It was mostly inspired by the first few chapters of Reinforcement Learning: An Introduction

installation

There are a few ways to install the library

  • pip: pip install https://github.com/The-Faust/bandit_agents
  • local: clone or download a release of the library and add it to your python path

Note: Once I have a decent documentation going on I intend on putting the library in pypi and conda-forge

development installation

If you intend on forking the project or you are a contributor working with me you will have to setup the development environment

requirements for installation of the dev env

To make use of the setup script you must have anaconda3 installed and have conda-lock installed in the environment in which you will execute setup_dev_env.sh

setup script

to execute the setup script execute the following

chmod +x setup_dev_env.sh
./setup_dev_env.sh

executing setup_dev_env.sh will do the following:

  1. give the roght permissions to build_env.sh and compile_env_lock.sh
  2. if lock file does not exists; execute compile_env_lock.sh
  3. create the conda environement by executing build_env.sh
  4. install the pre-commit hooks found in .pre-commit-config.yaml

You can then activate the environement in conda: conda activate bandit_agent_dev_env

Exemples

I made a few exemples that can teach you the basics as to how to use the library

all exemples can be found in the Exemples folder of the project

Simulations

The simulations exemples are to help you develop an intuitive understanding of how the solvers perform depending on the context in which you put them

Basic simulations

Basic simulations are simply to show the performance of different solvers givent a very simple context

The basic simulations exemples are as follow

agents

Agents exemples are designed to show you the intended usage for the library

The agent exemples are as follow"

An actual exemple

Following is a simplified version of the basic agent exemple with more comment

Say we have two possible pseudo-random actions defined by func_a and func_b

def func_a(): float:
    return gamma.rvs(a=6.8, scale=0.1, loc=0, size=1)[0] # A mystery number

def func_b(): float:
    return gamma.rvs(a=2.2, scale=0.2, loc=0, size=1)[0] # A mystery number

for this exemple we will assume that we don't really know which of the action is better. We want to find out which action will return the best reward.

If we were to try and find that out by ourselves without any tool or help we'd have to execute the actions a few times and then remember which one tended to wield the best score after a few executions, then we would assume that said action is the "best" and exploit it by only calling it for a while. If after a while we doubt that the action we found is still best we would have to start that process all over again.

The purpose of Bandit Agents library is to automate that process, using different strategies that wield varying degrees of success given a few different contexts.

Let's continue our exemple; To exploit the best action we instanciate an agent with our mystery functions

action_a = ('action_a', func_a)
action_b = ('action_b', func_b)

actions = (action_a, action_b)

agent = Agent(actions)

We now have our agent, the agent is the object that will exploreand exploit our actions and will attemp to wield the best possible reward

Say we want the agent to work for a 100 epochs in which it will explore and exploit the actions we gave it, we would do that with the following code

for i in range(100):
    indexes, targets = agent.act()
    agent = agent.fit(x=indexes, y=targets)

If we want to see which action worked best, we can loog at the weights returned by the agent

print(agent.info())

Here is the complete example without interruption

from BanditAgents import Agent
from scipy.stats import gamma

def func_a(): float:
    return gamma.rvs(a=6.8, scale=0.1, loc=0, size=1)[0] # A mystery number

def func_b(): float:
    return gamma.rvs(a=2.2, scale=0.2, loc=0, size=1)[0] # A mystery number

action_a = ('action_a', func_a)
action_b = ('action_b', func_b)

actions = (action_a, action_b)

agent = Agent(actions)

for i in range(100):
    indexes, targets = agent.act()
    agent = agent.fit(x=indexes, y=targets)

print(agent.info())

Run exemples via the terminal

You can run all the exemples via the terminal. To do so go into the project's folder and run python3 -m . -e basic for the basic exemples and python3 -m . -e agents to run the agents exemples.

performances

To have get a breakdown of the agents performances I suggest you take a look at README.ipynb

There are many more options you can use in the library to solve your problems depending on your needs and I hope you will install it and use it for your projects.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bandit_agents-0.5.21.tar.gz (24.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bandit_agents-0.5.21-py3-none-any.whl (39.3 kB view details)

Uploaded Python 3

File details

Details for the file bandit_agents-0.5.21.tar.gz.

File metadata

  • Download URL: bandit_agents-0.5.21.tar.gz
  • Upload date:
  • Size: 24.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for bandit_agents-0.5.21.tar.gz
Algorithm Hash digest
SHA256 128cbaaf0b6fb1a7a730fb0a62f797c27939efc751c31407ab07f1cb113cc66f
MD5 8ac29a4ac01a80dff9fab6702d4f9c3b
BLAKE2b-256 054558ce46c75288b040eb0d3d0c142470b3a266d793e3d23a2590d6addd5cad

See more details on using hashes here.

Provenance

The following attestation bundles were made for bandit_agents-0.5.21.tar.gz:

Publisher: publish-to-test-pypi.yml on The-Faust/bandit_agents

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bandit_agents-0.5.21-py3-none-any.whl.

File metadata

  • Download URL: bandit_agents-0.5.21-py3-none-any.whl
  • Upload date:
  • Size: 39.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for bandit_agents-0.5.21-py3-none-any.whl
Algorithm Hash digest
SHA256 2326fae03341e0cf02c371f30a3b44b549faacf538ffbfa2c2dd08983e564070
MD5 bf125de18066fb3da2e4a251080cd00d
BLAKE2b-256 d7af97d183e9dc5819c7154fa6fc9c8af6b00bc2fc37ef518fd4902888302510

See more details on using hashes here.

Provenance

The following attestation bundles were made for bandit_agents-0.5.21-py3-none-any.whl:

Publisher: publish-to-test-pypi.yml on The-Faust/bandit_agents

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page