Skip to main content

convert pulp model into matrix formulation for scipy.optimize.milp

Project description

pulp2mat

Convert pulp model into matrix formulation.

It can be easily thrown to scipy.optimize.milp function.

How to install

$ pip install pulp2mat

Without poetry, please look at pyproject.toml and install all dependencies manually.

Quick Example

For example, the binpacking problem can be formulated with pulp as below;

import pulp as pl
import numpy as np

item_sizes = np.array([7, 3, 3, 1, 6, 8, 4, 9, 5, 2])
num_items = len(item_sizes)
num_bins = len(item_sizes)
bin_size = 10

# Variables * must be defined as dictionaries
x = {
    (i, j): pl.LpVariable("x_{}_{}".format(i, j), cat=pl.LpBinary)
    for i in range(num_items)
    for j in range(num_bins)
}
y = {
    j: pl.LpVariable("y_{}".format(j), cat=pl.LpBinary)
    for j in range(num_bins)
}

problem = pl.LpProblem()

# Bin size constraint for each bin
for j in range(num_bins):
    problem += (
        pl.lpSum(
            x[i, j] * item_sizes[i] for i in range(num_items)
        )
        <= bin_size * y[j]
    )
# One-hot constraint for each item
for i in range(num_items):
    problem += pl.lpSum(x[i, j] for j in range(num_bins)) == 1

# Objective: minimize number of bins used.
problem += pl.lpSum(y[j] for j in range(num_bins))

the pulp.LpProblem object and the list of variable dictionaries can be converted to the matrix format for scipy.optimize.milp.

import pulp2mat
from scipy.optimize import milp
c, integrality, constraints, bounds = pulp2mat.convert_all(problem)
result = milp(c, integrality=integrality, constraints=constraints, bounds=bounds)

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

pulp2mat-0.1.5.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pulp2mat-0.1.5-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file pulp2mat-0.1.5.tar.gz.

File metadata

  • Download URL: pulp2mat-0.1.5.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pulp2mat-0.1.5.tar.gz
Algorithm Hash digest
SHA256 2f7b3297e878e0874be8c3d3c318d6366593f5d87775a34cd846d16e1180b06d
MD5 59aa7c5293130e206fdbc50b26f32139
BLAKE2b-256 97cae3cfb044cc2beea2f58a57d6a566dfde380379bbf309c2392d59ed82a61d

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulp2mat-0.1.5.tar.gz:

Publisher: python-publish.yml on rtonoue/pulp2mat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pulp2mat-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pulp2mat-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for pulp2mat-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 c62af5733a40395574431032a844f7398ae363a3798a5bab50e97548786bc301
MD5 1c1ff2a1d09917a6a47b893bbd23c411
BLAKE2b-256 3bd8772deb0c4d120518d9f4d19a37d54b3b90dacfb7d5f557268015a6e7de23

See more details on using hashes here.

Provenance

The following attestation bundles were made for pulp2mat-0.1.5-py3-none-any.whl:

Publisher: python-publish.yml on rtonoue/pulp2mat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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