Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

SciPy · Function

optimize.linprog

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.

How it works

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.

Syntax

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)

Parameters

NameTypeDescription
carray_like1-D array of objective coefficients to minimize.
A_ubarray_like, optional2-D inequality matrix; each row defines coefficients for A_ub @ x <= b_ub.
b_ubarray_like, optional1-D upper bounds corresponding to rows of A_ub.
A_eqarray_like, optional2-D equality matrix; each row defines coefficients for A_eq @ x == b_eq.
b_eqarray_like, optional1-D equality targets corresponding to rows of A_eq.
boundssequence, optionalSequence of (minimum, maximum) pairs for each variable; default is (0, None).
methodstr, optionalAlgorithm used to solve the standard-form problem; default is 'highs'.
callbackcallable, optionalCallback invoked by supported non-HiGHS methods during optimization.
optionsdict, optionalSolver-specific options.
x01-D array, optionalInitial guess for supported methods.
integrality1-D array_like, optionalIntegrality requirements for variables; supported by HiGHS methods.

Returns

An OptimizeResult containing the solution vector x, objective value fun, success flag, status, message, constraint residuals, and available marginals.

Key points

  • linprog minimizes a linear objective while enforcing equality and inequality constraints.
  • Rows of A_ub encode the coefficient vectors for individual upper-bound inequalities.
  • b_ub supplies the right-hand-side upper bound for each inequality row.
  • Rows of A_eq define equality coefficient vectors, paired positionally with b_eq.
  • bounds sets lower and upper limits separately for each decision variable.
  • HiGHS is the default solver family, with dual-simplex and interior-point variants available.

Example: Solve a small linear program

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.

When to use it

Allocate limited resources while minimizing cost or maximizing benefit by negating the objective.

Resource allocation

Model capacity, demand, and balance relationships with inequality and equality constraints.

Scheduling and planning

Represent discrete decision variables through integrality with a HiGHS method.

Mixed-integer optimization

Interview questions

QWhat problem does linprog solve, and what are its core constraints?

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.

QHow are inequality and equality constraints represented?

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.

QWhich result fields should production code inspect?

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.linprog
  • scipy.optimize.OptimizeResult

Source: linprog. Checked 2026-10-15.