For example, you have several lists of doubles, which you need to distribute in several “buckets” of fixed size (bucket size is also a double). There are two additional constraints:
-
Values from certain lists can go only to certain (pre-specified) buckets:
bucket1 <-\ |--- list1 / / bucket2 <--\ bucket3 <---- list2 bucket4 <--/ bucket5 <--- list3 -
The resulting distribution must be as uniform as possible (so that all buckets have load factor of, for example,
0.5).
Specific example of such problem: imagine if you have several power supply units (“buckets”), and several boards of lamps. Each supply unit is connected to one or several boards, each supply unit has different capacity, lamps consume different amounts of energy. If some board is connected to several supply units, then you can “assign” some of the lamps to first power supply, some – to second, etc.
Doing this with a bruteforce quickly becomes non-feasible for big number of elements.
Is there an efficient approach?
EDIT: I devised the following approach – which seems to converge on the needed result fairly quickly. The idea is as following:
- First, for each list, I distribute items into allowed buckets, using least-loaded-bucket heuristic.
- Then, I do the following:
- for each list:
- remove the items corresponding to the list from the buckets
- distribute them again, using the same least-loaded heuristic
- calculate the ratio of max-loaded bucket size to least-loaded (for allowed buckets)
- terminate the loop if ratio is less than some constant (I took
1.02), or if too many steps passed.
- for each list:
The general idea is to “smooth” the buckets until the distribution becomes flat enough, which usually means that we reached the needed target.
Is it a good algorithm?
Sounds to me like a bin packing problem or knapsack problem with additional constraints (certain lists can only go to specific buckets) for which you want a solution that fills all buckets to at least a specific load factor. Speaking of “efficient”, I would say that what you describe should be NP-hard (“should” because I haven’t had the time to think about a reduction from a NP-problem to your specific problem yet but I am pretty sure there is one).
What you could try: First solve the constraint problem and determine which lines can go in which buckets and then fill the buckets greedily. If your constraints are hard you can do backtracking.