0
0
SciPydata~20 mins

Bounds and constraints in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Bounds and Constraints Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of constrained optimization with bounds
What is the value of result.x after running this code that minimizes f(x) = (x-3)^2 with bounds 0 <= x <= 2?
SciPy
from scipy.optimize import minimize

f = lambda x: (x - 3)**2
bounds = [(0, 2)]
result = minimize(f, x0=[0], bounds=bounds)
print(result.x)
A[1.5]
B[3.]
C[0.]
D[2.]
Attempts:
2 left
💡 Hint
Think about the minimum of the function without bounds and how bounds limit the solution.
data_output
intermediate
2:00remaining
Number of iterations with linear constraint
How many iterations does the optimizer perform when minimizing f(x) = (x[0]-1)^2 + (x[1]-2)^2 with constraint x[0] + x[1] = 3 and no bounds?
SciPy
from scipy.optimize import minimize

f = lambda x: (x[0]-1)**2 + (x[1]-2)**2
cons = {'type': 'eq', 'fun': lambda x: x[0] + x[1] - 3}
result = minimize(f, x0=[0,0], constraints=cons)
print(result.nit)
A2
B1
C3
D0
Attempts:
2 left
💡 Hint
The problem is simple and quadratic with a linear equality constraint.
🔧 Debug
advanced
2:00remaining
Identify error in constraint definition
What error does this code raise when trying to minimize f(x) = x[0]^2 + x[1]^2 with constraint x[0] - 1 > 0 defined incorrectly?
SciPy
from scipy.optimize import minimize

f = lambda x: x[0]**2 + x[1]**2
cons = {'type': 'ineq', 'fun': lambda x: x[0] - 1 > 0}
result = minimize(f, x0=[0,0], constraints=cons)
AValueError
BSyntaxError
CTypeError
DNo error, runs successfully
Attempts:
2 left
💡 Hint
Check what the constraint function returns.
🚀 Application
advanced
2:00remaining
Choosing bounds for parameter estimation
You want to estimate parameters a and b for a model y = a * x + b using least squares. You know a must be positive and b between -5 and 5. Which bounds should you use in scipy.optimize.minimize?
Abounds=[(0, None), (-5, 5)]
Bbounds=[(None, 0), (-5, 5)]
Cbounds=[(0, None), (None, 5)]
Dbounds=[(0, 5), (-5, 5)]
Attempts:
2 left
💡 Hint
None means no limit in that direction.
🧠 Conceptual
expert
2:00remaining
Effect of bounds and constraints on solution uniqueness
Which statement is true about the effect of bounds and constraints on the uniqueness of the solution in optimization problems?
AAdding bounds or constraints can make a unique solution non-unique by restricting the feasible region.
BBounds and constraints always guarantee a unique solution by limiting the search space.
CBounds and constraints always make the problem unsolvable.
DBounds and constraints never affect the uniqueness of the solution.
Attempts:
2 left
💡 Hint
Think about how restricting the feasible region can create multiple optimal points.