Search code examples
pythonmathematical-optimizationbin-packing

How to solve 3D multi bin-packing with OpenOpt


I'm learning about optimization and I'm new to OpenOpt.

I would like to represent processes where each have 3 resources usage indicators (CPU, memory and network), and I would like to assign N processes to groups / bins according to the following restrictions:

sum(cpu) within a group < 100
sum(mem) within a group < 100
sum(net) within a group < 100
Minimize(number of groups) or maximize the sum of each resource within a group.

Ideally I would like to have this type of output:

VM 1 assigned to group 1
VM 2 assigned to group 1
VM 3 assigned to group 1
VM 4 assigned to group 2
VM 5 assigned to group 2
VM 6 assigned to group 3
... and so on

Quesiton: How can I do that? If it is not possible to do this with OpenOpt, is there any other lib that could help me with this?

Here my initial code: https://github.com/vonpupp/mdbp/blob/master/ksp_2.py

Many thanks!


Solution

  • The idea is to assign a weight on each item (in this case, all are the same), and maximize the number of items limited by the constraints.

    This is how the items are created:

    def gen_vms():
        tg = tracegen.TraceGenerator()
        trace = tg.gen_trace()
        items = [{
                      'name': 'item %d' % i,
                      'weight': 1,
                      'cpu': t[0],
                      'mem': t[1],
                      'disk': t[2],
                      'net': t[3],
                      'n': 1
                 } for i,t in islice(enumerate(trace), 200)]
        return items
    

    And this is how the constraints are created:

    def add_constraint(values, constraint):
        return values[constraint] < 99
    
    def add_constraints(values, constraint_list):
        return [add_constraint(values, constraint) for constraint in constraint_list]
    
    def gen_costraints(constraint_list):
        global constraints
        constraints = lambda values: (add_constraints(values, constraint_list))
    

    The solving process is like this:

    def solve_host():
        global items
        global constraints
        p = KSP('weight', list(items), constraints = constraints)
        result = p.solve('glpk', iprint = -1)
    

    Further details on how to do this can be found here: https://github.com/vonpupp/2013-sbrc-experiments/blob/e2e8a2be320c8f77d67a5bc6bb822510564e80f3/myksp_oo.py