0% found this document useful (0 votes)
13 views10 pages

Yogen SMa 4

This document details the solution to a transportation problem using Python code. It provides the code to solve the transportation problem using the Northwest Corner Method and additional methods to check for degenerate solutions and find the optimal solution. Sample input and output is also provided showing the application of the algorithms to solve a transportation problem.

Uploaded by

Yogen PS48
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)
13 views10 pages

Yogen SMa 4

This document details the solution to a transportation problem using Python code. It provides the code to solve the transportation problem using the Northwest Corner Method and additional methods to check for degenerate solutions and find the optimal solution. Sample input and output is also provided showing the application of the algorithms to solve a transportation problem.

Uploaded by

Yogen PS48
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/ 10

Assignment 4

Python code: -
import numpy as np
import queue
MAX=9999
EPSILON=0.00001
print("Name : Yogendra Pratap Singh")
print("Roll No: 20CS8096")
def northwest_corner_method(cost_matrix, supply, demand):
supply_copy = supply.copy()
demand_copy = demand.copy()
num_sources, num_destinations = cost_matrix.shape
total_cost = 0
allocations=0
# Initialize the transportation plan matrix with zeros
transportation_plan = np.zeros((num_sources, num_destinations))

# Indices for the current northwest corner of the cost matrix


source_idx = 0
destination_idx = 0

# Perform the allocation until all supply and demand requirements are fulfilled
while source_idx < num_sources and destination_idx < num_destinations:
# Calculate the amount to be transported from the current cell
amount_to_transport = min(supply_copy[source_idx], demand_copy[destination_idx])

# Update the transportation plan matrix


transportation_plan[source_idx, destination_idx] = amount_to_transport
total_cost += amount_to_transport * cost_matrix[source_idx, destination_idx]
allocations+=1
# Update supply and demand values after allocation
supply_copy[source_idx] -= amount_to_transport
demand_copy[destination_idx] -= amount_to_transport

# Move to the next row or column based on which one was depleted
if supply_copy[source_idx] == 0:
source_idx += 1
if demand_copy[destination_idx] == 0:
destination_idx += 1

return transportation_plan , total_cost,allocations

def handle_degeneracy(cost_matrix_real,tplan):
cost_matrix=cost_matrix_real.copy()
num_s,num_d=cost_matrix.shape
mini=minj=0
minv=MAX
flag=0
while True:
for i in range(num_s):
for j in range(num_d):
if tplan[i][j]!=0:
continue
if minv>cost_matrix[i][j]:
minv=cost_matrix[i][j]
mini,minj=i,j
visited=np.zeros((num_s,num_d))
visited[mini,minj]=1
odds=evens=[]
cond=isLoop(tplan,mini,minj,visited,mini,minj,odds,evens,True)
if not cond:
break
cost_matrix[mini][minj]=MAX
tplan[mini,minj]=EPSILON
def check_handle_degeneracy(cost_matrix,tplan,allocations):
print("no of allocations : ",allocations)
print("m+n-1: ",m+n-1)
if(allocations==m+n-1):
print("solution is non degenerate")
else:
print("solution is degenerate")
#handle degeneracy
for i in range(m+n-1-allocations):
handle_degeneracy(cost_matrix,transportation_plan)

def isLoop(tplan,x,y,visited,x0,y0,odds,evens,isodd=1):
num_s,num_d=tplan.shape
if isodd:
odds.append((x,y))
else:
evens.append((x,y))
#left to right
i=x
if y==y0:
for j in range(num_d):
if i==x and j==y:
continue
if visited[i][j]==1:
return True
if tplan[i][j]==0:
continue

cond=isLoop(tplan,i,j,visited,x,y,odds,evens,not isodd)
if cond:
return True
#top to bottom
j=y
if x==x0:
for i in range(num_s):
if i==x and j==y:
continue
if visited[i][j]==1:
return True
if tplan[i][j]==0:
continue

cond=isLoop(tplan,i,j,visited,x,y,odds,evens,not isodd)
if cond:
return True
if isodd:
odds.pop(-1)
else:
evens.pop(-1)
return False

def calculate_uv(cost_matrix,allocation):

num_suppliers, num_consumers = cost_matrix.shape


q , max_value = queue.Queue(), 1e6
u, v = np.full(num_suppliers, max_value), np.full(num_consumers, max_value)

for i in range (num_suppliers) :


for j in range(num_consumers) :
if allocation[i][j] != 0 : q.put((i, j))

i, j = q.get()
u[i], v[j] = 0, cost_matrix[i, j]

while not q.empty() :


i, j = q.get()
if (u[i] + v[j]) == 2*max_value : q.put((i, j))
else :
if u[i] == max_value : u[i] = cost_matrix[i, j] - v[j]
else : v[j] = cost_matrix[i, j] - u[i]

return u, v
def get_delij(cost_m,tplan,u,v):
num_s,num_d=cost_m.shape
delij=np.zeros((num_s,num_d))
for i in range(num_s):
for j in range(num_d):
if(tplan[i][j]!=0):
continue
delij[i][j]=cost_m[i][j]-(u[i]+v[j])
return delij
def check_optimality(cost_matrix,tplan):
u,v=calculate_uv(cost_matrix,tplan)
print("u:",u,"\nv:",v)
delij=get_delij(cost_matrix,tplan,u,v)
print("Delij:\n",delij)
iszero=False
num_s,num_d=tplan.shape
for i in range(num_s):
for j in range(num_d):
if(tplan[i][j]!=0):
continue
if(delij[i][j]==0):
iszero=True
if np.all(delij>=0):
if iszero:
print("optimal solution but there exists alternate solutions")
else:
print("It is optimal solution")
return True,delij
else:
# print("It is not optimal")
return False,delij

def calculate_optimal_solution(cost_matrix,tplan):
num_s,num_d=cost_matrix.shape
iteration=1
print("####ITERATION ",iteration," ###########################")
isoptimal,delij=check_optimality(cost_matrix,tplan)
while not isoptimal:
isloop=False
odds=[]
evens=[]
while not isloop:
i,j=np.unravel_index(np.argmin(delij),delij.shape)
visited=np.zeros((num_s,num_d))
visited[i][j]=1
odds=[]
evens=[]
isloop=isLoop(tplan,i,j,visited,i,j,odds,evens,True)
if(isloop):
print("for Delij[",i,",",j,"]= ",delij[i][j]," loop is present")
else:
print("for Delij[",i,",",j,"]= ",delij[i][j],"loop is not present")
delij[i][j]=MAX
minval=MAX
for i,j in evens:
if tplan[i][j]<minval:
minval=tplan[i][j]
print("minimum value at -ve locations of loop is: ",minval)
for i,j in evens:
tplan[i][j]-=minval
for i,j in odds:
tplan[i][j]+=minval
print("allocations are:\n",tplan)
iteration+=1
print("#### ITERATION ",iteration," ###########################")
isoptimal,delij=check_optimality(cost_matrix,tplan)

# Example transportation problem data


cost_matrix = np.loadtxt("cost.txt", delimiter=" ")
supply = np.loadtxt("supply.txt", delimiter=" ")
demand = np.loadtxt("demand.txt", delimiter=" ")
#if supply not equals demand then add dummy row or col as required
total_supply=sum(supply)
total_demand=sum(demand)
print("total supply = ",total_supply,", total demand = ",total_demand)
if total_supply>total_demand:
cost_matrix=np.insert(cost_matrix,len(cost_matrix[0]),0,1)
demand=np.insert(demand,len(demand),total_supply-total_demand)
print("cost matrix after adding dummy column")
elif total_supply<total_demand:
cost_matrix=np.insert(cost_matrix,len(cost_matrix),0,0)
supply=np.insert(supply,len(supply),total_demand-total_supply)
print("cost matrix after adding dummy row")
if total_supply!=total_demand:
print(cost_matrix)
print("supply:",supply)
print("demand: ",demand)
# Solve the transportation problem using the Northwest Corner Method
transportation_plan, total_cost,allocations = northwest_corner_method(cost_matrix, supply,
demand)
m,n=cost_matrix.shape
print("cost \n",cost_matrix)
print("supply ",supply)
print("demand",demand)
print("Initial solution:")
print(transportation_plan)
print(f"Initial cost: {total_cost}")

print("Degeneracy:- ")
check_handle_degeneracy(cost_matrix,transportation_plan,allocations)
print("Optimal Solution:-")
calculate_optimal_solution(cost_matrix,transportation_plan)
print("Optimal solution is:")
print(transportation_plan)
#total cost
r,c=transportation_plan.shape
total_cost=0
for i in range(r):
for j in range(c):
if transportation_plan[i][j]==0:
continue
total_cost+=transportation_plan[i][j]*cost_matrix[i][j]
print("Optimal cost is: ",total_cost)
Q1
Input: -
Filename(cost.txt):
23 27 16 18
12 17 20 51
22 28 12 32
Filename(supply.txt):
30 40 53
Filename(demand.txt):
22 35 25 41

Output: -
Name : Yogendra Pratap Singh
Roll No: 20CS8096
total supply = 123.0 , total demand = 123.0
cost
[[23. 27. 16. 18.]
[12. 17. 20. 51.]
[22. 28. 12. 32.]]
supply [30. 40. 53.]
demand [22. 35. 25. 41.]
Initial solution:
[[22. 8. 0. 0.]
[ 0. 27. 13. 0.]
[ 0. 0. 12. 41.]]
Initial cost: 2897.0
Degeneracy:-
no of allocations : 6
m+n-1: 6
solution is non degenerate
Optimal Solution:-
####ITERATION 1 ###########################
u: [ 0. -10. -18.]
v: [23. 27. 30. 50.]
Delij:
[[ 0. 0. -14. -32.]
[ -1. 0. 0. 11.]
[ 17. 19. 0. 0.]]
for Delij[ 0 , 3 ]= -32.0 loop is present
minimum value at -ve locations of loop is: 8.0
allocations are:
[[22. 0. 0. 8.]
[ 0. 35. 5. 0.]
[ 0. 0. 20. 33.]]
#### ITERATION 2 ###########################
u: [ 0. 22. 14.]
v: [23. -5. -2. 18.]
Delij:
[[ 0. 32. 18. 0.]
[-33. 0. 0. 11.]
[-15. 19. 0. 0.]]
for Delij[ 1 , 0 ]= -33.0 loop is present
minimum value at -ve locations of loop is: 5.0
allocations are:
[[17. 0. 0. 13.]
[ 5. 35. 0. 0.]
[ 0. 0. 25. 28.]]
#### ITERATION 3 ###########################
u: [ 0. -11. 14.]
v: [23. 28. -2. 18.]
Delij:
[[ 0. -1. 18. 0.]
[ 0. 0. 33. 44.]
[-15. -14. 0. 0.]]
for Delij[ 2 , 0 ]= -15.0 loop is present
minimum value at -ve locations of loop is: 17.0
allocations are:
[[ 0. 0. 0. 30.]
[ 5. 35. 0. 0.]
[17. 0. 25. 11.]]
#### ITERATION 4 ###########################
u: [ 0. 4. 14.]
v: [ 8. 13. -2. 18.]
Delij:
[[15. 14. 18. 0.]
[ 0. 0. 18. 29.]
[ 0. 1. 0. 0.]]
It is optimal solution
Optimal solution is:
[[ 0. 0. 0. 30.]
[ 5. 35. 0. 0.]
[17. 0. 25. 11.]]
Optimal cost is: 2221.0
Q2
Input: -
Filename(cost.txt):
21 16 25 13
17 18 14 23
32 27 18 41
Filename(supply.txt):
11 13 19
Filename(demand.txt):
6 10 12 15
Output: -
Name : Yogendra Pratap Singh
Roll No: 20CS8096
total supply = 43.0 , total demand = 43.0
cost
[[21. 16. 25. 13.]
[17. 18. 14. 23.]
[32. 27. 18. 41.]]
supply [11. 13. 19.]
demand [ 6. 10. 12. 15.]
Initial solution:
[[ 6. 5. 0. 0.]
[ 0. 5. 8. 0.]
[ 0. 0. 4. 15.]]
Initial cost: 1095.0
Degeneracy:-
no of allocations : 6
m+n-1: 6
solution is non degenerate
Optimal Solution:-
####ITERATION 1 ###########################
u: [0. 2. 6.]
v: [21. 16. 12. 35.]
Delij:
[[ 0. 0. 13. -22.]
[ -6. 0. 0. -14.]
[ 5. 5. 0. 0.]]
for Delij[ 0 , 3 ]= -22.0 loop is present
minimum value at -ve locations of loop is: 5.0
allocations are:
[[ 6. 0. 0. 5.]
[ 0. 10. 3. 0.]
[ 0. 0. 9. 10.]]
#### ITERATION 2 ###########################
u: [ 0. 24. 28.]
v: [ 21. -6. -10. 13.]
Delij:
[[ 0. 22. 35. 0.]
[-28. 0. 0. -14.]
[-17. 5. 0. 0.]]
for Delij[ 1 , 0 ]= -28.0 loop is present
minimum value at -ve locations of loop is: 3.0
allocations are:
[[ 3. 0. 0. 8.]
[ 3. 10. 0. 0.]
[ 0. 0. 12. 7.]]
#### ITERATION 3 ###########################
u: [ 0. -4. 28.]
v: [ 21. 22. -10. 13.]
Delij:
[[ 0. -6. 35. 0.]
[ 0. 0. 28. 14.]
[-17. -23. 0. 0.]]
for Delij[ 2 , 1 ]= -23.0 loop is present
minimum value at -ve locations of loop is: 3.0
allocations are:
[[ 0. 0. 0. 11.]
[ 6. 7. 0. 0.]
[ 0. 3. 12. 4.]]
#### ITERATION 4 ###########################
u: [ 0. 19. 28.]
v: [ -2. -1. -10. 13.]
Delij:
[[23. 17. 35. 0.]
[ 0. 0. 5. -9.]
[ 6. 0. 0. 0.]]
for Delij[ 1 , 3 ]= -9.0 loop is present
minimum value at -ve locations of loop is: 4.0
allocations are:
[[ 0. 0. 0. 11.]
[ 6. 3. 0. 4.]
[ 0. 7. 12. 0.]]
#### ITERATION 5 ###########################
u: [ 0. 10. 19.]
v: [ 7. 8. -1. 13.]
Delij:
[[14. 8. 26. 0.]
[ 0. 0. 5. 0.]
[ 6. 0. 0. 9.]]
It is optimal solution
Optimal solution is:
[[ 0. 0. 0. 11.]
[ 6. 3. 0. 4.]
[ 0. 7. 12. 0.]]
Optimal cost is: 796.0

You might also like