Skip to content

Python: item and bin type attributes as keyword arguments - #607

Merged
fontanf merged 1 commit into
masterfrom
python-builder-kwargs
Sep 30, 2026
Merged

fontanf merged 1 commit into
masterfrom
python-builder-kwargs

Conversation

@fontanf

@fontanf fontanf commented Sep 30, 2026 •

Copy link
Copy Markdown
Owner

Summary

InstanceBuilder.add_item_type and InstanceBuilder.add_bin_type now take the attributes of the item or bin type as keyword arguments. The per-type set_item_type_* / set_bin_type_* methods are removed from the Python API: an instance can't be modified after build() anyway. The C++ API is unchanged.

# Before
bin_type_id = instance_builder.add_bin_type(100, 100)
instance_builder.set_bin_type_copies(bin_type_id, 10)
item_type_id = instance_builder.add_item_type(30, 40)
instance_builder.set_item_type_copies(item_type_id, 20)
instance_builder.set_item_type_profit(item_type_id, 5)

# After
instance_builder.add_bin_type(100, 100, copies=10)
instance_builder.add_item_type(30, 40, copies=20, profit=5)

Design

  • Keyword-only. Only the geometry stays positional. The other parameters of the C++ add_item_type/add_bin_type (e.g. oriented, stack_id) become keyword-only with their C++ defaults, so add_item_type(30, 40, 2) can't be misread.
  • None means "C++ default". A keyword left to None doesn't call its C++ setter. Several C++ defaults are only resolved in build(), e.g. the profit defaulting to the area and copies_min, so they're unchanged.
  • Lists and dicts. Per-type list adders become list keywords: eligibility_ids, rotations (box, boxstacks) and allowed_rotations (irregular). Setters with several values take a dict: trims (rectangleguillotine) and semi_trailer_truck_parameters (boxstacks).
  • irregular shapes. add_item_type also accepts a single Shape, ShapeWithHoles or ItemShape, besides a list of ItemShape. The common single-shape item is then simply add_item_type(psi.build_shape(vertices), copies=2), consistent with add_bin_type, which already takes a shape.
  • Kept as methods:
    • instance-wide methods (set_objective, set_item_types_*, set_bin_types_*, global parameters);
    • methods creating something with its own id (add_defect, add_bin_type_resource, add_resource_consumption, add_item_type_precedence).
  • Kept in sync with C++. Each problem type's tests parse the C++ instance_builder.hpp and check that every per-type setter is either a keyword or explicitly listed as not bound. A setter added later in C++ can't be forgotten in Python.

README

  • Python interface section: one example per problem type, the same as the "Basic usage" example of each page of the documentation, written with the Python interface and followed by the image of its solution.
    • Each example builds exactly the documentation's instance (checked by comparing Instance.format(verbosity_level=2) with the instance read from the documentation's CSV/JSON files) and gives the documented result.
    • The images (img/python_<type>.png) are drawn by each example's visualize(...) call. The README references them by absolute URL, so they also render on PyPI; they only display once they're on master.
  • Command-line interface section: the command-line documentation, including the compilation instructions, moves under it.

Also: garbage collection of the new solution callback

A new_solution_callback usually references its OptimizeParameters back, e.g. through its module globals. That cycle goes through the C++ object, which the garbage collector couldn't see. Such cycles were therefore never collected, and nanobind reported leaked ... functions/instances/types at interpreter exit. OptimizeParameters now implements tp_traverse/tp_clear, and test_parameters.py checks that the cycle is collected for every problem type.

Testing

  • Python tests with PACKINGSOLVER_BUILD_PYTHON=ON: 260 passed, 7 expected failures (unchanged).
  • The six README examples run, give the documented results, and exit without leak warnings.

@fontanf
fontanf force-pushed the python-builder-kwargs branch from f06a851 to daf9d14 Compare September 30, 2026 22:20
'InstanceBuilder.add_item_type' and 'InstanceBuilder.add_bin_type' take the
attributes of the item or bin type as optional keyword-only arguments, e.g.
'add_item_type(30, 40, copies=20, profit=5)', instead of the per-type
'set_item_type_*' / 'set_bin_type_*' methods, which are removed from the
Python API (an instance can't be modified after 'build()' anyway). The C++
API is unchanged.

- Only the geometry stays positional; the other parameters of the C++
  'add_item_type' / 'add_bin_type' (e.g. 'oriented') become keyword-only
  with their C++ default.
- A keyword left to None doesn't call its C++ setter, so the C++ defaults,
  some of which are only resolved in 'build()' (e.g. the profit defaulting
  to the area, 'copies_min'), are unchanged.
- Per-type list adders become list keywords ('eligibility_ids',
  'rotations', 'allowed_rotations'); multi-value setters take a dict
  ('trims', 'semi_trailer_truck_parameters').
- irregular: 'add_item_type' also accepts a single 'Shape',
  'ShapeWithHoles' or 'ItemShape', besides a list of 'ItemShape', e.g.
  'add_item_type(psi.build_shape(vertices), copies=2)'.
- Instance-wide methods ('set_objective', 'set_item_types_*',
  'set_bin_types_*', global parameters) and methods creating something
  with its own id ('add_defect', 'add_bin_type_resource',
  'add_resource_consumption', 'add_item_type_precedence') are kept.
- Each problem type's tests parse the C++ 'instance_builder.hpp' and check
  that every per-type setter is either a keyword or explicitly listed as
  not bound, so that a setter added later in C++ isn't forgotten.

Also make 'OptimizeParameters' support the garbage collector: a new
solution callback usually references the parameters back (e.g. through its
module globals), and without 'tp_traverse' / 'tp_clear' such cycles were
never collected, keeping the parameters, the callback and everything they
reference alive until the interpreter exits.

README: the Python interface section shows the example of each problem
type's documentation, written with the Python interface, followed by the
image of its solution ('img/python_<type>.png', drawn with 'visualize').
The command-line documentation, including the compilation instructions,
moves to a 'Command-line interface' section.
@fontanf
fontanf force-pushed the python-builder-kwargs branch from daf9d14 to 826faa6 Compare September 30, 2026 22:24
@fontanf
fontanf merged commit fac2eb6 into master Sep 30, 2026
4 checks passed
@fontanf
fontanf deleted the python-builder-kwargs branch October 6, 2026 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant