0% found this document useful (0 votes)
11 views11 pages

Operations Researchers

Dhhgg

Uploaded by

gadaderohan19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Operations Researchers

Dhhgg

Uploaded by

gadaderohan19
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Progressive Education Societiy’s

Modern College of Art’s, Science and Commerce (Autonomous),

Shivajinagar, Pune - 5

S.Y.B.Sc.(Computer Science) (Semester-IV)

Subject : Mathematics Practical IV (19CsMATU403) Lecture.: Mrs Rane Milan S.

Chapter 3. Study of Operational Research in Python

To solve optimization problems using linear programming program we use package PuLP. We have to install PuLP

package on command prompt as pip install pulp

I. Linear Programming in Python

Functions used for solving LLP are lised below:

1. LpProblem: This function creates a new LP Problem with the specified associated parameters.

2. LpVariable: It is used to assign an LP Variable 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

solution status, it gives different outputs. Consider the following table:

Solution status Numeric output

Optimal 1

Not solved 0

Infeasible −1

Unbounded −2

Undefined −3

4. model.objective.value(): It is used to display optimum value of objective function.

5. value: It is used to display optimum value of associated variables.

Example 1: Solve the following LPP:

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

>>> from pulp import *

>>> 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)

>>> #objective function

>>> model+=3*x+5*y+4*z

>>> model

NoName:

MAXIMIZE

3*x + 5*y + 4*z + 0

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

Example 3: Solve the following LPP:

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

>>> from pulp import *

>>> 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

1*x + 2*y + 1*z + 0

SUBJECT TO

_C1: x + 0.5 y + 0.5 z <= 1

_C2: 1.5 x + 2 y + z >= 8

VARIABLES

x Continuous

y Continuous

z Continuous

>>> model.solve()

-1

>>> #solution is infeasible

3
Example 4: Solve the following LPP :

Min. x + y

subject to,

x≥6

y≥6

x + y ≤ 11

x ≥ 0, y ≥ 0

>>> from pulp import *

>>> 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

>>> #solution is infeasible

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.

The transportation costs (in Rs.) are given below.


shop

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

>>> from pulp import *

>>> #creates a list of all the supply nodes

>>> 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}

>>> #create a list of all demad nodes

>>> 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}

>>> #create a list of costs of each transportation path

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

>>> Routes=[(w,r) for w in company for r in shop]

>>> #A dictionary caiied ’vars’ is created to contain the routes

>>> vars=LpVariable.dicts("Route",(company,shop),0,None,LpInteger)

>>> # The objective function is added to ’prob’ first

>>> prob+=lpSum([vars[w][r]*costs[w][r] for (w,r) in Routes]),"Sum_of_Costs"

>>> #The supply maximum constraint are added to prob for each supply node

>>> for w in company:

prob+=lpSum([vars[w][r] for r in shop ])<=supply[w],"Sum_Prod_%s"%w

>>> #The demand minimum costriants are added to prob for each demand node

>>> for r in shop:

prob+=lpSum([vars[w][r] for w in company ])>=demand[r],"Sum_Prod_%s"%r

>>> prob.writeLP

<bound method LpProblem.writeLP of Transportation:

MINIMIZE

3*Route_A_1 + 5*Route_A_2 + 7*Route_A_3 + 6*Route_A_4 + 2*Route_B_1 + 5*Route_B_2 + 8*Route_B_3 + 2*Route

SUBJECT TO

Sum_Prod_A: Route_A_1 + Route_A_2 + Route_A_3 + Route_A_4 <= 50

Sum_Prod_B: Route_B_1 + Route_B_2 + Route_B_3 + Route_B_4 <= 75

Sum_Prod_C: Route_C_1 + Route_C_2 + Route_C_3 + Route_C_4 <= 25

Sum_Prod_1: Route_A_1 + Route_B_1 + Route_C_1 >= 20

Sum_Prod_2: Route_A_2 + Route_B_2 + Route_C_2 >= 20

Sum_Prod_3: Route_A_3 + Route_B_3 + Route_C_3 >= 50

6
Sum_Prod_4: Route_A_4 + Route_B_4 + Route_C_4 >= 60

VARIABLES

0 <= Route_A_1 Integer

0 <= Route_A_2 Integer

0 <= Route_A_3 Integer

0 <= Route_A_4 Integer

0 <= Route_B_1 Integer

0 <= Route_B_2 Integer

0 <= Route_B_3 Integer

0 <= Route_B_4 Integer

0 <= Route_C_1 Integer

0 <= Route_C_2 Integer

0 <= Route_C_3 Integer

0 <= Route_C_4 Integer

>

>>> prob.solve()

>>> value(prob.objective)

610.0

>>> # To display all values of variables

>>> for v in prob.variables():

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)

Routes=[(w,r) for w in company for r in shop]

vars=LpVariable.dicts("Route",(company,shop),0,None,LpInteger)

prob+=lpSum([vars[w][r]*costs[w][r] for (w,r) in Routes]), "Sum_of_Costs"

for w in company:

prob+=lpSum([vars[w][r] for r in shop])<=supply[w],"Sum_of_Prod_%s"%w

for r in shop:

prob+=lpSum([vars[w][r] for w in company])>=demand[r],"Sum_of_Prod_%s"%r

print(prob.writeLP)

prob.solve()

print(’Minimum transportation cost’,"=" ,value(prob.objective))

for v in prob.variables():

if v.varValue>0:

print(v.name,"=",v.varValue)

Example 2 : Solve the following transportation problem

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

from pulp import *

company=["A","B","C"]

supply={"A":50,}

from pulp import *

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)

Routes=[(i,j) for i in origin for j in destination]

vars=LpVariable.dicts("Route",(origin,destination),0,None,LpInteger)

prob+=lpSum([vars[i][j]*costs[i][j] for (i,j) in Routes]), "Sum_of_Costs"

for i in origin:

prob+=lpSum([vars[i][j] for j in destination])<=supply[i],"Sum_of_Prod_%s"%i

for j in destination:

prob+=lpSum([vars[i][j] for i in origin])>=demand[j],"Sum_of_Prod_%s"%j

print(prob.writeLP)

prob.solve()

print(’Minimum transportation cost’,"=" ,value(prob.objective))

9
for v in prob.variables():

if v.varValue>0:

print(v.name,"=",v.varValue)

Ans: <bound method LpProblem.writeLP of Transportaion:

MINIMIZE

23*Route_O1_D1 + 27*Route_O1_D2 + 16*Route_O1_D3 + 18*Route_O1_D4 + 12*Route_O2_D1 + 17*Route_O2_D2 + 20*

SUBJECT TO

Sum_of_Prod_O1: Route_O1_D1 + Route_O1_D2 + Route_O1_D3 + Route_O1_D4 <= 30

Sum_of_Prod_O2: Route_O2_D1 + Route_O2_D2 + Route_O2_D3 + Route_O2_D4 <= 40

Sum_of_Prod_O3: Route_O3_D1 + Route_O3_D2 + Route_O3_D3 + Route_O3_D4 <= 53

Sum_of_Prod_D1: Route_O1_D1 + Route_O2_D1 + Route_O3_D1 >= 22

Sum_of_Prod_D2: Route_O1_D2 + Route_O2_D2 + Route_O3_D2 >= 35

Sum_of_Prod_D3: Route_O1_D3 + Route_O2_D3 + Route_O3_D3 >= 25

Sum_of_Prod_D4: Route_O1_D4 + Route_O2_D4 + Route_O3_D4 >= 41

VARIABLES

0 <= Route_O1_D1 Integer

0 <= Route_O1_D2 Integer

0 <= Route_O1_D3 Integer

0 <= Route_O1_D4 Integer

0 <= Route_O2_D1 Integer

0 <= Route_O2_D2 Integer

0 <= Route_O2_D3 Integer

0 <= Route_O2_D4 Integer

0 <= Route_O3_D1 Integer

0 <= Route_O3_D2 Integer

0 <= Route_O3_D3 Integer

0 <= Route_O3_D4 Integer

>

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

Example 2 : Solve the following transportation problem


Destination

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

You might also like