Skip to main content

Vectorized Reinforcement Learning Library

Project description

Samsara RL

A vectorized NumPy implementation of foundational reinforcement learning algorithms, following David Silver's RL lecture series. Built for clarity and learning — each algorithm maps directly to the equations in the lectures.

Applications of RL include robotic manipulation, LLM fine-tuning, financial portfolio management, and adaptive control systems.


Table of Contents

  1. Installation
  2. Quick Start
  3. Planning
  4. Model-Free Prediction
  5. Model-Free Control
  6. Function Approximation

Installation

pip install samsara-rl

Quick Start

from samsara_rl.mdp.grid_world.grid_world_mdp import GridWorldMDP
from samsara_rl.planning.policy_iteration import PolicyIteration

mdp = GridWorldMDP()
pi = PolicyIteration(mdp)
policy = pi.find_optimal_policy()

Planning

Planning algorithms assume full knowledge of environment dynamics (transition probabilities and reward function). While not "true RL" — agents never have access to dynamics in practice — planning provides the theoretical foundation all RL algorithms build on.

MDP Structure

MDPs are represented as NumPy arrays. The included GridWorldMDP implements the 4x4 grid world from David Silver's Lecture 3.

Attribute Shape Description
state_action_transition_matrix (S, A, S') T(s, a, s') — transition probabilities
reward_matrix (S, A, S') R(s, a, s') — reward for each transition

Policy Iteration

Alternates between evaluating the current policy using the Bellman expectation equation and improving it greedily until the policy stops changing.

PolicyIteration(mdp, bellman_tolerance)

Argument Type Default Description
mdp MDP MDP instance to solve
bellman_tolerance float 0.01 Convergence threshold for policy evaluation

find_optimal_policy(max_iter)

Argument Type Default Description
max_iter int 99 Maximum number of policy iteration steps

Value Iteration

Applies the Bellman optimality equation directly each iteration. Equivalent to policy iteration with k=1 evaluation steps per improvement. Policy is extracted once at convergence.

ValueIteration(mdp, bellman_tolerance)

Argument Type Default Description
mdp MDP MDP instance to solve
bellman_tolerance float 0.01 Convergence threshold for value iteration

Examples

from samsara_rl.mdp.grid_world.grid_world_mdp import GridWorldMDP
from samsara_rl.planning.policy_iteration import PolicyIteration
from samsara_rl.planning.value_iteration import ValueIteration

mdp = GridWorldMDP()

policy = PolicyIteration(mdp, bellman_tolerance=0.001).find_optimal_policy(max_iter=50)
policy = ValueIteration(mdp, bellman_tolerance=0.001).find_optimal_policy()

Model-Free Prediction

Model-free methods learn value functions directly from experience (sampled episodes) without access to the MDP's transition or reward dynamics.

Monte Carlo

Monte Carlo prediction estimates Q(s, a) by averaging sampled returns. After each episode, the return G_t (discounted cumulative reward from time step t onward) is computed for every visited state-action pair, and the Q-table is updated using constant-alpha learning:

Q(s, a) ← Q(s, a) + α (G_t − Q(s, a))

This is an every-visit implementation — if the same (s, a) pair appears multiple times in an episode, each occurrence triggers an update. Both every-visit and first-visit MC converge to the true Q^π.

Returns are computed in a fully vectorized pass using a cumulative-sum trick that avoids the standard reverse loop over time steps.

MonteCarloPrediction(mdp, policy, alpha, gamma)

Argument Type Default Description
mdp MDP MDP instance to sample episodes from
policy array Stochastic policy of shape (S, A)
alpha float 0.01 Learning rate for incremental Q updates
gamma float 0.9 Discount factor

evaluate(max_iter)

Argument Type Default Description
max_iter int 40000 Number of episodes to sample from

Example

from samsara_rl.mdp.grid_world.grid_world_mdp import GridWorldMDP
from samsara_rl.prediction.monte_carlo import MonteCarloPrediction

mdp = GridWorldMDP()
policy = mdp.random_policy()

mc = MonteCarloPrediction(mdp, policy, alpha=0.01, gamma=0.9)
mc.evaluate(max_iter=10000)

# V(s) for the random policy (expected value over actions)
v = mc.q_table.mean(axis=1).reshape(4, 4)

Model-Free Control

TODO


Function Approximation

TODO

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

samsara_rl-0.0.3.tar.gz (111.3 kB view details)

Uploaded Source

Built Distribution

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

samsara_rl-0.0.3-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file samsara_rl-0.0.3.tar.gz.

File metadata

  • Download URL: samsara_rl-0.0.3.tar.gz
  • Upload date:
  • Size: 111.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for samsara_rl-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a7d36216803c7d1d88b4c76a8781f164b93314614d2ec351bac7bed789dee5ea
MD5 128a20602bff20e0ef29fda4754de722
BLAKE2b-256 3286ce0f59ed5b2f40fa8ed5ab110702808af82a73f1bb2ac7ba5e0f0e809109

See more details on using hashes here.

File details

Details for the file samsara_rl-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: samsara_rl-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for samsara_rl-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2c23484f41f09a7b609e541d315bf84c0c0a94780f17638ac348b2900f33564b
MD5 43967495fac00cdd6623dde097958404
BLAKE2b-256 4fd8438092ab3223a1744374259934e8dada17b5fd1cabbf5d7656cc9bc28a86

See more details on using hashes here.

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