# Solving the Least Cost Transportation Problem with Python

Minor cost transportation problems (LCTP) are among supply chain management’s most common and essential issues. LCTP involves determining the most cost-effective way to transport goods from many sources to multiple destinations with varying costs.

Linear programming concepts in linear algebra give us a robust framework for solving such optimization problems. These concepts also involve equation and constraint formation.

This article will define and solve an LCTP problem using the Python programming language and the Numpy library.

The Least Cost Transportation Problem (LCTP) can be solved using Python and the Numpy library. This method determines the most cost-effective routes from multiple sources to various destinations, optimizing the total transportation costs

Recommended: Maximizing Cost Savings Through Offshore Development: A Comprehensive Guide

Recommended: Delivery Route Optimization using Python: A Step-by-Step Guide

## Understanding the Transportation Problem

In our problem, a company produces goods in three different factories, A, B, and C, and the goods have to be transported to three distribution centres, D1, D2, and D3. The transportation costs are as follows.

1. From Factory A to Distribution Center D1: Rs 6
2. From Factory A to Distribution Center D2: Rs 8
3. From Factory A to Distribution Center D3: Rs 10
4. From Factory B to Distribution Center D1: Rs 9
5. From Factory B to Distribution Center D2: Rs 12
6. From Factory B to Distribution Center D3: Rs 13
7. From Factory C to Distribution Center D1: Rs 14
8. From Factory C to Distribution Center D2: Rs 16
9. From Factory C to Distribution Center D3: Rs 18

Moreover, each distribution center has a specific demand, and each factory has some capacity. Let’s also look at the additional constraints.

1. Factory A Capacity: 30 units
2. Factory B Capacity: 20 units
3. Factory C Capacity: 50 units
4. Distribution Center D1 Demand: 40 units
5. Distribution Center D2 Demand: 70 units
6. Distribution Center D3 Demand: 30 units

Now that we have defined the problem, let’s solve it using the Numpy library of the Python programming language.

## Solving the Transportation Problem Using Python

Traditionally, we have formed constraints regarding the supply and demand centres, and using the simplex method, we have found a feasible solution to this transportation problem. The code below defines the supply, demand, and transportation costs from each factory to the distribution centre. We have used the power of the Numpy library to obtain our answer.

We used the Northwest Corner Rule to find a feasible solution initially. Then, we calculated the total transportation costs. Let us now look at the Python code step by step.

```import numpy as np
```

We solved this problem using the Numpy library of the Python programming language. Let’s move forward and define and initialize our variables.

```def transportation_cost(supply, demand, costs):
num_suppliers = len(supply)
num_customers = len(demand)

# Initialize variables
supply_remaining = np.array(supply)
demand_remaining = np.array(demand)
allocation = np.zeros((num_suppliers, num_customers))

# Iterate until all supply is used
while np.sum(supply_remaining) > 0:
# Find the northwest corner cell
supplier_index = np.argmax(supply_remaining)
customer_index = np.argmax(demand_remaining)
```

We have a user-defined function with variables of supply, demand, and costs. We then create an array of supply and demand for inputs. After that, we use the northwest rule to loop it until the whole supply is finished.

```# Calculate how much to allocate
quantity = min(supply_remaining[supplier_index], demand_remaining[customer_index])

# Update allocation and remaining supply/demand
allocation[supplier_index, customer_index] = quantity
supply_remaining[supplier_index] -= quantity
demand_remaining[customer_index] -= quantity

# Calculate total cost
total_cost = np.sum(allocation * costs)

return allocation, total_cost
```

Our objective function will be the total cost function, and we aim to minimize it. The Northwest rule attaches the minimum cost demand to the highest supplier. This rule is reiterated over and over until every demand and supply is exhausted. We then return the total cost and allocation.

```# Example problem data
supply = [30, 20, 50]
demand = [40, 70, 30]
costs = np.array([[6, 8, 10],
[9, 12, 13],
[14, 16, 18]])

# Solve the transportation problem
allocation, total_cost = transportation_cost(supply, demand, costs)

# Print allocation and total cost
print("Optimal Allocation:")
print(allocation)
print("Total Cost:", total_cost)
```

We have defined the demand and supply array mentioned in the problem statement. After that, we run it through our user-defined function. We ultimately print our results.

Let us now look at the output for the code above.

From the output above, we can see that the most optimal solution would be –

1. Thirty units are to be transported from A to D1.
2. Twenty units are to be transported from A to D3.
3. Fifty units are to be transported from A to D2.

This gives us the total transportation cost, which is the minimum of all the cases, to be Rs 1240. Please note that this problem is of an unbalanced category. Namely, demand and supply are not equal.

## Conclusion

Here you go!! Now you know how to minimize costs in a transportation problem. There are multiple other methods to do the same like the Simplex method, Vogel’s Approximation, and the Hungarian approach. All of these approaches will help you determine the optimal solution.