我正在尝试编写一个脚本来随机化锦标赛的循环赛时间表。
限制条件是:
我的代码理论上运行良好,但是当它生成时,它有时会在某些周冻结,因为该周只剩下两支球队,并且两场可能的比赛都已经打完。我使用 numpy 数组来检查已经进行了哪些比赛。
目前我的代码如下所示:
import random
import numpy
regular_season_games = 14
regular_season_week = 0
checker = numpy.full((8,8), 0)
for x in range (0,8):
checker[x][x] = 1
teams_left = list(range(8))
print ("Week " + str(regular_season_week+1))
while (regular_season_week < regular_season_games):
game_set = False
get_away_team = False
while get_away_team == False:
Team_A = random.choice(teams_left)
if 0 in checker[:,Team_A]:
for x in range (0,8):
if checker[x][Team_A] == 0 and x in teams_left:
teams_left.remove(Team_A)
get_away_team = True
break
while …Run Code Online (Sandbox Code Playgroud) python random algorithm python-3.x sports-league-scheduling-problem