0
0
SciPydata~20 mins

Linear programming (linprog) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Linprog Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple linear programming problem

What is the output of the following code that solves a linear programming problem?

SciPy
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))
A[5. 5.]
B[10. 0.]
C[8. 8.]
D[0. 16.]
Attempts:
2 left
💡 Hint

Think about maximizing the objective by minimizing the negative coefficients under the constraints.

data_output
intermediate
2:00remaining
Number of iterations in linprog solution

How many iterations does the solver take to solve this linear programming problem?

SciPy
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)
A0
B2
C3
D1
Attempts:
2 left
💡 Hint

Check the solver's iteration count attribute after solving.

🔧 Debug
advanced
2:00remaining
Identify the error in linear programming code

What error will this code raise when executed?

SciPy
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)
ANo error, prints False
BValueError: A_eq and b_eq dimensions mismatch
CLinAlgError: Singular matrix
DTypeError: unsupported operand type(s)
Attempts:
2 left
💡 Hint

Check if the dimensions of A_eq and b_eq match and if the problem is feasible.

🚀 Application
advanced
2:00remaining
Interpreting linprog results for resource allocation

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?

SciPy
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))
A80.0
B75.0
C103.33
D105.0
Attempts:
2 left
💡 Hint

Remember to negate the objective function value to get maximum profit.

🧠 Conceptual
expert
2:00remaining
Understanding linprog infeasibility output

What does the linprog result indicate if result.success is False and result.status is 2?

AThe problem is unbounded; objective can decrease indefinitely.
BThe problem is infeasible; no solution satisfies all constraints.
CThe solver encountered a numerical error during optimization.
DThe problem has multiple optimal solutions.
Attempts:
2 left
💡 Hint

Check the meaning of status codes in linprog documentation.