Constraint to add only one type of items in a bin - Bin Pacing Problem SCIP

95 views
Skip to first unread message

Ritesh Sarode

unread,
Jun 9, 2022, 1:15:32 PM6/9/22
to or-tools-discuss
Is there a way to add a constraint to pack only one type of items in one bin?

Items = [{name: item_a, weight: w1, type: type1}, {name: item_b, weight: w2, type: type2}, {name: item_c, weight: w3, type: type3}, {name: item_d, weight: w4, type: type1}, {name: item_e, weight: w5, type: type2}, {name: item_f, weight: w6, type: type3}, {name: item_g, weight: w7, type: type1}]

I want items with "type1" packed in to one bin and so on. 

Thanks.

Daniel Rogers

unread,
Jun 9, 2022, 4:18:57 PM6/9/22
to or-tools-discuss
This problem is synonymous with solving the bin packing problem n times, where n is the total number of types. Therefore, I think it would be much easier to break your Items list into sublists, each consisting of one type. Then, use each sublist as input to its own bin packing problem. The solution to each subproblem will be the minimum # of bins for that specific type. The sum of the subproblem solutions will be your final solution (i.e. the minimum bins for all types).

Not sure if you're using Python, but Here's some quick Python to get your Items into sublists:

```
import itertools

items = [{'name': 'item_a', 'weight': 'w1', 'type': 'type1'}, {'name': 'item_b', 'weight': 'w2', 'type': 'type2'}, {'name': 'item_c', 'weight': 'w3', 'type': 'type3'}, {'name': 'item_d', 'weight': 'w4', 'type': 'type1'}, {'name': 'item_e', 'weight': 'w5', 'type': 'type2'}]
sorted_items_by_type = sorted(items, key=lambda x: x['type']) # must be sorted for the itertools.groupby function

sublists = []
for k, g in itertools.groupby(sorted_items_by_type, key=lambda x: x['type']):
    group = list(g)
    print(f'Type {k}:', group)
    sublists.append(group)

# now 'sublists' contains a list for each type
```



Ritesh Sarode

unread,
Jun 10, 2022, 9:50:46 AM6/10/22
to or-tools-discuss

Thanks for your response. I think that wont be the most ideal way to do the packing, since I have multiple such constraints that I am applying to the items. 

I am looking to see if there are any constraint functions that can be used to do the same thing that you mentioned above along with 10 different constraints that I have already added in place.

Daniel Rogers

unread,
Jun 10, 2022, 12:11:05 PM6/10/22
to or-tools-discuss
Could you not replicate the constraints for each subproblem? I don't believe it's common to have this typed-BPP you describe... breaking it up into subproblems seems to me like it would be a more readable approach/solution.

If you absolutely must create constraints to capture this functionality, I think you will have to turn the "types" into unique numbers (instead of strings), then define mathematically what it means for there to be one type per bin. So, if you imagine an i x j boolean matrix, where i is your items, and j is the bins, matrix[i, j] would be the decision of whether item i is in the jth bin. You need a constraint on the j columns (i.e. the bins) that somehow captures this same-typeness. It's not clear to me how you would capture that characteristic.

Another possible approach is using channeling constraints.
Reply all
Reply to author
Forward
0 new messages