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.

135 lines
5.5 KiB

  1. /* Covering code generator, especially for football pool systems */
  2. /* Written and converted to GNU MathProg by NASZVADI, Peter, 199x-2017
  3. <vuk@cs.elte.hu> */
  4. /*
  5. Looks up for minimal covering codes in the specified Hamming-space.
  6. Without specifying model data, by default it looks up for covering
  7. for a mixed covering code in Hamming-space {X, 1, 2, 3}*{X, 1}^4
  8. with one layer.
  9. Hamming space is a set of finite words with all the same length over
  10. a finite alphabet: the space could be decomposed to Cartesian
  11. products of subsets of the alphabet, e.g. the first letter of an
  12. element can be chosen from a 2-element set, the next from 6 letters,
  13. and so on.
  14. There is a natural metric function in these spaces: the
  15. Hamming-distance (hence the name, from now referred as: distance).
  16. The distance of two (equal-length) words is the number of different
  17. letter pairs in the corresponding positions.
  18. Covering Hamming-spaces with minimal number of spheres with given
  19. radius - usually difficult problem excluding special cases.
  20. Relationship with sports:
  21. Football pool system in Hungarian: "Toto'kulcs", so Toto, totogol and
  22. other football pool systems are usually need mixed ternary/binary
  23. code coverings in order to minimize loss of the gambler.
  24. See more at:
  25. https://en.wikipedia.org/wiki/Covering_code
  26. A tricky workaround is used:
  27. floor(), abs() and cosine() magic are used at 'coverings' constraints,
  28. because GMPL lacks proper boolean<->integer evaluation/casting.
  29. */
  30. param ArgNum1, >= 1, default 1;
  31. param ArgNum2, >= 1, default 1;
  32. param ArgNum3, >= 1, default 1;
  33. param ArgNum4, >= 1, default 1;
  34. param ArgNum5, >= 1, default 1;
  35. param ArgNum6, >= 1, default 1;
  36. param ArgNum7, >= 1, default 1;
  37. param ArgNum8, >= 1, default 1;
  38. param ArgNum9, >= 1, default 1;
  39. param ArgNum10, >= 1, default 1;
  40. param ArgNum11, >= 1, default 1;
  41. param ArgNum12, >= 1, default 1;
  42. param ArgNum13, >= 1, default 1;
  43. /* at most 13 matches' outcomes */
  44. param Radius, >= 1, default 1;
  45. /* covering radius */
  46. param Layer, >= 1, default 1;
  47. /* each point of space must be covered at least Layer times */
  48. set X := 0..ArgNum1 - 1 cross 0..ArgNum2 - 1 cross 0..ArgNum3 - 1 cross
  49. 0..ArgNum4 - 1 cross 0..ArgNum5 - 1 cross 0..ArgNum6 - 1 cross
  50. 0..ArgNum7 - 1 cross 0..ArgNum8 - 1 cross 0..ArgNum9 - 1 cross
  51. 0..ArgNum10 - 1 cross 0..ArgNum11 - 1 cross 0..ArgNum12 - 1 cross
  52. 0..ArgNum13 - 1;
  53. /* the Hamming-space generated by the Cartesian-products of sets
  54. with elements ArgNum[n] */
  55. var x{X}, integer, >=0;
  56. /* denotes each point's amount of containing covering sets */
  57. var objvalue;
  58. s.t. coverings{(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) in X}:
  59. sum{(j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13) in X:
  60. floor(abs(cos(i1 - j1))) + floor(abs(cos(i2 - j2))) +
  61. floor(abs(cos(i3 - j3))) + floor(abs(cos(i4 - j4))) +
  62. floor(abs(cos(i5 - j5))) + floor(abs(cos(i6 - j6))) +
  63. floor(abs(cos(i7 - j7))) + floor(abs(cos(i8 - j8))) +
  64. floor(abs(cos(i9 - j9))) + floor(abs(cos(i10 - j10))) +
  65. floor(abs(cos(i11 - j11))) + floor(abs(cos(i12 - j12))) +
  66. floor(abs(cos(i13 - j13))) >= 13 - Radius
  67. } x[j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13] >= Layer;
  68. /* covering constraints, select at least 'Layer' amount of spheres that cover
  69. (i1,i2,...) and has radius 'Radius' */
  70. s.t. oneisset: x[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] >= 1;
  71. /* this does not violate symmetry nor excludes important solutions but
  72. boosts the solving process */
  73. s.t. objc: sum{(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) in X}
  74. x[i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13] = objvalue;
  75. /* the total number of pools (covering sets) */
  76. minimize obj: objvalue;
  77. /* Also 'objc' could be used directly instead of 'obj', but for
  78. experiments, it is useful to set up additional constraints for
  79. introduced objvalue variable */
  80. solve;
  81. printf 'Solution: %s\nRadius: %s\nLayer: %s\n',
  82. objvalue.val, Radius, Layer;
  83. /* report important scalars */
  84. printf 'Selected bets:\n';
  85. for{(i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13) in X:
  86. x[i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13]}{
  87. printf ' Times %s:',
  88. x[i1, i2, i3, i4, i5, i6, i7, i8, i9, i10, i11, i12, i13].val;
  89. printf '%s', if ArgNum1 == 1 then '' else ' ' & if i1 then i1 else 'X';
  90. printf '%s', if ArgNum2 == 1 then '' else '-' & if i2 then i2 else 'X';
  91. printf '%s', if ArgNum3 == 1 then '' else '-' & if i3 then i3 else 'X';
  92. printf '%s', if ArgNum4 == 1 then '' else '-' & if i4 then i4 else 'X';
  93. printf '%s', if ArgNum5 == 1 then '' else '-' & if i5 then i5 else 'X';
  94. printf '%s', if ArgNum6 == 1 then '' else '-' & if i6 then i6 else 'X';
  95. printf '%s', if ArgNum7 == 1 then '' else '-' & if i7 then i7 else 'X';
  96. printf '%s', if ArgNum8 == 1 then '' else '-' & if i8 then i8 else 'X';
  97. printf '%s', if ArgNum9 == 1 then '' else '-' & if i9 then i9 else 'X';
  98. printf '%s', if ArgNum10 == 1 then '' else '-' & if i10 then i10 else 'X';
  99. printf '%s', if ArgNum11 == 1 then '' else '-' & if i11 then i11 else 'X';
  100. printf '%s', if ArgNum12 == 1 then '' else '-' & if i12 then i12 else 'X';
  101. printf '%s', if ArgNum13 == 1 then '' else '-' & if i13 then i13 else 'X';
  102. printf '\n';
  103. }
  104. /* pretty-print a generated football pool system (covering code) */
  105. data;
  106. param ArgNum1 := 4;
  107. param ArgNum2 := 2;
  108. param ArgNum3 := 2;
  109. param ArgNum4 := 2;
  110. param ArgNum5 := 2;
  111. end;