This repository contains an environment compatible with Gymnasium's API to solve the 3D bin packing problem with reinforcement learning (RL).
The environment consists of a list of 3D boxes of varying sizes and a single container of fixed size. The goal is to pack as many boxes as possible in the container minimizing the empty volume. We assume that rotation of the boxes is not possible.
The function boxes_generator in the file utils.py generates instances of the 3D Bin Packing problem using the
algorithm described in Ranked Reward: Enabling Self-Play Reinforcement Learning for Combinatorial Optimization
(Algorithm 2, Appendix).
The documentation source lives in the docs folder and is served with MkDocs.
Run docs locally:
uv run mkdocs serveBuild docs:
uv run mkdocs build --strictWe recommend Python 3.12. Install uv and then run:
git clone https://github.com/luisgarciar/3D-bin-packing.git
cd 3D-bin-packing
uv sync --extra devThis creates a local .venv and installs all runtime and development dependencies from pyproject.toml.
Run Python commands through uv so they execute in the managed environment:
uv run pytest
uv run python src/train.pyThe module packing_kernel (located in src/packing_kernel.py) implements the Container and Box objects that are
used in the Gymnasium environment. To add custom features (for example, to allow rotations), see the documentation of this module.
The Gymnasium environment is implemented in the module src/packing_env.py.
A demo notebook demo_ffd implementing the heuristic-based method 'First Fit Decreasing' is available in the nb
folder.
The folder tests contains unit tests to be run with pytest (uv run pytest).
GitHub Actions pipelines are available in .github/workflows:
ci.yml: runs tests on pushes tomainand pull requests (Python 3.12).release.yml: builds distribution artifacts on version tags (v*) and publishes a GitHub release with the built files.
The following updates have been made to the repository:
- Added the
packing_env.pyfile with the Gymnasium environment. - Added unit tests for the Gymnasium environment.
- Updated the documentation with the full description of the state and action space.
- Updated the demo notebooks.
The following updates have been made to the repository:
- Added functionality for saving rollouts of a policy in a .gif file and
- Added a demo notebook for the random policy.
- Updated the project dependencies.
- Added a demo script for training agents with Maskable PPO.
The following updates have been made to the repository:
- Updated the demo notebook for training agents with Maskable PPO in Google colab.
- Fixed issues with the tests.
The following updates have been made to the repository:
- Migrated build system to modern
pyproject.tomlwithuvpackage manager anduv.lockfor reproducible builds. - Updated Python support to 3.12 and removed legacy Python <3.12 compatibility.
- Migrated environment and training code from legacy
gymto moderngymnasiumAPI (updatedreset(),step(), and action masking interfaces).
