HPR-LP-C Python Interface
Source: official HPR-LP-C documentation.
Python interface for HPR-LP-C 0.1.3. The build detects the GPU architecture;
CUDA 13.3+ uses cusparseSpMVOp, while older toolkits use cusparseSpMV.
Installation
cd bindings/python
python -m pip install . # or: python -m pip install -e .
Examples
Example 1: Build model directly from matrices
cd examples
python example_direct_lp.py # Solve from arrays
Quick overview: The following snippet demonstrates how to define and solve an LP problem directly from matrices. For a complete version with additional options, see example_direct_lp.py.
import numpy as np
from scipy import sparse
import hprlp
# Define LP: minimize c'x subject to AL <= Ax <= AU, l <= x <= u
A = sparse.csr_matrix([[1.0, 2.0], [3.0, 1.0]])
AL = np.array([-np.inf, -np.inf])
AU = np.array([10.0, 12.0])
l = np.array([0.0, 0.0])
u = np.array([np.inf, np.inf])
c = np.array([-3.0, -5.0])
# Create model
model = hprlp.Model.from_arrays(A, AL, AU, l, u, c)
# Configure solver
param = hprlp.Parameters()
param.stop_tol = 1e-9
param.device_number = 0
# Solve
result = model.solve(param)
if result.is_optimal():
print(f"Optimal: {result.primal_obj}")
print(f"Solution: {result.x}")
Model.from_arrays(A, AL, AU, l, u, c)
Create an LP model from matrices and vectors.
Arguments:
A- Constraint matrix (scipy sparse or dense)AL,AU- Constraint bounds (numpy arrays)l,u- Variable bounds (numpy arrays)c- Objective coefficients (numpy array)
Returns: Model object
Example 2: Solve from MPS File
cd examples
python example_mps_file.py # Solve from MPS file
Quick overview: The following snippet demonstrates how to define and solve an LP problem directly from an MPS file. For a complete version with additional options, see example_mps_file.py.
import hprlp
# Create model from MPS file
model = hprlp.Model.from_mps("problem.mps")
# Solve
result = model.solve()
print(result)
Model.from_mps(filename)
Create an LP model from the MPS file.
Arguments:
filename- Path to MPS file
Returns: Model object
Model.solve(param=None)
Solve the LP model.
Arguments:
param- (Optional) Parameters object
Returns: Results object
Parameters
Solver configuration:
max_iter- Maximum iterations (default: unlimited)stop_tol- Stopping tolerance (default: 1e-6)time_limit- Time limit in seconds (default: 1000)device_number- CUDA device ID (default: 0)check_iter- Convergence check interval (default: 150)CUSPARSE_spmv- Force the cuSPARSE SpMVOp path and disable fused-kernel autotuning (default: False)autotune_verbose- Print backend autotuning diagnostics when fused kernels are enabled (default: False)use_Ruiz_scaling- Ruiz scaling (default: True)use_Pock_Chambolle_scaling- Pock-Chambolle scaling (default: True)use_bc_scaling- Bounds/cost scaling (default: True)use_presolve- Enable presolve/postsolve (default: True)presolver- Presolver backend (0=PSLP,1=GPU,2=none; default:1)enable_gpu_folding- GPU presolver folding (default: True)use_reduced_matrix- Adaptive reduced-matrix iterations (default: True)auto_reduced_compression_policy- Automatic reduced/compression selection from presolved dimensions (default: False)
Results
Solution information:
status- “OPTIMAL”, “TIME_LIMIT”, “ITER_LIMIT”, “ERROR”x- Primal solutiony- Dual solutionz- Bound-dual solutionprimal_obj- Primal objective valuegap- Duality gapresiduals- Final KKT residualiter- Total iterationstime- Solve time (seconds)iter4/6/8- Iterations to reach 1e-4/6/8 tolerancetime4/6/8- Time to reach tolerancetiming- Presolve, setup, scaling, analysis, power-iteration, solve, and total-time breakdownfolding_time- GPU presolver folding timereduced_active_iteration_ratio,reduced_average_column_ratio,reduced_average_nnz_ratio,reduced_minimum_column_ratio, andreduced_minimum_nnz_ratio- Reduced-matrix execution statistics
BatchedResults
Batched solution information:
status- list of per-instance statusesx,z- primal and bound-dual matrices shaped(n, B)y- dual matrix shaped(m, B)primal_obj,gap,residuals,iter- per-instance vectorstime,setup_time,solve_time,power_time- batched timing fields