0

我即将创建一个用于足球比赛调度的算法。主要的调度规则(据我所知)是:

  1. 有偶数队
  2. 一个赛季包括2轮
  3. 在一个比赛日,每支球队只打一场比赛
  4. 上一个比赛日的主队应该是下一个比赛日的客队(如果可能的话-我查看了意甲赛程,并且排除了此规则-每支球队连续踢2次(主/客-第一轮/复仇回合)在一个赛季
  5. 有2轮比赛,规则如下:第一轮第一场比赛(比赛日1)A队与B队(A:B)比赛,所以复仇比赛B:A应该在- matchday进行allMatchDays/2 + i,其中i = 1

目前的做法:


  private static Map<Integer, List<Match>> createSchedule(/*Set<Team> allTeams*/) {

    Set <Team> allTeams = Set.of(

            Team.builder().name("A").build(),
            Team.builder().name("B").build(),
            Team.builder().name("C").build(),
            Team.builder().name("D").build(),
            Team.builder().name("E").build(),
            Team.builder().name("F").build(),
            Team.builder().name("G").build(),
            Team.builder().name("H").build(),
            Team.builder().name("I").build(),
            Team.builder().name("J").build()

    );

    int teamsNumber = allTeams.size();
    int matchDays = (teamsNumber - 1) * 2;
    int gamesNumberPerMatchDay = teamsNumber / 2;

    List<Match> allMatches = allTeams.stream()
            .map(team ->
                    allTeams.stream().filter(otherTeam -> !otherTeam.equals(team))
                            .map(otherTeam -> Match.builder().homeTeam(team)
                                    .awayTeam(otherTeam).build()).collect(Collectors.toList())
            )
            .flatMap(Collection::stream)
            .collect(Collectors.toList());

    Map<Integer, List<Match>> schedule = IntStream.rangeClosed(1, matchDays)
            .boxed()
            .collect(Collectors.toMap(
                    matchDay -> (Integer) matchDay,
                    matchDay -> new ArrayList<>()));

    Set<Team> homeTeamsInCurrentMatchDay = new HashSet<>();
    List<Team> teamsScheduledForCurrentMatchDay = new ArrayList<>();

    /*matchDay*/
    for (int i = 1; i <= matchDays / 2; i++) {

        List<Team> homeTeamsInPreviousMatchDay = new ArrayList<>(homeTeamsInCurrentMatchDay);
        homeTeamsInCurrentMatchDay.clear();

        /*match number in current Matchday*/
        for (int j = 1; j <= gamesNumberPerMatchDay; j++) {

            /*first match*/

            Match match = drawMatch(allMatches, teamsScheduledForCurrentMatchDay, homeTeamsInPreviousMatchDay);

            allMatches.remove(match);

            schedule.get(i).add(match);

            homeTeamsInCurrentMatchDay.add(match.getHomeTeam());

            teamsScheduledForCurrentMatchDay.add(match.getHomeTeam());
            teamsScheduledForCurrentMatchDay.add(match.getAwayTeam());

            /*revenge match*/

            Match revengeMatch = Match.builder().homeTeam(match.getAwayTeam()).awayTeam(match.getHomeTeam()).build();

            schedule.get((matchDays / 2) + i).add(revengeMatch);

            allMatches.remove(revengeMatch);

        }
        teamsScheduledForCurrentMatchDay.clear();
    }

    return schedule;
}

private static Match drawMatch(List<Match> matches, List<Team> alreadyScheduledTeamsInCurrentMatchDay, List<Team> homeTeamsInPreviousMatchDay) {


    List<Match> possibleMatches = matches.stream()
            .filter(match -> !alreadyScheduledTeamsInCurrentMatchDay.contains(match.getHomeTeam()) && !alreadyScheduledTeamsInCurrentMatchDay.contains(match.getAwayTeam()))
            .collect(Collectors.toList());

    List<Match> possibleMatchesWithExclusionOfHomeTeams = new ArrayList<>(possibleMatches);

    possibleMatchesWithExclusionOfHomeTeams.removeIf(match -> homeTeamsInPreviousMatchDay.contains(match.getHomeTeam()));

    if (possibleMatchesWithExclusionOfHomeTeams.size() != 0) {
        possibleMatches = new ArrayList<>(possibleMatchesWithExclusionOfHomeTeams);
    }

    int randomNumber = new Random().nextInt(possibleMatches.size());
    return possibleMatches.get(randomNumber);
}

团队和比赛蓝图如下:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
class Team {
    String name;
}


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
class Match {

    private Team homeTeam;
    private Team awayTeam;
}

该算法不起作用。这

线程“主”java.lang.IllegalArgumentException 中的异常:绑定必须为正

被抛出 - 这意味着对于某些球队的某个比赛日有 0 场可能的比赛,它们满足开头列出的那些规则。我不确定,我在这里缺少什么以及调度算法应该如何反映真实的算法。换句话说,为什么possibleMatches.size()会发生这种情况0以及应该进行哪些更正以使该算法正常工作?

4

0 回答 0