jeudi 19 février 2015

How to create random combination with certain rules

This might be a bit of a math problem but I need to do it programatically and that is what I struggle with. So here is the problem:



  • I have sport categories (each with arbitrary number of competitors) with times they start at (they may start at the same time or at different times arbitrarily). I.e. Category A starts at 10:00, B at 10:00, C at 10:30 etc.

  • I need to randomly reorder each categories' competitors and then assign each competitor a start time in this new order in the interval of 5 minutes starting from the category start. I.e. Competitor1 in category A at 10:00, Competitor2 in Category A at 10:05, Competitor1 in category B at 10:00, Competitor1 in category C at 10:30 etc.

  • Each competitor has a country and there is a rule that there must NOT be two competitors from the same country that start at the same time (regardless of category). If that would happen a person must switch places inside their category with someone else so that it does not happen.


I am struggling in creating an algorithm that would give me the solution if it exists AND would exit when there is no solution. So far I managed to randomly reorder the categories and give them start times. But when applying the rules to this result and reordering elements in various fashion I either get into the infinite loop or end up with errors in the result (two people from the same country starting at the same time).





Aucun commentaire:

Enregistrer un commentaire