Combinations.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python2
  2. from MatchAnalysis import *
  3. import sys, TickBasedRandom
  4. def GetAllCombinations(inputList, groupsize):
  5. output = []
  6. indices = range(groupsize)
  7. while indices[0] < len(inputList) - groupsize + 1:
  8. output.append([inputList[a] for a in indices])
  9. indices[groupsize - 1] += 1
  10. for i in [ groupsize - a - 1 for a in range(groupsize) ]:
  11. if indices[i] >= len(inputList) - groupsize + i + 1:
  12. if i > 0:
  13. indices[i - 1] += 1
  14. indices[i] = indices[i - 1] + 1
  15. else:
  16. indices[i] += 1
  17. for j in [a + i + 1 for a in range(groupsize - i - 1) ]:
  18. indices[j] = indices[i] + j - i
  19. return output
  20. def GenerateBestMatches(teams, teamsPerMatch, matchCount, origTeamList):
  21. if teamsPerMatch * matchCount > len(teams):
  22. raise ValueError("Too many matches or teams per match requested.")
  23. if matchCount == 1 and teamsPerMatch * matchCount == len(teams):
  24. return [ teams ]
  25. combinations = GetAllCombinations(teams, teamsPerMatch)
  26. analysisDictionary = { tuple(a): [ SameRegionMatchCount(a), RepeatMatchCount(a), RepeatRegionMatchCount(a, origTeamList), TickBasedRandom.GetInteger(0, sys.maxsize) ] for a in combinations }
  27. combinations.sort(key=lambda a: analysisDictionary[tuple(a)])
  28. indices = []
  29. for team in teams:
  30. combinationsForTeam = list(combinations)
  31. indices = list(set(indices).union({ [ i for i in range(len(combinationsForTeam)) if len(set(combinationsForTeam[i]).union({team})) == teamsPerMatch + 1][0] }))
  32. matches = [ combinationsForTeam[list(indices).pop()] ]
  33. i = 0
  34. while len(matches) < matchCount:
  35. if not any([len(a) + teamsPerMatch > len(set(a).union(set(combinationsForTeam[i]))) for a in matches]):
  36. matches.append(combinationsForTeam[i])
  37. combinationsForTeam = list(combinations)
  38. indices = list(set(indices).union({i}))
  39. i = -1
  40. i += 1
  41. return sorted(sorted(sorted(sorted([b for b in GetAllCombinations([combinations[a] for a in indices], matchCount) if len(set().union(*[set(c) for c in b])) == len(teams)], key=lambda a: TickBasedRandom.GetInteger(0, sys.maxsize)), key=lambda a: sum([analysisDictionary[tuple(b)][2] for b in a])), key=lambda a: sum([analysisDictionary[tuple(b)][1] for b in a])), key=lambda a: sum([analysisDictionary[tuple(b)][0] for b in a]))[0]