mdpsolver 0.9.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mdpsolver-0.9.1/LICENSE +21 -0
- mdpsolver-0.9.1/MANIFEST.in +3 -0
- mdpsolver-0.9.1/PKG-INFO +146 -0
- mdpsolver-0.9.1/README.md +131 -0
- mdpsolver-0.9.1/pyproject.toml +30 -0
- mdpsolver-0.9.1/setup.cfg +4 -0
- mdpsolver-0.9.1/src/mdpsolver/__init__.py +1 -0
- mdpsolver-0.9.1/src/mdpsolver/model.py +131 -0
- mdpsolver-0.9.1/src/mdpsolver/problems/cbmaux.py +39 -0
- mdpsolver-0.9.1/src/mdpsolver/problems/tbmaux.py +39 -0
- mdpsolver-0.9.1/src/mdpsolver/solvermodule.cp310-win_amd64.pyd +0 -0
- mdpsolver-0.9.1/src/mdpsolver/solvermodule.cpython-310-x86_64-linux-gnu.so +0 -0
- mdpsolver-0.9.1/src/mdpsolver.egg-info/PKG-INFO +146 -0
- mdpsolver-0.9.1/src/mdpsolver.egg-info/SOURCES.txt +14 -0
- mdpsolver-0.9.1/src/mdpsolver.egg-info/dependency_links.txt +1 -0
- mdpsolver-0.9.1/src/mdpsolver.egg-info/top_level.txt +1 -0
mdpsolver-0.9.1/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Anders Reenberg Andersen and Jesper Fink Andersen
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
mdpsolver-0.9.1/PKG-INFO
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: mdpsolver
|
|
3
|
+
Version: 0.9.1
|
|
4
|
+
Summary: An efficient solver for Markov Decision Processes
|
|
5
|
+
Author-email: Anders Reenberg Andersen <andersra@live.dk>, Jesper Fink Andersen <jfan@dtu.dk>
|
|
6
|
+
Project-URL: Homepage, https://github.com/areenberg/MDPSolver
|
|
7
|
+
Project-URL: Issues, https://github.com/areenberg/MDPSolver/issues
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
|
|
16
|
+
# `mdpsolver`: An efficient solver for Markov Decision Processes
|
|
17
|
+
|
|
18
|
+
`mdpsolver` is the Python package for deriving the policies of Markov Decision Processes (MDPs) with infinite-horizon and the expected total discounted reward optimality criterion.
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
* Available on PyPI.
|
|
23
|
+
* Solver-engine developed in C++.
|
|
24
|
+
* Available optimization algorithms: *Value iteration*, *Policy iteration*, and *Modified policy iteration*.
|
|
25
|
+
* Includes a variety of input formats available for users to choose from.
|
|
26
|
+
|
|
27
|
+
# Quick start guide
|
|
28
|
+
|
|
29
|
+
The following shows how to get quickly started with `mdpsolver`.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Download and install `mdpsolver` directly from PyPI.
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
pip install mdpsolver
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
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**.
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
#Import packages
|
|
45
|
+
import mdpsolver
|
|
46
|
+
|
|
47
|
+
#Rewards (3 states x 2 actions)
|
|
48
|
+
#e.g. choosing second action in first state gives reward=-1
|
|
49
|
+
rewards = [[5,-1],
|
|
50
|
+
[1,-2],
|
|
51
|
+
[50,0]]
|
|
52
|
+
|
|
53
|
+
#Transition probabilities (3 from_states x 2 actions x 3 to_states)
|
|
54
|
+
#e.g. choosing first action in third state gives a probability of 0.6 of staying in third state
|
|
55
|
+
tranMatWithZeros = [[[0.9,0.1,0.0],[0.1,0.9,0.0]],
|
|
56
|
+
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
|
|
57
|
+
[[0.2,0.2,0.6],[0.5,0.5,0.0]]]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Now, create the model object and insert the problem parameters.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
#Create model object
|
|
64
|
+
mdl = mdpsolver.model()
|
|
65
|
+
|
|
66
|
+
#Insert the problem parameters
|
|
67
|
+
mdl.mdp(discount=0.8,
|
|
68
|
+
rewards=rewards,
|
|
69
|
+
tranMatWithZeros=tranMatWithZeros)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
We can now optimize the policy.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
mdl.solve()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
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.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
print(mdl.getPolicy())
|
|
82
|
+
#[1, 1, 0]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Large transition matrix?
|
|
86
|
+
|
|
87
|
+
`mdpsolver` has three alternative formats for large and highly sparse transition probability matrices.
|
|
88
|
+
|
|
89
|
+
**(1) Elementwise representation (excluding elements containing zeros):**
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
#[from_state,action,to_state,probability]
|
|
93
|
+
tranMatElementwise = [[0,0,0,0.9],
|
|
94
|
+
[0,0,1,0.1],
|
|
95
|
+
[0,1,0,0.1],
|
|
96
|
+
[0,1,1,0.9],
|
|
97
|
+
[1,0,0,0.4],
|
|
98
|
+
[1,0,1,0.5],
|
|
99
|
+
[1,0,2,0.1],
|
|
100
|
+
[1,1,0,0.3],
|
|
101
|
+
[1,1,1,0.5],
|
|
102
|
+
[1,1,2,0.2],
|
|
103
|
+
[2,0,0,0.2],
|
|
104
|
+
[2,0,1,0.2],
|
|
105
|
+
[2,0,2,0.6],
|
|
106
|
+
[2,1,0,0.5],
|
|
107
|
+
[2,1,1,0.5]]
|
|
108
|
+
|
|
109
|
+
mdl.mdp(discount=0.8,
|
|
110
|
+
rewards=rewards,
|
|
111
|
+
tranMatElementwise=tranMatElementwise)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**(2) Probabilities and column (to_state) indices in separate lists:**
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
tranMatProbs = [[[0.9,0.1],[0.1,0.9]],
|
|
118
|
+
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
|
|
119
|
+
[[0.2,0.2,0.6],[0.5,0.5]]]
|
|
120
|
+
|
|
121
|
+
tranMatColumns = [[[0,1],[0,1]],
|
|
122
|
+
[[0,1,2],[0,1,2]],
|
|
123
|
+
[[0,1,2],[0,1]]]
|
|
124
|
+
|
|
125
|
+
mdl.mdp(discount=0.8,
|
|
126
|
+
rewards=rewards,
|
|
127
|
+
tranMatProbs=tranMatProbs,
|
|
128
|
+
tranMatColumns=tranMatColumns)
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**(3) Load the elementwise representation from a file:**
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
mdl.mdp(discount=0.8,
|
|
136
|
+
rewards=rewards,
|
|
137
|
+
tranMatFromFile="transitions.csv")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
# User manual
|
|
141
|
+
|
|
142
|
+
Further information can be found in the wiki for `mdpsolver` on Github.
|
|
143
|
+
|
|
144
|
+
# How to cite
|
|
145
|
+
|
|
146
|
+
[](https://zenodo.org/badge/latestdoi/294063917)
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# `mdpsolver`: An efficient solver for Markov Decision Processes
|
|
2
|
+
|
|
3
|
+
`mdpsolver` is the Python package for deriving the policies of Markov Decision Processes (MDPs) with infinite-horizon and the expected total discounted reward optimality criterion.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
* Available on PyPI.
|
|
8
|
+
* Solver-engine developed in C++.
|
|
9
|
+
* Available optimization algorithms: *Value iteration*, *Policy iteration*, and *Modified policy iteration*.
|
|
10
|
+
* Includes a variety of input formats available for users to choose from.
|
|
11
|
+
|
|
12
|
+
# Quick start guide
|
|
13
|
+
|
|
14
|
+
The following shows how to get quickly started with `mdpsolver`.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
Download and install `mdpsolver` directly from PyPI.
|
|
19
|
+
|
|
20
|
+
```
|
|
21
|
+
pip install mdpsolver
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
## Usage
|
|
25
|
+
|
|
26
|
+
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**.
|
|
27
|
+
|
|
28
|
+
```python
|
|
29
|
+
#Import packages
|
|
30
|
+
import mdpsolver
|
|
31
|
+
|
|
32
|
+
#Rewards (3 states x 2 actions)
|
|
33
|
+
#e.g. choosing second action in first state gives reward=-1
|
|
34
|
+
rewards = [[5,-1],
|
|
35
|
+
[1,-2],
|
|
36
|
+
[50,0]]
|
|
37
|
+
|
|
38
|
+
#Transition probabilities (3 from_states x 2 actions x 3 to_states)
|
|
39
|
+
#e.g. choosing first action in third state gives a probability of 0.6 of staying in third state
|
|
40
|
+
tranMatWithZeros = [[[0.9,0.1,0.0],[0.1,0.9,0.0]],
|
|
41
|
+
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
|
|
42
|
+
[[0.2,0.2,0.6],[0.5,0.5,0.0]]]
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Now, create the model object and insert the problem parameters.
|
|
46
|
+
|
|
47
|
+
```python
|
|
48
|
+
#Create model object
|
|
49
|
+
mdl = mdpsolver.model()
|
|
50
|
+
|
|
51
|
+
#Insert the problem parameters
|
|
52
|
+
mdl.mdp(discount=0.8,
|
|
53
|
+
rewards=rewards,
|
|
54
|
+
tranMatWithZeros=tranMatWithZeros)
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
We can now optimize the policy.
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
mdl.solve()
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
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.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
print(mdl.getPolicy())
|
|
67
|
+
#[1, 1, 0]
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Large transition matrix?
|
|
71
|
+
|
|
72
|
+
`mdpsolver` has three alternative formats for large and highly sparse transition probability matrices.
|
|
73
|
+
|
|
74
|
+
**(1) Elementwise representation (excluding elements containing zeros):**
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
#[from_state,action,to_state,probability]
|
|
78
|
+
tranMatElementwise = [[0,0,0,0.9],
|
|
79
|
+
[0,0,1,0.1],
|
|
80
|
+
[0,1,0,0.1],
|
|
81
|
+
[0,1,1,0.9],
|
|
82
|
+
[1,0,0,0.4],
|
|
83
|
+
[1,0,1,0.5],
|
|
84
|
+
[1,0,2,0.1],
|
|
85
|
+
[1,1,0,0.3],
|
|
86
|
+
[1,1,1,0.5],
|
|
87
|
+
[1,1,2,0.2],
|
|
88
|
+
[2,0,0,0.2],
|
|
89
|
+
[2,0,1,0.2],
|
|
90
|
+
[2,0,2,0.6],
|
|
91
|
+
[2,1,0,0.5],
|
|
92
|
+
[2,1,1,0.5]]
|
|
93
|
+
|
|
94
|
+
mdl.mdp(discount=0.8,
|
|
95
|
+
rewards=rewards,
|
|
96
|
+
tranMatElementwise=tranMatElementwise)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
**(2) Probabilities and column (to_state) indices in separate lists:**
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
tranMatProbs = [[[0.9,0.1],[0.1,0.9]],
|
|
103
|
+
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
|
|
104
|
+
[[0.2,0.2,0.6],[0.5,0.5]]]
|
|
105
|
+
|
|
106
|
+
tranMatColumns = [[[0,1],[0,1]],
|
|
107
|
+
[[0,1,2],[0,1,2]],
|
|
108
|
+
[[0,1,2],[0,1]]]
|
|
109
|
+
|
|
110
|
+
mdl.mdp(discount=0.8,
|
|
111
|
+
rewards=rewards,
|
|
112
|
+
tranMatProbs=tranMatProbs,
|
|
113
|
+
tranMatColumns=tranMatColumns)
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
**(3) Load the elementwise representation from a file:**
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
mdl.mdp(discount=0.8,
|
|
121
|
+
rewards=rewards,
|
|
122
|
+
tranMatFromFile="transitions.csv")
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
# User manual
|
|
126
|
+
|
|
127
|
+
Further information can be found in the wiki for `mdpsolver` on Github.
|
|
128
|
+
|
|
129
|
+
# How to cite
|
|
130
|
+
|
|
131
|
+
[](https://zenodo.org/badge/latestdoi/294063917)
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=59.6.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[tool.setuptools]
|
|
6
|
+
include-package-data = true
|
|
7
|
+
|
|
8
|
+
[tool.setuptools.packages.find]
|
|
9
|
+
where = ["src"]
|
|
10
|
+
|
|
11
|
+
[project]
|
|
12
|
+
name = "mdpsolver"
|
|
13
|
+
version = "0.9.1"
|
|
14
|
+
authors = [
|
|
15
|
+
{ name="Anders Reenberg Andersen", email="andersra@live.dk" },
|
|
16
|
+
{ name="Jesper Fink Andersen", email="jfan@dtu.dk" },
|
|
17
|
+
]
|
|
18
|
+
description = "An efficient solver for Markov Decision Processes"
|
|
19
|
+
readme = "README.md"
|
|
20
|
+
requires-python = ">=3.10"
|
|
21
|
+
classifiers = [
|
|
22
|
+
"Development Status :: 3 - Alpha",
|
|
23
|
+
"Programming Language :: Python :: 3",
|
|
24
|
+
"License :: OSI Approved :: MIT License",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
]
|
|
27
|
+
|
|
28
|
+
[project.urls]
|
|
29
|
+
Homepage = "https://github.com/areenberg/MDPSolver"
|
|
30
|
+
Issues = "https://github.com/areenberg/MDPSolver/issues"
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from mdpsolver.model import model
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
from mdpsolver import solvermodule
|
|
2
|
+
|
|
3
|
+
#auxilary methods
|
|
4
|
+
from mdpsolver.problems.tbmaux import tbmaux
|
|
5
|
+
from mdpsolver.problems.cbmaux import cbmaux
|
|
6
|
+
|
|
7
|
+
class model:
|
|
8
|
+
|
|
9
|
+
def __init__(self):
|
|
10
|
+
self.initialize()
|
|
11
|
+
|
|
12
|
+
def initialize(self):
|
|
13
|
+
#create the solver object
|
|
14
|
+
self.mdl = solvermodule.Model()
|
|
15
|
+
|
|
16
|
+
def solve(self,
|
|
17
|
+
algorithm="mpi",
|
|
18
|
+
tolerance=1e-3,
|
|
19
|
+
update="standard",
|
|
20
|
+
parIterLim=100,
|
|
21
|
+
SORrelaxation=1.0,
|
|
22
|
+
initPolicy=list(),
|
|
23
|
+
initValueVector=list(),
|
|
24
|
+
verbose=False):
|
|
25
|
+
#solves the problem employing the specified algorithm
|
|
26
|
+
self.mdl.solve(algorithm=algorithm,
|
|
27
|
+
tolerance=tolerance,
|
|
28
|
+
update=update,
|
|
29
|
+
parIterLim=parIterLim,
|
|
30
|
+
SORrelaxation=SORrelaxation,
|
|
31
|
+
initPolicy=initPolicy,
|
|
32
|
+
initValueVector=initValueVector,
|
|
33
|
+
verbose=verbose)
|
|
34
|
+
|
|
35
|
+
def getRuntime(self):
|
|
36
|
+
#returns the runtime in milliseconds
|
|
37
|
+
return self.mdl.getRuntime()
|
|
38
|
+
|
|
39
|
+
def printPolicy(self):
|
|
40
|
+
#print the entire policy in the terminal
|
|
41
|
+
self.mdl.printPolicy()
|
|
42
|
+
|
|
43
|
+
def printValueVector(self):
|
|
44
|
+
#print the entire value vector in the terminal
|
|
45
|
+
self.mdl.printValueVector()
|
|
46
|
+
|
|
47
|
+
def getAction(self,stateIndex=0):
|
|
48
|
+
#returns an action from the optimized policy
|
|
49
|
+
return self.mdl.getAction(stateIndex=stateIndex)
|
|
50
|
+
|
|
51
|
+
def getValue(self,stateIndex=0):
|
|
52
|
+
#returns a value from the optimized value vector
|
|
53
|
+
return self.mdl.getValue(stateIndex=stateIndex)
|
|
54
|
+
|
|
55
|
+
def getPolicy(self):
|
|
56
|
+
#returns the entire policy
|
|
57
|
+
return self.mdl.getPolicy()
|
|
58
|
+
|
|
59
|
+
def getValueVector(self):
|
|
60
|
+
#returns the entire value vector
|
|
61
|
+
return self.mdl.getValueVector()
|
|
62
|
+
|
|
63
|
+
def saveToFile(self,fileName="result.csv",type="policy"):
|
|
64
|
+
#saves the optimized policy or value vector to a comma-separated file
|
|
65
|
+
return self.mdl.saveToFile(fileName=fileName,type=type)
|
|
66
|
+
|
|
67
|
+
def mdp(self,
|
|
68
|
+
discount=0.99,
|
|
69
|
+
rewards=list(),
|
|
70
|
+
rewardsElementwise=list(),
|
|
71
|
+
rewardsFromFile="rewards.csv",
|
|
72
|
+
tranMatWithZeros=list(),
|
|
73
|
+
tranMatElementwise=list(),
|
|
74
|
+
tranMatProbs=list(),
|
|
75
|
+
tranMatColumns=list(),
|
|
76
|
+
tranMatFromFile="transitions.csv"
|
|
77
|
+
):
|
|
78
|
+
#the generic MDP model
|
|
79
|
+
self.mdl.mdp(discount=discount,
|
|
80
|
+
rewards=rewards,
|
|
81
|
+
rewardsElementwise=rewardsElementwise,
|
|
82
|
+
rewardsFromFile=rewardsFromFile,
|
|
83
|
+
tranMatWithZeros=tranMatWithZeros,
|
|
84
|
+
tranMatElementwise=tranMatElementwise,
|
|
85
|
+
tranMatProbs=tranMatProbs,
|
|
86
|
+
tranMatColumns=tranMatColumns,
|
|
87
|
+
tranMatFromFile=tranMatFromFile)
|
|
88
|
+
|
|
89
|
+
def tbm(self,discount=0.99,
|
|
90
|
+
components=2,
|
|
91
|
+
stages=10,
|
|
92
|
+
replacementCost=-10,
|
|
93
|
+
setupCost=-10,
|
|
94
|
+
unexpectedFailureCost=-20,
|
|
95
|
+
expiredNotFixedCost=-1e6,
|
|
96
|
+
failureProb=0.1,
|
|
97
|
+
failureProbMin=0.01,
|
|
98
|
+
failureProbHat=0.1):
|
|
99
|
+
#the time-based maintenance problem
|
|
100
|
+
self.mdl.tbm(discount=discount,
|
|
101
|
+
components=components,
|
|
102
|
+
stages=stages,
|
|
103
|
+
replacementCost=-10,
|
|
104
|
+
setupCost=setupCost,
|
|
105
|
+
unexpectedFailureCost=unexpectedFailureCost,
|
|
106
|
+
expiredNotFixedCost=expiredNotFixedCost,
|
|
107
|
+
failureProb=failureProb,
|
|
108
|
+
failureProbMin=failureProbMin,
|
|
109
|
+
failureProbHat=failureProbHat)
|
|
110
|
+
self.aux = tbmaux(components=components,stages=stages)
|
|
111
|
+
|
|
112
|
+
def cbm(self,discount=0.99,
|
|
113
|
+
components=2,
|
|
114
|
+
stages=10,
|
|
115
|
+
pCompMat=list(),
|
|
116
|
+
preventiveCost=-5,
|
|
117
|
+
correctiveCost=-11,
|
|
118
|
+
setupCost=-4,
|
|
119
|
+
failurePenalty=-300,
|
|
120
|
+
kOfN=-1):
|
|
121
|
+
#the condition-based maintenance problem
|
|
122
|
+
self.mdl.cbm(discount=discount,
|
|
123
|
+
components=components,
|
|
124
|
+
stages=stages,
|
|
125
|
+
pCompMat=pCompMat,
|
|
126
|
+
preventiveCost=preventiveCost,
|
|
127
|
+
correctiveCost=correctiveCost,
|
|
128
|
+
setupCost=setupCost,
|
|
129
|
+
failurePenalty=failurePenalty,
|
|
130
|
+
kOfN=kOfN)
|
|
131
|
+
self.aux = cbmaux(components=components,stages=stages)
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
#Condition-based maintenance problem
|
|
4
|
+
|
|
5
|
+
#Note: The class is only used for retrieving/converting information
|
|
6
|
+
#(i.e. returning an action of the policy). The policy is optimized
|
|
7
|
+
#in the 'model' (model.py) class.
|
|
8
|
+
|
|
9
|
+
class cbmaux:
|
|
10
|
+
|
|
11
|
+
def __init__(self,components,stages):
|
|
12
|
+
self.components=components
|
|
13
|
+
self.stages=stages
|
|
14
|
+
|
|
15
|
+
def compToState(self,compStates):
|
|
16
|
+
#input: list containing the stage of each component
|
|
17
|
+
#output: the associated state index
|
|
18
|
+
prod = self.components * [1]
|
|
19
|
+
if self.components>1:
|
|
20
|
+
for i in range(self.components-2,-1,-1):
|
|
21
|
+
prod[i]=prod[i+1]*self.stages
|
|
22
|
+
stateIndex=0
|
|
23
|
+
for i in range(self.components):
|
|
24
|
+
stateIndex += compStates[i]*prod[i]
|
|
25
|
+
return stateIndex
|
|
26
|
+
|
|
27
|
+
def actionToReplace(self,actionIndex):
|
|
28
|
+
#input: action index from the optimized policy
|
|
29
|
+
#output: list of bools indicating which comps to replace
|
|
30
|
+
prod = self.components * [1]
|
|
31
|
+
if self.components>1:
|
|
32
|
+
for i in range(self.components-2,-1,-1):
|
|
33
|
+
prod[i]=prod[i+1]*2
|
|
34
|
+
replace = self.components * [False]
|
|
35
|
+
for i in range(self.components):
|
|
36
|
+
if math.floor(actionIndex/prod[i])>0:
|
|
37
|
+
replace[i]=True
|
|
38
|
+
actionIndex-=prod[i]
|
|
39
|
+
return replace
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import math
|
|
2
|
+
|
|
3
|
+
#Time-based maintenance problem
|
|
4
|
+
|
|
5
|
+
#Note: The class is only used for retrieving/converting information
|
|
6
|
+
#(i.e. returning an action of the policy). The policy is optimized
|
|
7
|
+
#in the 'model' (model.py) class.
|
|
8
|
+
|
|
9
|
+
class tbmaux:
|
|
10
|
+
|
|
11
|
+
def __init__(self,components,stages):
|
|
12
|
+
self.components=components
|
|
13
|
+
self.stages=stages
|
|
14
|
+
|
|
15
|
+
def compToState(self,compStates):
|
|
16
|
+
#input: list containing the stage of each component
|
|
17
|
+
#output: the associated state index
|
|
18
|
+
prod = self.components * [1]
|
|
19
|
+
if self.components>1:
|
|
20
|
+
for i in range(self.components-2,-1,-1):
|
|
21
|
+
prod[i]=prod[i+1]*self.stages
|
|
22
|
+
stateIndex=0
|
|
23
|
+
for i in range(self.components):
|
|
24
|
+
stateIndex += compStates[i]*prod[i]
|
|
25
|
+
return stateIndex
|
|
26
|
+
|
|
27
|
+
def actionToReplace(self,actionIndex):
|
|
28
|
+
#input: action index from the optimized policy
|
|
29
|
+
#output: list of bools indicating which comps to replace
|
|
30
|
+
prod = self.components * [1]
|
|
31
|
+
if self.components>1:
|
|
32
|
+
for i in range(self.components-2,-1,-1):
|
|
33
|
+
prod[i]=prod[i+1]*2
|
|
34
|
+
replace = self.components * [False]
|
|
35
|
+
for i in range(self.components):
|
|
36
|
+
if math.floor(actionIndex/prod[i])>0:
|
|
37
|
+
replace[i]=True
|
|
38
|
+
actionIndex-=prod[i]
|
|
39
|
+
return replace
|
|
Binary file
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: mdpsolver
|
|
3
|
+
Version: 0.9.1
|
|
4
|
+
Summary: An efficient solver for Markov Decision Processes
|
|
5
|
+
Author-email: Anders Reenberg Andersen <andersra@live.dk>, Jesper Fink Andersen <jfan@dtu.dk>
|
|
6
|
+
Project-URL: Homepage, https://github.com/areenberg/MDPSolver
|
|
7
|
+
Project-URL: Issues, https://github.com/areenberg/MDPSolver/issues
|
|
8
|
+
Classifier: Development Status :: 3 - Alpha
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Requires-Python: >=3.10
|
|
13
|
+
Description-Content-Type: text/markdown
|
|
14
|
+
License-File: LICENSE
|
|
15
|
+
|
|
16
|
+
# `mdpsolver`: An efficient solver for Markov Decision Processes
|
|
17
|
+
|
|
18
|
+
`mdpsolver` is the Python package for deriving the policies of Markov Decision Processes (MDPs) with infinite-horizon and the expected total discounted reward optimality criterion.
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
* Available on PyPI.
|
|
23
|
+
* Solver-engine developed in C++.
|
|
24
|
+
* Available optimization algorithms: *Value iteration*, *Policy iteration*, and *Modified policy iteration*.
|
|
25
|
+
* Includes a variety of input formats available for users to choose from.
|
|
26
|
+
|
|
27
|
+
# Quick start guide
|
|
28
|
+
|
|
29
|
+
The following shows how to get quickly started with `mdpsolver`.
|
|
30
|
+
|
|
31
|
+
## Installation
|
|
32
|
+
|
|
33
|
+
Download and install `mdpsolver` directly from PyPI.
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
pip install mdpsolver
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
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**.
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
#Import packages
|
|
45
|
+
import mdpsolver
|
|
46
|
+
|
|
47
|
+
#Rewards (3 states x 2 actions)
|
|
48
|
+
#e.g. choosing second action in first state gives reward=-1
|
|
49
|
+
rewards = [[5,-1],
|
|
50
|
+
[1,-2],
|
|
51
|
+
[50,0]]
|
|
52
|
+
|
|
53
|
+
#Transition probabilities (3 from_states x 2 actions x 3 to_states)
|
|
54
|
+
#e.g. choosing first action in third state gives a probability of 0.6 of staying in third state
|
|
55
|
+
tranMatWithZeros = [[[0.9,0.1,0.0],[0.1,0.9,0.0]],
|
|
56
|
+
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
|
|
57
|
+
[[0.2,0.2,0.6],[0.5,0.5,0.0]]]
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
Now, create the model object and insert the problem parameters.
|
|
61
|
+
|
|
62
|
+
```python
|
|
63
|
+
#Create model object
|
|
64
|
+
mdl = mdpsolver.model()
|
|
65
|
+
|
|
66
|
+
#Insert the problem parameters
|
|
67
|
+
mdl.mdp(discount=0.8,
|
|
68
|
+
rewards=rewards,
|
|
69
|
+
tranMatWithZeros=tranMatWithZeros)
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
We can now optimize the policy.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
mdl.solve()
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
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.
|
|
79
|
+
|
|
80
|
+
```python
|
|
81
|
+
print(mdl.getPolicy())
|
|
82
|
+
#[1, 1, 0]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Large transition matrix?
|
|
86
|
+
|
|
87
|
+
`mdpsolver` has three alternative formats for large and highly sparse transition probability matrices.
|
|
88
|
+
|
|
89
|
+
**(1) Elementwise representation (excluding elements containing zeros):**
|
|
90
|
+
|
|
91
|
+
```python
|
|
92
|
+
#[from_state,action,to_state,probability]
|
|
93
|
+
tranMatElementwise = [[0,0,0,0.9],
|
|
94
|
+
[0,0,1,0.1],
|
|
95
|
+
[0,1,0,0.1],
|
|
96
|
+
[0,1,1,0.9],
|
|
97
|
+
[1,0,0,0.4],
|
|
98
|
+
[1,0,1,0.5],
|
|
99
|
+
[1,0,2,0.1],
|
|
100
|
+
[1,1,0,0.3],
|
|
101
|
+
[1,1,1,0.5],
|
|
102
|
+
[1,1,2,0.2],
|
|
103
|
+
[2,0,0,0.2],
|
|
104
|
+
[2,0,1,0.2],
|
|
105
|
+
[2,0,2,0.6],
|
|
106
|
+
[2,1,0,0.5],
|
|
107
|
+
[2,1,1,0.5]]
|
|
108
|
+
|
|
109
|
+
mdl.mdp(discount=0.8,
|
|
110
|
+
rewards=rewards,
|
|
111
|
+
tranMatElementwise=tranMatElementwise)
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**(2) Probabilities and column (to_state) indices in separate lists:**
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
tranMatProbs = [[[0.9,0.1],[0.1,0.9]],
|
|
118
|
+
[[0.4,0.5,0.1],[0.3,0.5,0.2]],
|
|
119
|
+
[[0.2,0.2,0.6],[0.5,0.5]]]
|
|
120
|
+
|
|
121
|
+
tranMatColumns = [[[0,1],[0,1]],
|
|
122
|
+
[[0,1,2],[0,1,2]],
|
|
123
|
+
[[0,1,2],[0,1]]]
|
|
124
|
+
|
|
125
|
+
mdl.mdp(discount=0.8,
|
|
126
|
+
rewards=rewards,
|
|
127
|
+
tranMatProbs=tranMatProbs,
|
|
128
|
+
tranMatColumns=tranMatColumns)
|
|
129
|
+
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
**(3) Load the elementwise representation from a file:**
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
mdl.mdp(discount=0.8,
|
|
136
|
+
rewards=rewards,
|
|
137
|
+
tranMatFromFile="transitions.csv")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
# User manual
|
|
141
|
+
|
|
142
|
+
Further information can be found in the wiki for `mdpsolver` on Github.
|
|
143
|
+
|
|
144
|
+
# How to cite
|
|
145
|
+
|
|
146
|
+
[](https://zenodo.org/badge/latestdoi/294063917)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
src/mdpsolver/__init__.py
|
|
6
|
+
src/mdpsolver/model.py
|
|
7
|
+
src/mdpsolver/solvermodule.cp310-win_amd64.pyd
|
|
8
|
+
src/mdpsolver/solvermodule.cpython-310-x86_64-linux-gnu.so
|
|
9
|
+
src/mdpsolver.egg-info/PKG-INFO
|
|
10
|
+
src/mdpsolver.egg-info/SOURCES.txt
|
|
11
|
+
src/mdpsolver.egg-info/dependency_links.txt
|
|
12
|
+
src/mdpsolver.egg-info/top_level.txt
|
|
13
|
+
src/mdpsolver/problems/cbmaux.py
|
|
14
|
+
src/mdpsolver/problems/tbmaux.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
mdpsolver
|