# Python | Linear Programming in Pulp

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" = 12 -x + y "= 3 x" = 4 y "= 3 x, y" = 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" = 0 y = p.LpVariable ( "y" , lowBound = 0 )  # Create variable y" = 0   # Objective function Lp_prob + = 3 * x + 5 * y    Restrictions: Lp_prob + = 2 * x + 3 * y" = 12 Lp_prob + = - x + y & lt ; = 3 Lp_prob + = x" = 4 Lp_prob + = y " = 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.

## Shop Best laptop for Excel

\$ Best laptop for Solidworks

\$399+ Best laptop for Roblox

\$399+ Best laptop for development

\$499+ Best laptop for Cricut Maker

\$299+ Best laptop for hacking

\$890 Best laptop for Machine Learning

\$699+ Raspberry Pi robot kit

\$150

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

PythonStackOverflow

Check if one list is a subset of another in Python

PythonStackOverflow

How to specify multiple return types using type-hints

PythonStackOverflow

Printing words vertically in Python

PythonStackOverflow

Python Extract words from a given string

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

PythonStackOverflow

Python os.path.join () method

PythonStackOverflow

Flake8: Ignore specific warning for entire file

## Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries