Operations Researchers
Operations Researchers
Shivajinagar, Pune - 5
To solve optimization problems using linear programming program we use package PuLP. We have to install PuLP
1. LpProblem: This function creates a new LP Problem with the specified associated parameters.
3. model.solve(): returns the integer status of the solution, which will be 1 if the optimum is found. For different
Optimal 1
Not solved 0
Infeasible −1
Unbounded −2
Undefined −3
Min Z = 3.5x + 2y
subject to ,x + y ≥ 5
x≥4
y≤2
x ≥ 0, y ≥ 0
1
Example 2: Solve the following LPP:
Max Z = 3x + 5y + 4z
subject to ,2x + 3y ≤ 8
2y + 5z ≤ 10
3x + 2y + 4z ≤ 15
x ≥ 0, y ≥ 0, z ≥ 0
>>> model=LpProblem(sense=LpMaximize)
>>> x=LpVariable(name="x",lowBound=0)
>>> y=LpVariable(name="y",lowBound=0)
>>> z=LpVariable(name="z",lowBound=0)
>>> model+=(2*x+3*y<=8)
>>> model+=(2*y+5*z<=10)
>>> model+=(3*x+2*y+4*z<=15)
>>> model+=3*x+5*y+4*z
>>> model
NoName:
MAXIMIZE
SUBJECT TO
_C1: 2 x + 3 y <= 8
_C2: 2 y + 5 z <= 10
_C3: 3 x + 2 y + 4 z <= 15
VARIABLES
x Continuous
y Continuous
z Continuous
>>> model.solve()
>>> model.objective.value()
18.658536500000004
2
>>> x.value()
2.1707317
>>> y.value()
1.2195122
>>> z.value()
1.5121951
Min Z = x + 2y + z
1 1
subject to ,x + y + z ≤ 1
2 2
3
x + 2y + z ≥ 8
2
x ≥ 0, y ≥ 0, z ≥ 0
>>> model=LpProblem(sense=LpMinimize)
>>> x=LpVariable(name="x",lowBound=0)
>>> y=LpVariable(name="y",lowBound=0)
>>> z=LpVariable(name="z",lowBound=0)
>>> model+=(x+0.5*y+0.5*z<=1)
>>> model+=(1.5*x+2*y+z>=8)
>>> model+=x+2*y+z
>>> model
NoName:
MINIMIZE
SUBJECT TO
VARIABLES
x Continuous
y Continuous
z Continuous
>>> model.solve()
-1
3
Example 4: Solve the following LPP :
Min. x + y
subject to,
x≥6
y≥6
x + y ≤ 11
x ≥ 0, y ≥ 0
>>> model=LpProblem(sense=LpMinimize)
>>> x=LpVariable(name="x",lowBound=0)
>>> y=LpVariable(name="y",lowBound=0)
>>> model+=(x>=6)
>>> model+=(y>=6)
>>> model+=(x+y<=11)
>>> model
NoName:
MINIMIZE
None
SUBJECT TO
_C1: x >= 6
_C2: y >= 6
_C3: x + y <= 11
VARIABLES
x Continuous
y Continuous
>>> model.solve()
-1
4
II Transportation Problem
Example 1: The ICARE company has three plants located throughout a state with production capacity 50,75 and 25 gallons. Each
day the firm must furnish its four retail shops R1, R2, R3 and R4 with at least 20,20,50 and 60 gallons respectively.
Company R1 R2 R3 R4 Supply
A 3 5 7 6 50
B 2 5 8 2 75
C 3 6 9 2 25
Demand 20 20 50 60
Supply constraints:
A1 + A2 + A3 + A4 ≤ 50
B1 + B2 + B3 + B4 ≤ 75
C1 + C2 + C3 + C4 ≤ 25
Demand constraint:
A1 + B1 + C1 ≥ 20
A2 + B2 + C2 ≥ 20
A3 + B3 + C3 ≥ 50
A4 + B4 + C4 ≥ 60
>>> company=["A","B","C"]
>>> #create a dictionary for the number of units of supply for each supply node
>>> supply={"A":50,
"B":75,
"C":25}
>>> shop=["1","2","3","4"]
>>> #create a dictionary for the number of units of demand for each demand node
>>> demand={"1":20,
"2":20,
"3":50,
"4":60}
5
>>> costs={"A":{"1":3,"2":5,"3":7,"4":6},
"B":{"1":2,"2":5,"3":8,"4":2},
"C":{"1":3,"2":6,"3":9,"4":2}}
>>> prob=LpProblem("Transportation",LpMinimize)
>>> #create a list of tuples containing all the possible routes for transport
>>> vars=LpVariable.dicts("Route",(company,shop),0,None,LpInteger)
>>> #The supply maximum constraint are added to prob for each supply node
>>> #The demand minimum costriants are added to prob for each demand node
>>> prob.writeLP
MINIMIZE
SUBJECT TO
6
Sum_Prod_4: Route_A_4 + Route_B_4 + Route_C_4 >= 60
VARIABLES
>
>>> prob.solve()
>>> value(prob.objective)
610.0
print(v.name,"=",v.varValue)
Route_A_1 = 0.0
Route_A_2 = 0.0
Route_A_3 = 50.0
Route_A_4 = 0.0
Route_B_1 = 20.0
Route_B_2 = 20.0
Route_B_3 = 0.0
Route_B_4 = 35.0
Route_C_1 = 0.0
Route_C_2 = 0.0
Route_C_3 = 0.0
Route_C_4 = 25.0
7
from pulp import *
company=["O1","O2","O3","O4"]
supply={"O1":5,
"O2":6,
"O3":2,
"O4":9}
shop=["D1","D2","D3","D4","D5","D6"]
demand={"D1":4,
"D2":4,
"D3":6,
"D4":2,
"D5":4,
"D6":2}
costs={"O1":{"D1":9,"D2":12,"D3":9,"D4":6,"D5":9,"D6":10},
"O2":{"D1":7,"D2":3,"D3":7,"D4":7,"D5":5,"D6":5},
"O3":{"D1":6,"D2":5,"D3":9,"D4":12,"D5":3,"D6":11},
"O4":{"D1":6,"D2":8,"D3":11,"D4":2,"D5":2,"D6":10}}
prob=LpProblem("Transportaion",LpMinimize)
vars=LpVariable.dicts("Route",(company,shop),0,None,LpInteger)
for w in company:
for r in shop:
print(prob.writeLP)
prob.solve()
for v in prob.variables():
if v.varValue>0:
print(v.name,"=",v.varValue)
8
Destination
Origins D1 D2 D3 D4 Supply
O1 23 27 16 18 30
O2 12 17 20 51 40
O3 22 28 12 32 53
Demand 22 35 25 41 123
company=["A","B","C"]
supply={"A":50,}
origin=["O1","O2","O3"]
supply={"O1":30 ,
"O2":40,
"O3":53}
destination=["D1","D2","D3","D4"]
demand={"D1":22,
"D2":35,
"D3":25,
"D4":41}
costs={"O1":{"D1":23,"D2":27,"D3":16,"D4":18},
"O2":{"D1":12,"D2":17,"D3":20,"D4":51},
"O3":{"D1":22,"D2":28,"D3":12,"D4":32}}
prob=LpProblem("Transportaion",LpMinimize)
vars=LpVariable.dicts("Route",(origin,destination),0,None,LpInteger)
for i in origin:
for j in destination:
print(prob.writeLP)
prob.solve()
9
for v in prob.variables():
if v.varValue>0:
print(v.name,"=",v.varValue)
MINIMIZE
SUBJECT TO
VARIABLES
>
10
Minimum transportation cost = 2221.0
Route_O1_D4 = 30.0
Route_O2_D1 = 5.0
Route_O2_D2 = 35.0
Route_O3_D1 = 17.0
Route_O3_D3 = 25.0
Route_O3_D4 = 11.0
>>>
∴ M inimumtransportationcost
Origins D1 D2 D3 D4 Supply
O1 1 2 1 4 30
O2 3 3 2 1 50
O3 4 2 5 9 20
Required 20 40 30 10
content...
Warehouse
Factory W1 W2 W3 W4 Supply
F1 19 30 50 10 7
Example:
F2 70 30 40 60 9
F3 40 8 70 20 18
Required 5 8 7 14
11