0
0
SciPydata~5 mins

Bounds and constraints in SciPy

Choose your learning style9 modes available
Introduction

Bounds and constraints help control the values variables can take during optimization. They keep solutions realistic and within limits.

When you want to find the best solution but variables must stay within certain ranges, like budget limits.
When solving problems where some values cannot go below zero, such as quantities or prices.
When you want to restrict variables to satisfy rules, like total weight not exceeding a limit.
When optimizing a function but some variables must add up to a fixed number.
When you want to avoid impossible or unsafe solutions in engineering or finance.
Syntax
SciPy
from scipy.optimize import minimize

# Define bounds as a sequence of (min, max) pairs for each variable
bounds = [(min1, max1), (min2, max2), ...]

# Define constraints as a list of dictionaries
constraints = [
    {'type': 'eq', 'fun': lambda x: expression_equal_to_zero},  # equality constraint
    {'type': 'ineq', 'fun': lambda x: expression_greater_or_equal_zero}  # inequality constraint
]

# Call minimize with bounds and constraints
result = minimize(objective_function, initial_guess, bounds=bounds, constraints=constraints)

Bounds limit each variable individually between min and max values.

Constraints can be equality ('eq') or inequality ('ineq'). Inequality means the function must be >= 0.

Examples
Simple bounds example for two variables.
SciPy
bounds = [(0, 10), (1, 5)]
# Variable 1 between 0 and 10, Variable 2 between 1 and 5
Equality constraint example where sum of variables equals 5.
SciPy
constraints = [{'type': 'eq', 'fun': lambda x: x[0] + x[1] - 5}]
# Variables must add up to 5
Inequality constraint example enforcing a minimum value.
SciPy
constraints = [{'type': 'ineq', 'fun': lambda x: x[0] - 2}]
# Variable 1 must be at least 2 (x[0] - 2 >= 0)
Sample Program

This program finds the point (x, y) closest to (3, 2) but with x and y between 0 and 5, and their sum exactly 5.

SciPy
from scipy.optimize import minimize

# Objective function: minimize (x-3)^2 + (y-2)^2
# The best solution is at x=3, y=2

def objective(x):
    return (x[0] - 3)**2 + (x[1] - 2)**2

# Bounds: x between 0 and 5, y between 0 and 5
bounds = [(0, 5), (0, 5)]

# Constraint: x + y must be equal to 5
constraints = [{'type': 'eq', 'fun': lambda x: x[0] + x[1] - 5}]

# Initial guess
x0 = [0, 0]

result = minimize(objective, x0, bounds=bounds, constraints=constraints)

print('Optimal solution:', result.x)
print('Objective value:', result.fun)
OutputSuccess
Important Notes

Always provide an initial guess close to expected solution for better results.

Bounds and constraints help guide the optimizer to valid solutions.

Equality constraints require the function to be exactly zero; inequality constraints require the function to be zero or positive.

Summary

Bounds limit each variable to a range.

Constraints enforce rules variables must follow.

Use bounds and constraints together to solve realistic optimization problems.