Constrained optimization helps find the best solution while following rules or limits. It is useful when you want to maximize or minimize something but must stay within certain boundaries.
Constrained optimization in SciPy
from scipy.optimize import minimize result = minimize( fun, # function to minimize x0, # initial guess method='SLSQP', # optimization method constraints=constraints, # constraints as dict or list bounds=bounds # variable bounds )
fun is the function you want to minimize.
constraints can be equality or inequality conditions.
from scipy.optimize import minimize def fun(x): return x[0]**2 + x[1]**2 constraints = [{'type': 'eq', 'fun': lambda x: x[0] + x[1] - 1}] x0 = [0, 0] result = minimize(fun, x0, method='SLSQP', constraints=constraints) print(result.x)
from scipy.optimize import minimize fun = lambda x: (x[0]-1)**2 + (x[1]-2.5)**2 constraints = [ {'type': 'ineq', 'fun': lambda x: x[0] - 1}, {'type': 'ineq', 'fun': lambda x: 2 - x[0]}, {'type': 'ineq', 'fun': lambda x: x[1] - 0}, {'type': 'ineq', 'fun': lambda x: 2 - x[1]} ] x0 = [2, 0] result = minimize(fun, x0, method='SLSQP', constraints=constraints) print(result.x)
This program finds the values of x and y that minimize the distance to point (3, -1) while following two rules: x + 2y must equal 2, and x must be at least 1 more than y.
from scipy.optimize import minimize # Objective function: minimize (x-3)^2 + (y+1)^2 # Constraints: x + 2y = 2 (equality), x - y >= 1 (inequality) def objective(x): return (x[0] - 3)**2 + (x[1] + 1)**2 constraints = [ {'type': 'eq', 'fun': lambda x: x[0] + 2*x[1] - 2}, {'type': 'ineq', 'fun': lambda x: x[0] - x[1] - 1} ] x0 = [0, 0] # initial guess result = minimize(objective, x0, method='SLSQP', constraints=constraints) print('Optimal values:', result.x) print('Objective function value:', result.fun)
Use 'SLSQP' method for problems with constraints.
Constraints are dictionaries with 'type' ('eq' or 'ineq') and a function 'fun' that returns zero or positive values.
Initial guess x0 can affect the solution found.
Constrained optimization finds the best solution within limits.
Use scipy.optimize.minimize with constraints and bounds.
Define constraints as functions returning zero (equalities) or positive (inequalities).
Practice
scipy.optimize.minimize?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
Theminimizefunction 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 CQuick Check:
Constrained optimization = best solution within limits [OK]
- Thinking constraints are optional or ignored
- Confusing constrained with unconstrained optimization
- Assuming constraints only check validity, not guide solution
scipy.optimize.minimize?Solution
Step 1: Recall inequality constraint definition
Inequality constraints require the function to be >= 0. So,fun(x) >= 0means the constraint holds.Step 2: Check each option
constraint = {'type': 'ineq', 'fun': lambda x: x[0] - 1}:lambda x: x[0] - 1meansx[0] - 1 >= 0orx[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]} means1 - x[0] >= 0orx[0] <= 1, which is a different constraint.Final Answer:
constraint = {'type': 'ineq', 'fun': lambda x: x[0] - 1} -> Option BQuick Check:
Inequality means fun(x) >= 0 [OK]
- Using 'eq' instead of 'ineq' for inequalities
- Reversing the inequality sign in the function
- Confusing the function output sign for constraints
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))Solution
Step 1: Understand the objective and constraint
The function minimizes distance from point (2,3). The constraint requiresx[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)^2withx0+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 atx0=2, thenx1=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 DQuick Check:
Minimum at (2,3) on constraint line [OK]
- Ignoring the constraint when finding minimum
- Confusing initial guess with solution
- Assuming solver returns initial guess
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)Solution
Step 1: Check initial guess against constraint
The inequality constraint requiresx[0] + x[1] - 1 >= 0, orx[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 AQuick Check:
Initial guess must satisfy constraints [OK]
- Ignoring initial guess validity
- Misunderstanding constraint function sign
- Thinking constraints must be single dict
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?Solution
Step 1: Set bounds for inequalities
Constraintsx[0] >= 0andx[1] >= 0are bounds:[(0, None), (0, None)]means lower bound 0, no upper bound.Step 2: Define equality constraint
The equalityx[0] + 2*x[1] = 3is expressed asfun(x) = x[0] + 2*x[1] - 3 = 0with type 'eq'.Step 3: Check options
bounds = [(0, None), (0, None)]; cons = {'type': 'eq', 'fun': lambda x: x[0] + 2*x[1] - 3} correctly sets bounds and equality constraint.
bounds = [(None, 0), (None, 0)]; cons = {'type': 'ineq', 'fun': lambda x: 3 - x[0] - 2*x[1]} reverses bounds and uses inequality instead of equality.
bounds = [(0, None), (0, None)]; cons = {'type': 'ineq', 'fun': lambda x: x[0] + 2*x[1] - 3} uses inequality for equality constraint.
bounds = [(0, None), (0, None)]; cons = {'type': 'ineq', 'fun': lambda x: 3 - x[0] - 2*x[1]} uses inequality (x[0] + 2*x[1] <= 3) instead of equality.Final Answer:
bounds = [(0, None), (0, None)]; cons = {'type': 'eq', 'fun': lambda x: x[0] + 2*x[1] - 3} -> Option AQuick Check:
Bounds for inequalities + 'eq' for equality [OK]
- Using 'ineq' for equality constraints
- Reversing bounds limits
- Confusing constraint function sign
Ready to prove it?
Pass the test and this pattern moves unknown
