Skip to content

boxstacks: add the JSON instance format - #609

Merged
fontanf merged 1 commit into
masterfrom
boxstacks-json
Oct 1, 2026
Merged

fontanf merged 1 commit into
masterfrom
boxstacks-json

Conversation

@fontanf

@fontanf fontanf commented Oct 1, 2026

Copy link
Copy Markdown
Owner

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

  • Reading: 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.
  • Writing: Instance::write(instance_path, format) writes CSV (the default, as before) or JSON, through new write_csv and write_json, matching the box API.
  • Truck data: SemiTrailerTruckData::read_json / to_json, in the shared truck.hpp so that other types can reuse them.
  • Command line: --input instance.json, as an alternative to --items/--bins/--defects/--parameters.
  • Python: InstanceBuilder.read, Instance.write(instance_path, format=InstanceFormat.Csv) and Instance.write_defects.

CSV fixes

Writing an instance to CSV and reading it back lost two things. Both now round-trip:

  • the objective, which wasn't written to the parameters file;
  • the defects, for which there was no writer. They're now written to <instance_path>_defects.csv when there are any.

Testing

  • New python/tests/test_boxstacks_json.py (11 tests):

    • reading every feature, with defaults for omitted keys;
    • JSON and CSV round trips, comparing Instance.format(verbosity_level=2);
    • solving a JSON instance;
    • errors for invalid input.

    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.json solves the instance. A missing file gives an explicit error, and running with no input prints "Missing --input or --items".

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
fontanf merged commit 5d3c9de into master Oct 1, 2026
11 checks passed
@fontanf
fontanf deleted the boxstacks-json branch October 1, 2026 18:13
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