This project is a fork from Enzo Ruiz implementation of 3D Bin Packing available here version py3dbp==1.1.2. Original 3D Bin Packing implementation is based on this paper. The code is based on gedex implementation in Go.
from py3dbp import Packer, Bin, Item
from py3dbp import execute_packing
bins = load_box_types() # load available bin types from JSON file <-- 'box' should be refactored to 'bins'
items = load_items_types() # load the items which needs to be packed from JSON file
items_to_fit = create_items(items) # create objects to be passed to packer
bin_types = create_bins(bins) # create bin types to which we will pack the items
execute_packing(items_to_fit: List[Item], bin_types: List[Bin]) # do the packing
api
script, which will setup an uvicorn
session on http://127.0.0.1:8000.curl -X 'POST' \
'http://localhost:8000/packer' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-d '{
"items": {
"item1": {
"name": "euro_pallet",
"quantity": 40,
"width": 2.625,
"hight": 5,
"depth": 3.94,
"weight": 600
}},
"bins": {
"container1": {
"name": "40_ft_container",
"width": 39.5,
"hight": 7.75,
"depth": 7.75,
"max_weight": 20000
}}
}'
Sorting Bins and Items:
[bigger_first=False/True | default=True]
By default all the bins and items are sorted from the biggest to the smallest, also it can be vice versa, to make the packing in such ordering.
Number of decimals:
[number_of_decimals=X]
Define the limits of decimals of the inputs and the outputs. By default is 3.
Visualisation of packed bins:
visualize_results(best_bins: List[Bin], export_img=False)
Plots the items fitted into the bins.
[export_img=False/True | default=False]
- it is possible to export the visual configuration of each bin to .png file to separate folder: reports.
Textualization of packed bins:
textualize_results(best_bins: List[Bin])
Prints to console the packing list as the tree of bins and items packed into each of the bins.
API returning best packed bins in JSON format
Bin types and items to be fitted are loaded from the JSON files:
load_box_types(file="boxes.json")
- will load the bin types that are available to fit the items in. Each bin should be defined in JSON file as below:
{
"box_type": {
"name": str,
"width": float,
"hight": float,
"depth": float,
"max_weight": float
}
}
load_items_types(file="items.json")
- will load the items details that should be fitted into the bins. It is possible to create multiple items of the same type by defining the quatity
. Items details should be defined in JSON file as below:
{
"item_type": {
"name": str ,
"quantity": int ,
"width": float ,
"hight": float ,
"depth": float ,
"weight": float }
}
create_bins(bins: dict) -> list
create_items(items: dict) -> list
quantity
parameter in previously loaded JSON file.Bin and Items have the same creation params:
my_bin = Bin(name, width, height, depth, max_weight)
my_item = Item(name, width, height, depth, weight)
packer = Packer() # Packer definition
packer.add_bin(my_bin) # Adding Bins to packer instance
my_bin
will be added to packer.bins as one of the types of bins that can be used to pack items. packer.add_item(my_item) # Adding Items to packer instance
my_item
will be added to packer.items to be fittedd into the binspacker.remove_item(my_item) # Removes Item from packer instance
packer.pack() # Analyze the Items fitting each Bin - by default (bigger_first=False ~~distribute_items=False,~~ number_of_decimals=3)
packer
to all Bins added to packer
. Calling this function will result in each Bin being filled with as many Item as possible. After calling this method each of the Bin in packer
instance will hold:
my_bin.items
- the list of fitted itemsexecute_packing()
function to find the most optimal way of packing all items into available box types.execute_packing(items_to_fit, bin_types, visualize=False, export_img=False, textualize=False)
the function workflow is as follow:
Function will return best_packed_bins list.