Linear programming fits decisions whose objective and constraints are all linear, such as choosing a low-cost blend, allocation, or production mix. In SciPy, scipy.optimize.linprog() takes coefficient arrays for the objective, inequalities, equalities, and bounds, then returns an OptimizeResult with the optimum or the reason no optimum was found.
The linprog() solver minimizes c @ x, so maximization problems need their objective coefficients multiplied by -1 before solving. Inequality rows use A_ub @ x <= b_ub, exact balance rules use A_eq @ x == b_eq, and per-variable limits belong in bounds.
The sample problem buys ten units from two suppliers while meeting a minimum quality score. The higher-quality supplier costs more, so the optimum should use just enough premium units to meet the quality limit and keep the total cost at 52.00.
Steps to solve a linear programming problem with SciPy:
- Create a Python script named solve_linear_program.py.
- solve_linear_program.py
import numpy as np from scipy.optimize import linprog cost = np.array([4.0, 7.0]) # Demand: regular + premium = 10 A_eq = np.array([(1.0, 1.0)]) b_eq = np.array([10.0]) # Quality: regular + 2 * premium >= 14 A_ub = np.array([(-1.0, -2.0)]) b_ub = np.array([-14.0]) bounds = [(0.0, None), (0.0, None)] result = linprog( c=cost, A_ub=A_ub, b_ub=b_ub, A_eq=A_eq, b_eq=b_eq, bounds=bounds, method="highs", ) regular, premium = result.x quality_points = regular + 2.0 * premium print(f"success: {result.success}") print(f"message: {result.message}") print(f"minimum_cost: {result.fun:.2f}") print(f"regular_units: {regular:.1f}") print(f"premium_units: {premium:.1f}") print(f"quality_points: {quality_points:.1f}") print(f"demand_residual: {result.eqlin.residual[0]:.2e}") print(f"quality_slack: {result.ineqlin.residual[0]:.2e}")
method=“highs” selects SciPy's HiGHS linear optimization interface. The bounds list keeps both decision variables nonnegative.
- Run the linear programming script.
$ python solve_linear_program.py success: True message: Optimization terminated successfully. (HiGHS Status 7: Optimal) minimum_cost: 52.00 regular_units: 6.0 premium_units: 4.0 quality_points: 14.0 demand_residual: 0.00e+00 quality_slack: 0.00e+00
- Confirm that the solver reported success.
success should be True before the returned variables are used. message gives the HiGHS termination reason when the model is infeasible, unbounded, or limited by iteration or time settings.
- Read the decision variables and objective value.
result.x contains the supplier quantities, and result.fun contains the minimized cost. The output chooses 6.0 regular units and 4.0 premium units for a cost of 52.00.
- Check the equality residual and inequality slack.
demand_residual near zero confirms the equality row still totals ten units. quality_slack near zero means the quality requirement is active at the optimum rather than exceeded.
- Replace the cost vector and constraint arrays with the model from your project.
Write every inequality in the form A_ub @ x <= b_ub. Multiply both sides by -1 for minimum requirements such as regular + 2 * premium >= 14.
- Model whole-number choices separately.
Do not round result.x to force integer, binary, or semi-continuous decisions. Use scipy.optimize.milp() or an explicit integrality setting and verify that mixed-integer result separately.
- Remove the demo script when it was only created for the check.
$ rm solve_linear_program.py
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.