I have several different numbers in a group that range in sizes and would like to calculate which group the numbers should go in based on the max size the group can be.
Example of the numbers: 10,20,30,40,50,60
Example of conditions: the maximum total the numbers can add up to in a group
is 60
So from the example above the answer would be:
group 1 would have the numbers 10,20,30
group 2 would have the number 40
group 3 would have the number 50
group 4 would have the number 60
Is there a way in matlab/octave or excel/librecalc this can be computed?
PS: A group can also have the number 40 and 20 the group total just can’t go over 60. But they can only use each number once.
Is there a math term used for this?
EDIT:
The solution below uses a brute-force approach to generating powersets of powersets (although trimmed). Then checks for groups that satisfy the conditions set (namely divide all the numbers into groups such that no group contain a sum of more than 60). I borrowed some code from the
powerset.mfunction in PMTK3 toolbox.This should work fine for a small problem like this one, but I suspect it would grow exponentially in size for larger input. I’m sure there are better heuristic/algorithms out there, so take this as a starting point…
This gave me 12 solutions. For example: