Bird
Raised Fist0
SciPydata~10 mins

Constrained optimization in SciPy - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Constrained optimization
Define objective function
↓
Define constraints
↓
Choose initial guess
↓
Call optimizer
↓
Check constraints satisfied?
No→Adjust and retry
Yes↓
Return optimal solution
The process starts by defining the function to minimize and constraints, then uses an optimizer to find the best solution that meets all constraints.
Execution Sample
SciPy
from scipy.optimize import minimize

# Objective: minimize x^2 + y^2
# Constraint: x + y = 1

res = minimize(lambda v: v[0]**2 + v[1]**2, [0,0], constraints={'type':'eq','fun': lambda v: v[0]+v[1]-1})
print(res.x)
This code finds the point (x,y) closest to origin where x + y = 1.
Execution Table
StepCurrent guess (x,y)Objective valueConstraint value (x+y-1)Action
1[0, 0]0-1Start at initial guess, constraint not met
2[0.5, 0.5]0.50Constraint met, objective improved
3[0.6, 0.4]0.520Constraint met, objective worse, optimizer tries better
4[0.5, 0.5]0.50Optimizer returns best found solution
Exit---Optimization converged with constraint satisfied
💡 Optimization stops when constraint is met and objective cannot be improved further.
Variable Tracker
VariableStartAfter 1After 2After 3Final
x00.50.60.50.5
y00.50.40.50.5
Objective00.50.520.50.5
Constraint-10000
Key Moments - 2 Insights
Why does the optimizer start with a guess that does not satisfy the constraint?
The optimizer begins with the initial guess provided (0,0) even if it violates constraints. It then adjusts guesses to satisfy constraints as shown in steps 1 to 2 in the execution table.
Why can the objective value increase in some steps during optimization?
Sometimes the optimizer tries points with worse objective values temporarily to find a better overall solution that satisfies constraints, as seen in step 3 where objective increased but constraint was met.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the constraint value at step 2?
A-1
B0
C0.5
D1
💡 Hint
Check the 'Constraint value' column at step 2 in the execution_table.
At which step does the optimizer first meet the constraint exactly?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the first step where 'Constraint value' is zero in the execution_table.
If the initial guess was [1,1], how would the constraint value at start change?
AIt would be 1
BIt would be 0
CIt would be 2
DIt would be -1
💡 Hint
Constraint is x + y - 1, so sum the initial guess values and subtract 1.
Concept Snapshot
Constrained optimization:
- Minimize a function subject to constraints
- Define objective and constraints
- Provide initial guess
- Use scipy.optimize.minimize with constraints
- Optimizer adjusts guess to satisfy constraints
- Returns solution meeting constraints with minimal objective
Full Transcript
Constrained optimization finds the best solution that meets rules. We start with a guess, check if it meets constraints, and improve it step by step. The optimizer tries different points, sometimes worse, to find the best valid answer. The process stops when constraints are met and no better solution is found.

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

  1. 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.
  2. 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.
  3. Final Answer:

    To find the best solution while respecting given limits or rules -> Option C
  4. 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?
easy
A. constraint = {'type': 'eq', 'fun': lambda x: x[0] - 1}
B. constraint = {'type': 'ineq', 'fun': lambda x: x[0] - 1}
C. constraint = {'type': 'ineq', 'fun': lambda x: 1 - x[0]}
D. constraint = {'type': 'eq', 'fun': lambda x: 1 - x[0]}

Solution

  1. Step 1: Recall inequality constraint definition

    Inequality constraints require the function to be >= 0. So, fun(x) >= 0 means the constraint holds.
  2. 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.
  3. Final Answer:

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

    Inequality means fun(x) >= 0 [OK]
Hint: Inequality means function output ≥ 0 [OK]
Common Mistakes:
  • Using 'eq' instead of 'ineq' for inequalities
  • Reversing the inequality sign in the function
  • 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

  1. Step 1: Understand the objective and constraint

    The function minimizes distance from point (2,3). The constraint requires x[0] + x[1] = 5.
  2. 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.
  3. 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].
  4. Final Answer:

    [2.0 3.0] -> Option D
  5. 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

  1. 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.
  2. 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.
  3. Final Answer:

    Initial guess violates the inequality constraint -> Option A
  4. 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?
hard
A. bounds = [(0, None), (0, None)]; cons = {'type': 'eq', 'fun': lambda x: x[0] + 2*x[1] - 3}
B. bounds = [(None, 0), (None, 0)]; cons = {'type': 'ineq', 'fun': lambda x: 3 - x[0] - 2*x[1]}
C. bounds = [(0, None), (0, None)]; cons = {'type': 'ineq', 'fun': lambda x: x[0] + 2*x[1] - 3}
D. bounds = [(0, None), (0, None)]; cons = {'type': 'ineq', 'fun': lambda x: 3 - x[0] - 2*x[1]}

Solution

  1. Step 1: Set bounds for inequalities

    Constraints x[0] >= 0 and x[1] >= 0 are bounds: [(0, None), (0, None)] means lower bound 0, no upper bound.
  2. Step 2: Define equality constraint

    The equality x[0] + 2*x[1] = 3 is expressed as fun(x) = x[0] + 2*x[1] - 3 = 0 with type 'eq'.
  3. 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.
  4. Final Answer:

    bounds = [(0, None), (0, None)]; cons = {'type': 'eq', 'fun': lambda x: x[0] + 2*x[1] - 3} -> Option A
  5. Quick Check:

    Bounds for inequalities + 'eq' for equality [OK]
Hint: Use bounds for ≥ constraints, 'eq' for equalities [OK]
Common Mistakes:
  • Using 'ineq' for equality constraints
  • Reversing bounds limits
  • Confusing constraint function sign
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