Optimal way to group items of varying quantities into sets of 5, with no duplicates in a set?

56 Views Asked by At

Let’s say you are a trading card manufacturer and you have an inventory of cards that you want to divide into packs of 5 cards each, with no duplicates in a single pack.

There are 17 different types of cards (A through Q), and some are more rare than others.

You have the following inventory:

  • 25 of A
  • 50 of B
  • 50 of C
  • 50 of D
  • 50 of E
  • 100 of F
  • 100 of G
  • 100 of H
  • 100 of I
  • 200 of J
  • 200 of K
  • 200 of L
  • 200 of M
  • 400 of N
  • 400 of O
  • 400 of P
  • 400 of Q

This gives a total of 3025 cards, which is divisible by 5 but may not be when we impose the restriction of having no duplicates per pack.

Ideally, there would be some variety to the assortments, so packs would not contain simply A through E cards, or M through Q. There should be some randomness to them, or at least enough variation to seem random.

What is the cleanest way to approach this?

I have tried using a SQL database and assigning each individual card a random ID, then sorting by that ID and grabbing the first 5 rows with distinct types. This worked fairly well, but I was left with 15 cards of type L and 3 of type C, which was not ideal.

2

There are 2 best solutions below

4
On BEST ANSWER

One approach is to use integer linear programming to minimize the maximum number of times that a particular pack is used. There are $\binom{17}{5}+1=6189$ nonnegative integer decision variables and $\binom{17}{5}+17=6205$ constraints.

Explicitly, let $C$ be the set of cards ($|C|=17$), let $a_c$ be the inventory of card $c\in C$, let $P$ be the set of packs ($|P|=\binom{17}{5}=6188$), let $C_p$ be the set of (five) cards in pack $p\in P$, let nonnegative integer decision variable $x_p$ be the number of times that pack $p$ is used, and let nonnegative integer decision variable $z$ represent $\max_p x_p$. The problem is to minimize $z$ subject to \begin{align} \sum_{p\in P: c \in C_p} x_p &= a_c &&\text{for $c\in C$} \tag1 \\ z &\ge x_p &&\text{for $p\in P$} \tag2 \end{align} Constraint $(1)$ assigns the full inventory of cards to packs. Constraint $(2)$ enforces the minimax objective.

The minimum $z$ turns out to be $2$, attained by the following set of packs, where the fifth column shows $x_p$:

A B C F J 1 
A J N O Q 2 
A J N P Q 2 
A J O P Q 2 
A K N O P 2 
A K N P Q 2 
A K O P Q 2 
A L N O P 2 
A L O P Q 2 
A M N O P 2 
A M N O Q 2 
A M O P Q 2 
A N O P Q 2 
B C G N O 1 
B D N O P 2 
B F N O P 2 
B G K N Q 2 
B G L O Q 2 
B G N O P 2 
B G N P Q 2 
B H N O Q 2 
B H N P Q 2 
B H O P Q 2 
B I N O P 2 
B I N O Q 2 
B I N P Q 2 
B J M N Q 1 
B J M O Q 2 
B J N O P 2 
B J N O Q 2 
B J N P Q 2 
B J O P Q 2 
B K M P Q 1 
B K N O Q 2 
B L N O Q 2 
B L N P Q 2 
B L O P Q 2 
B M N O P 2 
B N O P Q 2 
C F N P Q 2 
C G N O P 2 
C G N P Q 2 
C H N P Q 2 
C I N O P 2 
C J K N P 2 
C J K N Q 2 
C J L N Q 2 
C J M O P 2 
C J N O P 2 
C J N O Q 2 
C J O P Q 2 
C K L N P 2 
C K M P Q 2 
C K N O Q 2 
C K N P Q 2 
C L M O P 2 
C L N O P 2 
C L N P Q 2 
C L O P Q 2 
C M N O P 2 
C M N O Q 2 
C M O P Q 2 
C N O P Q 2 
D F N P Q 2 
D F O P Q 2 
D G N O P 2 
D G O P Q 2 
D H N P Q 2 
D H O P Q 2 
D I N P Q 2 
D I O P Q 2 
D J L N O 2 
D J L N P 2 
D J N P Q 2 
D J O P Q 2 
D K N O P 2 
D K N O Q 2 
D K N P Q 2 
D K O P Q 2 
D L N O Q 2 
D L N P Q 2 
D L O P Q 2 
D M N O P 2 
D M N O Q 2 
D M N P Q 2 
D M O P Q 2 
D N O P Q 2 
E F N O Q 2 
E F N P Q 2 
E F O P Q 2 
E G N O Q 2 
E G O P Q 2 
E H N O Q 2 
E I N O Q 2 
E I O P Q 2 
E J K P Q 2 
E J M O P 2 
E J N O P 2 
E J N O Q 2 
E J N P Q 2 
E J O P Q 2 
E K M N P 2 
E K N O P 2 
E K N O Q 2 
E K N P Q 2 
E L N O Q 2 
E L N P Q 2 
E M N O P 2 
E M N O Q 2 
E M N P Q 2 
E M O P Q 2 
E N O P Q 2 
F G N O P 2 
F G N O Q 2 
F G N P Q 2 
F G O P Q 2 
F H N O P 2 
F H N P Q 2 
F H O P Q 2 
F I M N O 1 
F J K O Q 2 
F J L N O 2 
F J L N Q 2 
F J L O P 2 
F J L O Q 2 
F J L P Q 2 
F J M N O 2 
F J M N P 2 
F J M N Q 2 
F J M O P 2 
F J N O P 2 
F J N O Q 2 
F J N P Q 2 
F J O P Q 2 
F K L N O 2 
F K L N P 2 
F K L O P 2 
F K M N Q 2 
F K M P Q 2 
F K N O P 2 
F K N O Q 2 
F K N P Q 2 
F L M N Q 2 
F L M O P 2 
F L M O Q 2 
F L M P Q 2 
F L N O P 2 
F L N O Q 2 
F L N P Q 2 
F L O P Q 2 
F M N O P 2 
F M N O Q 2 
F M N P Q 2 
F M O P Q 2 
F N O P Q 2 
G H K O Q 1 
G H N O P 2 
G H N P Q 2 
G I N O Q 2 
G I N P Q 2 
G I O P Q 2 
G J K N O 2 
G J K N Q 2 
G J K P Q 2 
G J L N Q 2 
G J M N P 2 
G J M N Q 2 
G J M O Q 2 
G J N O Q 2 
G J O P Q 2 
G K L N O 2 
G K L N P 2 
G K L N Q 2 
G K L O P 2 
G K M N Q 2 
G K M O P 2 
G K M O Q 2 
G K N O P 2 
G K N O Q 2 
G K N P Q 2 
G K O P Q 2 
G L M N O 2 
G L M N P 2 
G L N O P 2 
G L N O Q 2 
G L N P Q 2 
G L O P Q 2 
G M N O P 2 
G M N O Q 2 
G M N P Q 2 
G N O P Q 2 
H I K N O 1 
H I L N O 2 
H I N O Q 2 
H I O P Q 2 
H J K M Q 1 
H J K N O 2 
H J K N P 2 
H J K O P 2 
H J K O Q 2 
H J L O P 2 
H J L O Q 2 
H J M N P 2 
H J M O P 2 
H J M O Q 2 
H J N O P 2 
H J N O Q 2 
H J N P Q 2 
H J O P Q 2 
H K L N P 2 
H K L N Q 2 
H K L O P 2 
H K M N O 2 
H K M N P 2 
H K M N Q 2 
H K M O Q 2 
H K N O P 2 
H K N O Q 2 
H K N P Q 1 
H L M N P 2 
H L M O P 2 
H L M O Q 2 
H L M P Q 2 
H L N O P 2 
H L N O Q 2 
H L N P Q 2 
H L O P Q 2 
H M N O P 2 
H M N P Q 2 
H M O P Q 2 
I J K N O 2 
I J K N Q 2 
I J K O P 2 
I J K O Q 2 
I J K P Q 2 
I J L N P 2 
I J L O P 2 
I J L P Q 2 
I J M N O 2 
I J M O P 2 
I J M O Q 2 
I J N P Q 2 
I J O P Q 2 
I K L N Q 2 
I K L O P 2 
I K L O Q 2 
I K L P Q 2 
I K M N O 2 
I K M N P 2 
I K M O P 2 
I K M O Q 2 
I K N O Q 2 
I K N P Q 2 
I K O P Q 2 
I L M N O 2 
I L M O Q 2 
I L M P Q 2 
I L N O P 2 
I L N O Q 2 
I L N P Q 2 
I L O P Q 2 
I M N O P 2 
I M N O Q 2 
I M N P Q 2 
I M O P Q 2 
J K L N O 2 
J K L N P 2 
J K L N Q 2 
J K L O P 2 
J K L P Q 2 
J K M N O 2 
J K M N P 2 
J K M O P 1 
J K M O Q 2 
J K N O P 2 
J K N O Q 2 
J K N P Q 2 
J K O P Q 2 
J L M N O 2 
J L M N P 2 
J L M N Q 2 
J L M O Q 2 
J L M P Q 2 
J L N O P 2 
J L N P Q 2 
J L O P Q 2 
J M N O P 2 
J M N O Q 2 
J M N P Q 2 
J M O P Q 2 
K L M N O 2 
K L M N P 2 
K L M N Q 2 
K L M O P 2 
K L M P Q 2 
K L N O P 2 
K L N O Q 2 
K L N P Q 2 
K L O P Q 2 
K M N O Q 2 
K M N P Q 2 
K M O P Q 2 
K N O P Q 2 
L M N O P 2 
L M N O Q 2 
L M O P Q 2 
L N O P Q 2 
M N O P Q 1 
1
On

A very non-random way would be:

  • Make 400 packs with N, O, P, Q; fill 200 with M and 200 with L
  • Make 100 packs with J,K,I,H and 100 with J,K,F,G; fill 50 each with B or C or D or E.

Unfortunately, this leaves us with the 25 A. Pick five of the packs without A and replace the $i$th Card of the $i$th pack with A. The five replaced cards can form another pack. Repeat this until all A are placed. Done. However, „more random/mixed“ would probably be nicer.

Alternative approach:

Take 35 from each of N,O,P,Q,J,K,L,M. For each of the ${8\choose 5}=56$ subsets, create a pack. Repeat 5 times. This leaves you with 225 N-Q, 100 F-I, 50 B-E, 25 A and J-M. And we created 280 packs.

Take 35 from each of N,O,P,Q,F,G,H,I. Proceed as above, but repeat only twice. This leaves you with 155 N-Q, 50 B-E, 30 F-I, 25 A, J-M. And we created another 112 packs, 233 to go.

Take 35 from each of N,I,P,Q,B,C,D,E and proceed as above (without repetition). This leaves you with 120 N-Q, 30 F-I, 25 A, J-M, 15 B-E.

Take 20 of N,O,P,Q and create 20 packs by filling five with F, five with G, five with H, five with I. Now we have 100 N-Q, nine stacks of 25, four stacks of 15.

Take 45 of N,O,P,Q and create 45 packs, five each for each stack of size 25. Now we have 55 N-Q, nine stacks of 20, four stacks of 15.

Finish: Now keep creating new packs by always picking from five of the highest heaps (but otherwise randomly chosen). After 36 rounds, this will reduce N-Q to 19, nine other stacks to 16, the rest stays at 15. After another five rounds, all 17 stacks have size 15. From now on, stack sizes will never differ by more than 1, which guarantees that the last five items will be on five different stacks.

We could have started the „finish“ method earlier, but the „mix“ may be more appealing this way.