Skip to main content

pyckingsolver

PackingSolver from Python: irregular 2D nesting (Shapely polygons welcome), 3D box packing and 1D cutting, used like upstream's own modules, every parameter included, each solve in a crash-proof child process.

PyPI version Python 3.13+ License: AGPL v3 Build

Holed plates, brackets and a gusset on one sheet, small discs inside the bolt holes

Install

pip install pyckingsolver

It brings upstream's own solver module, packingsolver (wheels for Windows x64 and x86, Linux x86_64 and aarch64 with glibc 2.28 or newer, macOS arm64 and x86_64), pinned to one upstream commit. Python 3.13+.

Quick start

import pyckingsolver as ps
from shapely.geometry import Point, box

plate = box(0, 0, 150, 100).difference(Point(75, 50).buffer(30))  # Shapely: a plate with a hole
ib = ps.InstanceBuilder()
ib.set_objective(ps.Objective.Knapsack)
ib.set_item_item_minimum_spacing(2)
ib.add_bin_type(ps.build_rectangle(500, 300))
ib.add_item_type(plate, copies=6, allowed_rotations=[(0, 0, False), (90, 90, False)])
ib.add_item_type(ps.build_circle(12, ps.Point(12, 12)), copies=40)  # native: one exact arc

output = ps.optimize(ib.build(), time_limit=10,
                     stall_timeout=2,  # stop 2 s after the last improvement
                     new_solution_callback=lambda o: print("profit", o.solution.profit()))

s = output.solution
for k, item in enumerate(s.bin(0).items):
    placed = ps.to_shapely(s.shape_scaled(0, k))  # the item as placed
    print(item.item_type_id, item.bl_corner.x, item.bl_corner.y, item.angle, item.mirror)

Any upstream parameter goes the same way, ps.optimize(instance, many_items_in_section_threshold=8, optimization_mode=ps.OptimizationMode.Anytime), or on an ps.OptimizeParameters() you keep and pass.

A placed item is its shape mirrored about the y axis (when mirror), turned angle degrees about the origin, then moved by bl_corner; shape_scaled returns exactly that.

Examples

examples/example.py runs every scenario below in about 35 s and writes these images: cd test && uv run python ../examples/example.py [scenario ...].

laser_sheet: discs nest in the plates' holes; new_solution_callback counts improvements bin_packing: arc parts from build_shape on as few sheets as possible
knapsack: the sheet is too small, profit decides resource: a bin resource caps the sheet at 20 parts
strip: OpenDimensionX, the used length minimised variable_sized: three sheet sizes, the cheapest mix
leftovers: BinPackingWithLeftovers, LeftoverMode.BottomLeft defects: two defects kept 10 mm clear
polygon_bin: an offcut bin whose hole becomes a defect rotations: mirrored copies and a continuous 0 to 90 degree range
fixed_item: a frame fixed before the search, parts around and inside it

API

One layout for the three problems: ps.irregular, ps.box and ps.onedimensional each have InstanceBuilder, OptimizeParameters, optimize, Output and Solution, named as upstream's packingsolver.irregular, .box and .onedimensional (bindings). import pyckingsolver as ps puts the irregular names at the top level.

Shapes (irregular). Point, ShapeElement, Shape, ShapeWithHoles, build_line_segment, build_circular_arc, build_shape(points) ((x, y) vertices, (x, y, 1) / (x, y, -1) the centre of an anticlockwise / clockwise arc to the next vertex), build_path, build_triangle, build_rectangle, build_square, build_circle. Every shape argument also takes a Shapely Polygon (holes included); to_shapely and from_shapely convert.

InstanceBuilder (irregular): ids are the order of the add_* calls; build() returns an Instance and resets the builder.

Method
set_objective(Objective.X) BinPacking, BinPackingWithLeftovers, OpenDimensionX, OpenDimensionY, OpenDimensionXY, Knapsack, VariableSizedBinPacking, Feasibility
set_item_item_minimum_spacing, set_open_dimension_xy_aspect_ratio, set_leftover_mode, set_leftover_all_bins instance parameters
add_bin_type(shape, *, cost, item_bin_minimum_spacing, copies, copies_min) cost defaults to the area, copies to 1, -1 is infinite
add_defect(bin_type_id, type, shape), set_item_defect_minimum_spacing(bin_type_id, defect_id, spacing) type -1 is untyped
add_bin_type_resource(bin_type_id, capacity, penalize, penalty), add_resource_consumption(bin_type_id, resource_id, item_type_id, schedule), set_bin_type_resource_tolerance per-copy consumption
add_item_type(shapes, *, allowed_rotations, profit, copies, copies_min) allowed_rotations: (start, end, mirror) ranges, default [(0, 0, False)]; profit defaults to the area
add_fixed_item(bin_type_id, item_type_id, (x, y), angle=0, mirror=False) a placement fixed in every bin of the type
set_bin_types_infinite_copies, set_bin_types_unweighted, set_item_types_unweighted, set_item_types_continuous_rotations, read(path)

OptimizeParameters: every field of upstream's, by the same name (time_limit, verbosity_level, optimization_mode, the use_* algorithm switches, many_items_in_bins_threshold, periodic_packing_copies_threshold, tree_search_guides, the queue sizes, reduction_parameters and its switches, ...); None keeps upstream's default. Plus:

  • new_solution_callback(output): called with an Output on each new best solution.
  • stall_timeout: stop once no new solution came for this many seconds.
  • first_solution_timeout: stop if no solution came at all by then.
  • cancel: anything with .is_set(); once set the solve is killed and OptimizeCancelled raised.

optimize(instance, parameters=None, **overrides) -> Output: output.solution, output.time, the bounds (knapsack_bound, bin_packing_bound, ...), is_proven_infeasible, is_proven_optimal(). A solve that found nothing returns an empty solution; a rejected instance or a crash raises RuntimeError.

Solution: bins, bin(i) (bin_type_id, copies, items), number_of_bins(), number_of_items(), item_copies(t), shape_scaled(bin_pos, item_pos), the metrics (profit(), cost(), x_max(), full_waste_percentage(), leftover_value_orig(), ...), to_json(), write(path). SolutionItem: item_type_id, bl_corner.x/.y, angle, mirror, is_fixed.

Box (3D) and one-dimensional

pyckingsolver.box and pyckingsolver.onedimensional mirror upstream's packingsolver.box and packingsolver.onedimensional, with the same InstanceBuilder / OptimizeParameters / optimize shape, every upstream parameter, the same child-process solve and the same stall_timeout, first_solution_timeout, cancel and new_solution_callback:

import pyckingsolver.box as psb

ib = psb.InstanceBuilder()
ib.set_objective(psb.Objective.BinPacking)
ib.add_bin_type(10, 10, 10, copies=10)          # a bin type has one copy by default
ib.add_item_type(5, 5, 5, copies=9)
ib.add_item_type(10, 5, 2, copies=2, rotations=[psb.Rotation.XYZ, psb.Rotation.ZYX])
for item in psb.optimize(ib.build(), time_limit=10).solution.bin(0).items:
    print(item.item_type_id, item.bl_corner, item.rotation)

import pyckingsolver.onedimensional as ps1

ib = ps1.InstanceBuilder()
ib.set_objective(ps1.Objective.BinPacking)
ib.add_bin_type(6000, copies=10)          # 6 m bars
ib.add_item_type(1500, copies=7)
solution = ps1.optimize(ib.build(), time_limit=10).solution
print(solution.number_of_bins(), [[i.start for i in b.items] for b in solution.bins])

A box item is bl_corner.x/.y/.z and a Rotation (which item axis lies along the bin's x, y, z); a one-dimensional item is its start. Lengths are integers there (upstream's type); scale to a finer unit, a fractional length raises ValueError. to_csv() / write() give upstream's CSV certificate. Not reachable (no instance JSON key, or a default resolved inside upstream): set_weight_tolerance, box set_item_types_profits_auto, one-dimensional precedences and fixed items, the CSV read_* methods.

Differences from packingsolver's own module

  • Each solve runs upstream's module in a child process: a solver crash cannot take your process down, and stall_timeout, first_solution_timeout and cancel are hard kills. Starting the child costs about 120 ms per solve (Python plus the module import).
  • The builders write upstream's instance JSON, which the child reads with upstream's own reader.
  • Shapes are pure Python; a Shapely polygon goes wherever a shape does. build_circle takes an optional centre. A bin's holes are sent as defects (upstream reads a bin without holes).
  • add_fixed_item is upstream's C++ builder method; its JSON reader takes it, its bindings lack it.
  • optimize(instance, **overrides) sets parameters inline.
  • Defaults that differ: verbosity_level 0, and for irregular, reduction off when a bin has fixed items (upstream's reduction then throws) and the plain tree search for a Knapsack with copies_min (the maximal-spaces search ignores it).

Known solver limits

Measured on the solver:

  • A fixed item's holes can stay empty: the shape simplification shrinks them below every candidate item, and the first beam search then stops (details in AGENTS.md).
  • A knapsack can settle on a wasteful arrangement: tree search packs along a skyline, so a gap closed by two later parts is never refilled.
  • A resource on one item type leaves free space unused: in the knapsack scenario, capping the discs at 4 with a resource gives profit 48; copies=4 instead gives 64. A resource every item type consumes (the resource scenario) behaves.

What's new in 0.12.0

Every upstream parameter on OptimizeParameters, solved through upstream's own module (no bundled binaries); ps.irregular, ps.box (3D) and ps.onedimensional with one layout; optimize(instance, **overrides); Python 3.13+; solver at upstream fd246f7f. History in CHANGELOG.md.

Solver version

The solver is the packingsolver pin in python/pyproject.toml: upstream publishes each master commit as 0.1.<commits on master>. extern/packingsolver is upstream's checkout, for reading its sources. Checks: cd test && uv run python test_pyckingsolver.py.

License

AGPL-3.0-or-later, see LICENSE.

Built on PackingSolver by Florian Fontan (MIT), installed as its own packingsolver package. Its Python module is built without Clp (EPL-2.0): HiGHS, Boost, nlohmann/json and every fontanf dependency are MIT or BSL-1.0.

Metadata

Release files for pyckingsolver 0.12.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pyckingsolver 0.12.0
File Size Uploaded Provenance Actions
pyckingsolver-0.12.0.tar.gz 45.2 kB With provenance Details for pyckingsolver-0.12.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pyckingsolver 0.12.0
File Interpreter ABI Platform Actions
pyckingsolver-0.12.0-py3-none-any.whl Python 3 none any Details for pyckingsolver-0.12.0-py3-none-any.whl

Total release size: 87.3 kB

Release files / pyckingsolver-0.12.0.tar.gz

Download URL pyckingsolver-0.12.0.tar.gz
Size 45.2 kB
Tags Source
SHA-256 checksum
How to use checksums
18f5139fbbe670d2b763bde72636d4b423340d92626339eb93f5f45e1abe811b
BLAKE2b-256 checksum
How to use checksums
22d8556f70c9c1339c7de0f020fd3f30f508938b0be1af7b4b9f34ade2f977dc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / pyckingsolver-0.12.0-py3-none-any.whl

Download URL pyckingsolver-0.12.0-py3-none-any.whl
Size 42.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6477357dce2b14866f6791c9bfec82cfeb7ef10f57a446e1e6b67c6158143017
BLAKE2b-256 checksum
How to use checksums
482b1b88968ac86168a64c8c3f5eff62f9634cfde16646c004408ed0815a1864
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for pyckingsolver 0.12.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for pyckingsolver-0.12.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page