A Python-Powered Approach
In today’s world, supply chain design isn’t just about minimizing costs—it’s about balancing economic efficiency with environmental and social responsibility.
Companies are increasingly integrating sustainability metrics, such as carbon emissions and resource usage, into their decision-making processes.
In this blog post, we’ll dive into a concrete example of a supply chain design problem that incorporates sustainability metrics, solve it using Python in Google Colaboratory, and visualize the results with insightful graphs.
Let’s get started!
Problem Statement: Sustainable Supply Chain Design
Imagine a company that needs to design a supply chain to distribute goods from suppliers to distribution centers (DCs) and finally to customers.
The goal is to minimize total costs, which include transportation costs and carbon emissions (a proxy for environmental impact), while meeting customer demand and respecting capacity constraints at DCs.
Mathematical Formulation
Let’s formalize the problem. We have:
- Suppliers: $( S = {s_1, s_2, \dots, s_m} )$
- Distribution Centers: $( D = {d_1, d_2, \dots, d_n} )$
- Customers: $( C = {c_1, c_2, \dots, c_p} )$
Parameters:
- $( c_{sd} )$: Transportation cost from supplier $( s )$ to DC $( d )$ per unit.
- $( c_{dc} )$: Transportation cost from DC $( d )$ to customer $( c )$ per unit.
- $( e_{sd} )$: Carbon emissions (kg CO₂/unit) from supplier $( s )$ to DC $( d )$.
- $( e_{dc} )$: Carbon emissions (kg CO₂/unit) from DC $( d )$ to customer $( c )$.
- $( q_c )$: Demand of customer $( c )$.
- $( k_d )$: Capacity of DC $( d )$.
- $( w )$: Weight for carbon emissions in the objective function (to balance cost and emissions).
Decision Variables:
- $( x_{sd} )$: Quantity shipped from supplier $( s )$ to DC $( d )$.
- $( y_{dc} )$: Quantity shipped from DC $( d )$ to customer $( c )$.
Objective Function:
Minimize the total cost, which combines transportation costs and a monetized penalty for emissions:
$$
\text{Minimize} \quad Z = \sum_{s \in S} \sum_{d \in D} c_{sd} x_{sd} + \sum_{d \in D} \sum_{c \in C} c_{dc} y_{dc} + w \left( \sum_{s \in S} \sum_{d \in D} e_{sd} x_{sd} + \sum_{d \in D} \sum_{c \in C} e_{dc} y_{dc} \right)
$$
Constraints:
- Demand Satisfaction:
$$
\sum_{d \in D} y_{dc} = q_c \quad \forall c \in C
$$ - Flow Balance at DCs:
$$
\sum_{s \in S} x_{sd} = \sum_{c \in C} y_{dc} \quad \forall d \in D
$$ - DC Capacity:
$$
\sum_{s \in S} x_{sd} \leq k_d \quad \forall d \in D
$$ - Non-negativity:
$$
x_{sd} \geq 0, \quad y_{dc} \geq 0 \quad \forall s \in S, d \in D, c \in C
$$
Example Scenario
Let’s consider a small supply chain with:
- 2 suppliers ($( S_1, S_2 )$)
- 2 DCs ($( D_1, D_2 )$)
- 3 customers ($( C_1, C_2, C_3 )$)
Parameters:
- Customer demands: $( q_{C_1} = 100 )$, $( q_{C_2} = 150 )$, $( q_{C_3} = 200 )$
- DC capacities: $( k_{D_1} = 250 )$, $( k_{D_2} = 300 )$
- Transportation costs ($( c_{sd} )$, $( c_{dc} )$) and emissions ($( e_{sd} )$, $( e_{dc} )$) are given in tables (defined in the code).
- Weight for emissions: $( w = 0.5 )$ (to balance cost and environmental impact).
We’ll use the PuLP library in Python to model and solve this linear programming problem.
Python Code to Solve the Problem
Below is the complete Python code to model the supply chain problem, solve it, and visualize the results.
1 | import pulp |
Code Explanation
Let’s break down the code step-by-step to understand how it works.
Imports and Setup:
- We import
pulpfor linear programming,numpyfor array handling,matplotlib.pyplotfor plotting, andseabornfor creating heatmaps. - The data is defined: lists of suppliers, DCs, and customers; dictionaries for demands, capacities, costs, and emissions; and the emissions weight $( w = 0.5 )$.
- We import
Model Initialization:
- A PuLP model is created with
pulp.LpProblem("Supply_Chain_Sustainability", pulp.LpMinimize)to minimize the objective function.
- A PuLP model is created with
Decision Variables:
x[s, d]: Continuous variables for shipments from supplier $( s )$ to DC $( d )$.y[d, c]: Continuous variables for shipments from DC $( d )$ to customer $( c )$.- Both are created using
LpVariable.dictswith a lower bound of 0.
Objective Function:
- The objective combines transportation costs and emissions:
$$
\sum c_{sd} x_{sd} + \sum c_{dc} y_{dc} + w \left( \sum e_{sd} x_{sd} + \sum e_{dc} y_{dc} \right)
$$ - This is implemented using
pulp.lpSumto sum over all supplier-DC and DC-customer pairs.
- The objective combines transportation costs and emissions:
Constraints:
- Demand: Ensures each customer’s demand is met ($( \sum_d y_{dc} = q_c )$).
- Flow Balance: Ensures inflow to each DC equals outflow ($( \sum_s x_{sd} = \sum_c y_{dc} )$).
- Capacity: Ensures DC capacity is not exceeded ($( \sum_s x_{sd} \leq k_d )$).
- These are added using
model +=withpulp.lpSum.
Solving the Model:
model.solve()invokes the solver to find the optimal solution.- The status and objective value are extracted, and shipment quantities are stored in dictionaries
x_valuesandy_values.
Visualization:
- Two heatmaps are created using
seaborn.heatmap:- One for shipments from suppliers to DCs (
x_matrix). - One for shipments from DCs to customers (
y_matrix).
- One for shipments from suppliers to DCs (
- The heatmaps use
YlGnBuandYlOrRdcolormaps for visual clarity, with annotations showing exact quantities.
- Two heatmaps are created using
Results and Visualization
When you run the code in Google Colaboratory, you’ll see:
- Status: “Optimal” (indicating the solver found a solution).
- Total Cost: A value like 6712.50 (includes transportation costs and emissions penalty).
- Shipments: Lists of non-zero shipments, e.g.:
- $( S_1 \to D_1 )$: 150 units
- $( S_2 \to D_2 )$: 300 units
- $( D_1 \to C_1 )$: 100 units, $( D_1 \to C_3 )$: 50 units
- $( D_2 \to C_2 )$: 150 units, $( D_2 \to C_3 )$: 150 units
Status: Optimal Total Cost (including emissions penalty): 6712.50 Shipments from Suppliers to DCs: S1 -> D1: 150.00 units S2 -> D2: 300.00 units Shipments from DCs to Customers: D1 -> C1: 100.00 units D1 -> C3: 50.00 units D2 -> C2: 150.00 units D2 -> C3: 150.00 units


Heatmap Analysis
Supplier to DC Shipments:
- The heatmap shows quantities shipped from suppliers to DCs.
- For example, if $( S_1 \to D_1 = 150 )$ and $( S_2 \to D_2 = 300 )$, the heatmap highlights these flows with darker shades.
- This visualization helps identify which supplier-DC routes are heavily utilized, reflecting cost and emissions optimization.
DC to Customer Shipments:
- The heatmap shows how DCs serve customers.
- For example, $( D_2 )$ might supply all of $( C_2 )$ (150 units) and $( C_3 )$ (150 units), indicating $( D_2 )$ is a critical hub.
- The color intensity (from light to dark) reflects shipment volumes, making it easy to spot key distribution patterns.
Why Heatmaps?
Heatmaps are intuitive for visualizing flow matrices.
The annotations provide exact values, while the color gradients highlight the magnitude of shipments, making it easy to interpret the supply chain’s structure at a glance.
Summary and Insights
This example demonstrates how to incorporate sustainability metrics (carbon emissions) into supply chain design using linear programming.
The Python code, powered by PuLP, efficiently solves the optimization problem, and the heatmaps provide a clear visual summary of the results.
By adjusting the emissions weight $( w )$, you can explore trade-offs between cost and environmental impact—set $( w = 0 )$ for a cost-only focus or increase $( w )$ to prioritize sustainability.
To extend this model, consider:
- Adding fixed costs for opening DCs.
- Incorporating multiple time periods.
- Including social metrics, like labor conditions, in the objective function.
Run the code in Google Colaboratory to experiment with different parameters and visualize how the supply chain adapts.
Sustainability in supply chains isn’t just a buzzword—it’s a practical goal we can achieve with the right tools and mindset.


















