Skip to main content

Generic function for sequential dynamic programming

Project description

Tox result

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 returns True iff 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:

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

dynprog-0.1.2.tar.gz (24.2 kB view details)

Uploaded Source

Built Distribution

dynprog-0.1.2-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

File details

Details for the file dynprog-0.1.2.tar.gz.

File metadata

  • Download URL: dynprog-0.1.2.tar.gz
  • Upload date:
  • Size: 24.2 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for dynprog-0.1.2.tar.gz
Algorithm Hash digest
SHA256 667a5360b297ba79a67558a7d617aa2b07234e38b99b80cfb37c6264055383da
MD5 b1dbf053e4b60a99979a7bc62beb0cec
BLAKE2b-256 71c2dcb5cb4ae1cedabd033bf5f769a2faa300ca0c3046b7b5920a7c6539b768

See more details on using hashes here.

File details

Details for the file dynprog-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: dynprog-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 19.7 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for dynprog-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2cde322f2b8fdcf32b150480f1e3d0659258a365b62a6ab5a297bf54ece1e175
MD5 b5f2e851cecee1b6c4a1915f357d05ae
BLAKE2b-256 215c56b3770e909a0871b7a1e65ee5c67dba70739246191f25645bff5a9f7959

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page