Repository navigation
boxstacks: add the JSON instance format - #609
Merged
Merged
Conversation
Like the other problem types, boxstacks instances can now be read from and
written to a single JSON file, which supports every feature of the CSV
files:
- 'InstanceBuilder::read(instance_path)' reads the objective, the
parameters ('unloading_constraint', 'no_check_weight_constraints'), the
bin types (with 'maximum_stack_density', 'semi_trailer_truck' and
'defects') and the item types (with 'group_id', 'stackability_id',
'nesting_height', 'maximum_stackability', 'maximum_weight_above' and
'rotations'). Keys are the CSV column names in lower case; omitted keys
keep their defaults.
- 'Instance::write(instance_path, format)' writes CSV (default, as before)
or JSON files, through the new 'write_csv' and 'write_json'.
- 'SemiTrailerTruckData' gets 'read_json' / 'to_json'.
- The command line accepts '--input' (JSON) as an alternative to '--items'.
- Python: 'InstanceBuilder.read', 'Instance.write(instance_path, format)'
and 'Instance.write_defects'.
The CSV writer also writes the objective in the parameters file and the
defects in '<instance_path>_defects.csv': both were lost before when
writing an instance and reading it back.
fontanf
force-pushed
the
boxstacks-json
branch
from
October 1, 2026 16:25
3fc12b0 to
cb98605
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
boxstacks was the only problem type without a JSON instance format. It now reads and writes a single JSON file supporting every feature of its CSV files, like the other types. This is needed for the website's "upload a JSON instance" input.
{ "objective": "knapsack", "unloading_constraint": "increasing-x", "no_check_weight_constraints": [1], "bin_types": [ {"x": 13600, "y": 2400, "z": 2700, "copies": 2, "maximum_weight": 24000, "maximum_stack_density": 1.5, "semi_trailer_truck": {"tractor_weight": 7000, "harness_rear_axle_distance": 7500, "...": "..."}, "defects": [{"x": 100, "y": 200, "width": 300, "height": 400}]} ], "item_types": [ {"x": 1200, "y": 800, "z": 1000, "copies": 5, "group_id": 1, "weight": 300, "stackability_id": 3, "nesting_height": 50, "maximum_stackability": 4, "maximum_weight_above": 900, "rotations": ["XYZ", "YXZ"]} ] }Changes
InstanceBuilder::read(instance_path). Keys are the CSV column names in lower case, and omitted keys keep their defaults. Defects use the same keys as in the rectangle JSON format (x,y,width,height). Invalid values give explicit errors: a missing or unknown objective, an unknown unloading constraint or rotation, a negative group id.Instance::write(instance_path, format)writes CSV (the default, as before) or JSON, through newwrite_csvandwrite_json, matching the box API.SemiTrailerTruckData::read_json/to_json, in the sharedtruck.hppso that other types can reuse them.--input instance.json, as an alternative to--items/--bins/--defects/--parameters.InstanceBuilder.read,Instance.write(instance_path, format=InstanceFormat.Csv)andInstance.write_defects.CSV fixes
Writing an instance to CSV and reading it back lost two things. Both now round-trip:
<instance_path>_defects.csvwhen there are any.Testing
New
python/tests/test_boxstacks_json.py(11 tests):Instance.format(verbosity_level=2);The tests build their instances from JSON, so they don't depend on the builder API changed by Python: item and bin type attributes as keyword arguments #607.
Python tests: 271 passed, 7 expected failures. C++ boxstacks unit tests: 22 passed.
Command line:
packingsolver_boxstacks --input instance.jsonsolves the instance. A missing file gives an explicit error, and running with no input prints "Missing --input or --items".