
- 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 optimize.linprog() Function
The scipy.optimize.linprog() function is a tool in SciPy which is used for solving linear programming problems. It helps find the minimum of a linear objective function while adhering to both equality and inequality constraints. Additionally this function handles both bounded and unbounded variables by allowing for versatile problem-solving.
This function is designed to accommodate multiple optimization methods such as the 'simplex' method, 'revised simplex' and 'interior-point' method in which each suited to different types of problems.
This function results returned by the function include the optimal values for the decision variables, the objective function value at the optimal point, the success status of the optimization and a message describing how the process was terminated.
The common applications of this function which include the resource allocation, logistics and optimizing supply chain models, among others where constraints are often linear.
Syntax
Following is the syntax for calling the scipy.optimize.linprog() function to solve linear programming problems −
scipy.optimize.linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None, method='simplex', options=None)
Parameters
Here are the parameters of the scipy.optimize.linprog() function −
- c(array-like): Coefficients of the objective function to minimize.
- A_ub(array-like, optional): Coefficients for the inequality constraints (Ax b).
- b_ub(array-like, optional): Right-hand side values for the inequality constraints.
- A_eq(array-like, optional): Coefficients for the equality constraints (Ax = b).
- b_eq(array-like, optional): Right-hand side values for the equality constraints.
- bounds(sequence, optional): This parameter specifies the bounds for each variable in the form of a sequence of tuples.
- method(string, optional): This parameter specifies the algorithm used to solve the optimization problem. The default value is 'simplex'.
- options(dict, optional): A dictionary of solver-specific options such as maximum iterations.
Return Value
The scipy.optimize.linprog() function returns an object that contains important information regarding the optimization result −
- x: The optimal values of the decision variables.
- fun: The optimal value of the objective function.
- success: A boolean indicating whether the optimization was successful.
- message: A message providing details about the optimization process and termination status.
Example of Basic Linear Programming
Following is the example of performing the constrained optimation of the linear programming with the help of scipy.optimize.linprog(). Here in this example we are optimizing the equation f(x) = 2x1 + 3x2 −
from scipy.optimize import linprog # Objective function coefficients c = [2, 3] # Coefficients for inequality constraints A_ub = [[-1, -1], [2, 1]] b_ub = [-4, 6] # Bounds for the variables x_bounds = (0, None) y_bounds = (0, None) # Call linprog to solve the problem result = linprog(c, A_ub=A_ub, b_ub=b_ub, bounds=[x_bounds, y_bounds], method='highs') # Print the optimal solution and value print("Optimal solution:", result.x) print("Objective function value:", result.fun) print("Objective function value:", result.fun)
Heres the expected output for the linear programming problem by using the function scipy.optimize.linprog()−
Optimal solution: [2. 2.] Objective function value: 10.0
Linear Programming with Variable Bounds
This example demonstrates solving a linear problem with bounds on the variables:
from scipy.optimize import linprog # Coefficients of the objective function c = [1, 2] # Coefficients for the inequality constraints A = [[-1, 1], [3, 2]] b = [2, 12] # Bounds on the variables bounds = [(2, None), (0, None)] # Solve the problem with bounds on variables result = linprog(c, A_ub=A, b_ub=b, bounds=bounds, method='highs') # Output the result print("Optimal solution:", result.x) print("Objective function value:", result.fun)
Below is the output for this example −
Optimal solution: [2. 0.] Objective function value: 2.0
Linear Programming with Equality Constraints
In this example we are passing the equality constraints to the function scipy.optimize.linprog() −
from scipy.optimize import linprog # Coefficients of the objective function c = [1, 2] # Coefficients for the inequality constraints A = [[-1, 1], [3, 2]] b = [2, 12] # Equality constraint: x + y = 5 A_eq = [[1, 1]] b_eq = [5] # Solve the problem with equality constraint result = linprog(c, A_ub=A, b_ub=b, A_eq=A_eq, b_eq=b_eq, method='simplex') # Display the result print("Optimal solution:", result.x) print("Objective function value:", result.fun)
Here is the expected output when including equality constraints −
Optimal solution: [2. 3.] Objective function value: 8.0