Beginner’s guide to operations research with Python. Part 2: Transportation

Vivek Chaudhary
4 min readFeb 24, 2022

Introduction

In the previous installment of this series of articles on operations research using Python, we used the pulp library in python to solve a linear programming problem to determine the production of different varieties of beer at a brewery. In this article, we will solve another common type of problem in operations research, Transportation.

This article will solve a very basic example of a transportation problem using the pulp library in Python. Readers may use this as a reference to solve more complex, real-world problems.

The business problem

Let us consider a company that has three production facilities, namely A, B, and C. The three production facilities ship the product to four warehouses, namely D, E, F, and G

  • The production capacity of facilities A, B, and C are 35 units, 50 units, and 40 units respectively.
  • The capacity of warehouses D, E, F, and G are 45 units, 20 units, 30 units, and 30 units respectively.
  • The cost of transportation from the production facilities A, B, and C to warehouses D, E, F, and G are given by the matrix as follows:
Transportation cost matrix
  • The problem can be represented as the following table:
Problem summary

How many units of the product should be shipped from the production facilities A, B, and C to the warehouses D, E, F, and G to minimize the cost?

We will solve this problem using the pulp library in Python.

The solution

To start, we import the pulp library:

from pulp import *

We declare the list of the three production facilities as an array named “src”

# Source list
src = ['A', 'B', 'C']

We declare the list of the four warehouses as an array named “dest”

# Destination list
dest = ['D', 'E', 'F', 'G']

Then, we create a dictionary “supply” containing the production capacity of each one of the production facilities “A”, “B”, and “C”

# Supply from each source
supply = {'A' : 35, 'B' : 50, 'C' : 40}

Then, we create a dictionary “demand” containing the demand at each one of the warehouses “D”, “E”, “F”, and “G”

# Demand at each destination
demand = {'D' : 45, 'E' : 20, 'F' : 30, 'G' : 30 }

Now, we create the cost matrix containing the cost of transportation from the production facilities to the warehouses as a dictionary of dictionaries called “cost”

# Transport cost from each source to destination
cost = {'A': {'D' : 8, 'E' : 6, 'F' : 10, 'G' : 9},
'B': {'D' : 9, 'E' : 12, 'F' : 13, 'G' : 7},
'C': {'D' : 14, 'E' : 9, 'F' : 16, 'G' : 5},
}

We initialize the LpProblem instance as a minimization “prob” as m and name it “Transportation”

# Creating instance of LpProblem
prob = LpProblem("Transportation", LpMinimize)

In this problem we have 12 decision variables corresponding to the 12 combinations of production facilities and warehouses for example: (“A”, “D”), (“A”,” E”), (“A”, “F”), (“A”,” G”), (“B”, “D”), (“B”,” E”)….etc.

We create an array of tuples ‘routes’ containing all the 12 possible combinations of production facilities and warehouses.

# Creating routes
routes = [(i,j) for i in src for j in dest]

We create the 12 decision variables using the array of tuples created above:

# Defining decision variables
shipping_quantity = LpVariable.dicts("shipping_quantity" , (src, dest), 0)

With this model, we want to minimize the transportation cost. The objective function is created by using the following lines:

# Defining objective function
prob += lpSum(shipping_quantity[i][j]*cost[i][j] for (i,j) in routes)

We define the supply and demand constraints as follows:

# Adding demand constraint
for j in dest:
prob += lpSum(shipping_quantity[i][j] for i in src) == demand [j]

# Adding supply constraint
for i in src:
prob += lpSum(shipping_quantity[i][j] for j in dest) <= supply[i]

We can print the problem summary by using the following lines of code:

# We can print the problem summary like this
print(prob)

The problem will be used by the following lines of code:

# Solving the problem
prob.solve()

The decision variables can be printed by using the following lines of code:

# Printing the shipping quantities
for v in prob.variables():
if v.varValue > 0:
print(v.name, "=", v.varValue)

The objective function can then be printed using the following lines of code:

# Printing the objective
print("Total transportation cost = ", value(prob.objective))

Conclusion

In this part, we learned how to formulate and solve a transportation problem using pulp library in python. You can find complete code on my GitHub here.

In the next part, we will tinker with other interesting operations research problems. Stay tuned.

Cheers!

Connect with me on Linkedin!

--

--