Bird
0
0

Identify the error in this code for nonlinear constraint optimization:

medium📝 Debug Q14 of 15
SciPy - Advanced Optimization
Identify the error in this code for nonlinear constraint optimization:
from scipy.optimize import minimize

def obj(x):
    return x[0]**2 + x[1]**2

constraint = {'type': 'eq', 'fun': lambda x: x[0] + x[1] - 1}

result = minimize(obj, [0, 0], constraints=constraint, method='SLSQP')
print(result.x)
AInitial guess violates the equality constraint
BConstraint type should be 'ineq' instead of 'eq'
CObjective function must be linear
DMethod 'SLSQP' does not support constraints
Step-by-Step Solution
Solution:
  1. Step 1: Check initial guess against constraint

    Initial guess [0,0] does not satisfy x[0] + x[1] = 1.
  2. Step 2: Understand impact on solver

    Starting point violating equality constraints can cause solver to fail or converge slowly.
  3. Final Answer:

    Initial guess violates the equality constraint -> Option A
  4. Quick Check:

    Initial guess must satisfy equality constraints [OK]
Quick Trick: Start with guess satisfying equality constraints [OK]
Common Mistakes:
  • Using wrong constraint type
  • Assuming objective must be linear
  • Thinking SLSQP can't handle constraints

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More SciPy Quizzes