Skip to main content

High-performance batched linear programming solver using HiGHS with parallel execution

Project description

batch-lp

PyPI version License: MIT

A high-performance batched linear programming solver using the HiGHS solver with parallel execution capabilities. Built with Rust and optimized for solving multiple LP problems simultaneously.

Features

  • ⚡ Fast Parallel Solving: Solve multiple LP problems simultaneously using Rayon's work-stealing parallelism
  • 🎯 Standard LP Form: Supports inequality constraints, equality constraints, and variable bounds
  • 🐍 Python Bindings: Easy-to-use Python interface with NumPy arrays via PyO3
  • 🚀 High Performance: Built on the HiGHS solver, one of the fastest open-source LP solvers
  • 🔧 Thread Control: Optionally specify the number of threads for parallel solving

Installation

From PyPI (recommended)

pip install batch-lp

From source

pip install maturin
git clone https://github.com/landonclark97/batch-lp.git
cd batch-lp
maturin develop --release

Quick Start

Solve a Single LP

import numpy as np
from batch_lp import solve_lp, Problem

# Problem: min -x - y
#          s.t. x + y <= 2
#               x, y >= 0

c = np.array([-1.0, -1.0])
A = np.array([[1.0, 1.0]])
b = np.array([2.0])

problem = Problem(c=c, A=A, b=b)
solution = solve_lp(problem)

print(f"Status: {solution.status}")
print(f"Objective: {solution.objective}")
print(f"Solution: {solution.x}")

Solve Multiple LPs in Parallel

import numpy as np
from batch_lp import solve_batch_lp, Problem

# Create 100 LP problems using the Problem class
problems = []
for i in range(100):
    problems.append(Problem(
        c=np.array([1.0, -1.0]),
        A=np.array([[1.0, 1.0], [1.0, -1.0]]),
        b=np.array([float(i + 2), 1.0]),
    ))

# Solve all problems in parallel (uses all CPU cores by default)
solutions = solve_batch_lp(problems)

for i, sol in enumerate(solutions[:5]):
    print(f"Problem {i}: {sol.status}, obj={sol.objective}")

Control Number of Threads

# Use 4 threads
solutions = solve_batch_lp(problems, num_threads=4)

# Use single thread (sequential)
solutions = solve_batch_lp(problems, num_threads=1)

# Use all available cores (default)
solutions = solve_batch_lp(problems)

Standard Form

The solver accepts problems in the form:

minimize    c^T x
subject to  A x <= b          (inequality constraints)
            A_eq x == b_eq    (equality constraints)
            lb <= x <= ub     (variable bounds)

Problem Class

Both solve_lp() and solve_batch_lp() use the Problem class to define LP problems:

from batch_lp import Problem
import numpy as np

problem = Problem(
    c=np.array([1.0, 2.0]),           # Objective coefficients (required)
    A=np.array([[1.0, 1.0]]),         # Inequality constraint matrix (optional)
    b=np.array([5.0]),                # Inequality RHS (optional)
    A_eq=np.array([[1.0, -1.0]]),     # Equality constraint matrix (optional)
    b_eq=np.array([0.0]),             # Equality RHS (optional)
    bounds=(0, 10),                   # Variable bounds (optional, default: (0, None))
)

Parameters

Parameter Type Description Default
c ndarray Objective coefficients (n,) Required
A ndarray Inequality constraint matrix (m_ineq, n) None
b ndarray Inequality RHS (m_ineq,) None
A_eq ndarray Equality constraint matrix (m_eq, n) None
b_eq ndarray Equality RHS (m_eq,) None
bounds tuple Variable bounds as (lower, upper) (0, None)

Bounds

The bounds parameter is a tuple (lower, upper) where each element can be:

  • None — no bound (-inf for lower, +inf for upper)
  • scalar — broadcast to all variables
  • ndarray — per-variable bounds
# Default: non-negative variables (0 <= x <= inf)
Problem(c=c, A=A, b=b)

# Scalar bounds broadcast to all variables
Problem(c=c, bounds=(0, 10))

# Completely unbounded variables
Problem(c=c, bounds=(None, None))

# Per-variable bounds
Problem(c=c, bounds=(np.array([0, -1, 0]), np.array([10, 5, 20])))

# Mix scalar and per-variable
Problem(c=c, bounds=(0, np.array([10, 5, 20])))

Examples

See example.py for comprehensive examples including:

  • Single LP solving
  • Equality constraints
  • Variable bounds
  • Batch solving
  • Thread control
  • Portfolio optimization

Run the example:

python example.py

How It Works

batch-lp leverages Rust's zero-cost abstractions and Rayon's work-stealing parallelism to efficiently solve multiple LP problems:

  1. No GIL: Python's Global Interpreter Lock is released during solving, enabling true parallelism
  2. Work Stealing: Rayon automatically balances load across threads
  3. No Overhead: Direct Rust ↔ HiGHS communication without Python overhead
  4. Shared Memory: No data pickling or IPC overhead like multiprocessing

Development

Building

# Development build
maturin develop

# Release build (optimized)
maturin develop --release

# Build wheel
maturin build --release

Testing

# Run Rust tests
cargo test

# Run Python example
python example.py

License

MIT License - see LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Acknowledgments

  • Built on top of the excellent HiGHS solver
  • Uses Rayon for parallelism
  • Python bindings via PyO3

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

batch_lp-1.0.2.tar.gz (18.2 kB view details)

Uploaded Source

Built Distributions

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

batch_lp-1.0.2-cp39-abi3-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.9+Windows x86-64

batch_lp-1.0.2-cp39-abi3-manylinux_2_38_x86_64.whl (2.2 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.38+ x86-64

batch_lp-1.0.2-cp39-abi3-macosx_11_0_arm64.whl (1.7 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

File details

Details for the file batch_lp-1.0.2.tar.gz.

File metadata

  • Download URL: batch_lp-1.0.2.tar.gz
  • Upload date:
  • Size: 18.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for batch_lp-1.0.2.tar.gz
Algorithm Hash digest
SHA256 5f1098a7d91ea732b9a8670efd6e1d1507a090fc326c2fc535afd3d1662216b0
MD5 8e9172ae8b749d146801fef0aba3abd1
BLAKE2b-256 e0b3a0861204fbc8eb97b3486bc97d643daed3b6158a8b6df7367332b8e30434

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.2.tar.gz:

Publisher: release.yml on landonclark97/batch-lp

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

File details

Details for the file batch_lp-1.0.2-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: batch_lp-1.0.2-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 1.6 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for batch_lp-1.0.2-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 1600ede0455ed11ab55b3ef55092a66cbc0822d009066276323ab3a79771b0c4
MD5 06839e0953597c909972e45305a9cf0d
BLAKE2b-256 4e8ff0c02a73d119cedb7e4e801533d3c203adaee004282473a538b37ae2dc18

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.2-cp39-abi3-win_amd64.whl:

Publisher: release.yml on landonclark97/batch-lp

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

File details

Details for the file batch_lp-1.0.2-cp39-abi3-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for batch_lp-1.0.2-cp39-abi3-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 5f7db5f3400b2e631e0c1de7892581e43be47252f71e64b43ac4b20921147311
MD5 b2b51eff5a5ef74900cb01dd91f7c575
BLAKE2b-256 c496dc62a8c4f06809360d168b0fc42f2cd2356f203ab1aa885c911596d7eaab

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.2-cp39-abi3-manylinux_2_38_x86_64.whl:

Publisher: release.yml on landonclark97/batch-lp

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

File details

Details for the file batch_lp-1.0.2-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for batch_lp-1.0.2-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 91fdcc64810df05ab15417fa4f6980b7b9957f238a46faf9303af264a09e679c
MD5 872787f7a7e7fdc2d1c27cb38beaff21
BLAKE2b-256 bbbfbbb84a67f91244321a9dff0a4f75aa127f5824b9f18538fc2d370b0f40cd

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.2-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release.yml on landonclark97/batch-lp

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