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
scipy_optimize.htm
Advertisements