Bird
Raised Fist0
SciPydata~20 mins

Bounds and constraints in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of bounds in scipy.optimize functions?
easy
A. To set the maximum number of iterations
B. To define the objective function to minimize
C. To limit the range of values each variable can take
D. To specify the initial guess for variables

Solution

  1. Step 1: Understand the role of bounds

    Bounds restrict each variable to a specific range, for example, between 0 and 1.
  2. Step 2: Differentiate bounds from other parameters

    Bounds are not about the objective function, initial guess, or iteration limits; they only limit variable values.
  3. Final Answer:

    To limit the range of values each variable can take -> Option C
  4. Quick Check:

    Bounds = variable limits [OK]
Hint: Bounds always restrict variable ranges, not function or iterations [OK]
Common Mistakes:
  • Confusing bounds with constraints
  • Thinking bounds set initial guesses
  • Assuming bounds limit iterations
2. Which of the following is the correct way to define bounds for two variables x and y where x is between 0 and 5, and y is between -1 and 1 in scipy.optimize.minimize?
easy
A. bounds = [(0, 5), (-1, 1)]
B. bounds = {0:5, -1:1}
C. bounds = [0, 5, -1, 1]
D. bounds = (0, 5, -1, 1)

Solution

  1. Step 1: Recall bounds format in scipy.optimize

    Bounds are given as a list of tuples, each tuple for one variable's (min, max).
  2. Step 2: Check each option's format

    bounds = [(0, 5), (-1, 1)] uses a list of tuples correctly. Options A (flat list), B (dictionary), and D (flat tuple) are invalid formats.
  3. Final Answer:

    bounds = [(0, 5), (-1, 1)] -> Option A
  4. Quick Check:

    Bounds = list of (min, max) tuples [OK]
Hint: Use list of (min, max) tuples for bounds [OK]
Common Mistakes:
  • Using dictionary instead of list
  • Passing flat list instead of tuples
  • Confusing tuple and list syntax
3. What will be the output of the following code snippet?
from scipy.optimize import minimize

func = lambda x: (x[0]-1)**2 + (x[1]+2)**2
bounds = [(0, 2), (-3, -1)]
result = minimize(func, x0=[0, 0], bounds=bounds)
print(result.x)
medium
A. [0.5 -1.5]
B. [0. -3.]
C. [2. -1.]
D. [1. -2.]

Solution

  1. Step 1: Understand the objective function and bounds

    The function is minimized when x[0] = 1 and x[1] = -2. Bounds allow x[0] between 0 and 2, and x[1] between -3 and -1.
  2. Step 2: Check if the minimum is within bounds

    Since (1, -2) is inside the bounds, the optimizer will find this point.
  3. Final Answer:

    [1. -2.] -> Option D
  4. Quick Check:

    Minimum inside bounds = [1, -2] [OK]
Hint: Minimum inside bounds is the solution [OK]
Common Mistakes:
  • Ignoring bounds and picking unconstrained minimum
  • Confusing bounds order
  • Assuming initial guess is final answer
4. Identify the error in the following code that uses bounds in scipy.optimize.minimize:
from scipy.optimize import minimize

func = lambda x: x[0]**2 + x[1]**2
bounds = [0, 1, 0, 1]
result = minimize(func, x0=[0.5, 0.5], bounds=bounds)
print(result.x)
medium
A. Initial guess x0 is outside bounds
B. Bounds should be a list of tuples, not a flat list
C. Objective function must return a scalar
D. Bounds must be specified as a dictionary

Solution

  1. Step 1: Check bounds format

    The code uses bounds = [0, 1, 0, 1], which is a flat list. scipy expects a list of tuples for bounds.
  2. Step 2: Confirm other parameters

    Initial guess is inside bounds, function returns scalar, and bounds as dictionary is not required.
  3. Final Answer:

    Bounds should be a list of tuples, not a flat list -> Option B
  4. Quick Check:

    Bounds = list of tuples, not flat list [OK]
Hint: Use list of tuples for bounds, not flat list [OK]
Common Mistakes:
  • Using flat list instead of list of tuples
  • Setting initial guess outside bounds
  • Misunderstanding bounds format
5. You want to minimize the function f(x, y) = (x-3)^2 + (y+1)^2 with the constraints that x + y = 2 and bounds 0 ≤ x ≤ 4, -2 ≤ y ≤ 2. Which of the following correctly sets up the constraints and bounds for scipy.optimize.minimize?
hard
A. constraints = {'type': 'eq', 'fun': lambda v: v[0] + v[1] - 2}; bounds = [(0, 4), (-2, 2)]
B. constraints = [{'type': 'ineq', 'fun': lambda v: v[0] + v[1] - 2}]; bounds = [(0, 4), (-2, 2)]
C. constraints = {'type': 'eq', 'fun': lambda v: v[0] - v[1] - 2}; bounds = [(0, 4), (-2, 2)]
D. constraints = [{'type': 'eq', 'fun': lambda v: v[0] + v[1] - 2}]; bounds = [(0, 2), (-2, 2)]

Solution

  1. Step 1: Define the equality constraint

    The constraint is x + y = 2, so the function should return zero when this holds: lambda v: v[0] + v[1] - 2. It must be an equality constraint ('eq').
  2. Step 2: Set the correct bounds

    Bounds are 0 ≤ x ≤ 4 and -2 ≤ y ≤ 2, so bounds = [(0, 4), (-2, 2)].
  3. Final Answer:

    constraints = {'type': 'eq', 'fun': lambda v: v[0] + v[1] - 2}; bounds = [(0, 4), (-2, 2)] -> Option A
  4. Quick Check:

    Equality constraint + correct bounds = constraints = {'type': 'eq', 'fun': lambda v: v[0] + v[1] - 2}; bounds = [(0, 4), (-2, 2)] [OK]
Hint: Equality constraint returns zero when condition met [OK]
Common Mistakes:
  • Using inequality instead of equality constraint
  • Swapping bounds limits
  • Incorrect constraint function expression
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard