You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.2 KiB

  1. /* BPP, Bin Packing Problem */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* Given a set of items I = {1,...,m} with weight w[i] > 0, the Bin
  4. Packing Problem (BPP) is to pack the items into bins of capacity c
  5. in such a way that the number of bins used is minimal. */
  6. param m, integer, > 0;
  7. /* number of items */
  8. set I := 1..m;
  9. /* set of items */
  10. param w{i in 1..m}, > 0;
  11. /* w[i] is weight of item i */
  12. param c, > 0;
  13. /* bin capacity */
  14. /* We need to estimate an upper bound of the number of bins sufficient
  15. to contain all items. The number of items m can be used, however, it
  16. is not a good idea. To obtain a more suitable estimation an easy
  17. heuristic is used: we put items into a bin while it is possible, and
  18. if the bin is full, we use another bin. The number of bins used in
  19. this way gives us a more appropriate estimation. */
  20. param z{i in I, j in 1..m} :=
  21. /* z[i,j] = 1 if item i is in bin j, otherwise z[i,j] = 0 */
  22. if i = 1 and j = 1 then 1
  23. /* put item 1 into bin 1 */
  24. else if exists{jj in 1..j-1} z[i,jj] then 0
  25. /* if item i is already in some bin, do not put it into bin j */
  26. else if sum{ii in 1..i-1} w[ii] * z[ii,j] + w[i] > c then 0
  27. /* if item i does not fit into bin j, do not put it into bin j */
  28. else 1;
  29. /* otherwise put item i into bin j */
  30. check{i in I}: sum{j in 1..m} z[i,j] = 1;
  31. /* each item must be exactly in one bin */
  32. check{j in 1..m}: sum{i in I} w[i] * z[i,j] <= c;
  33. /* no bin must be overflowed */
  34. param n := sum{j in 1..m} if exists{i in I} z[i,j] then 1;
  35. /* determine the number of bins used by the heuristic; obviously it is
  36. an upper bound of the optimal solution */
  37. display n;
  38. set J := 1..n;
  39. /* set of bins */
  40. var x{i in I, j in J}, binary;
  41. /* x[i,j] = 1 means item i is in bin j */
  42. var used{j in J}, binary;
  43. /* used[j] = 1 means bin j contains at least one item */
  44. s.t. one{i in I}: sum{j in J} x[i,j] = 1;
  45. /* each item must be exactly in one bin */
  46. s.t. lim{j in J}: sum{i in I} w[i] * x[i,j] <= c * used[j];
  47. /* if bin j is used, it must not be overflowed */
  48. minimize obj: sum{j in J} used[j];
  49. /* objective is to minimize the number of bins used */
  50. data;
  51. /* The optimal solution is 3 bins */
  52. param m := 6;
  53. param w := 1 50, 2 60, 3 30, 4 70, 5 50, 6 40;
  54. param c := 100;
  55. end;