Challenge - 5 Problems
Nonlinear Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of nonlinear optimization with inequality constraint
What is the output of the following code that solves a nonlinear optimization problem with an inequality constraint?
SciPy
from scipy.optimize import minimize # Objective function: minimize (x-1)^2 + (y-1)^2 fun = lambda v: (v[0]-1)**2 + (v[1]-1)**2 # Inequality constraint: x + y >= 3 (rewritten as x + y - 3 >= 0) cons = {'type': 'ineq', 'fun': lambda v: v[0] + v[1] - 3} # Initial guess x0 = [0, 0] res = minimize(fun, x0, constraints=cons) print((round(res.x[0], 2), round(res.x[1], 2)))
Attempts:
2 left
💡 Hint
Think about the point closest to (1,1) that satisfies x + y >= 3.
✗ Incorrect
The objective tries to get close to (1,1). The constraint requires x + y >= 3. The closest point on the line x + y = 3 to (1,1) is (1.5,1.5).
❓ data_output
intermediate2:00remaining
Number of iterations in nonlinear constrained optimization
After running this nonlinear optimization with constraints, how many iterations did the solver perform?
SciPy
from scipy.optimize import minimize fun = lambda v: (v[0]-3)**2 + (v[1]+1)**2 cons = [{'type': 'eq', 'fun': lambda v: v[0]**2 + v[1]**2 - 4}] x0 = [0, 2] res = minimize(fun, x0, constraints=cons, options={'disp': False}) print(res.nit)
Attempts:
2 left
💡 Hint
Check the 'nit' attribute in the result object.
✗ Incorrect
The solver converges quickly for this simple problem, performing 5 iterations.
🔧 Debug
advanced2:00remaining
Identify the error in nonlinear optimization constraint definition
What error will this code raise when trying to run nonlinear optimization with constraints?
SciPy
from scipy.optimize import minimize fun = lambda v: v[0]**2 + v[1]**2 cons = {'type': 'ineq', 'fun': lambda v: v[0] + v[1] - 1, 'jac': lambda v: [1, 1]} x0 = [0, 0] res = minimize(fun, x0, constraints=cons) print(res.fun)
Attempts:
2 left
💡 Hint
Check the type of 'jac' parameter in constraint dictionary.
✗ Incorrect
The 'jac' parameter must be a callable function returning the Jacobian, not a list.
🧠 Conceptual
advanced1:30remaining
Effect of nonlinear equality constraints on solution space
Which statement best describes the effect of adding a nonlinear equality constraint to an optimization problem?
Attempts:
2 left
💡 Hint
Think about what an equality constraint means geometrically.
✗ Incorrect
An equality constraint restricts solutions to points where the constraint function equals zero, often forming a curve or surface.
🚀 Application
expert2:30remaining
Choosing the correct constraint type for a real-world problem
You want to optimize the shape of a container to hold exactly 100 liters of liquid. Which constraint type should you use in scipy.optimize to enforce this volume requirement?
Attempts:
2 left
💡 Hint
Exact volume means the volume must be equal to 100, not just greater or less.
✗ Incorrect
To enforce an exact volume, use an equality constraint that forces volume - 100 = 0.