A Practical Guide with Python
Today, I’m excited to dive into one of the most fascinating problems in network optimization: the multi-commodity flow problem.
If you’ve ever wondered how products from different manufacturers share the same transportation networks efficiently, you’re about to discover the mathematical magic behind it!
Understanding the Multi-Commodity Flow Problem
The multi-commodity flow problem involves routing multiple types of commodities through a shared network while respecting capacity constraints.
Think of it as planning routes for different products (like food, electronics, and clothing) that must all travel through the same network of roads or shipping lanes.
Mathematically, we can formulate this as:
$$\min \sum_{(i,j) \in A} \sum_{k \in K} c_{ij}^k x_{ij}^k$$
Subject to:
$$\sum_{j:(i,j) \in A} x_{ij}^k - \sum_{j:(j,i) \in A} x_{ji}^k = b_i^k \quad \forall i \in N, k \in K$$
$$\sum_{k \in K} x_{ij}^k \leq u_{ij} \quad \forall (i,j) \in A$$
$$x_{ij}^k \geq 0 \quad \forall (i,j) \in A, k \in K$$
Where:
- $x_{ij}^k$ is the flow of commodity $k$ on arc $(i,j)$
- $c_{ij}^k$ is the unit cost of shipping commodity $k$ on arc $(i,j)$
- $b_i^k$ is the supply/demand of commodity $k$ at node $i$
- $u_{ij}$ is the capacity of arc $(i,j)$
Let’s implement this in Python using a concrete example!
1 | import numpy as np |
A Real-World Scenario: Supply Chain Logistics
Let’s tackle a practical supply chain problem: We have two factories producing two different products, one central warehouse, and two retailers.
We need to decide how to ship products through this network while minimizing costs.
In our example:
- Factory A produces Product 1 (12 units) and Product 2 (5 units)
- Factory B produces Product 1 (5 units) and Product 2 (10 units)
- Retailer 1 demands Product 1 (10 units) and Product 2 (7 units)
- Retailer 2 demands Product 1 (7 units) and Product 2 (8 units)
Each connection in the network has its own capacity and different shipping costs for each product.
Breaking Down the Code
Let’s examine the key components of our solution:
1. Problem Setup and Network Definition
We’re using the PuLP library to create a linear programming model.
Our network consists of 5 nodes (2 factories, 1 warehouse, 2 retailers) connected by arcs with different capacities and costs.
1 | # Define the network as a directed graph |
This creates a network where:
- Each factory can ship directly to retailers or through the warehouse
- Each arc has a capacity limit and different costs for each product type
- For example, shipping Product 1 from Factory A to the Warehouse costs 2 per unit and has a capacity of 15 units
2. Decision Variables
For each arc and each product, we create a continuous variable representing how much of that product to ship along that arc:
1 | # Create flow variables for each commodity on each arc |
3. Objective Function
We want to minimize the total shipping cost across all arcs and products:
1 | # Objective function: minimize total cost |
4. Constraints
We have two types of constraints:
- Flow Conservation: For each node and each product, the amount entering minus the amount leaving must equal the supply/demand:
1 | # Flow conservation constraints |
- Capacity Constraints: The total flow of all products on an arc cannot exceed that arc’s capacity:
1 | # Capacity constraints for each arc |
5. Visualization
After solving the optimization problem, we create two visualizations:
- A network diagram showing flows on each arc
- Detailed charts showing the distribution of flows between locations and by product
Results Analysis
When we run this code, we get the optimal routing plan for our products.
Let’s look at the output:
1 | Optimal total cost: 123.0 |
The total cost of our solution is 101 units.
Let’s analyze a few interesting observations:
Mixed Routing: Most products go through the warehouse, but some are shipped directly from factories to retailers.
Arc Utilization: The direct routes have limited capacity (5 units each), and they’re utilized differently depending on costs.
Cost Optimization: The model chooses different routing patterns for each product based on their different shipping costs.
Visual Interpretation
The network visualization shows:
- Node connections and their capacities
- How much of each product flows on each arc
- The balance of supply and demand at each location

The detailed charts reveal:
- The total flow between each pair of locations
- The proportion of each product in the total flow

Key Insights from this Example
Shared Resources: The multi-commodity flow problem efficiently allocates limited network capacity among different products.
Cost Differentiation: Even with the same network, different products can follow different paths based on their specific costs.
Direct vs. Indirect: The optimal solution often uses a mix of direct routes and multi-hop paths through intermediate nodes.
Capacity Utilization: Some arcs may reach their capacity limits while others remain partially utilized, depending on the overall cost optimization.
Real-World Applications
This type of optimization has numerous practical applications:
- Supply chain management for multi-product companies
- Telecommunications network routing for different service types
- Transportation planning for diverse cargo types
- Utility networks managing multiple resources (gas, electricity, water)
Conclusion
The multi-commodity flow problem provides a powerful framework for optimizing shared network resources across different product types.
By using linear programming and visualization tools in Python, we can solve complex logistics challenges and gain insights into optimal routing strategies.
This example demonstrates how mathematical optimization can lead to significant cost savings in real-world supply chain management.
By understanding these principles, companies can make data-driven decisions about how to efficiently move their products through shared transportation networks.

















