Restrict number of items in Bin Packing

76 views
Skip to first unread message

UNS

unread,
Jun 11, 2020, 8:51:28 AM6/11/20
to or-tools-discuss
Hi Guys - i'm following the bin packing example from the docs here : https://developers.google.com/optimization/bin/bin_packing

I want to add another condition here that restricts the number of items that can be put in a bin. 

Currently, I achieve it in the way stated below - but I'm not sure if this is the right way to do it. The code is reproduced below
def main(store_weightsbin_capacitymax_items,print_suppress=True):
    
    data = create_data_model(store_weights, bin_capacity, max_items)
    # Create the mip solver with the CBC backend.
    solver = pywraplp.Solver('simple_mip_program', pywraplp.Solver.CBC_MIXED_INTEGER_PROGRAMMING)
    
    # Variables
    # x[i, j] = 1 if item i is packed in bin j.
    x = {}
    for i in data['items']:
        for j in data['bins']:
            x[(i, j)] = solver.IntVar(01'x_%i_%i' % (i, j))

    # y[j] = 1 if bin j is used.
    y = {}
    for j in data['bins']:
        y[j] = solver.IntVar(01'y[%i]' % j)
The above is exactly like the example in the docs. 

The part below is where I've made changes:data['max_items'] is the maximum number of items you can put in a bin.Below, I add it alongside the other constraints:
 # Constraints
    # Each item must be in exactly one bin.
    for i in data['items']:
        solver.Add(sum(x[i, j] for j in data['bins']) == 1)

    # The amount packed in each bin cannot exceed its capacity.
    for j in data['bins']:
        solver.Add(
                sum(x[(i, j)] * data['weights'][i] for i in data['items']) <= y[j] *
                data['bin_capacity'])
    
    # Cannot have more than data['max_items'] items in a bin
    for j in data['bins']:
        bin_count = 0
        for i in data['items']:
            bin_count += x[i, j]
        solver.Add(bin_count <= data['max_items'])
        
    
    # Objective: minimize the number of bins used.
    solver.Minimize(solver.Sum([y[j] for j in data['bins']]))


The rest of it just as in the docs example.

Do you guys think this is the correct way to restrict the number of items in a bin? Do you think this is causing any issues that I'm not considering? The output seems to restrict the number of items to data['max_items'] but I'm not sure if this is the right approach.

Thanks in advance!

Laurent Perron

unread,
Jun 11, 2020, 9:27:51 AM6/11/20
to or-tools-discuss
This is fine.
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/7c2796d3-17db-45a5-922f-687598748993o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages