Containers Loading Optimization with Python

How can we use heuristic algorithms to find the right strategy to load a maximum number of pallets in a sea container?

Samir Saci
Towards Data Science

--

Containers Loading Optimization with Python
Application of the 2D knapsack problem for pallets loading — (Image by Author)

With the recent surge in shipping prices due to container shortage, the price of a container from Shanghai to North Europe went from $2,000 in November to a peak of $12,000.

Therefore, optimizing your container loading became a priority.

Scenario
You are a Logistics Manager in an International Fashion Apparel Retailer.

You want 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 cost, 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 (%)

Your Finance Team is putting huge pressure on Logistics Operations because 4.45 % of profit is lost because of shipping costs.

As you have limited influence on the market price, your only solution is to improve your loading capacity to save space.

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

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

If you prefer watching, have a look at the video version of this article

I. How to optimize container loading?

You have received pallets from your plants and suppliers in China that are ready to be shipped to France.

You have two types of pallets:

  • European Pallets: Dimensions 80 (cm) x 120 (cm)
Example of European pallet-(Source: Rotom)
  • North American pallets: Dimensions 100 (cm) x 120 (cm)
Example of North American pallet— (Source: Chep)

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)

Constraints

  • European pallets and American can be mixed
  • 20' or 40' containers are available
  • 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

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

II. 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 maximized.

Exact algorithms for the two-dimensional guillotine knapsack (Mohammad Dolatabadia, Andrea Lodi, Michele Monaci) — (Link)

2. 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.

3. Results

Scenario: You need to load in a 40' Container

  • 20 European Pallets 80 x 120 (cm)
  • 4 North American Pallets 100 x 120 (cm)

Tentative 1: The Intuitive solution

2D knapsack problem for pallets loading: Initial Solution
Initial Solution — (Image by Author)

Comment: Your 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.

Tentative 2: The Optimization Algorithm Result

2D knapsack problem for pallets loading: New Solution
Optimized Solution (Left) | Initial Solution (Right) — (Image by Author)

Comment:

  • You have the solution on the left based on the algorithm output.

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. It’s based on non-intuitive placement that cannot be found without trying many combinations.
  • Our filling rate is increased, and pallets are more “packed”.

In the next part, we’ll see how to implement a model to get this solution.

💡 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

III. Build your model

To keep this article concise, we will not build the algorithm from scratch but use a Python library rectpack.

Example of results of rectpact library — (Source: Documentation)

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 1 container 20' et 1 container 40')
  • all_rects: list of all rectangles that could be included in the bins with their coordinates ready to be plot
  • 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
Example of output for 20 Euro pallets and 4 North American Pallets— (Image by Author)

Now you have everything to share your loading plan with your forklift driver :)

III. Conclusion

Generative AI: GPT for Supply Chain Optimization

Following the adoption of large language models (LLMs), I started experimenting with designing a LangChain Agent connected to a TMS.

Supply Chain Control Tower Agent with LangChain SQL Agent [Article Link] — (Image by Author)

The performance is quite impressive; the agent can answer operational questions by autonomously querying a database of delivery shipments.

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

Lean Six Sigma Super Agent — (Image by Author)

The idea is to equip customs GPTs with

  • Python Scripts of Optimization models like the container loading solution
  • Context, articles and knowledge about warehousing and transportation processes

Imagine you can help continuous improvement engineers with an agent to find the right test, perform it on datasets uploaded and provide answers.

For more information,

Reduce the Environmental Impact of your Transportation

The demand for transparency in carbon emissions of transportation from investors and customers has grown over the years.

What impact on the CO2 emissions of your transportation you could have after applying this optimization?

With the help of business intelligence and advanced analytics, you can track the CO2 emissions of your transportation by mode, shipment and period.

Formula using Emission Factor — (Image by Author)

You can then estimate the emissions reductions achieved with your optimization solution.

💡 For more details about data analytics for supply chain sustainability,

Deploy in a Web Application

A reader decided to take this prototype to another level and deploy it with a 3D interface.

(Image by Nienke Pieters)

Nienke Pieters used the code shared in this article as a basis to build an application that provides this impressive 3D interface.

For more information, you can check the GitHub repository: Nienke Pieters

Next Steps

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 could be the results if we apply it to stackable pallets?
  • What could be the results if we apply it to bulk cartons?

About Me

Let’s connect on Linkedin and Twitter, I am a Supply Chain Engineer that is 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

--

--

Top Supply Chain Analytics Writer — Follow my journey using Data Science for Supply Chain Sustainability 🌳 and Productivity ⌛