Skip to main content

prtpy

Pytest result PyPI version

Python code for multiway number partitioning and bin packing algorithms.

Supports several exact and approximate algorithms, with several input formats, optimization objectives and output formats.

Installation

Basic installation:

pip install prtpy

To run simulation experiments:

pip install prtpy[simulations]

To speed up the ILP code, you can install the GUROBI solver. See the documentation of Python-MIP for more information.

Usage

The function prtpy.partition can be used to activate all number-partitioning algorithms. For example, to partition the values [1,2,3,4,5] into two bins using the greedy approximation algorithm, do:

import prtpy
prtpy.partition(algorithm=prtpy.partitioning.greedy, numbins=2, items=[1,2,3,4,5])

To use the exact algorithm based on ILP, and maximize the smallest sum:

prtpy.partition(algorithm=prtpy.partitioning.ilp, numbins=2, items=[1,2,3,4,5], objective=prtpy.obj.MaximizeSmallestSum)

Similarly, the function prtpy.packing can be used to activate all bin-packing algorithms.

For more features and examples, see:

  1. Number-partitioning algorithms;
  2. Bin-packing algorithms;
  3. Bin-covering algorithms;
  4. Input formats;
  5. Optimization objectives;
  6. Output formats.

Adding new algorithms

To add a new algorithm for number partitioning, write a function that accepts the following parameters:

  • binner - an item of class Binner structure (see below).
  • numbins - an integer - how many bins to put the items into.
  • items - a list of item-names (the item values are given by the function binner.valueof).
  • Any other parameters that are required by your algorithm.

For an example, see the implementation of existing algorithms, e.g. greedy.

To add a new algorithm for bin packing or bin covering, write a function that accepts the following parameters:

  • binner - an item of class Binner structure (see below).
  • binsize - the capacity of a bin (maximum sum in bin-packing; minimum sum in bin-covering).
  • items - a list of item-names (the item values are given by the function binner.valueof).
  • Any other parameters that are required by your algorithm.

For an example, see the implementation of existing algorithms, e.g. first_fit.

The Binner class contains methods for handling bins in a generic way --- handling both item-names and item-values with a single interface. The main supported methods are:

  • bins = binner.new_bins(numbins) --- constructs a new array of empty bins.
  • binner.add_item_to_bin(bins, item, index) --- updates the given bins array by adding the given item to the bin with the given index.
  • binner.sums(bins) --- extracts, from the bins array, only the array of sums.
  • bins = binner.add_empty_bins(bins, numbins) --- creates a new bins array with some additional empty bins at the end.
  • bins = binner.remove_bins(bins, numbins) --- creates a new bins array with some bins removed at the end.
  • binner.valueof(item) --- returns the value (size) of the given item.

Related libraries

Limitations

The package is tested on Python versions 3.8, 3.9 and 3.10. Other versions are not supported.

Metadata

Release files for prtpy 0.8.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for prtpy 0.8.3
File Size Uploaded
prtpy-0.8.3.tar.gz 61.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for prtpy 0.8.3
File Interpreter ABI Platform
prtpy-0.8.3-py3-none-any.whl Python 3 none any Details

Total release size: 125.4 kB

Release files / prtpy-0.8.3.tar.gz

Download URL prtpy-0.8.3.tar.gz
Size 61.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a6cec04a8fd7c1add066aef6292a992048527ec8c0e6e0780e46acd5d02ee36a
BLAKE2b-256 checksum
How to use checksums
3d23907d3b406c8a9b506d11008163c0de10d7c643f91311fc3cd8d71228ab2a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release files / prtpy-0.8.3-py3-none-any.whl

Download URL prtpy-0.8.3-py3-none-any.whl
Size 64.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cb3159172589104a513bac64218ddfe773ecd58f4a25dedcc1f099cf8096700f
BLAKE2b-256 checksum
How to use checksums
c38961611b2146446a83cfe9103f91c4b6e30026ab7070dd6807dba5b65cce18
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.11

Release history Release notifications | RSS feed

This release

0.8.3 This release

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page