What is the output of the following code that solves a linear programming problem?
from scipy.optimize import linprog c = [-1, -2] A = [[2, 1], [1, 1]] b = [20, 16] result = linprog(c, A_ub=A, b_ub=b, method='highs') print(result.x.round(2))
Think about maximizing the objective by minimizing the negative coefficients under the constraints.
The linear program minimizes -x - 2y subject to 2x + y ≤ 20 and x + y ≤ 16. The optimal solution is x=0, y=16.
How many iterations does the solver take to solve this linear programming problem?
from scipy.optimize import linprog c = [3, 1] A = [[1, 2], [4, 0]] b = [8, 16] result = linprog(c, A_ub=A, b_ub=b, method='highs') print(result.nit)
Check the solver's iteration count attribute after solving.
The 'highs' method often solves small problems in zero iterations because it uses advanced presolve techniques.
What error will this code raise when executed?
from scipy.optimize import linprog c = [1, 2] A = [[-1, 1], [1, -2]] b = [1, 2] result = linprog(c, A_eq=A, b_eq=b, method='highs') print(result.success)
Check if the dimensions of A_eq and b_eq match and if the problem is feasible.
The dimensions of A_eq (2x2) and b_eq (length 2) match, but the problem is infeasible because the equality constraints require negative values for the variables (x=-4, y=-3), which violate the default bounds x >= 0, y >= 0, so no error occurs and success is False.
A factory produces two products with profits $5 and $3 per unit. It has 40 hours of labor and 30 units of raw material. Labor and material requirements per product are:
- Product 1: 2 hours labor, 1 unit material
- Product 2: 1 hour labor, 2 units material
Using linprog, what is the maximum profit?
from scipy.optimize import linprog c = [-5, -3] A = [[2, 1], [1, 2]] b = [40, 30] result = linprog(c, A_ub=A, b_ub=b, method='highs') print(round(-result.fun, 2))
Remember to negate the objective function value to get maximum profit.
The optimal production mix yields a maximum profit of $103.33 by solving the constraints and maximizing profit.
What does the linprog result indicate if result.success is False and result.status is 2?
Check the meaning of status codes in linprog documentation.
Status code 2 means the problem is infeasible, so no solution meets all constraints.