0
0
SciPydata~5 mins

Constrained optimization in SciPy

Choose your learning style9 modes available
Introduction

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.

You want to minimize cost but must keep quality above a certain level.
You want to maximize profit but cannot exceed a budget.
You want to find the shortest path but must avoid restricted areas.
You want to fit a model but parameters must satisfy specific conditions.
Syntax
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.

Examples
Minimize the sum of squares with the constraint that x + y = 1.
SciPy
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)
Minimize distance to point (1, 2.5) with x and y between 1 and 2, and 0 and 2 respectively.
SciPy
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)
Sample Program

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.

SciPy
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)
OutputSuccess
Important Notes

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.

Summary

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).