🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEscipy

scipy Documentation

LOADING ENGINE...

optimize.linprog()

AI & DATA SCIENCE // optimize-linprog

scipy.optimize.linprog() solves a linear programming problem — minimizing a linear objective function subject to linear equality and inequality constraints.

Syntax

scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None)

Deep Dive Course

linprog() minimizes a linear combination of decision variables x with coefficients c, subject to inequality constraints defined by A_ub and b_ub, equality constraints defined by A_eq and b_eq, and per-variable bounds — this exact mathematical structure, a linear objective with linear constraints, is what defines a linear program, a well-studied class of optimization problems solvable efficiently and reliably even for large numbers of variables. Since linprog() always minimizes, maximizing an objective requires negating the coefficients in c first, since minimizing the negation is equivalent to maximizing the original.

1Understanding optimize.linprog()

linprog() minimizes a linear combination of decision variables x with coefficients c, subject to inequality constraints defined by A_ub and b_ub, equality constraints defined by A_eq and b_eq, and per-variable bounds — this exact mathematical structure, a linear objective with linear constraints, is what defines a linear program, a well-studied class of optimization problems solvable efficiently and reliably even for large numbers of variables. Since linprog() always minimizes, maximizing an objective requires negating the coefficients in c first, since minimizing the negation is equivalent to maximizing the original.

💡

linprog() only ever minimizes — to maximize an objective, like maximizing profit, instead, negate the objective's coefficients and remember to negate the resulting objective value back when interpreting the final answer.

editor.html
from scipy import optimize

# Minimize -x - 2y (i.e. maximize x + 2y) subject to x + y <= 4, x >= 0, y >= 0
result = optimize.linprog(c=[-1, -2], A_ub=[[1, 1]], b_ub=[4], bounds=[(0, None), (0, None)])
print(result.x)
localhost:3000

2Practical Example

Here is a real-world application of optimize.linprog() showing how it is used in production SciPy code.

editor.html
from scipy import optimize

result = optimize.linprog(c=[-1, -2], A_ub=[[1, 1]], b_ub=[4], bounds=[(0, None), (0, None)])
print(-result.fun)
localhost:3000

3Best Practices

Follow these guidelines when working with optimize.linprog():

1. Negate the objective coefficients, and the resulting optimal value, when you actually want to maximize rather than minimize

2. Double-check the direction of inequality constraints, since A_ub/b_ub specifically represent less-than-or-equal constraints — a greater-than-or-equal constraint needs to be negated on both sides to fit that form

3. Use bounds to specify simple per-variable limits, like non-negativity, directly, rather than expressing them as additional rows in A_ub, for clarity and efficiency

⚠️

Tip: linprog() only ever minimizes — to maximize an objective, like maximizing profit, instead, negate the objective's coefficients and remember to negate the resulting objective value back when interpreting the final answer.

editor.html
from scipy import optimize

# Minimize -x - 2y (i.e. maximize x + 2y) subject to x + y <= 4, x >= 0, y >= 0
result = optimize.linprog(c=[-1, -2], A_ub=[[1, 1]], b_ub=[4], bounds=[(0, None), (0, None)])
print(result.x)
localhost:3000

Examples

Example 01Basic Usage
from scipy import optimize

# Minimize -x - 2y (i.e. maximize x + 2y) subject to x + y <= 4, x >= 0, y >= 0
result = optimize.linprog(c=[-1, -2], A_ub=[[1, 1]], b_ub=[4], bounds=[(0, None), (0, None)])
print(result.x)
Example 02Advanced Example
from scipy import optimize

result = optimize.linprog(c=[-1, -2], A_ub=[[1, 1]], b_ub=[4], bounds=[(0, None), (0, None)])
print(-result.fun)

Best Practices

  • Negate the objective coefficients, and the resulting optimal value, when you actually want to maximize rather than minimize
  • Double-check the direction of inequality constraints, since A_ub/b_ub specifically represent less-than-or-equal constraints — a greater-than-or-equal constraint needs to be negated on both sides to fit that form
  • Use bounds to specify simple per-variable limits, like non-negativity, directly, rather than expressing them as additional rows in A_ub, for clarity and efficiency

Interview Question

Why does maximizing an objective with linprog() require negating the coefficients, and why do you negate the resulting value back afterward?

Hint: Think about the mathematical relationship between maximizing f(x) and minimizing -f(x).

Maximizing a function f(x) always produces the exact same optimal input x as minimizing its negation — the point that makes f as large as possible is identical to the point that makes the negated function as small, most negative, as possible. Since linprog() is only implemented to minimize, passing negated coefficients effectively tricks it into solving the maximization problem indirectly by minimizing the negated objective. Because the objective value it reports back is the minimum of that negated function, negating it back gives you the actual maximum value of the original, non-negated objective you actually cared about.

Exercises

MediumPractice using optimize.linprog() in a real scenario.
View Solution
from scipy import optimize

# Minimize -x - 2y (i.e. maximize x + 2y) subject to x + y <= 4, x >= 0, y >= 0
result = optimize.linprog(c=[-1, -2], A_ub=[[1, 1]], b_ub=[4], bounds=[(0, None), (0, None)])
print(result.x)

Frequently Asked Questions

Why does maximizing an objective with linprog() require negating the coefficients, and why do you negate the resulting value back afterward?

Maximizing a function f(x) always produces the exact same optimal input x as minimizing its negation — the point that makes f as large as possible is identical to the point that makes the negated function as small, most negative, as possible. Since linprog() is only implemented to minimize, passing negated coefficients effectively tricks it into solving the maximization problem indirectly by minimizing the negated objective. Because the objective value it reports back is the minimum of that negated function, negating it back gives you the actual maximum value of the original, non-negated objective you actually cared about.

Related Functions

optimize-minimizenp-dotarithmetic-operators