The source code and dockerfile for the GSW2024 AI Lab.
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

111 lines
3.1 KiB

2 months ago
  1. /* min01ks.mod - finding minimal equivalent 0-1 knapsack inequality */
  2. /* Written in GNU MathProg by Andrew Makhorin <mao@gnu.org> */
  3. /* It is obvious that for a given 0-1 knapsack inequality
  4. a[1] x[1] + ... + a[n] x[n] <= b, x[j] in {0, 1} (1)
  5. there exist infinitely many equivalent inequalities with exactly the
  6. same feasible solutions.
  7. Given a[j]'s and b this model allows to find an inequality
  8. alfa[1] x[1] + ... + alfa[n] x[n] <= beta, x[j] in {0, 1}, (2)
  9. which is equivalent to (1) and where alfa[j]'s and beta are smallest
  10. non-negative integers.
  11. This model has the following formulation:
  12. minimize
  13. z = |alfa[1]| + ... + |alfa[n]| + |beta| = (3)
  14. = alfa[1] + ... + alfa[n] + beta
  15. subject to
  16. alfa[1] x[1] + ... + alfa[n] x[n] <= beta (4)
  17. for all x satisfying to (1)
  18. alfa[1] x[1] + ... + alfa[n] x[n] >= beta + 1 (5)
  19. for all x not satisfying to (1)
  20. alfa[1], ..., alfa[n], beta are non-negative integers.
  21. Note that this model has n+1 variables and 2^n constraints.
  22. It is interesting, as noticed in [1] and explained in [2], that
  23. in most cases LP relaxation of the MIP formulation above has integer
  24. optimal solution.
  25. References
  26. 1. G.H.Bradley, P.L.Hammer, L.Wolsey, "Coefficient Reduction for
  27. Inequalities in 0-1 Variables", Math.Prog.7 (1974), 263-282.
  28. 2. G.J.Koehler, "A Study on Coefficient Reduction of Binary Knapsack
  29. Inequalities", University of Florida, 2001. */
  30. param n, integer, > 0;
  31. /* number of variables in the knapsack inequality */
  32. set N := 1..n;
  33. /* set of knapsack items */
  34. /* all binary n-vectors are numbered by 0, 1, ..., 2^n-1, where vector
  35. 0 is 00...00, vector 1 is 00...01, etc. */
  36. set U := 0..2^n-1;
  37. /* set of numbers of all binary n-vectors */
  38. param x{i in U, j in N}, binary, := (i div 2^(j-1)) mod 2;
  39. /* x[i,j] is j-th component of i-th binary n-vector */
  40. param a{j in N}, >= 0;
  41. /* original coefficients */
  42. param b, >= 0;
  43. /* original right-hand side */
  44. set D := setof{i in U: sum{j in N} a[j] * x[i,j] <= b} i;
  45. /* set of numbers of binary n-vectors, which (vectors) are feasible,
  46. i.e. satisfy to the original knapsack inequality (1) */
  47. var alfa{j in N}, integer, >= 0;
  48. /* coefficients to be found */
  49. var beta, integer, >= 0;
  50. /* right-hand side to be found */
  51. minimize z: sum{j in N} alfa[j] + beta; /* (3) */
  52. phi{i in D}: sum{j in N} alfa[j] * x[i,j] <= beta; /* (4) */
  53. psi{i in U diff D}: sum{j in N} alfa[j] * x[i,j] >= beta + 1; /* (5) */
  54. solve;
  55. printf "\nOriginal 0-1 knapsack inequality:\n";
  56. for {j in 1..n} printf (if j = 1 then "" else " + ") & "%g x%d",
  57. a[j], j;
  58. printf " <= %g\n", b;
  59. printf "\nMinimized equivalent inequality:\n";
  60. for {j in 1..n} printf (if j = 1 then "" else " + ") & "%g x%d",
  61. alfa[j], j;
  62. printf " <= %g\n\n", beta;
  63. data;
  64. /* These data correspond to the very first example from [1]. */
  65. param n := 8;
  66. param a := [1]65, [2]64, [3]41, [4]22, [5]13, [6]12, [7]8, [8]2;
  67. param b := 80;
  68. end;