mdpsolver 0.9.8__tar.gz → 0.9.9__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.
Files changed (32) hide show
  1. {mdpsolver-0.9.8/src/mdpsolver.egg-info → mdpsolver-0.9.9}/PKG-INFO +5 -3
  2. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/pyproject.toml +3 -2
  3. mdpsolver-0.9.9/src/mdpsolver/__init__.py +1 -0
  4. mdpsolver-0.9.9/src/mdpsolver/model.py +189 -0
  5. mdpsolver-0.9.9/src/mdpsolver/problems/cbmaux.py +40 -0
  6. mdpsolver-0.9.9/src/mdpsolver/problems/tbmaux.py +40 -0
  7. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cpython-313-x86_64-linux-gnu.so +0 -0
  8. {mdpsolver-0.9.8 → mdpsolver-0.9.9/src/mdpsolver.egg-info}/PKG-INFO +5 -3
  9. mdpsolver-0.9.9/tests/test1.py +713 -0
  10. mdpsolver-0.9.9/tests/test2.py +87 -0
  11. mdpsolver-0.9.8/src/mdpsolver/__init__.py +0 -1
  12. mdpsolver-0.9.8/src/mdpsolver/model.py +0 -139
  13. mdpsolver-0.9.8/src/mdpsolver/problems/cbmaux.py +0 -39
  14. mdpsolver-0.9.8/src/mdpsolver/problems/tbmaux.py +0 -39
  15. mdpsolver-0.9.8/tests/test1.py +0 -686
  16. mdpsolver-0.9.8/tests/test2.py +0 -81
  17. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/LICENSE +0 -0
  18. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/MANIFEST.in +0 -0
  19. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/README.md +0 -0
  20. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/setup.cfg +0 -0
  21. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/problems/__pycache__/cbmaux.cpython-310.pyc +0 -0
  22. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/problems/__pycache__/tbmaux.cpython-310.pyc +0 -0
  23. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cp310-win_amd64.pyd +0 -0
  24. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cp311-win_amd64.pyd +0 -0
  25. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cp312-win_amd64.pyd +0 -0
  26. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cp313-win_amd64.pyd +0 -0
  27. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cpython-310-x86_64-linux-gnu.so +0 -0
  28. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cpython-311-x86_64-linux-gnu.so +0 -0
  29. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver/solvermodule.cpython-312-x86_64-linux-gnu.so +0 -0
  30. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver.egg-info/SOURCES.txt +0 -0
  31. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver.egg-info/dependency_links.txt +0 -0
  32. {mdpsolver-0.9.8 → mdpsolver-0.9.9}/src/mdpsolver.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: mdpsolver
3
- Version: 0.9.8
3
+ Version: 0.9.9
4
4
  Summary: A fast solver for Markov Decision Processes
5
5
  Author-email: Anders Reenberg Andersen <andersra@live.dk>, Jesper Fink Andersen <jesperfinkandersen@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/areenberg/MDPSolver
@@ -8,10 +8,12 @@ Project-URL: Issues, https://github.com/areenberg/MDPSolver/issues
8
8
  Classifier: Development Status :: 3 - Alpha
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
11
+ Classifier: Operating System :: Microsoft :: Windows
12
+ Classifier: Operating System :: POSIX :: Linux
12
13
  Requires-Python: >=3.10
13
14
  Description-Content-Type: text/markdown
14
15
  License-File: LICENSE
16
+ Dynamic: license-file
15
17
 
16
18
  # MDPSolver
17
19
 
@@ -10,7 +10,7 @@ where = ["src"]
10
10
 
11
11
  [project]
12
12
  name = "mdpsolver"
13
- version = "0.9.8"
13
+ version = "0.9.9"
14
14
  authors = [
15
15
  { name="Anders Reenberg Andersen", email="andersra@live.dk" },
16
16
  { name="Jesper Fink Andersen", email="jesperfinkandersen@gmail.com" },
@@ -22,7 +22,8 @@ classifiers = [
22
22
  "Development Status :: 3 - Alpha",
23
23
  "Programming Language :: Python :: 3",
24
24
  "License :: OSI Approved :: MIT License",
25
- "Operating System :: OS Independent",
25
+ "Operating System :: Microsoft :: Windows",
26
+ "Operating System :: POSIX :: Linux"
26
27
  ]
27
28
 
28
29
  [project.urls]
@@ -0,0 +1 @@
1
+ from mdpsolver.model import model
@@ -0,0 +1,189 @@
1
+ """
2
+ MDPSolver
3
+
4
+ This file defines a `model` class that wraps around the `solvermodule` to create
5
+ and solve Markov Decision Processes (MDPs). It provides methods to configure the model,
6
+ solve it, and extract the resulting policy and value vectors.
7
+ """
8
+
9
+ from mdpsolver import solvermodule
10
+
11
+
12
+ class model:
13
+ """
14
+ Creates an MDPSolver object.
15
+
16
+ This class provides methods to initialize, solve, and return results
17
+ from an MDP model.
18
+ """
19
+
20
+ def __init__(self):
21
+ """Initialize the model by creating a solver module object."""
22
+ self.initialize()
23
+
24
+ def initialize(self):
25
+ """Create and initialize the solver module object."""
26
+ self.mdl = solvermodule.Model()
27
+
28
+ def solve(
29
+ self,
30
+ algorithm="mpi",
31
+ tolerance=1e-3,
32
+ update="standard",
33
+ criterion="discounted",
34
+ parIterLim=100,
35
+ SORrelaxation=1.0,
36
+ initPolicy=list(),
37
+ initValueVector=list(),
38
+ verbose=False,
39
+ postProcessing=True,
40
+ makeFinalCheck=True,
41
+ parallel=True,
42
+ ):
43
+ """
44
+ Derive an epsilon-optimal policy for the selected MDP model.
45
+
46
+ Args:
47
+ algorithm (str): Algorithm to use.
48
+ tolerance (float): Convergence threshold for the algorithm.
49
+ update (str): The value-update method.
50
+ criterion (str): The optimality criterion.
51
+ parIterLim (int): The partial evaluation limit employed in the modified policy iteration algorithm.
52
+ SORrelaxation (float): Relaxation parameter for the Successive Over-Relaxation method.
53
+ initPolicy (list): A 1D-list defining the initial policy. This option can be used for "warm starting" the optimization procedure.
54
+ initValueVector (list): A 1D-list defining the initial value vector. This option can be used for "warm starting" the optimization procedure.
55
+ verbose (bool): If True, prints solver progress to console.
56
+ postProcessing (bool): If True, performs post-processing after solving.
57
+ makeFinalCheck (bool): If True, makes a final check of the value vector. This process checks if the resulting values are reasonable.
58
+ parallel (bool): If True, enables parallel computation for faster solving. This option is only available for the custom MDP model with standard updates.
59
+
60
+ Returns:
61
+ None
62
+ """
63
+ self.mdl.solve(
64
+ algorithm=algorithm,
65
+ tolerance=tolerance,
66
+ update=update,
67
+ criterion=criterion,
68
+ parIterLim=parIterLim,
69
+ SORrelaxation=SORrelaxation,
70
+ initPolicy=initPolicy,
71
+ initValueVector=initValueVector,
72
+ verbose=verbose,
73
+ postProcessing=postProcessing,
74
+ makeFinalCheck=makeFinalCheck,
75
+ parallel=parallel,
76
+ )
77
+
78
+ def getRuntime(self):
79
+ """
80
+ Get the runtime of the last solver execution.
81
+
82
+ Returns:
83
+ float: Runtime in milliseconds.
84
+ """
85
+ return self.mdl.getRuntime()
86
+
87
+ def printPolicy(self):
88
+ """Print the entire policy to the terminal."""
89
+ self.mdl.printPolicy()
90
+
91
+ def printValueVector(self):
92
+ """Print the entire value vector to the terminal."""
93
+ self.mdl.printValueVector()
94
+
95
+ def getAction(self, stateIndex=0):
96
+ """
97
+ Get the action from the optimized policy for a specific state.
98
+
99
+ Args:
100
+ stateIndex (int): Index of the state.
101
+
102
+ Returns:
103
+ int: Action for the given state.
104
+ """
105
+ return self.mdl.getAction(stateIndex=stateIndex)
106
+
107
+ def getValue(self, stateIndex=0):
108
+ """
109
+ Get the value from the optimized value vector for a specific state.
110
+
111
+ Args:
112
+ stateIndex (int): Index of the state.
113
+
114
+ Returns:
115
+ float: Value for the given state.
116
+ """
117
+ return self.mdl.getValue(stateIndex=stateIndex)
118
+
119
+ def getPolicy(self):
120
+ """
121
+ Get the entire optimized policy.
122
+
123
+ Returns:
124
+ list: Optimized policy.
125
+ """
126
+ return self.mdl.getPolicy()
127
+
128
+ def getValueVector(self):
129
+ """
130
+ Get the entire optimized value vector.
131
+
132
+ Returns:
133
+ list: Optimized value vector.
134
+ """
135
+ return self.mdl.getValueVector()
136
+
137
+ def saveToFile(self, fileName="result.csv", type="policy"):
138
+ """
139
+ Save the optimized policy or value vector to a file.
140
+
141
+ Args:
142
+ fileName (str): Name of the output file.
143
+ type (str): Type of result to save ('policy' or 'value').
144
+
145
+ Returns:
146
+ None
147
+ """
148
+ return self.mdl.saveToFile(fileName=fileName, type=type)
149
+
150
+ def mdp(
151
+ self,
152
+ discount=0.99,
153
+ rewards=list(),
154
+ rewardsElementwise=list(),
155
+ rewardsFromFile="rewards.csv",
156
+ tranMatWithZeros=list(),
157
+ tranMatElementwise=list(),
158
+ tranMatProbs=list(),
159
+ tranMatColumns=list(),
160
+ tranMatFromFile="transitions.csv",
161
+ ):
162
+ """
163
+ Define the generic MDP model.
164
+
165
+ Args:
166
+ discount (float): Discount factor.
167
+ rewards (list): A 2D-list containing the reward (float) of a particular action in a particular state.
168
+ rewardsElementwise (list): Alternative reward format. A 2D-list where each row corresponds to a combination of a state and an action.
169
+ rewardsFromFile (str): Load the rewards from a comma-separated (,) file.
170
+ tranMatWithZeros (list): A 3D-list containing the transition probabilities.
171
+ tranMatElementwise (list): Sparse transition probabilities (option 1). A 2D-list where each row corresponds to a combination of a current state, an action, and a next state.
172
+ tranMatProbs (list): Sparse transition probabilities (option 2, part 1). A 3D-list containing the non-zero transition probabilities.
173
+ tranMatColumns (list): Sparse transition probabilities (option 2, part 2). A 3D-list containing the columns of the non-zero transition probabilities.
174
+ tranMatFromFile (str): Load the transition probabilities from a comma-separated (,) file.
175
+
176
+ Returns:
177
+ None
178
+ """
179
+ self.mdl.mdp(
180
+ discount=discount,
181
+ rewards=rewards,
182
+ rewardsElementwise=rewardsElementwise,
183
+ rewardsFromFile=rewardsFromFile,
184
+ tranMatWithZeros=tranMatWithZeros,
185
+ tranMatElementwise=tranMatElementwise,
186
+ tranMatProbs=tranMatProbs,
187
+ tranMatColumns=tranMatColumns,
188
+ tranMatFromFile=tranMatFromFile,
189
+ )
@@ -0,0 +1,40 @@
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
+
10
+ class cbmaux:
11
+
12
+ def __init__(self, components, stages):
13
+ self.components = components
14
+ self.stages = stages
15
+
16
+ def compToState(self, compStates):
17
+ # input: list containing the stage of each component
18
+ # output: the associated state index
19
+ prod = self.components * [1]
20
+ if self.components > 1:
21
+ for i in range(self.components - 2, -1, -1):
22
+ prod[i] = prod[i + 1] * self.stages
23
+ stateIndex = 0
24
+ for i in range(self.components):
25
+ stateIndex += compStates[i] * prod[i]
26
+ return stateIndex
27
+
28
+ def actionToReplace(self, actionIndex):
29
+ # input: action index from the optimized policy
30
+ # output: list of bools indicating which comps to replace
31
+ prod = self.components * [1]
32
+ if self.components > 1:
33
+ for i in range(self.components - 2, -1, -1):
34
+ prod[i] = prod[i + 1] * 2
35
+ replace = self.components * [False]
36
+ for i in range(self.components):
37
+ if math.floor(actionIndex / prod[i]) > 0:
38
+ replace[i] = True
39
+ actionIndex -= prod[i]
40
+ return replace
@@ -0,0 +1,40 @@
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
+
10
+ class tbmaux:
11
+
12
+ def __init__(self, components, stages):
13
+ self.components = components
14
+ self.stages = stages
15
+
16
+ def compToState(self, compStates):
17
+ # input: list containing the stage of each component
18
+ # output: the associated state index
19
+ prod = self.components * [1]
20
+ if self.components > 1:
21
+ for i in range(self.components - 2, -1, -1):
22
+ prod[i] = prod[i + 1] * self.stages
23
+ stateIndex = 0
24
+ for i in range(self.components):
25
+ stateIndex += compStates[i] * prod[i]
26
+ return stateIndex
27
+
28
+ def actionToReplace(self, actionIndex):
29
+ # input: action index from the optimized policy
30
+ # output: list of bools indicating which comps to replace
31
+ prod = self.components * [1]
32
+ if self.components > 1:
33
+ for i in range(self.components - 2, -1, -1):
34
+ prod[i] = prod[i + 1] * 2
35
+ replace = self.components * [False]
36
+ for i in range(self.components):
37
+ if math.floor(actionIndex / prod[i]) > 0:
38
+ replace[i] = True
39
+ actionIndex -= prod[i]
40
+ return replace
@@ -1,6 +1,6 @@
1
- Metadata-Version: 2.2
1
+ Metadata-Version: 2.4
2
2
  Name: mdpsolver
3
- Version: 0.9.8
3
+ Version: 0.9.9
4
4
  Summary: A fast solver for Markov Decision Processes
5
5
  Author-email: Anders Reenberg Andersen <andersra@live.dk>, Jesper Fink Andersen <jesperfinkandersen@gmail.com>
6
6
  Project-URL: Homepage, https://github.com/areenberg/MDPSolver
@@ -8,10 +8,12 @@ Project-URL: Issues, https://github.com/areenberg/MDPSolver/issues
8
8
  Classifier: Development Status :: 3 - Alpha
9
9
  Classifier: Programming Language :: Python :: 3
10
10
  Classifier: License :: OSI Approved :: MIT License
11
- Classifier: Operating System :: OS Independent
11
+ Classifier: Operating System :: Microsoft :: Windows
12
+ Classifier: Operating System :: POSIX :: Linux
12
13
  Requires-Python: >=3.10
13
14
  Description-Content-Type: text/markdown
14
15
  License-File: LICENSE
16
+ Dynamic: license-file
15
17
 
16
18
  # MDPSolver
17
19