MDPSolver is a Python package for large Markov Decision Processes (MDPs) with infinite-horizons.
- Our C++ solver is fast. See the examples below or details in the documentation.
- Two optimality criteria: Discounted and Average reward.
- Three optimization algorithms: Value iteration, Policy iteration, and Modified policy iteration.
- Three value-update methods: Standard, Gauss–Seidel, and Successive over-relaxation.
- Supports sparse matrices and parallel computing.
- Easy installation and OS independence (Linux, Windows and macOS).
| States | Non-zero transitions | RAM | Solution time |
|---|---|---|---|
| 100,000 | 10,000,000 | 153 MB | 0.035 s |
| 1,000,000 | 100,000,000 | 1.3 GB | 0.419 s |
| 3,000,000 | 300,000,000 | 3.8 GB | 2.56 s |
| 5,000,000 | 500,000,000 | 6.3 GB | 5.03 s |
| 10,000,000 | 1,000,000,000 | 12.7 GB | 13.0 s |
Mean of 10 random models with 5 actions per state and 20 possible next states per state and action, loaded from NPY-files and solved by modified policy iteration under the discounted reward criterion with discount
Install directly from PyPI with:
pip install mdpsolver
The following shows how to get quickly started with mdpsolver.
Start by specifying the reward function and transition probabilities as NumPy arrays. The following is an example of a simple MDP containing three states and two actions in each state.
#Import packages
import numpy as np
import mdpsolver
#Rewards (3 states x 2 actions), one row per state and action: [state,action,reward]
#e.g. choosing second action in first state gives reward=-1
rewardsElementwise = np.array([[0,0,5],
[0,1,-1],
[1,0,1],
[1,1,-2],
[2,0,50],
[2,1,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 = np.array([[[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]]])The rewards can also be given as a matrix (states x actions), e.g. rewards = np.array([[5,-1],[1,-2],[50,0]]), and every format also accepts Python lists. In the elementwise format, and as lists, states may have different numbers of actions.
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,
rewardsElementwise=rewardsElementwise,
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 NumPy array and print it directly in the terminal.
print(mdl.getPolicy())
#[1 1 0]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 = np.array([[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 = mdpsolver.model()
mdl.mdp(discount=0.8,
rewardsElementwise=rewardsElementwise,
tranMatElementwise=tranMatElementwise)(2) Probabilities and column (to_state) indices in separate arrays:
#As arrays, every state-action pair needs the same number of entries,
#so pairs with fewer are padded with the probability 0 (and the column 0)
tranMatProbs = np.array([[[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]]])
tranMatColumns = np.array([[[0,1,0],[0,1,0]],
[[0,1,2],[0,1,2]],
[[0,1,2],[0,1,0]]])
mdl = mdpsolver.model()
mdl.mdp(discount=0.8,
rewardsElementwise=rewardsElementwise,
tranMatProbs=tranMatProbs,
tranMatColumns=tranMatColumns)As lists, the pairs may have different numbers of entries, without padding, e.g. tranMatProbs = [[[0.9,0.1],[0.1,0.9]], ...] and tranMatColumns = [[[0,1],[0,1]], ...].
(3) Load the elementwise representation from NPY files:
The rows of rewardsElementwise and tranMatElementwise can be saved as NumPy .npy files and given to rewardsFromFile and tranMatFromFile, which read them straight into MDPSolver's C++ core. See Loading models from NPY files in the wiki.
The documentation can be found in the wiki for MDPSolver.
Andersen et al., (2025). MDPSolver: An Efficient Solver for Markov Decision Processes. Journal of Open Source Software, 10(109), 7544, https://doi.org/10.21105/joss.07544
BibTeX
@article{Andersen2025,
doi = {10.21105/joss.07544},
url = {https://doi.org/10.21105/joss.07544},
year = {2025}, publisher = {The Open Journal},
volume = {10},
number = {109},
pages = {7544},
author = {Anders Reenberg Andersen and Jesper Fink Andersen},
title = {MDPSolver: An Efficient Solver for Markov Decision Processes},
journal = {Journal of Open Source Software} }