Chapter 2 - Linear Programming (Part 3 - Python)
Chapter 2 - Linear Programming (Part 3 - Python)
nzah@utm.my-20232024(S1)
3
PuLP
§ PuLP is a library for the Python scripting language that enables users to describe
mathematical programs.
§ PuLP works entirely within the syntax and natural idioms of the Python language by
providing Python objects that represent optimization problems and decision
variables, and allowing constraints to be expressed in a way that is very similar to
the original mathematical expression.
§ To keep the syntax as simple and intuitive as possible, PuLP has focused on
supporting linear and mixed-integer models.
4
PuLP
5)Solve Model
5
Step 1: Initialize Model
§ Install
The easiest way to install pulp is using pip.
§ Import:
6
Step 2: Define Decision Variables
Example: Reddy Mikk Company
Variables:
X1 = tons produced daily of exterior paint
X2 = tons produced daily of interior paint
Z = total daily profit (in thousands of dollars)
Objective: Maximize, Z = 5X1 + 4X2
Constraints:
1. 6𝑋! + 4𝑋" ≤ 24 (raw material M1)
2. 𝑋!+2𝑋" ≤ 6 (raw material M2)
3. 𝑋" − 𝑋! ≤ 1
4. 𝑋" ≤ 2
5. 𝑋! ≥ 0; 𝑋" ≥ 0
7
Step 2: Define Decision Variables (cont’d)
Create the variable to contain the problem data. Install using LpProblem function.
8
Step 2: Define Decision Variables (cont’d)
§ The problem variables X1 and X2 are create using LpVariable class.
ii. the lower bound on this variable(The bounds can be entered directly as a number, or
None as the default)
iv. the fourth is essentially the type of data (discrete or continuous) function. With the
default as Lpcontinous.
§ If the first few parameters are entered and the rest are ignored , they take their
default values.
9
Step 2: Define Decision Variables (cont’d)
10
Step 3: Define Objective Function
11
Step 4: Define Constraints
12
Step 4: Define Constraints (cont’d)
§ WriteLP function can be used to copy the information into a .lp file into the directory
that the code-block is running from.
§ The dot . between the variable/object and the function/method is important and is
seen frequently in Object Oriented software
13
Step 5: Solve Model
• The LP is solved using the solver that PuLP chooses. The input brackets
after solve( )are left empty in this case, however they can be used to
specify which solver to use.
14
Step 5: Solve Model (cont’d)
• Request the status of the solution, which can be one of “Not Solved”,
“Infeasible”, “Unbounded”, “Undefined” or “Optimal”.
15
Step 5: Solve Model (cont’d)
§ The variables and their resolved optimum values can now be printed to the screen.
§ The for loop is used to called all the problem variable names.
§ Prints each variable name, followed by an equal's sign, followed by its optimum
value.
§ The optimised objective function value is printed to the screen, using the value
(prob.objective) function.
16
Step 5: Solve Model (cont’d)
Output
17
EXERCISES
Use Python to solve the following problem.
#1: The Alex Garment Company manufactures men’s shirts and women’s blouses. The production process includes cutting,
sewing, and packaging. The company employs 25 workers in the cutting department, 35 in the sewing department, and 5 in
the packaging department. The factory works one 8-hr shift, 5 days a week. The following table gives the time requirements
and profits per unit to produce the two garments. Determine the optimal weekly production schedule for Alex Garment
Company.
#2: In Hamid grocery store, shelf space is limited and must be used effectively to increase profit. Two cereal items, Grano
and Wheatie, compete for a total shelf space of 60 ft2 . A box of Grano occupies 0.2 ft2 and a box of Wheatie needs 0.4 ft2 . The
maximum daily demands of Grano and Wheatie are 200 and 120 boxes, respectively. A box of Grano nets $1.00 in profit and a
box of Wheatie $1.35. Hamid thinks that because the unit profit of Wheatie is 35% higher than that of Grano, Wheatie should be
allocated 35% more space than Grano, which amounts to allocating about 57% to Wheatie and 43% to Grano. Determine the
optimal value for the items to be allocated on the shelf to maximise the profit.
18
EXERCISES
Use Python to solve the following problem.
#3: A firm has two bottling plant. One plant located at Coimbatore and other plant located at
Chennai. Each plant produces three types of drinks; Coca-Cola , Fanta and Thumps-up. The
following table show the data.
Number of bottles produced per day by plant at
Product Coimbatore Chennai
Coca-Cola 15000 15000
Fanta 30000 10000
Thumps-Up 20000 50000
Market survey indicates that during the month of April there will be a demand of 200,000 bottles of
Coca-cola , 400,000 bottles of Fanta and 440,000 bottles of Thumps-up. For how many days each
plant be run in April to minimize the production cost, while still meeting the market demand?
19