I would like for an activity to generate a table that meet certain conditions as know as:
- There are 16 groups
- There are 8 activities
- There are 8 rounds, during a round a team can only do one activity
- Every group need to do every activity
- Each activity must accommodate 2 group and no more
- We would like a group to never meet the same group again (so the people can see the maximum amount of other people :-) )
We tried to manually generate this in Excel but there are always some groups that see each other again.
We tried to manually "generate a list" but we always end we teams crossing each other, like team 7 and team 9 cross 3 time in this exemple : table generated so far
I though maybe a thre dimensional array could do , but it seems like an overkill to me and there is certainly some known way to handle those situations.
So, even though @p.phidot has already provided one solution, I went ahead and wrote a program that could solve it as well. I did this primarily because I have seen various versions of this question many times on StackOverflow, with few of them receiving a satisfactory answer.
The problem presented here is one version of the more general Sports League Scheduling problem, of which the Howell and Mitchell movements of Duplicate Bridge tournaments try to address different variants of this problem. It is in fact an obscure type Combinatorial Search problem that has had some work on mathematics and computer science papers over the years(see: 1, 2, and 3 which seems to be taking an approach similar to @p.phidot)
Although there are many variants, the base problem is to arrange ("schedule") some group of things ("teams") in pairs (a "game", "match" or "event") within a rectangular array of a scheduling time (a "round") and a unique resource (a "period", "field", or "boards" in Bridge, or "activity" in the question above). This has to be done within the following common minimum constraints:
Typically, the following two minimum constraints are also common (though some obscure variant problems may change these):
After these almost universal rules, there are a plethora of additional constraints which produce all of the many variants of this problem. The question asked above is what I call the "2x" problem which has the following additional constraints:
Numerically, this compels an additional rule that may also be treated as a pseudo-constraint for programming and logical inferencing purposes:
While it is know that some variants of the general problem are solvable for some sizes, and that others are unsolvable, for the most part, beyond obvious numerical conflicts (like too many teams/not enough periods) each variant is different enough that it is usually unclear which problems can be solved and which cannot. The exception to this is what I call the "Odd Rule", in that if any of the parameters (#Rounds, #Periods, #Teams) is odd, then it is usually solvable by simply rotating the teams and periods by different amounts each round. However, this doesn't work for all even numbers because with simple rotations, either the team's opponents or the periods will duplicate halfway through. This means that when all of the parameters are even, there's no simple combination of rotations that can be used and, if it can be solved it becomes more like a Sudoku puzzle.
So, I wrote this program to solve the version of the problem presented above, using a limited form of the Branch and Bound combinatorial search technique. The good news is that it can solve the specific problem asked by the OP (8 rounds, 8 periods, 16 teams), here is the answer that it gives:
(obviously, I have numbered everything from zero)
Now the bad news: Practically, it only works for up to 9 rounds. For 10 rounds and above it basically runs forever (I've let it run for hours without completing). So if someone wants to use it for larger problems, more optimization will need to be applied (Update: For 11 rounds it finished in about a day. For 12 rounds, my estimate is that it would take about a year). Keep in mind though, that odd numbers are always easy to solve manually, no matter how large.
So here's the main solver class (all code is in c#):
This code implements the brand-and-bound style recursion over a Schedule object that holds and tracks the scheduling assignments in a type of 4-dimensional (Rounds x Periods x Team1 vs Team2) sparse array "Hollow tabulating cube" structure that I find useful for solving types of 8-queens problems (i.e., one occurrence of an object in every dimension). The code for that class is here:
Finally, both of the classes above use
Assignment
objects that encapsulate two teams playing each other in a specific round and period:Below is some code from my form that illustrates how to use the solver class: