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)
    lb=np.array([0.0, 0.0]),          # Lower bounds (optional, default: zeros)
    ub=np.array([10.0, 10.0]),        # Upper bounds (optional, default: inf)
)

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
lb ndarray Lower bounds (n,) zeros
ub ndarray Upper bounds (n,) inf

Turning Off Bounds

By default, variables are constrained to be non-negative (lb=0, ub=∞). To allow unbounded variables:

# Allow negative values (unbounded below)
lb = np.full(n, -np.inf)

# Remove upper bounds (unbounded above) - this is already the default
ub = np.full(n, np.inf)

# Completely unbounded variables
problem = Problem(
    c=np.array([1.0, 2.0]),
    lb=np.array([-np.inf, -np.inf]),  # Can be negative
    ub=np.array([np.inf, np.inf]),    # Can be arbitrarily large
)

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.1.tar.gz (17.9 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.1-cp39-abi3-win_amd64.whl (1.6 MB view details)

Uploaded CPython 3.9+Windows x86-64

batch_lp-1.0.1-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.1-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.1.tar.gz.

File metadata

  • Download URL: batch_lp-1.0.1.tar.gz
  • Upload date:
  • Size: 17.9 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.1.tar.gz
Algorithm Hash digest
SHA256 a0403a02f171e786738db88a194e2026befd4787e40fbb25432d2419a648af4e
MD5 4cf2ecd9363e0b32e98a97fafc3854c8
BLAKE2b-256 4c4a44ec5d80a7ddd9d938226be79dcc516155827a5b0a564f085ab7382120a7

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.1.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.1-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: batch_lp-1.0.1-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.1-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 c43ea0a87a7c275d3d15c0687b82509c0abcf7697ec5937d21943d2ca081fd03
MD5 69f4dee1401c1100e0034e3f1e55444d
BLAKE2b-256 c55634f82ac6c022275b338d3c5a4b72e681e01eba4847a97ee9d14e7da2e92f

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.1-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.1-cp39-abi3-manylinux_2_38_x86_64.whl.

File metadata

File hashes

Hashes for batch_lp-1.0.1-cp39-abi3-manylinux_2_38_x86_64.whl
Algorithm Hash digest
SHA256 a314c9e1504249656743d96b2908dc8788f8e35ad167dec29969f76cde3f274f
MD5 97ffff9642eb018c98bec98464584eb0
BLAKE2b-256 c0a43017ba7fa8a9c67f4c89059afa52c452193fa868a5f1df8948e43d04ed4e

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.1-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.1-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for batch_lp-1.0.1-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0b4449faee0519f60c5f9a6dcf13398b389789ef06f0dcc1702f59ffe995691a
MD5 c03e29d2dd0d90414b37624d79b51d09
BLAKE2b-256 aca9402d8fe5469fce4252911786363c6d6f6376a5f45ca62542e94eff49926a

See more details on using hashes here.

Provenance

The following attestation bundles were made for batch_lp-1.0.1-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