4 The instance of the bin packing problem is represented by the two
5 lists of n items of sizes and quantity s=(s_i).
8 We use Martello and Toth (1990) formulation, and suggest
9 extensions with tie-breaking and SOS constraints.
11 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
14 from pyscipopt
import Model, quicksum
18 """First Fit Decreasing heuristics for the Bin Packing Problem.
20 - s: list with item widths
22 Returns a list of lists with bin compositions.
26 for item
in sorted(s, reverse=
True):
27 for (j, free)
in enumerate(remain):
34 remain.append(B - item)
39 """bpp: Martello and Toth's model to solve the bin packing problem.
41 - s: list with item widths
43 Returns a model, ready to be solved.
52 x[i, j] = model.addVar(vtype=
"B", name=
"x(%s,%s)" % (i, j))
54 y[j] = model.addVar(vtype=
"B", name=
"y(%s)" % j)
58 model.addCons(
quicksum(x[i, j]
for j
in range(U)) == 1,
"Assign(%s)" % i)
62 model.addCons(
quicksum(s[i] * x[i, j]
for i
in range(n)) <= B * y[j],
"Capac(%s)" % j)
67 model.addCons(x[i, j] <= y[j],
"Strong(%s,%s)" % (i, j))
70 for j
in range(U - 1):
71 model.addCons(y[j] >= y[j + 1],
"TieBrk(%s)" % j)
75 model.addConsSOS1([x[i, j]
for j
in range(U)])
77 model.setObjective(
quicksum(y[j]
for j
in range(U)),
"minimize")
84 """solveBinPacking: use an IP model to solve the in Packing Problem.
87 - s: list with item widths
90 Returns a solution: list of lists, each of which with the items in a roll.
99 bins = [[]
for i
in range(U)]
101 if model.getVal(x[i, j]) > .5:
103 for i
in range(bins.count([])):
115 """DiscreteUniform: create random, uniform instance for the bin packing problem."""
119 s[i] = random.randint(LB, UB)
123 if __name__ ==
"__main__":
127 print(
"bin size:", B)
130 print(
"\n\n\n FFD heuristic:")
133 print(len(ffd),
"bins")
135 print(
"\n\n\n IP formulation:")
139 print(len(bins),
"bins")