向if语句添加条件

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

我正在设计比赛时间表,但我遵守条件。

我已经制定了5个要满足这些条件的条件。

我必须以贪婪的算法来做。

到目前为止,我已经提出了:

while len(gamesgreedy) != len(games):
    v1 = CompetitionSchedule.violation_team_round_multiple(
        self, gamesreedy, start_index)
    v2 = CompetitionSchedule.violation_bye_per_team(
        self, gamesgreedy, start_index)
    v3 = CompetitionSchedule.violation_double_city(
        self, gamesgreedy, start_index)
    v4 = CompetitionSchedule.violations_double_match(self, gamesgreedy,)
    v5 = CompetitionSchedule.violations_home_away(
        self, gamesgreedy, start_index)

    for x in gamestup:
        gamesgreedy.append(x) if sum(v1, v2, v3, v3, v4, v5) <

我拥有所有可能的比赛池,我想将违规最少的比赛一一添加,依此类推,直到获得完整的赛程。

违反是整数。因此,在开头gamesgreedy是一个空列表,然后添加匹配项1,并检查与所有其他匹配项相比,是否带来最少的违规。

[此后,我将不得不重复此步骤,此时函数现在将检查具有匹配项gamesgreedy和其中具有匹配项12,依此类推。

python algorithm greedy
1个回答
0
投票

将评估移至功能。

def calculate_violations(self_, gamesgreedy, start_index):
    v1 = CompetitionSchedule.violation_team_round_multiple(
        self_, gamesgreedy, start_index)
    v2 = CompetitionSchedule.violation_bye_per_team(
        self_, gamesgreedy, start_index)
    v3 = CompetitionSchedule.violation_double_city(
        self_, gamesgreedy, start_index)
    v4 = CompetitionSchedule.violations_double_match(
        self_, gamesgreedy, start_index)
    v5 = CompetitionSchedule.violations_home_away(
        self_, gamesgreedy, start_index)
    return sum([v1, v2, v3, v4, v5])

在处理所有匹配项之前,将每个剩余的匹配项映射到违例数量,选择具有最小违例的匹配项,然后在下一步中使用它。

gamesgreedy = []
while len(gamesgreedy) < len(games):
    match_violations = {
        match: calculate_violations(self, gamesgreedy + [match], start_index)
        for match in games if match not in gamesgreedy}
    best_match = min(match_violations, key=lambda m: match_violations[m])
    gamesgreedy.append(best_match)

gamesgreedy变量然后以最佳顺序包含匹配项。

请注意,这只是考虑不清楚的目标的最佳猜测。

© www.soinside.com 2019 - 2024. All rights reserved.