Optimize E-Commerce Last-Mile Delivery with Python
Organize your routes to deliver parcels with a minimum number of drivers using optimization models with python
If you’re an e-commerce business owner, you know that last-mile delivery can be a complex and costly challenge.
You can use Python and optimization models to streamline your delivery routes and minimize the distance covered per route.
Introduction
If you travel to first and second-tier cities of China, you will find on the street many delivery drivers (Chinese: 快递).
They take the parcels from small warehouses called customer service centres (客户服务中心) located in each neighbourhood and deliver them to the final customers.
These centres are key elements of the Logistics Network of the major courier companies in China. They provide a large geographical coverage for last-mile delivery and a huge competitive advantage by offering the best service level and delivery lead time in the market.
This article will present a solution to optimize the last-mile delivery from these centres to reduce costs and ensure a uniform workload distribution to each driver.
💌 New articles straight in your inbox for free: Newsletter
📘 Your complete guide for Supply Chain Analytics: Analytics Cheat Sheet
I. How to optimize last-mile delivery with Python?
1. Problem Statement: Last Mile Delivery Optimization
2. Distance Matrix
3. Demand: number of parcels to deliver to each location
II. Build your Model
1. Import Distance Matrix and Init parameters
2. Create functions to calculate distances and order quantities
3. Build your model with constraints
4. Show the solution
III. Conclusion
1. Generative AI: GPT x Supply Chain Optimization
2. Simulate Several Scenarios with Digital Twin
3. Reduce Emissions: Green Inventory Management
4. Next Steps
I. How do you optimize last-mile delivery with Python?
Problem Statement: Last Mile Delivery Optimization
You are a manager in a local service centre with
- 4 drivers in your team
- 15 parcel capacity per vehicle
- 16 destinations to cover in the neighbourhood named Dj with j in [1, 16]
- D0 is the centre
- 1 route per driver
Distance Matrix
To build your model, you need to provide a distance matrix M as inputs defined by
- M(i, j) with i, j in [0, 16]
- M(i, j) = distance between Di and Dj
This distance matrix will be loaded from an Excel file. You can find an example for this scenario here: Link
Demand: number of parcels to deliver to each location
We will use here a Python list with the first value at zero (because you don’t deliver anything in the centre)
- Demand = [0, 1, 1, 2, 4, 2, 4, 8, 8, 1, 2, 1, 2, 4, 4, 8, 8]
Objective
- Deliver all parcels with a minimum number of drivers
- Optimize the routing to minimize the distance covered per route
Results
Route for driver 0
0 Parcels(0) -> 4 Parcels(4) -> 3 Parcels(6) -> 1 Parcels(7) -> 7 Parcels(15) -> 0 Parcels(15)
Distance of the route: 1552 (m)
Parcels Delivered: 15 (parcels)
Route for driver 1
0 Parcels(0) -> 14 Parcels(4) -> 16 Parcels(12) -> 10 Parcels(14) -> 9 Parcels(15) -> 0 Parcels(15)
Distance of the route: 1552 (m)
Parcels Delivered: 15 (parcels)
Route for driver 2
0 Parcels(0) -> 12 Parcels(2) -> 11 Parcels(3) -> 15 Parcels(11) -> 13 Parcels(15) -> 0 Parcels(15)
Distance of the route: 1552 (m)
Parcels Delivered: 15 (parcels)
Route for driver 3
0 Parcels(0) -> 8 Parcels(8) -> 2 Parcels(9) -> 6 Parcels(13) -> 5 Parcels(15) -> 0 Parcels(15)
Distance of the route: 1552 (m)
Parcels Delivered: 15 (parcels)
Total distance of all routes: 6,208 (m)
Parcels Delivered: 60/60
Based on these results, you can assign each of your four drivers a route that has the same total distance
- 100% of parcels are delivered with a minimum distance covered
- Drivers’ vehicles are loaded to their maximum capacity (15/15)
Using this model helps ensure that your drivers, who are paid a fixed amount by delivery, will be fairly assigned to a route.
You will avoid the issue of having drivers complaining because they have longer routes than their colleagues.
Moreover, you are using your resources at their maximum capacity.
💡 Follow me on Medium for more articles related to 🏭 Supply Chain Analytics, 🌳 Sustainability and 🕜 Productivity.
You can find the full code in this Github repository: Link
My portfolio with other projects: Samir Saci
II. Build your Model
Capacitated vehicle routing problem (CVRP) with Google OR-Tools
OR-Tools is an open-source collection of Google with tools for combinatorial optimization. The objective is to find the best solution out of many possible solutions.
Let us try to use this library to build the optimal routes.
Import Distance Matrix and Init parameters
Create functions to calculate distances and order quantities
Build your model with constraints
Show the solution
💡 Follow me on Medium for more articles related to 🏭 Supply Chain Analytics, 🌳 Sustainability and 🕜 Productivity.
III. Conclusion
Generative AI: GPT x Supply Chain Optimization
Following the trend of Generative AI with large language models (LLMs), I shared to experiment with their usage with this prototype of LangChain Agent, powered by GPT and connected to a TMS.
This agent’s performance is impressive; it can answer operational questions autonomously by querying a database and extracting the results.
What if we create a super agent for Supply Chain Optimization?
The idea would be to equip a GPT agent with
- Advanced optimization models written in Python in a core module: Supply Chain Optimization, Sustainable Sourcing, Supply Planning
- Documentation, articles and context to understand how to use them
This module could be included to cover the last-mile delivery.
It can be connected to store orders, warehouse operations and demand planning to optimise end-to-end supply chain.
Let us imagine agents that can interact with the users to get data, understand the constraints and run the solution to analyse results.
For more details,
Simulate Several Scenarios with Digital Twin
A digital twin is a digital replica of a physical object or system.
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.
In this digital world, you can model each element of your end-to-end supply chain with costs, energy, emissions, and lead time parameters.
When brainstorming potential route allocation strategies, you can simulate their impacts on the distribution network.
For example,
- What would impact if we increase the storage capacity per delivery vehicle?
- What would impact CO2 emissions if we use vans versus electric bikes?
- What would be the optimal distribution network (locations of your last-mile distribution centres) to minimize the total costs?
For more details,
Reduce Emissions: Green Inventory Management
Assuming that you’re using vehicles emitting CO2, you can adapt this model to consider the total emissions of your last-mile distribution network.
Green inventory management can be defined as managing inventory in an environmentally sustainable way.
For a distribution network, this can involve a set of processes and rules that aim to reduce the environmental impact of order transmission, preparation and delivery.
What would be the impact on CO2e emissions if we reduce the frequency of store replenishments?
In this example, we use data analytics to simulate the variation of store replenishment frequency and measure the impact on the overall environmental impact.
Next Steps
This model can help the centre manager to
- Optimize his fleet with full utilization of his drivers and vehicles
- Ensure that the workload is equally distributed among each driver
Question:
- What could be the result of higher capacity (boxes) per driver?
- What could be the results if we have a weight or volume constraint?
I will let you test it and share your results (or questions) in the comment area.
About Me
Let’s connect on Linkedin and Twitter; I am a Supply Chain Engineer using data analytics to improve logistics operations and reduce costs.
If you are interested in Data Analytics and Supply Chain, have a look at my website
References
- Google AI, Google OR-Tools Library, Link
- “Green Inventory Management — Case Study”, Towards Data Science, Samir Saci
- “What Is a Supply Chain Digital Twin?”, Towards Data Science, Samir Saci
- “4 Impacting Projects to Start Your Data Science for Supply Chain Journey”, Towards Data Science, Samir Saci