给定某些限制的随机循环联赛时间表

问题描述 投票:1回答:1

我正在尝试编写python脚本来随机安排我的模拟同盟时间表。

约束是:

  • 8个团队
  • 团队面对面两次,一次在家,一次离开
  • 14周,每队每周一场比赛

我的代码在理论上可以正常工作,但是当它生成时,有时在某些星期冻结,因为那一周只剩下两支球队,并且已经进行了两种可能的游戏。我使用矩阵检查玩过哪些游戏。

此刻我的代码如下:

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 game_set == False:
        Team_B = random.choice(teams_left)
        if checker[Team_B][Team_A] == 0:
            teams_left.remove(Team_B)
            print(str(Team_A) + " vs " + str(Team_B))
            checker[Team_B][Team_A] = 1
            game_set = True
            #print(checker)

    if not teams_left:
        print ("Week " + str(regular_season_week+2))
        teams_left = list(range(8))
        regular_season_week = regular_season_week + 1
python python-3.x schedule
1个回答
0
投票

我使用了here中的调度算法的一种改编来实现这一目标。基本上,我们生成球队列表-list(range(8))-并选择作为我们的初始比赛0 vs 4, 1 vs 5, 2 vs 6, 3 vs 7。然后,我们旋转列表(不包括第一个元素),然后选择作为下一个对位0 vs 3, 7 vs 4, 1 vs 5, 2 vs 6。我们以下面的方式继续进行,直到我们得到每一个配对。

我已经为主场与客场比赛添加了处理程序-如果已经进行过配对,我们将进行相反的主场/客场配对;如果没有进行配对,我们将随机选择。下面是代码,其中包括检查游戏列表是否有效的功能以及示例输出。

代码:

import random

# Generator function for list of matchups from a team_list
def games_from_list(team_list):
    for i in range(4):
        yield team_list[i], team_list[i+4]

# Function to apply rotation to list of teams as described in article
def rotate_list(team_list):
    team_list = [team_list[-1]] + team_list[0:-1]
    team_list[0], team_list[1] = team_list[1], team_list[0]
    return team_list

# Function to check if a list of games is valid
def checkValid(game_list):
    for week in range(14):
        teams = set()
        this_week_games = game_list[week*4:week*4 + 4]
        for game in this_week_games:
            teams.add(game[0])
            teams.add(game[1])
        if len(teams) < 8:
            return False
    else:
        return True


# Generate list of teams & empty list of games played
teams = list(range(8))
games_played = []

# Optionally shuffle teams before generating schedule
random.shuffle(teams)

# For each week -
for week in range(14):
    print(f"Week {week + 1}")

    # Get all the pairs of games from the list of teams.
    for pair in games_from_list(teams):
        # If the matchup has already been played:
        if pair in games_played:
            # Play the opposite match
            pair = pair[::-1]

        # If the opposite match has also not already been played:
        elif pair[-1::] not in games_played:
            # Randomly pick one of the pairings to play. 
            pair = random.choice([pair, pair[::-1]])

        # Print the matchup and append to list of games.
        print(f"{pair[0]} vs {pair[1]}")
        games_played.append(pair)

    # Rotate the list of teams
    teams = rotate_list(teams)

# Checks that the list of games is valid - should always be True.
print(checkValid(games_played))

样本输出:

Week 1
4 vs 2
5 vs 3
1 vs 7
0 vs 6
Week 2
4 vs 6
0 vs 2
5 vs 3
1 vs 7
Week 3
7 vs 4
6 vs 1
2 vs 0
3 vs 5
Week 4
3 vs 4
7 vs 5
6 vs 1
2 vs 0
Week 5
4 vs 0
3 vs 2
7 vs 5
6 vs 1
Week 6
1 vs 4
6 vs 0
2 vs 3
5 vs 7
Week 7
4 vs 5
7 vs 1
0 vs 6
3 vs 2
Week 8
2 vs 4
5 vs 3
1 vs 7
0 vs 6
Week 9
6 vs 4
2 vs 0
5 vs 3
1 vs 7
Week 10
4 vs 7
1 vs 6
2 vs 0
5 vs 3
Week 11
3 vs 4
7 vs 5
6 vs 1
2 vs 0
Week 12
0 vs 4
3 vs 2
7 vs 5
6 vs 1
Week 13
1 vs 4
0 vs 6
3 vs 2
7 vs 5
Week 14
5 vs 4
1 vs 7
0 vs 6
3 vs 2
True
© www.soinside.com 2019 - 2024. All rights reserved.