Supply Planning using Linear Programming with Python

Where must you allocate your stock to meet customers' demands and reduce transportation costs?

Samir Saci
Towards Data Science
8 min readAug 17, 2021

Supply Planning Problem with Python diagram showing the cheapest route to deliver products from plants to customers. Two plants (Plant 1 and Plant 2) send products to two distribution centers (D1 and D2), which then deliver to four stores (Store 1, Store 2, Store 3, and Store 4). Solid and dashed lines represent different routes between plants, distribution centers, and stores, emphasizing the need to choose the lowest-cost transportation paths for supply planning.
Transhipment Problem for Supply Planning — (Image by Author)

Supply planning is managing the inventory produced by manufacturing to fulfil the requirements created from the demand plan.

How many pallets do we need to ship to the DC1 for the next month?

Your target is to balance supply and demand to ensure the best service level at the lowest cost.

As a data scientist, how can you automate this process?

In this article, we will present a simple methodology to use Integer Linear Programming with Python to answer a complex Supply Planning Problem considering:

  • Inbound Transportation Costs from the Plants to the Distribution Centers (DC) ($/Carton)
  • Outbound Transportation Costs from the DCs to the final customer ($/Carton)
  • Customer Demand (Carton)
SUMMARY
I. Scenario
As a Supply Planning manager you need to optimize inventory allocation to reduce transportation costs.
II. Build your Model
1. Declare your decision variables

What are you trying to decide?
2. Declare your objective function
What do you want to minimize?
3. Define the constraints
What are the limits in resources?
4. Solve the model and prepare the results
What are the results of your simulation?
III. Conclusion
1. Generative AI: Supply Planning GPT Agent
2. Simulation with a Supply Chain Digital Twin

How do you optimize Supply Planning with Python?

As a Supply Planning manager of a mid-size manufacturing company, you received feedback that the distribution costs are too high.

Based on the analysis of the Transportation Manager, this is mainly due to the stock allocation rules.

What is the root cause?

Sometimes, your customers are not shipped from the closest distribution centre, which impacts your freight costs.

Let’s understand why!

Your Distribution Network

  • 2 plants producing products with infinite capacity
    Note: We’ll see later how we can improve this assumption easily
  • 2 distribution centres that receive finished goods from the two plants and deliver them to the final customers
    Note: We will consider that these warehouses operate X-Docking to avoid considering the concept of stock capacity in our model
  • 200 stores (delivery points)

To simplify the comprehension, let’s introduce some notations

Mathematical notations for the supply planning problem. Symbols represent the plants, distribution centers, and stores. Variables such as the quantity of products shipped and transportation costs are denoted. The objective is to minimize the inbound and outbound transportation costs from plants to distribution centers and from distribution centers to stores.
Notations — (Image by Author)

What drives the demand?

Store Demand
What is the demand per store?

A detailed formula defining the demand of store 𝑝 p as 𝐷 𝑝 = demand (cases) of store 𝑝 D p ​ =demand (cases) of store p
Notations — (Image by Author)

How much does it cost to move goods?

Transportation Costs

Our main goal is to reduce the total transportation costs, including inbound shipments (from the plants to the DCs) and outbound shipments (from the DCs to the stores).

A formula describing the quantity 𝐼 𝑛 𝑝 I np ​ shipped from plant 𝑛 n to distribution center (DC) 𝑝 p, and quantity 𝑄 𝑛 𝑝 Q np ​ shipped from DC 𝑛 n to store 𝑝 p used for the supply planning problem with Python.
Notations — (Image by Author)
A formula detailing the inbound and outbound transportation costs: 𝐼 𝐶 𝑛 𝑝 IC np ​ = inbound transportation cost between plant 𝑛 n and distribution center (DC) 𝑝 p. 𝑂 𝐶 𝑛 𝑝 OC np ​ = outbound transportation cost between DC 𝑛 n and store 𝑝 p used for the supply planning problem with Python.
Notations — (Image by Author)

Question

Which Plant i and Distribution n should I chose to produce and deliver 100 units to Store p at the lowest cost?

A box plot comparing outbound transportation costs between two distribution centers (D1 and D2). D1 shows a lower median cost compared to D2, indicating that more products should be directed through D1 to reduce overall costs using the supply planning model designed with Python.
Box Plot of Outbound Cost by Distribution Center — (Image by Author)

Comment

The box plot above shows that the D1 distribution of Unit Cost has a median value lower than D2.

What’s the impact?

Therefore, we expect the model to direct a significant part of the flow through D1.

Let’s explore that in detail in the next section.

🏫 Discover 70+ case studies using data analytics for supply chain sustainability🌳and business optimization 🏪 in this: Cheat Sheet

Build your Model

We will be using the Python PuLP library.

PuLP is a Python modelling framework for linear (LP) and Integer Programming (IP) problems.

How do we start?

Declare your decision variables.

What are you trying to decide?

This image defines the variables we are using in the supply planning problem with Python. 𝐼 𝑛 , 𝑝 I n,p ​ : Quantity shipped from plant 𝑛 n to distribution center 𝑝 p. 𝑄 𝑛 , 𝑝 Q n,p ​ : Quantity shipped from distribution center 𝑛 n to store 𝑝 p. These notations will be used in the linear programming model to represent the flow of goods from plants to distribution centers, then to stores.
Notations — (Image by Author)

We want to decide the quantity of Inbound and Outbound transportation.

What do we want to achieve?

Declare your objective function

We want to minimize the objective functions.

The formula shows the objective function of the linear programming model used for supply planning: 𝑇 𝐶 TC: Total transportation cost. The sum represents the cost of transporting goods from plants to distribution centers (inbound cost) and from distribution centers to stores (outbound cost). The goal is to minimize 𝑇 𝐶 TC.
Notations — (Image by Author)

We want to decide to minimize the inbound and outbound transportation costs.

However, we are limited by constraints.

Define the constraints

What are the limits in resources that will determine your feasible region?

This image introduces the demand constraints for stores: The total quantity shipped to each store from the distribution centers must meet or exceed the store’s demand. This ensures that no store runs out of inventory for the supply planning problem.
Notations — (Image by Author)

The Supply from DCs needs to meet the demand per store.

This image describes the flow conservation constraint for this supply planning problem: The total quantity shipped from plants to a distribution center must equal the total quantity shipped from that center to stores. This ensures that goods are neither created nor lost in the supply chain.
Notations — (Image by Author)

We don’t build any stock in the X-Docking platforms.

Can we add more constraints? Yes!

To keep the model simple, I only included volume constraints.

However, as business sustainability becomes a strategic topic in any company, we can include environmental constraints.

Including the environmental impact in your optimization problem is a great way to support your company's green transformation.

  • Maximum CO2 emissions per km driven?
  • Minimum truck fill rate for the store delivery

I won’t touch this topic here, but you can find more details in this article

Now that we have our constraints and objective function ready, let’s solve.

Solve the model and prepare the results

What are the results of your simulation?

The model takes the cheapest route for Inbound by linking P2 with D1 (resp. P1 with D2).

As expected, over 90% of the outbound flow goes through D1 to minimize Outbound Costs.

163 stores are delivered by D1
0 store is delivered by the two warehouses together

You can find the full code in this GitHub repository

💡 Follow me on Medium for more articles related to 🏭 Supply Chain Analytics, 🌳 Sustainability and 🕜 Productivity.

Conclusion

Using linear programming with Python can help supply planning managers optimize their distribution network and reduce transportation costs.

We can quickly adapt the model if we change the conditions and constraints.

The model presented here can be easily improved by adding operational constraints:

  • Production Costs in Plants ($/Case)
  • Maximal X-Docking Capacity in Distribution Centers (Cartons)

But also by improving the cost structure by adding

  • Fixed/Variable Costs Structures in Distribution Centers ($)
  • Fixed + Variable Transportation Costs Structure y = (Ax +b)

The only limit you will find is the linearity of the constraints and the objective functions.

Have you heard about Generative AI?

Generative AI: Machine Learning x GPT

As I wanted to understand the trend of Generative AI with large language models (LLMs), I started experimenting with its usage to improve user experience.

User: How many orders have delivered late?

Therefore, I built a smart agent with Langchain connected to a TMS that answers questions by querying a database.

Flow diagram illustrating generative AI interacting with a supply chain control tower agent to answer user queries and provide responses based on SQL database queries.
Supply Chain Control Tower Agent with LangChain SQL Agent [Article Link] — (Image by Author)

This greatly improves the user experience compared to a static, boring dashboard built with PowerBI.

What if we create a super agent for Supply Chain Optimization?

The idea would be to equip a GPT agent with

  • Advanced optimization models are written in Python in a core module
  • Documentation, articles and context to understand how to use them
Diagram of a supply chain control tower using LangChain SQL agent, illustrating a process where a user prompt triggers Python script execution, generating analysis based on Excel data, with final outputs guided by an AI agent.
Example of Architecture — (Image by Author)

We can imagine an agent interacting with the supply planning.

User: What if we increase the capacity of Plant P1 by 25%?

For more details on how to implement it,

Have you heard about Digital Twins?

Simulate different scenarios with a Digital Twin

A digital twin is a digital replica of a physical object or system.

The model built here only focuses on the transportation flows between the factories, cross-docking platforms and stores.

What if we want to simulate the processes inside the cross-docking platforms?

A Supply Chain digital twin is a computer model representing various components and processes involved in the supply chain, such as warehouses, transportation networks, and production facilities.

Supply chain diagram showing the flow from production to store, with Python integration at production, warehousing, and store replenishment stages. Historical sales data is used to inform replenishment orders, with a focus on simulating processes in cross-docking platforms for a digital twin.
(Image by Author)

In this case, we can use it to simulate several scenarios for cost reductions or decarbonisation of this distribution network.

What if we add set up a local warehouse to reduce the last-mile delivery distance?

  • Estimate the impact on the service level
  • Calculate the new warehousing costs (with more locations)
  • Predict the CO2 emissions reduction we can reach

Can we use it to optimize our supply chain?

For each scenario, you can manipulate the parameter linked to the initiative and see how much your overall performance will decrease.

Then, you can adapt the other metric (warehouse capacity and locations, replenishment lead time, etc.) until you reach the target.

For more details, have a look at this article

You will also find a simple example of a digital twin used to simulate multiple inventory management strategies for reducing carbon emissions.

About Me

Let’s connect on Linkedin and Twitter. I am a Supply Chain Engineer who uses data analytics to improve logistics operations and reduce costs.

For consulting or advice on analytics and sustainable supply chain transformation, feel free to contact me via Logigreen Consulting.

If you are interested in Data Analytics and Supply Chain, look at my website.

💌 New articles straight in your inbox for free: Newsletter
📘 Your complete guide for Supply Chain Analytics: Analytics Cheat Sheet

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Towards Data Science
Towards Data Science

Your home for data science and AI. The world’s leading publication for data science, data analytics, data engineering, machine learning, and artificial intelligence professionals.

Samir Saci
Samir Saci

Top Supply Chain Analytics Writer — Follow my journey using Data Science for Supply Chain Sustainability 🌳 and Productivity ⌛ https://samirsaci.com/about

No responses yet

What are your thoughts?