Bird
0
0

Identify the error in this code snippet that uses linprog:

medium📝 Debug Q14 of 15
SciPy - Advanced Optimization
Identify the error in this code snippet that uses linprog:
from scipy.optimize import linprog
c = [1, 2]
A = [[-1, 1], [3, 4]]
b = [1, 12]
res = linprog(c, A_eq=A, b_eq=b)
print(res.success)
AObjective coefficients should be negative to minimize
BMissing variable bounds argument
CUsing <code>A_eq</code> with inequality constraints instead of <code>A_ub</code>
DIncorrect import statement
Step-by-Step Solution
Solution:
  1. Step 1: Check constraint type usage

    The code uses A_eq and b_eq, which define equality constraints, but the constraints given are inequalities (e.g., -1*x1 + x2 <= 1).
  2. Step 2: Correct constraint parameter

    For inequality constraints, A_ub and b_ub should be used instead of A_eq and b_eq.
  3. Final Answer:

    Using A_eq with inequality constraints instead of A_ub -> Option C
  4. Quick Check:

    Use A_ub for inequalities, A_eq for equalities [OK]
Quick Trick: Use A_ub for inequalities, A_eq for equalities [OK]
Common Mistakes:
  • Confusing equality and inequality constraint parameters
  • Assuming linprog automatically detects constraint types
  • Ignoring error messages about constraint shapes

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes