SciPy - Constrained Optimization



The SciPy Constrained optimization involves finding the optimal value of an objective function f(x) subject to constraints. These constraints can be equality constraints as h(x)=0, inequality constraints as g(x)0) or simple bounds lxu.

SciPy's scipy.optimize module provides powerful tools for solving constrained optimization problems. In this chapter we are going to see in detail, how the Constrained Optimization works.

Components of a Constrained Optimization

A constrained optimization problem consists of several key components that define its structure and behavior. These components are as follows −

  • Objective Function: This is the scalar function to minimize or maximize.
  • Decision Variables: These are the variables that are optimized to achieve the objective.
  • Equality Constraints: These are the conditions that must be satisfied exactly.
  • Inequality Constraints: These are the conditions that impose upper or lower limits.
  • Bounds: They are the Lower and upper limits for individual variables.
  • Feasible Region: This is the region where set of all points that satisfy the constraints.
  • Optimization Direction: This indicates whether to minimize or maximize the constrained optimization.

Optimization methods in SciPy

SciPy provides several optimization methods through the scipy.optimize module by catering to various problem types which include unconstrained, constrained and large-scale optimization. Heres a list of commonly used optimization methods and the function that we can use to atchieve it in SciPy −

S.No Optimization Method Function and Description
1 Sequential Least Squares Programming (SLSQP) scipy.optimize.minimize()
A method for solving small- to medium-scale nonlinear constrained problems.
2 Linear Programming scipy.optimize.linprog()
Used for solving linear objective functions with linear constraints.
3 Nonlinear Least Squares scipy.optimize.least_squares()
Suitable for curve fitting and parameter estimation under bounds.
4 Root Finding (Nonlinear Equations) scipy.optimize.root()
Used for solving systems of nonlinear equations with equality constraints.
5 Simulated Annealing scipy.optimize.dual_annealing()
A global optimization technique for solving non-convex problems under bounds.

Applications of Constrained Optimization

Constrained optimization is widely used across various fields to solve real-world problems where certain conditions or limitations must be satisfied. Below are some of the main applications −

  • Engineering Design: We can use the Constrained optimization for structural and mechanical designs.
  • Economics: Used in Portfolio optimization with constraints on risk and return.
  • Machine Learning: Used in regularization in training models with bounded parameters.

Example

Let's consider a business optimization problem where a company wants to produce two products with limited resources such as labor and materials. The goal is to maximize the profit which subject to constraints on the available resources. In this example we are going to solve this problem by using constrained optimization techniques such as Linear Programming (LP) with the help of scipy.optimize.linprog() function −

import numpy as np
from scipy.optimize import linprog

# Coefficients of the objective function (profit from A and B)
c = [-5, -4]  # Negative because we are maximizing

# Coefficients of the inequality constraints (Labor and Material)
A = [[2, 4], [3, 2]]

# Right-hand side values of the constraints (100 labor, 80 material)
b = [100, 80]

# Bounds for the variables (Product A and B cannot be negative)
x_bounds = (0, None)
y_bounds = (0, None)

# Solve the optimization problem
result = linprog(c, A_ub=A, b_ub=b, bounds=[x_bounds, y_bounds], method='highs')

# Output the result
print("Optimal number of Product A:", result.x[0])
print("Optimal number of Product B:", result.x[1])
print("Maximum Profit:", -result.fun)

Following is the output of the Constrained optimization performed using the function scipy.optimize.linprog()

Optimal number of Product A: 15.0
Optimal number of Product B: 17.5
Maximum Profit: 145.0
Advertisements