Python interfaces
This page shows how to use the Python interface of HPR-LP: run demos, build custom problems, adjust solver settings, and interpret results.
Running examples
The demo LP problem is:
Test problem (LP)
The default demo problem we ship is a small linear program of the form
After installing the environment, you can run the solver in two ways: model-based API or .mps API.
1. Model-based API
You can also build and solve an LP directly from NumPy / SciPy arrays.
import os, sys
import numpy as np
from scipy import sparse
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
if PROJECT_ROOT not in sys.path:
sys.path.insert(0, PROJECT_ROOT)
from src.model_api import Model, Parameters
A = sparse.csr_matrix(
[[1.0, 2.0],
[3.0, 1.0]],
dtype=np.float64
)
# Row bounds on A x:
AL = np.array([-np.inf, -np.inf], dtype=np.float64)
AU = np.array([10.0, 12.0 ], dtype=np.float64)
# Variable bounds:
l = np.array([0.0, 0.0], dtype=np.float64)
u = np.array([np.inf, np.inf], dtype=np.float64)
# Objective vector for "maximize c^T x"
c = np.array([-3.0, -5.0], dtype=np.float64)
# Build a Model object directly from arrays (no disk I/O)
model = Model.from_arrays(A, AL, AU, l, u, c)
param = Parameters()
# These names match what you set in run_single_file.py
param.time_limit = 3600.0
param.stoptol = 1e-9
param.device_number = 0
res = model.solve(param)
# -----------------------------------------------------------------------------
# 5. Print result
# -----------------------------------------------------------------------------
print("status: ", getattr(res, "output_type", None))
print("objective: ", getattr(res, "primal_obj", None))
print("solution x: ", getattr(res, "x", None))
print("iterations: ", getattr(res, "iter", None))
print("solve_time: ", getattr(res, "time", None))
2. MPS file API
python scripts/run_single_file.py
This calls run_single_file.py with its default arguments, which solve one LP model in MPS format.
You can also override any of these from the command line, for example:
python scripts/run_single_file.py \
--file_name /path/to/your_problem.mps \
--device_number 0 \
--stoptol 1e-8 \
--time_limit 3600 \
--check_iter 150 \
--warm_up True \
--use_Ruiz_scaling True \
--use_Pock_Chambolle_scaling True \
--use_bc_scaling True
Parameters
Parameters
Below is a list of the parameters in HPR-LP along with their default values and usage:
Parameter |
Default Value |
Description |
|---|---|---|
|
|
Determines if a warm-up phase is performed before main execution. |
|
|
Maximum allowed runtime (seconds) for the algorithm. |
|
|
Stopping tolerance for convergence checks. |
|
|
GPU device number (only relevant if |
|
|
Maximum number of iterations allowed. |
|
|
Number of iterations to check residuals. |
|
|
Whether to apply Ruiz scaling. |
|
|
Whether to use the Pock-Chambolle scaling. |
|
|
Whether to use the scaling for b and c. |
|
|
Whether to use GPU or not. |
|
|
Print the log every |
Results
After solving an instance, you can access the result variables as shown below:
Category |
Variable |
Description |
|---|---|---|
Iteration Counts |
|
Total number of iterations performed by the algorithm. |
|
Number of iterations required to achieve an accuracy of |
|
|
Number of iterations required to achieve an accuracy of |
|
Time Metrics |
|
Total time in seconds taken by the algorithm. |
|
Time in seconds taken to achieve an accuracy of |
|
|
Time in seconds taken to achieve an accuracy of |
|
|
Time in seconds used by the power method. |
|
Objective Values |
|
The primal objective value obtained. |
|
The gap between the primal and dual objective values. |
|
Residuals |
|
Relative residuals of the primal feasibility, dual feasibility, and duality gap. |
Algorithm Status |
|
The final status of the algorithm:
|
Solution Vectors |
|
The final solution vector x. |
|
The final solution vector y. |
|
|
The final solution vector z. |