# Minimum transportation cost

```
# Minimum transportation cost using the Northwest corner rule
import numpy as np
def min_transportation_cost(transportation_costs, supply_by_factory, demand_by_warehouse):
if np.sum(supply_by_factory) != np.sum(demand_by_warehouse):
return "Sums of supply and demand must be equal"
xshape, yshape = transportation_costs.shape
matrix_type = supply_by_factory.dtype
steps = np.zeros((xshape, yshape), dtype=matrix_type)
length = len(supply_by_factory)
row, col, lst_idx, i = 0, 0, 0, 0
steps[row, col] = demand_by_warehouse[0]
while (row, col) != (length-1, length-1):
start_cell = steps[row, col]
if i % 2 == 0:
col += 1
array_name = supply_by_factory
else:
row += 1
array_name = demand_by_warehouse
lst_idx += 1
steps[row, col] = array_name[lst_idx] - start_cell
i += 1
return np.sum(transportation_costs * steps)
transportation_costs = np.array([
[4, 5, 6, 7],
[5,10,12,6],
[6,12,8,10],
[8,11,3,7]
])
supply_by_factory = np.array([330, 350, 320, 220])
demand_by_warehouse = np.array([250, 260, 310, 400])
print(min_transportation_cost(transportation_costs, supply_by_factory, demand_by_warehouse))
# Initial basic feasible solution: 9700
```