dynprog
Implements a generic routine for sequential dynamic programming, based on the "Simple DP" framework defined in:
Gerhard J. Woeginger, When Does a Dynamic Programming Formulation Guarantee the Existence of a FPTAS?, INFORMS journal of computing, 2000.
Installation
pip install dynprog
Usage
To define a dynamic program, extend the class SequentialDynamicProgram and override the methods:
initial_states- returns a list of states where the search for a solution starts.transition_functions- returns a list of functions, each of which accepts a state and an input, and returns a new state.filter_functions- returns a list of functions, one for each transition function. Each of which accepts a state and an input, and returnsTrueiff the transition is feasible.value_function- returns a function that accepts a state and returns its value (higher is better).
These functions are sufficient for computing the optimal (maximum) value, using the method max_value.
In order to also construct the optimal solution, you need to override two additional methods:
initial_solution- returns an initial (usually empty) solution.construction_functions- returns a list of functions, one for each transition function. Each of which accepts a solution and an input, and returns a new solution.
Example
Let us define a dynamic program for solving the Subset Sum problem. Here, the state contains a single number: the sum of items in the subset. Here is the class definition:
from dynprog.sequential import SequentialDynamicProgram
class SubsetSumDP(SequentialDynamicProgram):
def __init__(self, capacity:int):
self.capacity = capacity
def initial_states(self):
return [0] # There is one initial state - 0.
def initial_solution(self):
return [] # There is one initial solution - an empty subset.
def transition_functions(self):
return [ # There are two possible transitions from each state and input:
lambda state,input: state+input, # adding the input
lambda state,input: state+0, # not adding the input
]
def filter_functions(self):
return [ # There are two corresponding filter functions:
lambda state,input: state+input<=self.capacity, # adding the input
lambda _,__: True, # not adding the input
]
def construction_functions(self):
return [ # There are two possible construction functions:
lambda solution,input: solution+[input], # adding the input
lambda solution,_: solution, # not adding the input
]
def value_function(self): # The value of a state is just the state itself:
return lambda state: state
We can use it to compute the optimal value:
SubsetSumDP(capacity=4005).max_value(inputs=[100,200,400,700,1100,1600,2200,2900,3700])
or the optimal solution:
SubsetSumDP(capacity=4005).max_value_solution(inputs=[100,200,400,700,1100,1600,2200,2900,3700])[2]
More examples
More examples can be found in the examples folder:
Metadata
Release files for dynprog 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| dynprog-0.1.2.tar.gz | 24.2 kB | Without provenance | Details for dynprog-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| dynprog-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for dynprog-0.1.2-py3-none-any.whl |
Total release size: 43.9 kB
Release files / dynprog-0.1.2.tar.gz
| Download URL | dynprog-0.1.2.tar.gz |
|---|---|
| Size | 24.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
667a5360b297ba79a67558a7d617aa2b07234e38b99b80cfb37c6264055383da
|
|
BLAKE2b-256 checksum How to use checksums |
71c2dcb5cb4ae1cedabd033bf5f769a2faa300ca0c3046b7b5920a7c6539b768
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Release files / dynprog-0.1.2-py3-none-any.whl
| Download URL | dynprog-0.1.2-py3-none-any.whl |
|---|---|
| Size | 19.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2cde322f2b8fdcf32b150480f1e3d0659258a365b62a6ab5a297bf54ece1e175
|
|
BLAKE2b-256 checksum How to use checksums |
215c56b3770e909a0871b7a1e65ee5c67dba70739246191f25645bff5a9f7959
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.10.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2
|
Security for dynprog 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.