Bird
Raised Fist0
SciPydata~20 mins

Nonlinear constraint optimization in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of using nonlinear constraint optimization in scipy.optimize.minimize?
easy
A. To generate random numbers
B. To sort data in ascending order
C. To calculate the mean of a dataset
D. To find the best solution while respecting complex rules or limits

Solution

  1. Step 1: Understand the goal of optimization

    Optimization aims to find the best value of a function, often minimum or maximum.
  2. Step 2: Recognize the role of constraints

    Nonlinear constraint optimization includes rules that the solution must follow, making it more complex.
  3. Final Answer:

    To find the best solution while respecting complex rules or limits -> Option D
  4. Quick Check:

    Optimization with constraints = best solution with rules [OK]
Hint: Optimization with constraints means best solution obeying rules [OK]
Common Mistakes:
  • Confusing optimization with sorting
  • Thinking it calculates statistics like mean
  • Assuming it generates random data
2. Which of the following is the correct way to specify nonlinear constraints in scipy.optimize.minimize?
easy
A. constraints = {'ineq', lambda x: x[0] - 1}
B. constraints = ['type' = 'ineq', 'fun' = lambda x: x[0] - 1]
C. constraints = {'type': 'ineq', 'fun': lambda x: x[0] - 1}
D. constraints = ('ineq', lambda x: x[0] - 1)

Solution

  1. Step 1: Recall the constraints format

    Constraints must be a dictionary with keys 'type' and 'fun'.
  2. Step 2: Check each option's syntax

    constraints = {'type': 'ineq', 'fun': lambda x: x[0] - 1} uses a dictionary with correct keys and lambda function syntax.
  3. Final Answer:

    constraints = {'type': 'ineq', 'fun': lambda x: x[0] - 1} -> Option C
  4. Quick Check:

    Constraints as dict with 'type' and 'fun' keys = constraints = {'type': 'ineq', 'fun': lambda x: x[0] - 1} [OK]
Hint: Constraints need dict with 'type' and 'fun' keys [OK]
Common Mistakes:
  • Using list or tuple instead of dict
  • Wrong assignment syntax inside list
  • Missing keys or using set instead of dict
3. What is the output of this code snippet?
from scipy.optimize import minimize

obj_fun = lambda x: (x[0]-2)**2 + (x[1]-3)**2
constraint = {'type': 'ineq', 'fun': lambda x: x[0] + x[1] - 4}
result = minimize(obj_fun, [0, 0], constraints=constraint, method='SLSQP')
print(round(result.fun, 2))
medium
A. 0.00
B. 1.00
C. 2.00
D. 4.00

Solution

  1. Step 1: Understand the objective function

    The function measures distance squared from point (2,3).
  2. Step 2: Apply the constraint and minimize

    The constraint requires x[0] + x[1] >= 4. The closest point to (2,3) on this line is (1,3), giving value (1-2)^2+(3-3)^2=1.
  3. Final Answer:

    1.00 -> Option B
  4. Quick Check:

    Minimum distance squared with constraint = 1.00 [OK]
Hint: Check closest point on constraint line to target point [OK]
Common Mistakes:
  • Ignoring the constraint
  • Rounding incorrectly
  • Confusing objective function value with variables
4. 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)
medium
A. Initial guess violates the equality constraint
B. Constraint type should be 'ineq' instead of 'eq'
C. Objective function must be linear
D. Method 'SLSQP' does not support constraints

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]
Hint: Start with guess satisfying equality constraints [OK]
Common Mistakes:
  • Using wrong constraint type
  • Assuming objective must be linear
  • Thinking SLSQP can't handle constraints
5. You want to minimize f(x) = (x[0]-1)^2 + (x[1]-2)^2 subject to nonlinear constraints x[0]^2 + x[1]^2 <= 2 and x[0] - x[1] >= 0. Which is the correct way to define these constraints for scipy.optimize.minimize with method 'SLSQP'?
hard
A. [{'type': 'ineq', 'fun': lambda x: 2 - (x[0]**2 + x[1]**2)}, {'type': 'ineq', 'fun': lambda x: x[0] - x[1]}]
B. [{'type': 'eq', 'fun': lambda x: 2 - (x[0]**2 + x[1]**2)}, {'type': 'eq', 'fun': lambda x: x[0] - x[1]}]
C. [{'type': 'ineq', 'fun': lambda x: (x[0]**2 + x[1]**2) - 2}, {'type': 'ineq', 'fun': lambda x: x[1] - x[0]}]
D. [{'type': 'ineq', 'fun': lambda x: (x[0]**2 + x[1]**2) - 2}, {'type': 'ineq', 'fun': lambda x: x[0] - x[1]}]

Solution

  1. Step 1: Translate constraints to 'ineq' form

    For 'ineq', function must be >= 0. So x0^2+x1^2 <= 2 becomes 2 - (x0^2+x1^2) >= 0.
  2. Step 2: Check second constraint

    x0 - x1 >= 0 is already in correct form.
  3. Final Answer:

    [{'type': 'ineq', 'fun': lambda x: 2 - (x[0]**2 + x[1]**2)}, {'type': 'ineq', 'fun': lambda x: x[0] - x[1]}] -> Option A
  4. Quick Check:

    Constraints must be 'ineq' with function >= 0 [OK]
Hint: Rewrite constraints so function >= 0 for 'ineq' type [OK]
Common Mistakes:
  • Using 'eq' instead of 'ineq' for inequalities
  • Reversing inequality signs
  • Not rewriting constraints to >= 0 form