mdp-solver-ic
A Python library of algorithms for solving infinite-horizon, discounted Markov Decision Processes (MDPs), with a performance-critical core written in C++ (via pybind11).
Implements:
- Value Iteration (Jacobi and Gauss-Seidel variants)
- Policy Iteration
- Q-Learning and SARSA (model-free reinforcement learning)
Installation
pip install mdp-solver-ic
Building from source requires a C++17 compiler (the extension is compiled at install time):
git clone https://github.com/LuccaGiannelli/mdp-solver.git
cd mdp-solver
pip install .
Basic usage
from mdp import MDP
S = [0, 1, 2] # states
A = [0, 1, 2] # actions
def Pt(sn, a, sn1):
# returns P(sn1 | sn, a)
...
def rt(sn, a, sn1):
# returns the reward
...
problem = MDP(S, A, Pt, rt)
# Value Iteration
problem.VI(gamma=0.9, tol=1e-6)
problem.print_results()
# Policy Iteration
problem.PI(gamma=0.9, tol=1e-6)
problem.print_results()
See examples/basic_example.py for a complete example (a machine maintenance problem).
Available algorithms
| Method | Description |
|---|---|
VI |
Value Iteration (Jacobi) |
VIGS |
Value Iteration with Gauss-Seidel |
PI |
Policy Iteration |
QLearning |
Q-Learning (off-policy TD control) |
SARSA |
SARSA (on-policy TD control) |
Common parameters
| Parameter | Description |
|---|---|
gamma |
Discount factor (0 ≤ γ < 1) |
tol |
Convergence tolerance |
max_iter |
Maximum number of iterations |
v0 |
Initial value vector (optional) |
verbose |
Print progress on completion |
norm_type |
0 = relative sup norm, 1 = sup norm, 2 = euclidean norm |
epsilon |
Final epsilon for the epsilon-greedy policy (Q-Learning / SARSA) |
alpha |
Learning rate (Q-Learning / SARSA) |
How it works
You define the state space S, action space A, a transition probability function Pt(sn, a, sn1), and a reward function rt(sn, a, sn1). The constructor enumerates all feasible transitions into a CSV (sn,a,sn1,prob,reward) and loads it into a NumPy array; the algorithms then hand that array to the C++ core, which does the inner loops with direct memory access for performance.
After solving, results are available as:
| Attribute | Description |
|---|---|
value |
optimal value for each state |
policy |
optimal action for each state |
time |
execution time (seconds) |
n_iterations |
number of iterations to convergence |
Adding a new algorithm
- Create
mdp/algorithms/new_algo.pywith a functiondef NewAlgo(self, ...): - Import it in
mdp/algorithms/__init__.py - Attach it to the class in
mdp/core.py:NewAlgo = NewAlgo
Tests
pip install pytest
pytest tests/
About
mdp-solver-ic was developed as part of an undergraduate research project ("Iniciação Científica") titled Programação Rodoviária.
License
TBD.
Metadata
Release files for mdp-solver-ic 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mdp_solver_ic-1.0.0.tar.gz | 12.1 kB | Details |
Release files / mdp_solver_ic-1.0.0.tar.gz
| Download URL | mdp_solver_ic-1.0.0.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
44ce68ee4abc2c4e9b54605bdf449130794593991a59cd2a43c9cdd07778a809
|
|
BLAKE2b-256 checksum How to use checksums |
1c55a7ac5426ad2b1b90ca3cd21d67e7604fda4529750adb224c4de8398cd158
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.2
|