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