Yogen SMa 4
Yogen SMa 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))
# 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])
# 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
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):
i, j = q.get()
u[i], v[j] = 0, cost_matrix[i, j]
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)
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