Repository navigation
Python: item and bin type attributes as keyword arguments - #607
Merged
Merged
Conversation
fontanf
force-pushed
the
python-builder-kwargs
branch
from
September 30, 2026 22:20
f06a851 to
daf9d14
Compare
'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
force-pushed
the
python-builder-kwargs
branch
from
September 30, 2026 22:24
daf9d14 to
826faa6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
InstanceBuilder.add_item_typeandInstanceBuilder.add_bin_typenow take the attributes of the item or bin type as keyword arguments. The per-typeset_item_type_*/set_bin_type_*methods are removed from the Python API: an instance can't be modified afterbuild()anyway. The C++ API is unchanged.Design
add_item_type/add_bin_type(e.g.oriented,stack_id) become keyword-only with their C++ defaults, soadd_item_type(30, 40, 2)can't be misread.Nonemeans "C++ default". A keyword left toNonedoesn't call its C++ setter. Several C++ defaults are only resolved inbuild(), e.g. the profit defaulting to the area andcopies_min, so they're unchanged.eligibility_ids,rotations(box, boxstacks) andallowed_rotations(irregular). Setters with several values take a dict:trims(rectangleguillotine) andsemi_trailer_truck_parameters(boxstacks).add_item_typealso accepts a singleShape,ShapeWithHolesorItemShape, besides a list ofItemShape. The common single-shape item is then simplyadd_item_type(psi.build_shape(vertices), copies=2), consistent withadd_bin_type, which already takes a shape.set_objective,set_item_types_*,set_bin_types_*, global parameters);add_defect,add_bin_type_resource,add_resource_consumption,add_item_type_precedence).instance_builder.hppand 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
Instance.format(verbosity_level=2)with the instance read from the documentation's CSV/JSON files) and gives the documented result.img/python_<type>.png) are drawn by each example'svisualize(...)call. The README references them by absolute URL, so they also render on PyPI; they only display once they're onmaster.Also: garbage collection of the new solution callback
A
new_solution_callbackusually references itsOptimizeParametersback, 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 reportedleaked ... functions/instances/typesat interpreter exit.OptimizeParametersnow implementstp_traverse/tp_clear, andtest_parameters.pychecks that the cycle is collected for every problem type.Testing
PACKINGSOLVER_BUILD_PYTHON=ON: 260 passed, 7 expected failures (unchanged).