You are on page 1of 5

MS LAB

TRANSPORTATION PROBLEM
CODING EXPERIMENT

Group 4

Group members:
Submitted by Christopher Basumatary

OBJECTIVE
To develop a code using to calculation transportation problem using North West Corner
method.

SOLUTION
The code is developed in such a way that it is able to read the input as shown in the format
mentioned below
Mumba
  Delhi i Supply
Kochi 80 215 1000
Coimbator
e 100 108 1500
Vizag 102 68 1200
Demand 2300 1400 3700

Language chosen – Python


PYTHON CODE
import pandas as pd
#the below command is used to read the input from csv file format and store
it to a dataframe
df_1 = pd.read_csv("R:/Documents/Scanned Documents/Amith
Mba/s3/MSL/input.csv”)
#the column with row names is not required and hence it is removed
df_1 = df_1.drop(labels="Unnamed: 0",axis = 1)

#total rows and columns of dataset is measured


totrows = len(df_1)
totcols = len(df_1.columns)
#the values of supply column are stored to dataframe
lastcol = list(df_1.columns)
#last value is removed because it corresponds to the total value and is not
required
lastcol = lastcol[-1]

coltotal = {}
rowtotal = {}
#total of each column is stored in a dictionary
for i in df_1.columns:
coltotal[i]=df_1[i].loc[totrows-1]

#the last value is removed since it is not required


coltotal.popitem()

#total of each row is stored in a dictionary


for i in range(len(df_1)):
rowtotal[i]=df_1[lastcol].iloc[i]

#the last value is removed since it is not required


rowtotal.popitem()

#variable “a” is defined to store minimum value in dataset


a = 100000000000000
#variable “mincost” is defined as cost with the minimum variable
#this value us appended with different values of minimum variable and gives
our final output
mincost =0

#a while loop with the condition to stop when rowtotal anc column total
becomes zero is defined
while (max(rowtotal.values())!=0) and (max(coltotal.values())!=0):
#below loop identifies minimum value from the dataset
#takes the column name and checks the corresponding total in the dictionary
#if column total is not zero, it checks row total of different rows of same
column
#if column total of any column or row total of any row is zero, it is
skipped
#minimum values are identified where row total and column total is not zero

for i in coltotal.keys():
if coltotal[i]==0:
continue
else:
for j in rowtotal.keys():
if rowtotal[j]==0:
continue
else:
if df_1[i].loc[j] <= a:
a = df_1[i].loc[j]
col = i
row = j
#once minimum value is identified, the corresponding row total or column
total is checked if high
#reduces the higher total value with lower total value and replaces it in
the dictionary in the place of the higher total value
#lower total values is updated as zero
#minimum cost is computed by multiplying variable “a” value with minimum
total value
#this value is then appended to minimum cost variable and continues the
process till all row total and column total are zero

if coltotal[col] < rowtotal[row]:


rowtotal[row]=rowtotal[row]-coltotal[col]
mincost= mincost + a *coltotal[col]
coltotal[col]=0
a = 100000000000000
else:
coltotal[col]=coltotal[col]-rowtotal[row]
mincost= mincost + a *rowtotal[row]
rowtotal[row]=0
a = 100000000000000
#the minimum cost value is then printed
print("The Solution of the problem using Least Cost Method is",mincost)

OUTPUT FROM THE CODE

INFERENCE
The same code can be used to solve different transportation problems with different
conditions such when the number of sources or destinations are different using the minimum
cost method.
For example, consider the situation,
  1 2 3 4 Supply
1 10 2 20 11 15
2 12 7 9 20 25
3 4 14 16 18 10
Deman
d 5 15 15 15 50

Comparing with our initial problem, the number of destinations is more in the above case.
However, this can be solved using the same code by giving this data as input to the code and
running it. The output of this case is as shown below:
As long as the last row is the demand row and the last column is supply column, the same
code would be sufficient to solve transportation problem using Minimum Cost Method.

input.csv

You might also like