I am trying to solve the following problem of working teams rearrangement. I have 100 teams, each with a different fixed number of members. For each team I have a set of value that represent the number of people with a specific role needed to complete the team (e.g.: Team 1 needs 5 people with Role 1 and 8 with Role 2).
| Team | Role 1 | Role 2 | Role 3 | members |
|------------|------------|-----------|----------|-----------|
| 1 | 5 | 8 | 0 | 13 |
| 2 | 0 | 5 | 6 | 11 |
| 3 | 6 | 0 | 6 | 12 |
...
I also have 1500 workers, and for each worker I know its current team, its role and his skill levels
| Worker id | Role | Skill 1 | Skill 2 | Skill 3 |
|------------|------------|------------|-----------|----------|
| 1 | 1 | 7 | 7 | 5 |
| 2 | 2 | 4 | 8 | 0 |
| 3 | 2 | 6 | 8 | 3 |
| 4 | 3 | 6 | 0 | 6 |
| 5 | 3 | 9 | 0 | 0 |
...
What I would like to obtain is the optimal teams composition that minimize the average skill level difference between the teams, with the additional constraint of moving only workers with the same role between teams. I am calculating the average skill level as the average of all the skills of all of the team's workers. I need a little help to translate these constraints in a mathematical form, specifically the one about the "same role", so that I could use something like constraint programming (MiniZinc or such) to get the solution. Thanks in advance.