The Wayback Machine - https://web.archive.org/web/20240620092720/https://www.geeksforgeeks.org/python-linear-programming-in-pulp/
Open In App

Python | Linear Programming in Pulp

Last Updated : 26 Aug, 2019
Improve
Improve
Like Article
Like
Save
Share
Report

Linear Programming (LP), also known as linear optimization is a mathematical programming technique to obtain the best result or outcome, like maximum profit or least cost, in a mathematical model whose requirements are represented by linear relationships. Linear programming is a special case of mathematical programming, also known as mathematical optimization.
Generally, an organization or a company has mainly two objectives, the first one is minimization and the other is maximization. Minimization means to minimize the total cost of production while maximization means to maximize their profit. So with the help of linear programming graphical method, we can find the optimum solution.

Basic terminologies of Linear Programming

  • Objective Function: The main aim of the problem, either to maximize of to minimize, is the objective function of linear programming. In the problem shown below, Z (to minimize) is the objective function.
  • Decision Variables: The variables used to decide the output as decision variables. They are the unknowns of the mathematical programming model. In the below problem, we are to determine the value of x and y in order to minimize Z. Here, x and y are the decision variables.
  • Constraints: These are the restrictions on the decision variables. The limitations on the decision variables given under subject to the constraints in the below problem are the constraints of the Linear programming.
  • Non – negativity restrictions: In linear programming, the values for decision variables are always greater than or equal to 0.

Note: For a problem to be a linear programming problem, the objective function, constraints, and the non – negativity restrictions must be linear.

Example 1: Consider the following problem:

Minimize :  Z = 3x + 5y
Subject to the constraints: 
2x + 3y >= 12
-x + y <= 3
x >= 4
y <= 3
x, y >= 0

Solving the above linear programming problem in Python:
PuLP is one of many libraries in Python ecosystem for solving optimization problems. You can install PuLp in Jupyter notebook as follows:




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


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




# import the library pulp as p
import pulp as p
  
# Create a LP Minimization problem
Lp_prob = p.LpProblem('Problem', p.LpMinimize) 
  
# Create problem Variables 
x = p.LpVariable("x", lowBound = 0)   # Create a variable x >= 0
y = p.LpVariable("y", lowBound = 0)   # Create a variable y >= 0
  
# Objective Function
Lp_prob += 3 * x + 5 * y   
  
# Constraints:
Lp_prob += 2 * x + 3 * y >= 12
Lp_prob += -x + y <= 3
Lp_prob += x >= 4
Lp_prob += y <= 3
  
# Display the problem
print(Lp_prob)
  
status = Lp_prob.solve()   # Solver
print(p.LpStatus[status])   # The solution status
  
# Printing the final solution
print(p.value(x), p.value(y), p.value(Lp_prob.objective))  


Explanation :

Now, let’s understand the code step by step:

  • Line 1-2: First import the library pulp as p.
  • Line 4-5: Define the problem by giving a suitable name to your problem, here I have given the name ‘Problem’. Also, specify your aim for the objective function of whether to Maximize or Minimize.
  • Line 7-9: Define LpVariable to hold the variables of the objective functions. The next argument specifies the lower bound of the defined variable, i.e. 0, and the upper bound is none by default. You can specify the upper bound too.
  • Line 11-12: Denotes the objective function in terms of defined variables.
  • Line 14-18: These are the constraints on the variables.
  • Line 21: This will show you the problem in the output screen.
  • Line 23: This is the problem solver.
  • 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.

Review the Output




# Display the problem
print(Lp_prob)


Output




status = Lp_prob.solve()   # Solver
print(p.LpStatus[status])   # The solution status


Output

Optimal




# Printing the final solution
print(p.value(x), p.value(y), p.value(Lp_prob.objective))


Output

6.0 0.0 18.0

The optimal value for x and y are 6.0 and 0.0 respectively. The optimised objective function value is 18.0.



Similar Reads

Learn Programming Languages- List of Top 11 Programming Languages
In this rapidly growing world, programming languages are also rapidly expanding, and it is very hard to determine the exact number of programming languages. It is an essential part of software development because it creates a communication bridge between humans and computers. Now, if you are a beginner who wants to learn, search the internet, you w
9 min read
Python Program for Linear Search
Problem: Given an array arr[] of n elements, write a function to search a given element x in arr[] using Python. Examples : Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170} x = 110;Output : 6Element x is present at index 6Input : arr[] = {10, 20, 80, 30, 60, 50, 110, 100, 130, 170} x = 175;Output : -1Element x is not present in arr[].A
4 min read
Creating linear kernel SVM in Python
Prerequisite: SVM Let's create a Linear Kernel SVM using the sklearn library of Python and the Iris Dataset that can be found in the dataset library of Python. Linear Kernel is used when the data is Linearly separable, that is, it can be separated using a single Line. It is one of the most common kernels to be used. It is mostly used when there are
3 min read
Python | Linear search on list or tuples
Let us see a basic linear search operation on Python lists and tuples. A simple approach is to do a linear search, that is Start from the leftmost element of the list and one by one compare x with each element of the list.If x matches with an element, return True.If x doesn’t match with any of the elements, return False. Example #1: Linear Search o
2 min read
Univariate Linear Regression in Python
In this article, we will explain univariate linear regression. It is one of the simplest types of regression. In this regression, we predict our target value on only one independent variable. Univariate Linear Regression in Python Univariate Linear Regression is a type of regression in which the target variable depends on only one independent varia
6 min read
Raise a square matrix to the power n in Linear Algebra using NumPy in Python
In this article, we will discuss how to raise a square matrix to the power n in the Linear Algebra in Python. The numpy.linalg.matrix_power() method is used to raise a square matrix to the power n. It will take two parameters, The 1st parameter is an input matrix that is created using a NumPy array and the 2nd parameter is the exponent n, which ref
3 min read
Python - Solve the Linear Equation of Multiple Variable
Prerequisite: Sympy.solve() In this article, we will discuss how to solve a linear equation having more than one variable. For example, suppose we have two variables in the equations. Equations are as follows: x+y =1 x-y =1 When we solve this equation we get x=1, y=0 as one of the solutions. In Python, we use Eq() method to create an equation from
2 min read
How to implement linear interpolation in Python?
Linear Interpolation is the technique of determining the values of the functions of any intermediate points when the values of two adjacent points are known. Linear interpolation is basically the estimation of an unknown value that falls within two known values. Linear Interpolation is used in various disciplines like statistical, economics, price
4 min read
Solve Linear Equations using eval() in Python
Linear equations using one variable of the form a + bx = c + dx can be solved in Python using eval() function. The input type will be a linear equation in the form of a string. Syntax: eval(expression, globals=None, locals=None) Here, we will transform the equation into an expression of real and imaginary numbers such that eval can easily process i
3 min read
Discrete Linear Convolution of Two One-Dimensional Sequences and Get Where they Overlap in Python
In this article, we will be looking at the approach to returning the discrete linear convolution of two one-dimensional sequences and getting where they overlap in Python. Numpy np.convolve() To return the discrete linear convolution of two one-dimensional sequences, the user needs to call the numpy.convolve() method of the Numpy library in Python.
3 min read
Article Tags :
Practice Tags :