Splitting boxes by height and weight such that difference of sum is minimum

Given a dataframe of box dimensions where height and weight are variable, I want to split it into n groups such that the difference of sum of height and weight is minimum, so that all stacks have similar height and weight.

Eg. Original:

id, height, weight
0, 4, 5
1, 3, 9
2, 6, 5
3, 2, 1
4, 8, 9
5, 7, 1
6, 10, 10

Number of groups to split in (n) = 4

Result:

Group1:
id, height, weight
0, 4, 5
2, 6, 5

Group2:
id, height, weight
1, 3, 9
5, 7, 1

Group 3:
id, height, weight
3, 2, 1
4, 8, 9

Group 4:
id, height, weight
6, 10, 10

here sum of heights for all boxes in a group is 10 and sum of weights for all boxes is 10 and the difference of these sums are zero, which will result in stacks of similar height and weight. The number of boxes in a split can vary.



Read more here: https://stackoverflow.com/questions/68471059/splitting-boxes-by-height-and-weight-such-that-difference-of-sum-is-minimum

Content Attribution

This content was originally published by jmankani at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: