Use SciPy Library in Python to Solve Linear Programming Problems
scipy.optimize
SciPy provides functions for minimizing (or maximizing) objective functions, possibly subject to constraints. It includes solvers for nonlinear problems (with support for both local and global optimization algorithms), linear programming, constrained and nonlinear least-squares, root finding, and curve fitting.
This is the official manual of scipy.optimize
: Optimization and root finding (scipy.optimize) — SciPy v1.13.0 Manual
scipy.optimize.linprog
is used here to solve the linear programming problem. More information about scipy.optimize.linprog
: scipy.optimize.linprog — SciPy v1.13.0 Manual
Note:
-
Before calling
scipy.optimize.linprog
, the maximization problem needs to be converted into a minimization problem. -
The sign of the inequality constraint needs to be converted to less than or equal to.
Install the SciPy Library
Install the SciPy library in Python:
1 | pip install scipy |
Example
S.T. :
Bounds:
Code
1 | from scipy.optimize import linprog |
Results
The output of the program is as follows:
1 | message: Optimization terminated successfully. (HiGHS Status 7: Optimal) |