How Container Loading Optimization with Python Can Save Logistics?
A simple heuristic algorithm helps freight forwarders load the maximum number of pallets into their customers' sea containers.
Rising inflation and operational costs are putting unprecedented pressure on European and American supply chains, with transportation, warehousing, and inventory expenses all climbing significantly.
Therefore, optimising container loading has become essential for companies looking to control costs and protect margins in this challenging economic environment.
As a data scientist, how can you use Python support the reduction of sea freight costs?
Multiple heuristic algorithms could be adapted to this problem to develop a tool that operations would use.
In this article, we will use a Two-Dimensional adaptation of the knapsack problem to build optimal strategies for container loading with Python.
📫 For business inquiries, contact me: Samir Saci
Summary
I. How to optimize container loading?
II. Two-dimensional knapsack problem applied to pallet loading
1. Two-Dimensional knapsack problem
2. Tentative 1: The Intuitive solution
3. Tentative 2: The Optimization Algorithm Result
III. Build the Model
1. Initialize the model and set parameters
2. Build your Optimization Model
3. Plot your result
IV. Conclusion
1. Generative AI: GPT for Supply Chain Optimization
2. Reduce the Environmental Impact of your Transportation
3. Do you want to generate animated graph of the solutions?How to optimize container loading?
Scenario
As data scientist in the logistics department of an International Fashion Apparel Retailer, your role is to help operational save costs with the support of data.
The logistics manager wants to ship 200 containers from Yangshan Port (Shanghai, PRC) to Le Havre Port (Le Havre, France).
- Retail value (USD): your goods’ retail value is 225,000$ per container
- Profit Margin (%): Based on pre-crisis shipping costs, your profit margin is 8.5%
- Shipping Costs — Previous (%): 100 x 2,000 / 225,000 = 0.88 (%)
- Shipping Costs — Current (%): 100 x 12,000 / 225,000 = 5.33 (%)
The finance team is putting huge pressure on Logistics Operations because 4.45% of profit is lost to shipping costs.
How can you support this effort of cost optimization?
As the manager has limited influence over market prices, her only solution is to increase your loading capacity to save space.
Let’s see how you can support her!
Parameters
She has received pallets from your plants and suppliers in China, ready to be shipped to France.
You have two types of pallets:
- European Pallets: Dimensions 80 (cm) x 120 (cm)
- North American pallets: Dimensions 100 (cm) x 120 (cm)
You can use two types of containers
- Dry container 20': Inner Length (5,9 m), Inner Width (2,35 m), Inner Height (2,39 m)
- Dry container 40': Inner Length (12,03 m), Inner Width (2,35 m), Inner Height (2,39 m)
What’s the difference? Their size 👇
What are the constraints for loading?
Constraints
- European pallets and American pallets can be mixed
- No Pallet Stacking (put a pallet above another pallet)
- The loading strategy must be performed in real life (using a counter-balance truck)
Objective: Load a maximum number of pallets per container
Now that we have the constraints and the objective, we can tackle the optimisation part.
If you prefer watching, have a look at the video version of this article
Two-dimensional knapsack problem applied to pallet loading
Two-Dimensional knapsack problem
Given a set of rectangular pieces and a rectangular container, the two-dimensional knapsack problem (2D-KP) consists of orthogonally packing a subset of the pieces within the container such that the sum of the values of the packed pieces is maximised.
Adapt it to our problem
If we consider that
- Pallets cannot be stacked
- Pallets have to be orthogonally packed to respect the loading constraints
- Pallets Height is always lower than the internal height of your containers
We can transform our 3D problem into a 2D knapsack problem and apply this algorithm to find an optimal solution.
We need to load in a 40' Container
- 20 European Pallets 80 x 120 (cm)
- 4 North American Pallets 100 x 120 (cm)
How would we do without advanced tools?
Tentative 1: The Intuitive Solution
A forklift driver tried to fit a maximum number of European pallets and find some space for the 4 North American Pallets.
Results
- 20/20 Euro Pallets loaded, 2/4 American pallets loaded.
- You need another container for the two remaining pallets.
Can we do better with Python’s support?
Tentative 2: The Optimization Algorithm Result
On the left, you can see the solution of the optimization algorithm.
Results
- 20/20 Euro Pallets loaded, 4/4 American pallets loaded.
- You don’t need another container.
Conclusion
- The optimized solution can fit 100% of pallets.
- Our filling rate is increased, and pallets are more “packed”.
The solution is based on a non-intuitive placement that can only be found by trying many combinations.
In the next part, we’ll see how to implement a model to get this solution.
🏫 Discover 70+ case studies using data analytics for supply chain sustainability🌳and business optimization 🏪 in this: Cheat Sheet
Build your model
To keep this article concise, we will not build the algorithm from scratch.
There is a Python library called rectpack.
Initialize the model and set parameters
- bx, by: we add a 5 cm buffer on the x-axis and y-axis to ensure that we do not damage the pallets
- bins20, bins40: container dimensions by type
Build your Optimization Model
- bins: the list of available containers (e.g. bins = [bin20, bin40] means that you have one container 20' and one container 40')
- all_rects: list of all rectangles that could be included in the bins, with their coordinates ready to be plotted
- all_pals: the list of pallets that could be loaded in the containers listed in bins
Plot your result
- color: black for 80x120, red for 100 x120
Now, you have everything to share your loading plan with your forklift driver.
You can find the complete code in this GitHub repository 👇
Conclusion
We increased the pallet loading rate in both examples vs. the intuitive approach.
This solution was based on a simple scenario of pallets that cannot be stacked.
- What would the results be if we applied it to stackable pallets?
- What would the results be if we applied it to bulk cartons?
These improvements would increase your container fill rate and reduce your cost per carton shipped.
We have an issue.
The charts presented as outputs do not provide any information on the order in which you load pallets.
Do you want to help the forklift drivers load the pallets in the right order?
Generate Animated Graphs using Pillow.
My solution is to animate these graphs to show the order in which the pallets must be loaded.
With Python’s library Pillow, you can animate your graph like the one below.
This example shows the difference between two pathfinding algorithms dynamically.
If you want to apply this method to the graph generated for the loading plan, check the article linked below,
What about the environmental impact?
Reduce the Environmental Impact of Logistics
The demand for transparency in the carbon emissions of transportation from investors and customers has grown over the years.
What impact this tool could have on the CO2 emissions?
Business intelligence and advanced analytics can help you track your transportation’s CO2 emissions by mode, shipment, and period.
You can then estimate the emissions reductions achieved with your optimization solution with simple formulas based on the
- Weight of goods
- Transportation distance and mode
For more details on how to measure it, 👇
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 to your inbox for free: Newsletter
📘 Your complete guide for Supply Chain Analytics: Analytics Cheat Sheet
