Please help in solving the following problem:
"Given a collection of labelled balls and labelled boxes, each ball must be placed in one of the boxes. However balls are restricted to which boxes they can be placed in, and the objective is to distribute the balls among the boxes as evenly as possible."
For example we have 3 boxes and 12 balls.
Rules are the following:
ball1 can only be placed in subset of boxes [box1, box2, box3]
ball2 can only be placed in [box1]
ball3 can only be placed in [box1]
ball4 can only be placed in subset of boxes [box1, box2]
ball5 can only be placed in subset of boxes [box1, box2]
ball6 can only be placed in subset of boxes [box1, box3]
ball7 can only be placed in [box2]
ball8 can only be placed in [box2]
ball9 can only be placed in subset of boxes [box2, box3]
ball10 can only be placed in [box3]
ball11 can only be placed in [box3]
ball12 can only be placed in [box1]
A given ball is of count 1 and can be put only in 1 box (it cannot be placed in 2 or more boxes at the same time).
So that balls can be natively spread between boxes in the following way:
box1 {ball2, ball3, ball5, ball12}
box2 {ball4, ball7, ball8, ball9}
box3 {ball1, ball6, ball10, ball11}
Could you please point me to possible approaches that could help in solving such type of a problem?
I checked the similar questions and in most cases number of possible distributions has to be discovered. But for this particular problem the task is to minimize the difference of the number of balls among the boxes. For now that I found is this paper that is close to the problem: https://qspace.qu.edu.qa/bitstream/handle/10576/8020/06-02-15-0012-fulltext.pdf?sequence=8 Still having some difficulties in implementing fair distribution according to proposed formulas.
Thanks.

You can solve the problem via integer linear programming as follows. For ball $i$, let $J_i$ be the set of allowed boxes. Let binary decision variable $x_{i,j}$ indicate whether ball $i$ is assigned to box $j$. Let $y_\min$ and $y_\max$ be the smallest and largest number of balls in a box, respectively. The problem is to minimize $y_\max-y_\min$ subject to
\begin{align} \sum_{j\in J_i} x_{i,j} &= 1 &&\text{for all $i$} \tag1 \\ \sum_{i:j\in J_i} x_{i,j} &\ge y_\min &&\text{for all $j$} \tag2 \\ \sum_{i:j\in J_i} x_{i,j} &\le y_\max &&\text{for all $j$} \tag3 \end{align} Constraint $(1)$ assigns each ball to exactly one box. Constraints $(2)$ and $(3)$, together with the objective, enforce the definitions of $y_\min$ and $y_\max$.
For your example, an optimal solution $x_{i,j}$ is as follows, with $y_\min=y_\max=4$ balls in each box: