Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在一定的约束条件下,随机安排循环赛的赛程_Python_Python 3.x_Algorithm_Random_Schedule - Fatal编程技术网

Python 在一定的约束条件下,随机安排循环赛的赛程

Python 在一定的约束条件下,随机安排循环赛的赛程,python,python-3.x,algorithm,random,schedule,Python,Python 3.x,Algorithm,Random,Schedule,我正试图写一个脚本来随机安排一个锦标赛的循环赛时间表 制约因素包括: 8队 两队面对面两次,一次在主场,一次在客场 14周,每队每周一场比赛 我的代码在理论上运行得很好,但当它生成时,有时会在某个星期冻结,因为该星期只剩下两支球队,而且两种可能的游戏都已经玩过了。我使用numpy数组来检查已经进行了哪些比赛 目前,我的代码如下所示: import random import numpy regular_season_games = 14 regular_season_week = 0 c

我正试图写一个脚本来随机安排一个锦标赛的循环赛时间表

制约因素包括:

  • 8队
  • 两队面对面两次,一次在主场,一次在客场
  • 14周,每队每周一场比赛
我的代码在理论上运行得很好,但当它生成时,有时会在某个星期冻结,因为该星期只剩下两支球队,而且两种可能的游戏都已经玩过了。我使用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 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

    if not teams_left:
        print ("Week " + str(regular_season_week+2))
        teams_left = list(range(8))
        regular_season_week = regular_season_week + 1
随机导入
进口numpy
常规赛比赛=14场
常规季周=0
checker=numpy.full((8,8),0)
对于范围(0,8)内的x:
棋盘格[x][x]=1
团队左=列表(范围(8))
打印(“周”+str(常规季+周+1))
而(常规赛周<常规赛比赛):
博弈集=False
离开团队=错误
而get_away_team==False:
团队A=随机选择(团队左)
如果检查器[:,团队A]中为0:
对于范围(0,8)内的x:
如果检查器[x][Team_A]==0且Team_中的x离开:
团队左。移除(团队A)
离开团队=正确
打破
当game_set==False时:
团队B=随机选择(团队左)
如果检查人[Team_B][Team_A]==0:
团队左。移除(团队B)
打印(str(A队)+“vs”+str(B队))
检查人[团队B][团队A]=1
游戏集=真
如果没有离开:
打印(“周”+str(常规季+周+2))
团队左=列表(范围(8))
常规季周=常规季周+1

为了实现这一点,我使用了来自的调度算法的自适应。基本上,我们生成一个团队列表-
列表(范围(8))
,并选择作为我们的初始匹配
0对4,1对5,2对6,3对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[4]] + team_list[0:3] + team_list[5:8] + [team_list[3]]
    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):
    if len(set(game_list)) != len(game_list):
        return False
    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]

        # 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 
print(checkValid(games_played))
Week 1
0 vs 7
4 vs 3
6 vs 1
5 vs 2
Week 2
0 vs 3
7 vs 1
4 vs 2
6 vs 5
Week 3
0 vs 1
3 vs 2
7 vs 5
4 vs 6
Week 4
0 vs 2
1 vs 5
3 vs 6
7 vs 4
Week 5
0 vs 5
2 vs 6
1 vs 4
3 vs 7
Week 6
0 vs 6
5 vs 4
2 vs 7
1 vs 3
Week 7
0 vs 4
6 vs 7
5 vs 3
2 vs 1
Week 8
7 vs 0
3 vs 4
1 vs 6
2 vs 5
Week 9
3 vs 0
1 vs 7
2 vs 4
5 vs 6
Week 10
1 vs 0
2 vs 3
5 vs 7
6 vs 4
Week 11
2 vs 0
5 vs 1
6 vs 3
4 vs 7
Week 12
5 vs 0
6 vs 2
4 vs 1
7 vs 3
Week 13
6 vs 0
4 vs 5
7 vs 2
3 vs 1
Week 14
4 vs 0
7 vs 6
3 vs 5
1 vs 2
True