Optimization Using Python: Dr. R K Jena
Optimization Using Python: Dr. R K Jena
Dr. R K Jena
IMT, Nagpur
What is Optimization?
4
Applications of optimization
• Scheduling of Buses/trains (Transport)
• Transportation network design
• Supply chain optimization
• Optimum circuit design of PCB
• Design optimization of various mechanical components
• Process optimization in chemical industry
• Designing of the sharing network in internet
• Search engine optimization
• Shop floor layout planning
• Production planning and scheduling
• Hospital management systems etc. 5
Python interface for optimization
6
Solving a Linear Programming problem with Python
(Pulp)
• Linear Programming is a type of optimisation where
an objective function should be
maximized/minimized with given constraints.
7
Solving using Pulp
• from pulp import *
• # declare your variables
• x1=LpVariable("x1",0)
• x2=LpVariable("x2",0)
• x3=LpVariable("x3",0)
• # defines the problem
prob = LpProblem("problem", LpMaximize)
• # defines the constraints
• prob += x1+2*x2+3*x3 <=10
• prob += x2+2*x3<=5
8
Solving using Pulp
• # defines the objective function to maximize
prob+=1000*x1+2000*x2+3000*x3
# solve the problem
• status = prob.solve()
• LpStatus[status]
• # print the results x1, x2, x3
value(x1)
value(x2)
Value(x3)
• # Print the value of the objective
print("Objective = %f" % value(prob.objective)) 9
Solving a Linear Programming problem with Python
(Pulp)
• Example:
Let us say that you want to maximize profits by selling wood soldiers
(denoted x1) and wood trains (denoted x2) given that the margin is 3$
for one soldier and 2$ for one train, you want to maximize the profit.
In addition, you have the following constraints per week:
• a soldier requires 2 hours of finishing labour. a train requires 1
hour of finishing labour. You have only 100 hours of finishing
labour avaiable weekly
• a soldier requires 1 hour of carprentry labour. Same for a train.
You have only 80 hours of carpentry labour available weekly
• Demand for soldiers not more than 40 per week
• Demand of trains not more than 1000 per week 10
Problem Formulation
• Maximize : 3x1+2x2
• Subject to:
• 2*x1 + x2 <= 100
• x1 + x2 <= 80
• x1 <=40
• X2<=1000
• x1>=0 and x2>=0
11
Solving using Pulp
• from pulp import *
• # declare your variables
x1 = LpVariable("x1", 0, 40) # 0<= x1 <= 40
x2 = LpVariable("x2", 0, 1000) # 0<= x2 <= 1000
• # defines the problem
prob = LpProblem("problem", LpMaximize)
• # defines the constraints
prob += 2*x1+x2 <= 100
prob += x1+x2 <= 80
12
Solving using Pulp
• # defines the objective function to maximize
prob += 3*x1+2*x2
• # solve the problem
• status = prob.solve()
• LpStatus[status]
• # print the results x1 = 20, x2 = 60
value(x1)
value(x2)
• # Print the value of the objective
print("Objective = %f" % value(prob.objective))
13
Solving using Pulp
A company makes two products (X and Y) using two machines (A and B).
Each unit of X that is produced requires 50 minutes processing time on
machine A and 30 minutes processing time on machine B. Each unit of Y
that is produced requires 24 minutes processing time on machine A and
33 minutes processing time on machine B.
At the start of the current week there are 30 units of X and 90 units of Y
in stock. Available processing time on machine A is forecast to be 40
hours and on machine B is forecast to be 35 hours.
The demand for X in the current week is forecast to be 75 units and for Y
is forecast to be 95 units. Company policy is to maximise the combined
sum of the units of X and the units of Y in stock at the end of the week.
Formulate the problem of deciding how much of each product to make
in the current week as a linear program. 14
Solving using Pulp
Maximise (x+30-75) + (y+90-95) = x+y-50
Subjected to:
•50x + 24y <= 2400 # machine A time
•30x + 33y <= 2100 # machine B time
•x >= 45
•y >= 5
•Answer:
•x=45 and y=6.25; Value of the objective function is
1.25
15
16
Thanks
17