Python | Linear Programming in Pulp

Python Methods and Functions

Basic linear programming terms

  • Objective function: The main goal of the problem, either maximize or minimize, is the objective function of linear programming. In the problem below, Z (for minimization) is the objective function.
  • Decision Variables: Variables used to define the output as decision variables. These are unknown mathematical programming models. In the task below, we have to define the x and y values ​​to minimize Z. Here x and y are the decision variables.
  • Constraints: are the constraints on the decision variables. Constraints on decision variables given in accordance with the constraints in the following problem are constraints of linear programming.
  • Constraints of non-negativity: in linear programming, the values ​​of decision variables are always greater than or equal to 0.

Note. For the problem to be a linear programming problem, the objective function, constraints and nonnegativity constraints must be linear.

Example 1. Consider the following problem:

  Minimize:  Z = 3x + 5y  Subject to the constraints : 2x + 3y & gt; = 12 -x + y & lt; = 3 x & gt; = 4 y & lt; = 3 x, y & gt; = 0 

Solution of the above linear programming problem in Python:
PuLP — one of the many libraries in the Python ecosystem for solving optimization problems. You can install PuLp in Jupyter notebook like this:

import sys! {Sys.executable} - m pip install pulp

Code: To solve the above linear programming problem in Python:

# import library mass as p

import pulp as p

 
# Create LP minify task

Lp_prob = p.LpProblem ( ' Problem' , p.LpMinimize) 

  
# Create problem variables

x = p.LpVariable ( "x" , lowBound = 0 # Create variable x & gt; = 0

y = p.LpVariable ( "y" , lowBound = 0 # Create variable y & gt; = 0

 
# Objective function

Lp_prob + = 3 * x + 5 *

 
Restrictions:

Lp_prob + = 2 * x + 3 * y & gt; = 12

Lp_prob + = - x + y & lt ; = 3

Lp_prob + = x & gt; = 4

Lp_prob + = y & lt; = 3

 
# Show issue

print (Lp_prob)

 

status = Lp_prob.solve ()  # Solver

print   (p.LpStatus [status])  # Solution status

  
# Print the final decision

print (p.value (x), p.value (y), p.value (Lp_prob.objective)) 

Explanation :

Now let's go through the code step by step:

  • Line 1-2: first import the library mass as p.
  • Line 4-5: Define the problem by giving a suitable name for your problem, here I am gave the name "Problem". Also, specify your target for the objective function: expand or collapse.
  • Line 7-9: Define an LpVariable to store the variables of the objective functions. The next argument sets the lower bound for the specified variable, that is, 0, and the upper bound is zero by default. You can also specify an upper bound.
  • Line 11-12: denotes the objective function in terms of defined variables.
  • Line 14-18: these are variable restrictions.
  • Line 21: this will show you the problem on the output screen.
  • Line 23: this is the solution to the problems.
  • Line 24: will display the status of the problem.
  • Line 27: will print the value for x and y and the minimum value for the objective function.

View the output

# Show issue

print (Lp_prob)

Exit

status = Lp_prob.solve ()  # Solver

print (p.LpStatus [status])  # Status solutions

Exit

 Optimal 

# Print the final decision

print (p.value (x), p.value (y), p.value (Lp_prob.objective))

Exit

 6.0 0.0 18.0 

Optimal values ​​for x and y are 6.0 and 0.0 , respectively. The optimized objective function value is 18.0.





Get Solution for free from DataCamp guru