Skip to content

Commit 98e53c4

Browse files
committed
Initial Commit
0 parents  commit 98e53c4

10 files changed

+3417
-0
lines changed

.gitignore

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
share/python-wheels/
24+
*.egg-info/
25+
.installed.cfg
26+
*.egg
27+
MANIFEST
28+
29+
# PyInstaller
30+
# Usually these files are written by a python script from a template
31+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
32+
*.manifest
33+
*.spec
34+
35+
# Installer logs
36+
pip-log.txt
37+
pip-delete-this-directory.txt
38+
39+
# Unit test / coverage reports
40+
htmlcov/
41+
.tox/
42+
.nox/
43+
.coverage
44+
.coverage.*
45+
.cache
46+
nosetests.xml
47+
coverage.xml
48+
*.cover
49+
*.py,cover
50+
.hypothesis/
51+
.pytest_cache/
52+
cover/
53+
54+
# Translations
55+
*.mo
56+
*.pot
57+
58+
# Django stuff:
59+
*.log
60+
local_settings.py
61+
db.sqlite3
62+
db.sqlite3-journal
63+
64+
# Flask stuff:
65+
instance/
66+
.webassets-cache
67+
68+
# Scrapy stuff:
69+
.scrapy
70+
71+
# Sphinx documentation
72+
docs/_build/
73+
74+
# PyBuilder
75+
.pybuilder/
76+
target/
77+
78+
# Jupyter Notebook
79+
.ipynb_checkpoints
80+
81+
# IPython
82+
profile_default/
83+
ipython_config.py
84+
85+
# pyenv
86+
# For a library or package, you might want to ignore these files since the code is
87+
# intended to run in multiple environments; otherwise, check them in:
88+
# .python-version
89+
90+
# pipenv
91+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
93+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
94+
# install all needed dependencies.
95+
#Pipfile.lock
96+
97+
# poetry
98+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
99+
# This is especially recommended for binary packages to ensure reproducibility, and is more
100+
# commonly ignored for libraries.
101+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
102+
#poetry.lock
103+
104+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
105+
__pypackages__/
106+
107+
# Celery stuff
108+
celerybeat-schedule
109+
celerybeat.pid
110+
111+
# SageMath parsed files
112+
*.sage.py
113+
114+
# Environments
115+
.env
116+
.venv
117+
env/
118+
venv/
119+
ENV/
120+
env.bak/
121+
venv.bak/
122+
123+
# Spyder project settings
124+
.spyderproject
125+
.spyproject
126+
127+
# Rope project settings
128+
.ropeproject
129+
130+
# mkdocs documentation
131+
/site
132+
133+
# mypy
134+
.mypy_cache/
135+
.dmypy.json
136+
dmypy.json
137+
138+
# Pyre type checker
139+
.pyre/
140+
141+
# pytype static type analyzer
142+
.pytype/
143+
144+
# Cython debug symbols
145+
cython_debug/
146+
147+
# PyCharm
148+
# JetBrains specific template is maintainted in a separate JetBrains.gitignore that can
149+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
150+
# and can be added to the global gitignore or merged into this file. For a more nuclear
151+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
152+
#.idea/
153+
output.json

README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# BinPack Solver With Instructions
2+
3+
## Setup Instructions
4+
5+
1. Create a virtualenv with `python -m virtualenv venv`
6+
2. Activate the virtualenv (using appropriate script in venv/Scripts)
7+
3. Install dependencies with `pip install -r requirements.txt`
8+
9+
## Running the solver
10+
11+
1. Place input parameters in `input.json`
12+
2. Run `python main.py`
13+
3. Output will be presented in `output.json`, statistics of the packing will be displayed on the terminal
14+
15+
16+
## Visualization
17+
18+
![](img/viz.png)
19+
20+
To visualize your packing process , go to [binpack-viewer.amith.ml](https://binpack-viewer.amith.ml/) and drag and drop the generated `viz.json` file
21+
22+
23+
## Example Output
24+
25+
26+
```
27+
❯ python main.py
28+
Completed solving with 93.07% fill. (6.93% trim loss)
29+
Generated 45 instructions after simplification.
30+
Find results in output.json
31+
```

img/viz.png

35.9 KB
Loading

input.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"container": {
3+
"l": "19.4ft",
4+
"w": "7.8ft",
5+
"h": "8.6ft"
6+
},
7+
"carton_small": {
8+
"l": "12in",
9+
"w": "18in",
10+
"h": "24in",
11+
"count": 300
12+
},
13+
"carton_big": {
14+
"l": "24in",
15+
"w": "18in",
16+
"h": "36in",
17+
"count": 250
18+
}
19+
}

instruction_encode.py

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
from pack_solver import *
2+
from itertools import groupby
3+
4+
def rot_instruction(box: SolvedBox):
5+
rot = box.position.rotation
6+
if rot == Rotation.ORIGINAL:
7+
return 'Place'
8+
else:
9+
return 'Rotate sideways and place'
10+
11+
12+
def row_reset(x1, y1, z1, x2, y2, z2):
13+
st = 'on the next row'
14+
if y2 > y1:
15+
st = f'{st} on top'
16+
elif z2 > z1:
17+
st = f'{st} in front'
18+
return st
19+
20+
21+
def rel_placement(x1, y1, z1, x2, y2, z2):
22+
end = 'the previous box'
23+
if x2 == 0 and x1 != x2:
24+
return row_reset(x1, y1, z1, x2, y2, z2)
25+
if x2 > x1:
26+
return f'to the right of {end}'
27+
elif x2 < x1:
28+
return f'to the left of {end}'
29+
elif y2 > y1:
30+
return f'above {end}'
31+
elif y2 < y1:
32+
return f'below {end}'
33+
elif z2 < z1:
34+
return f'in front of {end}'
35+
elif z2 > z1:
36+
return f'behind {end}'
37+
38+
def simplify_instructions(instructions: List):
39+
simplified_instructions = []
40+
for k, g in groupby(instructions):
41+
repeat_c = len(list(g))
42+
if repeat_c == 1:
43+
simplified_instructions.append(f'{k[0]} {k[1]}')
44+
else:
45+
simplified_instructions.append(f'{k[0]} {repeat_c} of {k[1]}')
46+
return simplified_instructions
47+
48+
49+
def make_instructions(solved_container: SolvedContainer):
50+
instructions_expanded = []
51+
for i, box in enumerate(solved_container.solved_boxes):
52+
if i == 0:
53+
instructions_expanded.append(
54+
[f'To start stacking, {rot_instruction(box)}', f'{box.name} at left-back of container'])
55+
continue
56+
prev = solved_container.solved_boxes[i-1]
57+
px, py, pz = prev.position.x, prev.position.y, prev.position.z
58+
x, y, z = box.position.x, box.position.y, box.position.z
59+
instructions_expanded.append(
60+
[rot_instruction(box), f'{box.name} {rel_placement(px,py,pz,x,y,z)}'])
61+
return simplify_instructions(instructions_expanded)
62+
63+

main.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import json
2+
from pack_solver import *
3+
from instruction_encode import make_instructions
4+
from pint import UnitRegistry
5+
from visualize import make_visuals
6+
7+
ureg = UnitRegistry()
8+
Q_ = ureg.Quantity
9+
10+
problem_input = None
11+
with open("input.json", "r") as infile:
12+
problem_input = json.load(infile)
13+
14+
15+
def to_inch(x: str):
16+
return round(Q_(x).to("inch").to_tuple()[0], 2)
17+
18+
19+
container_dimensions = Dimension(to_inch(problem_input["container"]["l"]),
20+
to_inch(problem_input["container"]["w"]),
21+
to_inch(problem_input["container"]["h"]))
22+
23+
carton_small_dimensions = Dimension(to_inch(problem_input["carton_small"]["l"]),
24+
to_inch(
25+
problem_input["carton_small"]["w"]),
26+
to_inch(problem_input["carton_small"]["h"]))
27+
28+
carton_big_dimensions = Dimension(to_inch(problem_input["carton_big"]["l"]),
29+
to_inch(problem_input["carton_big"]["w"]),
30+
to_inch(problem_input["carton_big"]["h"]))
31+
32+
33+
packer = Packer()
34+
packer.add_container(Container("Container", container_dimensions))
35+
packer.add_boxes(Box("carton_small", carton_small_dimensions,
36+
problem_input["carton_small"]["count"]))
37+
packer.add_boxes(Box("carton_big", carton_big_dimensions,
38+
problem_input["carton_big"]["count"]))
39+
packing_results = packer.pack()
40+
41+
container = packing_results[0]
42+
boxes = container.solved_boxes
43+
44+
carton_small_count = len(
45+
list(filter(lambda x: x.name == "carton_small", boxes)))
46+
carton_big_count = len(list(filter(lambda x: x.name == "carton_big", boxes)))
47+
instructions = make_instructions(packing_results[0])
48+
out_payload = {
49+
"carton_small": carton_small_count,
50+
"carton_big": carton_big_count,
51+
"instructions": instructions
52+
}
53+
54+
with open("output.json", "w") as outfile:
55+
outfile.write(json.dumps(out_payload, indent=4))
56+
57+
with open("viz.json", "w") as outfile:
58+
outfile.write(json.dumps(make_visuals(packer.containers[0], container), indent=4))
59+
60+
print(
61+
f"Completed solving with {container.percentage_fill:.2f}% fill. ({100-container.percentage_fill:.2f}% trim loss)")
62+
print(f"Generated {len(instructions)} instructions after simplification.")
63+
print("Find results in output.json")
64+
print("Visualization output created as viz.json")

0 commit comments

Comments
 (0)