Bird
0
0

Which is the correct way to define these constraints for scipy.optimize.minimize with method 'SLSQP'?

hard📝 Application Q15 of 15
SciPy - Advanced Optimization
You want to minimize f(x) = (x[0]-1)^2 + (x[1]-2)^2 subject to nonlinear constraints x[0]^2 + x[1]^2 <= 2 and x[0] - x[1] >= 0. Which is the correct way to define these constraints for scipy.optimize.minimize with method 'SLSQP'?
A[{'type': 'ineq', 'fun': lambda x: 2 - (x[0]**2 + x[1]**2)}, {'type': 'ineq', 'fun': lambda x: x[0] - x[1]}]
B[{'type': 'eq', 'fun': lambda x: 2 - (x[0]**2 + x[1]**2)}, {'type': 'eq', 'fun': lambda x: x[0] - x[1]}]
C[{'type': 'ineq', 'fun': lambda x: (x[0]**2 + x[1]**2) - 2}, {'type': 'ineq', 'fun': lambda x: x[1] - x[0]}]
D[{'type': 'ineq', 'fun': lambda x: (x[0]**2 + x[1]**2) - 2}, {'type': 'ineq', 'fun': lambda x: x[0] - x[1]}]
Step-by-Step Solution
Solution:
  1. Step 1: Translate constraints to 'ineq' form

    For 'ineq', function must be >= 0. So x0^2+x1^2 <= 2 becomes 2 - (x0^2+x1^2) >= 0.
  2. Step 2: Check second constraint

    x0 - x1 >= 0 is already in correct form.
  3. Final Answer:

    [{'type': 'ineq', 'fun': lambda x: 2 - (x[0]**2 + x[1]**2)}, {'type': 'ineq', 'fun': lambda x: x[0] - x[1]}] -> Option A
  4. Quick Check:

    Constraints must be 'ineq' with function >= 0 [OK]
Quick Trick: Rewrite constraints so function >= 0 for 'ineq' type [OK]
Common Mistakes:
  • Using 'eq' instead of 'ineq' for inequalities
  • Reversing inequality signs
  • Not rewriting constraints to >= 0 form

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes