A fast solver for Markov Decision Processes
Project description
mdpsolver
: A fast solver for Markov Decision Processes
mdpsolver
is the Python package for Markov Decision Processes (MDPs) with discounted rewards and infinite-horizon.
Features
- Fast solver-engine: Up-to 30x faster than other Python-based solvers (see details in the documentation).
- Available on PyPI.
- Three optimization algorithms: Value iteration, Policy iteration, and Modified policy iteration.
- Three value-update methods: Standard, Gauss–Seidel, Successive over-relaxation.
- Includes support for sparse matrices.
Quick start guide
The following shows how to get quickly started with mdpsolver
.
Installation
Download and install mdpsolver
directly from PyPI.
pip install mdpsolver
Usage
Start by specifying the reward function and transition probabilities as lists. The following is an example of a simple MDP containing three states and two actions in each state.
#Import packages
import mdpsolver
#Rewards (3 states x 2 actions)
#e.g. choosing second action in first state gives reward=-1
rewards = [[5,-1],
[1,-2],
[50,0]]
#Transition probabilities (3 from_states x 2 actions x 3 to_states)
#e.g. choosing first action in third state gives a probability of 0.6 of staying in third state
tranMatWithZeros = [[[0.9,0.1,0.0],[0.1,0.9,0.0]],
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
[[0.2,0.2,0.6],[0.5,0.5,0.0]]]
Now, create the model object and insert the problem parameters.
#Create model object
mdl = mdpsolver.model()
#Insert the problem parameters
mdl.mdp(discount=0.8,
rewards=rewards,
tranMatWithZeros=tranMatWithZeros)
We can now optimize the policy.
mdl.solve()
The optimized policy can be returned in a variety of ways. Here, we return the policy as a list and print directly in the terminal.
print(mdl.getPolicy())
#[1, 1, 0]
Sparse transition matrix?
mdpsolver
has three alternative formats for large and highly sparse transition probability matrices.
(1) Elementwise representation (excluding elements containing zeros):
#[from_state,action,to_state,probability]
tranMatElementwise = [[0,0,0,0.9],
[0,0,1,0.1],
[0,1,0,0.1],
[0,1,1,0.9],
[1,0,0,0.4],
[1,0,1,0.5],
[1,0,2,0.1],
[1,1,0,0.3],
[1,1,1,0.5],
[1,1,2,0.2],
[2,0,0,0.2],
[2,0,1,0.2],
[2,0,2,0.6],
[2,1,0,0.5],
[2,1,1,0.5]]
mdl.mdp(discount=0.8,
rewards=rewards,
tranMatElementwise=tranMatElementwise)
(2) Probabilities and column (to_state) indices in separate lists:
tranMatProbs = [[[0.9,0.1],[0.1,0.9]],
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
[[0.2,0.2,0.6],[0.5,0.5]]]
tranMatColumns = [[[0,1],[0,1]],
[[0,1,2],[0,1,2]],
[[0,1,2],[0,1]]]
mdl.mdp(discount=0.8,
rewards=rewards,
tranMatProbs=tranMatProbs,
tranMatColumns=tranMatColumns)
(3) Load the elementwise representation from a file:
mdl.mdp(discount=0.8,
rewards=rewards,
tranMatFromFile="transitions.csv")
Documentation
Documentation can be found in the wiki for mdpsolver
on Github.
How to cite
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
Built Distribution
Hashes for mdpsolver-0.9.3-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 347a6e06d7a0e87b05cbcb0e32eec80d802e10e6f2f236f7ac076964d8f7e2de |
|
MD5 | 86a2b15f1cf982677b02ab0942be5661 |
|
BLAKE2b-256 | d8c8815bd16a1092cf9149a6456aaf97a744bfbce2a57da00169d7b2be8740bb |