
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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