Constrained optimization is a way to find the best solution to a problem while following some rules or limits called constraints.
Click to reveal answer
beginner
Name two types of constraints in optimization problems.
Two common types of constraints are: 1. Equality constraints (e.g., x + y = 10) 2. Inequality constraints (e.g., x ≥ 0)
Click to reveal answer
beginner
Which function in scipy.optimize is commonly used for constrained optimization?
The function scipy.optimize.minimize is commonly used for constrained optimization by specifying constraints and bounds.
Click to reveal answer
intermediate
How do you define constraints for scipy.optimize.minimize?
Constraints are defined as a list of dictionaries with keys like type ('eq' for equality, 'ineq' for inequality) and fun (a function that returns the constraint value).
Click to reveal answer
intermediate
What does the 'method' parameter in scipy.optimize.minimize control?
The 'method' parameter chooses the algorithm to solve the optimization, such as 'SLSQP' which supports constraints and bounds.
Click to reveal answer
What does an inequality constraint like 'x >= 0' mean in optimization?
Ax must be exactly 0
Bx must be greater than or equal to 0
Cx must be less than 0
Dx can be any value
✗ Incorrect
An inequality constraint 'x >= 0' means x must be greater than or equal to zero.
Which scipy.optimize method supports constraints directly?
ANelder-Mead
BCG
CSLSQP
DBFGS
✗ Incorrect
The 'SLSQP' method supports both equality and inequality constraints.
In scipy.optimize.minimize, how are equality constraints specified?
Atype='eq'
Btype='ineq'
Ctype='equal'
Dtype='equal_to'
✗ Incorrect
Equality constraints use type='eq' in the constraints dictionary.
What is the purpose of bounds in constrained optimization?
ATo limit the range of variables
BTo define the objective function
CTo set the initial guess
DTo choose the solver method
✗ Incorrect
Bounds limit the range of values that variables can take.
What does the function passed to 'fun' in a constraint dictionary return?
AThe initial guess
BThe objective function value
CThe gradient of the objective
DThe constraint value (should be zero or positive)
✗ Incorrect
The function returns the value of the constraint, which should be zero for equality or non-negative for inequality.
Explain how to set up a constrained optimization problem using scipy.optimize.minimize.
Think about the inputs needed for minimize to handle constraints.
You got /4 concepts.
Describe the difference between equality and inequality constraints with examples.
Consider how each constraint restricts possible solutions.
You got /3 concepts.
Practice
(1/5)
1. What is the main purpose of constrained optimization in scipy.optimize.minimize?
easy
A. To only check if constraints are valid
B. To find any random solution without restrictions
C. To find the best solution while respecting given limits or rules
D. To maximize the function without any constraints
Solution
Step 1: Understand constrained optimization goal
Constrained optimization aims to find the best (minimum or maximum) value of a function while following certain limits or rules called constraints.
Step 2: Relate to scipy.optimize.minimize usage
The minimize function in scipy can include constraints and bounds to ensure the solution respects these limits.
Final Answer:
To find the best solution while respecting given limits or rules -> Option C
Quick Check:
Constrained optimization = best solution within limits [OK]
Hint: Constrained means limits must be followed [OK]
Common Mistakes:
Thinking constraints are optional or ignored
Confusing constrained with unconstrained optimization
Assuming constraints only check validity, not guide solution
2. Which of the following is the correct way to define the inequality constraint `x[0] >= 1` for scipy.optimize.minimize?
Inequality constraints require the function to be >= 0. So, fun(x) >= 0 means the constraint holds.
Step 2: Check each option
constraint = {'type': 'ineq', 'fun': lambda x: x[0] - 1}: lambda x: x[0] - 1 means x[0] - 1 >= 0 or x[0] >= 1, correct for the constraint. Options B and D use 'eq' which means equality, not inequality. constraint = {'type': 'ineq', 'fun': lambda x: 1 - x[0]} means 1 - x[0] >= 0 or x[0] <= 1, which is a different constraint.
Confusing the function output sign for constraints
3. What will be the output of this code snippet?
from scipy.optimize import minimize
fun = lambda x: (x[0]-2)**2 + (x[1]-3)**2
cons = ({'type': 'eq', 'fun': lambda x: x[0] + x[1] - 5})
res = minimize(fun, [0, 0], constraints=cons)
print(res.x.round(2))
medium
A. [2.5 2.5]
B. [1.0 4.0]
C. [0.0 5.0]
D. [2.0 3.0]
Solution
Step 1: Understand the objective and constraint
The function minimizes distance from point (2,3). The constraint requires x[0] + x[1] = 5.
Step 2: Find point on line x0+x1=5 closest to (2,3)
We want to minimize (x0-2)^2 + (x1-3)^2 with x0+x1=5. Using substitution: x1=5-x0. Objective becomes (x0-2)^2 + (5-x0-3)^2 = (x0-2)^2 + (2 - x0)^2. Expanding: (x0-2)^2 + (2-x0)^2 = 2*(x0-2)^2. Minimum at x0=2, then x1=3, which satisfies the constraint.
Step 3: Check options
[2.0 3.0] matches the minimum (2.0, 3.0). [1.0 4.0] sums to 5 but is farther from (2,3). The solver converges to [2.0, 3.0].
Final Answer:
[2.0 3.0] -> Option D
Quick Check:
Minimum at (2,3) on constraint line [OK]
Hint: Check constraint line and closest point to target [OK]
Common Mistakes:
Ignoring the constraint when finding minimum
Confusing initial guess with solution
Assuming solver returns initial guess
4. Identify the error in this constrained optimization code:
from scipy.optimize import minimize
fun = lambda x: x[0]**2 + x[1]**2
cons = [{'type': 'ineq', 'fun': lambda x: x[0] + x[1] - 1}]
res = minimize(fun, [0, 0], constraints=cons)
print(res.x)
medium
A. Initial guess violates the inequality constraint
B. Constraint function should return negative values
C. Constraints must be a dictionary, not a list
D. Objective function must be linear
Solution
Step 1: Check initial guess against constraint
The inequality constraint requires x[0] + x[1] - 1 >= 0, or x[0] + x[1] >= 1. The initial guess is [0,0], sum is 0, which violates the constraint.
Step 2: Understand impact of violating initial guess
Starting from a point that violates constraints can cause solver to fail or give warnings. Other options are incorrect because: - Constraint function should return >=0 for valid points. - Constraints can be a list of dicts. - Objective function can be nonlinear.
Final Answer:
Initial guess violates the inequality constraint -> Option A
Quick Check:
Initial guess must satisfy constraints [OK]
Hint: Check if initial guess meets constraints [OK]
Common Mistakes:
Ignoring initial guess validity
Misunderstanding constraint function sign
Thinking constraints must be single dict
5. You want to minimize f(x) = (x[0]-1)^2 + (x[1]-2)^2 with constraints x[0] >= 0, x[1] >= 0, and x[0] + 2*x[1] = 3. Which setup correctly applies these constraints using scipy.optimize.minimize?