Allocate limited resources while minimizing cost or maximizing benefit by negating the objective.
Resource allocation
SciPy · Function
Solves a linear program by minimizing a linear objective subject to linear equality and inequality constraints. It supports variable bounds, selectable HiGHS algorithms, and integrality constraints with HiGHS methods.
linprog converts the supplied objective and constraints into a linear-programming problem and solves it with the selected algorithm. Inequality rows represent A_ub @ x <= b_ub, equality rows represent A_eq @ x == b_eq, and bounds constrain each decision variable individually.
scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=(0, None), method='highs', callback=None, options=None, x0=None, integrality=None)| Name | Type | Description |
|---|---|---|
c | array_like | 1-D array of objective coefficients to minimize. |
A_ub | array_like, optional | 2-D inequality matrix; each row defines coefficients for A_ub @ x <= b_ub. |
b_ub | array_like, optional | 1-D upper bounds corresponding to rows of A_ub. |
A_eq | array_like, optional | 2-D equality matrix; each row defines coefficients for A_eq @ x == b_eq. |
b_eq | array_like, optional | 1-D equality targets corresponding to rows of A_eq. |
bounds | sequence, optional | Sequence of (minimum, maximum) pairs for each variable; default is (0, None). |
method | str, optional | Algorithm used to solve the standard-form problem; default is 'highs'. |
callback | callable, optional | Callback invoked by supported non-HiGHS methods during optimization. |
options | dict, optional | Solver-specific options. |
x0 | 1-D array, optional | Initial guess for supported methods. |
integrality | 1-D array_like, optional | Integrality requirements for variables; supported by HiGHS methods. |
An OptimizeResult containing the solution vector x, objective value fun, success flag, status, message, constraint residuals, and available marginals.
from scipy.optimize import linprog
# Minimize -x - 2y subject to x + y <= 4, x <= 2, y <= 3
result = linprog(
c=[-1, -2],
A_ub=[[1, 1], [1, 0], [0, 1]],
b_ub=[4, 2, 3],
bounds=[(0, None), (0, None)],
method="highs",
)
print(result.x)
print(result.fun)The solver finds the feasible nonnegative values of x and y that minimize the supplied linear objective.
Resource allocation
Scheduling and planning
Mixed-integer optimization
The function minimizes c @ x subject to A_ub @ x <= b_ub, A_eq @ x == b_eq, and per-variable bounds. The default method is HiGHS.
Follow-up: How would you encode a variable that must be unrestricted in sign? Supply a bound such as (None, None) for that variable.
A_ub is a matrix whose rows define inequality coefficients, while b_ub supplies their upper bounds. A_eq and b_eq work analogously for equalities, and their row counts must match.
Follow-up: How do you represent a lower-bound inequality? Rewrite x >= l as -x <= -l and place the coefficients and right-hand side in A_ub and b_ub.
The result reports whether the solve succeeded and includes the optimizer, objective value, status, and message, along with residuals or marginals when available. Check success before consuming the solution.
Follow-up: What does an unsuccessful result imply? Inspect status and message to distinguish infeasibility, unboundedness, or another termination condition.
scipy.optimize.linprogscipy.optimize.OptimizeResult