0
0
SciPydata~20 mins

Nonlinear constraint optimization in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Nonlinear Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)))
A(1.5, 1.5)
B(1.0, 2.0)
C(0.0, 3.0)
D(1.0, 1.0)
Attempts:
2 left
💡 Hint
Think about the point closest to (1,1) that satisfies x + y >= 3.
data_output
intermediate
2: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)
A15
B10
C5
D20
Attempts:
2 left
💡 Hint
Check the 'nit' attribute in the result object.
🔧 Debug
advanced
2: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)
ANo error, prints 0.0
BSyntaxError
CTypeError: 'list' object is not callable
DValueError: jac must be callable or None
Attempts:
2 left
💡 Hint
Check the type of 'jac' parameter in constraint dictionary.
🧠 Conceptual
advanced
1: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?
AIt converts the problem into an unconstrained optimization.
BIt restricts the solution to a curve or surface where the constraint equals zero.
CIt expands the solution space by adding more feasible points.
DIt always makes the problem easier to solve.
Attempts:
2 left
💡 Hint
Think about what an equality constraint means geometrically.
🚀 Application
expert
2: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?
AAn inequality constraint specifying volume >= 100
BNo constraint needed, just minimize volume
CAn inequality constraint specifying volume <= 100
DAn equality constraint specifying volume - 100 = 0
Attempts:
2 left
💡 Hint
Exact volume means the volume must be equal to 100, not just greater or less.