You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

篮球模拟赛调度算法问题:球队比赛场次未达预期

篮球模拟游戏赛事调度系统问题

我正在开发一款篮球模拟游戏,需要一套赛事调度系统。当前实现的调度逻辑虽可运行,但无法让所有球队达到预设的比赛场次目标:

  • 每支球队需完成8场非联盟常规赛(Reg)和12场联盟内比赛(Conf),总计19-20场;
  • 要求球队每周仅赛1场,尽量避免重复对阵(除非无其他选择)。

目前球队仅能完成15-19场比赛,尝试多种调整仍无法达成预期。以下是我的Python实现代码:

import random
from typing import List, Dict, Tuple

class Team:
    def __init__(self, name: str, conference: str) -> None:
        self.name = name
        self.conference = conference

        self.opponents: List[str] = []
        self.reg_games_played: int = 0
        self.conf_games_played: int = 0

    def has_played(self, opponent: 'Team') -> bool:
        return opponent.name in self.opponents

    def __repr__(self) -> str:
        return f"<Team name={self.name!r}>"
    
class Schedule:
    def __init__(self, teams: Dict[str, Team]) -> None:
        self.teams = teams

    def generate_week(self) -> List[Tuple[Team, Team]]:
        games: List[Tuple[Team, Team]] = []
        available_teams = list(self.teams.values())

        while len(available_teams) > 1:
            team1 = available_teams.pop(random.randrange(len(available_teams)))

            # Remove teams that don't meet team1's game total needs
            eligible_opps = [
                x for x in available_teams 
                if (
                    (x.reg_games_played < 8 and x.conf_games_played < 12) 
                    and (
                        (team1.conference == x.conference and team1.conf_games_played < 12) 
                        or (team1.conference != x.conference and team1.reg_games_played < 8)
                    )
                )
            ]
            opps = [x for x in eligible_opps if not team1.has_played(x)]

            # If no possible opponents, refill opponents with all eligible opponents
            if not opps:
                opps = eligible_opps

            # Removes previous opponent (if any) so teams don't play the same team back-to-back
            if len(opps) > 1 and team1.opponents and (previous_opp := next((x for x in opps if x.name == team1.opponents[-1]), None)):
                opps.remove(previous_opp)

            if not opps:
                continue

            team2 = random.choice(opps)
            available_teams.remove(team2)

            if team1.conference == team2.conference:
                team1.conf_games_played += 1
                team2.conf_games_played += 1
            else:
                team1.reg_games_played += 1
                team2.reg_games_played += 1

            team1.opponents.append(team2.name)
            team2.opponents.append(team1.name)

            games.append((team1, team2))

        return games

conferences = {
    'AAC': ['Charlotte', 'East Carolina', 'FAU', 'Memphis', 'North Texas', 'Rice', 'SMU', 'South Florida', 'Temple', 'Tulane', 'Tulsa', 'UAB', 'UTSA', 'Wichita St.'],
    'ACC': ['Boston College', 'Clemson', 'Duke', 'Florida St.', 'Georgia Tech', 'Louisville', 'Miami', 'NC State', 'North Carolina', 'Notre Dame', 'Pittsburgh', 'Syracuse', 'Virginia', 'Virginia Tech', 'Wake Forest'],
}

teams = {team: Team(team, conf) for (conf, teams) in conferences.items() for team in teams}
schedule = Schedule(teams)

for i in range(1, 21):
    print(f'Week {i}')
    print('-')
    for team1, team2 in schedule.generate_week():
        print(f"{'C' if team1.conference == team2.conference else 'R'} {team1.name:>20} vs {team2.name}")
    print()

for team in teams.values():
    print(f"{team.name:>20}: {team.reg_games_played} Reg, {team.conf_games_played} Conf, {team.reg_games_played + team.conf_games_played} Total")

内容的提问来源于stack exchange,提问作者Wrath

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 14:50:06