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.
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)2Practical Example
Here is a real-world application of optimize.linprog() showing how it is used in production SciPy code.
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)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.
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)