0
0
SciPydata~20 mins

Constrained optimization in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constrained Optimization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of constrained minimization with inequality
What is the minimum value found by this constrained optimization code using scipy.optimize.minimize?
SciPy
from scipy.optimize import minimize

# Objective function
f = lambda x: (x[0]-1)**2 + (x[1]-2.5)**2

# Constraint: x0 + x1 >= 2
cons = ({'type': 'ineq', 'fun': lambda x: x[0] + x[1] - 2})

# Initial guess
x0 = [2, 0]

res = minimize(f, x0, constraints=cons)
print(round(res.fun, 4))
A0.25
B0.0
C1.25
D2.25
Attempts:
2 left
💡 Hint
Think about the point closest to (1, 2.5) that satisfies x0 + x1 >= 2.
data_output
intermediate
2:00remaining
Number of iterations in constrained optimization
How many iterations does the following constrained optimization take to converge?
SciPy
from scipy.optimize import minimize

f = lambda x: (x[0]-3)**2 + (x[1]+1)**2
cons = ({'type': 'eq', 'fun': lambda x: x[0] - 2*x[1]})
x0 = [0, 0]
res = minimize(f, x0, constraints=cons)
print(res.nit)
A7
B3
C10
D5
Attempts:
2 left
💡 Hint
Check the number of iterations attribute in the result object.
🔧 Debug
advanced
2:00remaining
Identify the error in constraint definition
What error will this code raise when running scipy.optimize.minimize with the given constraints?
SciPy
from scipy.optimize import minimize

f = lambda x: x[0]**2 + x[1]**2
cons = ({'type': 'ineq', 'fun': lambda x: x[0] - 1},
        {'type': 'eq', 'fun': lambda x: x[1] + 2})
x0 = [0, 0]
res = minimize(f, x0, constraints=cons)
print(res.fun)
ATypeError: 'tuple' object is not callable
BValueError: Constraints must be a dict or list of dicts
CRuntimeWarning: Initial guess does not satisfy constraints
DNo error, outputs 1.0
Attempts:
2 left
💡 Hint
Check if the initial guess satisfies the constraints.
visualization
advanced
3:00remaining
Plot of feasible region and minimum point
Which plot correctly shows the feasible region and the minimum point for the problem: Minimize f(x,y) = (x-2)^2 + (y-3)^2 Subject to constraints: x + y <= 4 and x >= 0, y >= 0?
SciPy
import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(-1,5,400)
y = np.linspace(-1,5,400)
X, Y = np.meshgrid(x,y)
Z = (X-2)**2 + (Y-3)**2

plt.contour(X, Y, Z, levels=30)
plt.fill_between(x, 0, 4 - x, where=(x<=4), color='lightblue', alpha=0.5)
plt.xlim(-1,5)
plt.ylim(-1,5)
plt.scatter(1.5, 2.5, color='red', label='Minimum')
plt.legend()
plt.show()
APlot with shaded square region from (0,0) to (4,4) with red dot at (2,3)
BPlot with shaded triangle region bounded by x=0, y=0, and x+y=4 with red dot at (1.5, 2.5)
CPlot with shaded circle region centered at (2,3) with red dot at (2,3)
DPlot with no shading and red dot at (0,0)
Attempts:
2 left
💡 Hint
The feasible region is where x and y are non-negative and their sum is at most 4.
🚀 Application
expert
3:00remaining
Interpreting Lagrange multipliers from constrained optimization
After solving the constrained optimization problem: Minimize f(x,y) = x^2 + y^2 Subject to x + 2y = 6 The solver returns Lagrange multiplier value 3. What does this multiplier represent?
AThe rate of increase in the minimum value of f when the constraint constant increases by 1
BThe minimum value of the function f at the solution point
CThe value of x at the solution point
DThe number of iterations the solver took
Attempts:
2 left
💡 Hint
Lagrange multipliers measure sensitivity of the optimal value to constraint changes.