Optimize the filling of M baskets that have an x count limit with n balls with basket preference

29 Views Asked by At

Think of this problem as an optimization problem. The platform has $M$ Baskets. Each Basket can hold up to $X$ items in it. This exercise is to fill all Baskets with as many balls as possible... But here's the caveat: the balls have predetermined preference for all baskets.

For example - say we have two baskets that fit two balls.Also we have five balls. The first three balls have a preference of $\{1,2\}$ and the two last balls have $\{2,1\}$. Iterating randomly we can place in basket: \begin{split} 1 \{\text{ball } 1&,\text{ball }2\}\\ 2 \{\text{ball }4&,\text{ball }5\} \end{split} and ball $3$ is out of luck and remains in the inventory.

I hope I articulated this problem good enough for your help. The goal is to program this to a set-based solution (SQL server) but I would like to pin down the mathematical logic first.