通过元组迭代 - 网络流动

发布于 2025-01-30 11:42:30 字数 2192 浏览 2 评论 0原文

希望提供一些帮助,试图定义一个函数,该功能可以通过创建网络流量来确定团队何时被取消,我可以肯定的是我几乎在那里,但是似乎有一个错误,我缺少了我&amp&无法弄清楚它是什么? 任何帮助都将受到赞赏!!!

最初的问题:

在下面完成team_elimined功能,该功能将存储在team的团队名称和wins,, games_to_play字典(上面是Compute_numbers函数的输出),并检查该团队是否被删除。您的功能应返回布尔值true如果团队被取消,false如果否则。

这是定义函数

def team_elimined(团队,获胜,games_to_play):

## Compute the best possible win total for the given team
W = wins[team] + sum([games_to_play[p] for p in games_to_play if p[0] == team or p[1] == team])

## Construct sets which don't contain the team
teamlist = [t for t in wins if t != team]
pairs = [p for p in games_to_play if p[0] != team and p[1] != team]

## Construct the index sets for the x variables
idx = [(h, a, h) for h, a in pairs] + [(h, a, a) for h, a in pairs]

## Boolean variable that you need to modify
team_eliminated = None

nodes = ['s', 't'] + [i for i in pairs] + [j for j in teamlist]

arcs = ({((k[0], k[1]), k[2]) for k in idx})

arcs.update({('s', i): games_to_play[i] for i in games_to_play})

arcs.update({(j, 't'): wins[j] for j in wins})

arcs.update({('t','s'): GRB.INFINITY})

outgoing = {i:[j for j in nodes if (((k[0],k[1]), k[2]) for k in idx) in arcs] for i in nodes}
incoming = {i:[j for j in nodes if ((k[2], (k[0],k[1])) for k in idx) in arcs] for i in nodes}

mod = gp.Model('team-eliminated')

x = mod.addVars(arcs, lb=0, ub=arcs, vtype=GRB.CONTINUOUS, name='x')

for i in nodes : 
    mod.addConstr(gp.quicksum(x[i,j] for j in outgoing[i]) - gp.quicksum(x[j,i] for j in incoming[i]) == 0)

mod.setObjective(x['t', 's'], sense=GRB.MINIMIZE)

mod.update()
mod.optimize()

return team_eliminated

这是实现上述

def who_teams_remaining

remaining = []
eliminated = []
for t in wins:
    elim = team_eliminated(t, wins, games_to_play)
    if (elim):
        eliminated.append(t)
    else:
        remaining.append(t)
return remaining, eliminated

的函数: 在此处输入图像描述

Hoping for some help, trying to define a function that determines when a team is eliminated through creating a network flow, I am pretty certain I am almost there however there seems to be an error somewhere that I am missing & cannot work out what it is?
Any help is appreciated!!!

The Initial Question:

Complete the team_eliminated function below that takes a team name stored in team, and the wins, games_to_play dictionaries (which are outputs of the compute_numbers function above) and checks whether that team is eliminated or not. Your function should return the Boolean True if the team is eliminated and False if otherwise.

This is the defining function

def team_eliminated(team, wins, games_to_play):

## Compute the best possible win total for the given team
W = wins[team] + sum([games_to_play[p] for p in games_to_play if p[0] == team or p[1] == team])

## Construct sets which don't contain the team
teamlist = [t for t in wins if t != team]
pairs = [p for p in games_to_play if p[0] != team and p[1] != team]

## Construct the index sets for the x variables
idx = [(h, a, h) for h, a in pairs] + [(h, a, a) for h, a in pairs]

## Boolean variable that you need to modify
team_eliminated = None

nodes = ['s', 't'] + [i for i in pairs] + [j for j in teamlist]

arcs = ({((k[0], k[1]), k[2]) for k in idx})

arcs.update({('s', i): games_to_play[i] for i in games_to_play})

arcs.update({(j, 't'): wins[j] for j in wins})

arcs.update({('t','s'): GRB.INFINITY})

outgoing = {i:[j for j in nodes if (((k[0],k[1]), k[2]) for k in idx) in arcs] for i in nodes}
incoming = {i:[j for j in nodes if ((k[2], (k[0],k[1])) for k in idx) in arcs] for i in nodes}

mod = gp.Model('team-eliminated')

x = mod.addVars(arcs, lb=0, ub=arcs, vtype=GRB.CONTINUOUS, name='x')

for i in nodes : 
    mod.addConstr(gp.quicksum(x[i,j] for j in outgoing[i]) - gp.quicksum(x[j,i] for j in incoming[i]) == 0)

mod.setObjective(x['t', 's'], sense=GRB.MINIMIZE)

mod.update()
mod.optimize()

return team_eliminated

This is the function to implement the above

def which_teams_remaining(wins, games_to_play):

remaining = []
eliminated = []
for t in wins:
    elim = team_eliminated(t, wins, games_to_play)
    if (elim):
        eliminated.append(t)
    else:
        remaining.append(t)
return remaining, eliminated

The Error:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江南月 2025-02-06 11:42:30

我没有在所有这些方面遇到错误,除了凹痕以外没有更改:

def team_eliminated(team, wins, games_to_play):

    ## Compute the best possible win total for the given team
    W = wins[team] + sum([games_to_play[p] for p in games_to_play if p[0] == team or p[1] == team])

    ## Construct sets which don't contain the team
    teamlist = [t for t in wins if t != team]
    pairs = [p for p in games_to_play if p[0] != team and p[1] != team]

    ## Construct the index sets for the x variables
    idx = [(h, a, h) for h, a in pairs] + [(h, a, a) for h, a in pairs]

    ## Boolean variable that you need to modify
    team_eliminated = None

    nodes = ['s', 't'] + [i for i in pairs] + [j for j in teamlist]

    arcs = ({((k[0], k[1]), k[2]) for k in idx})

    arcs.update({('s', i): games_to_play[i] for i in games_to_play})

    arcs.update({(j, 't'): wins[j] for j in wins})

    arcs.update({('t','s'): GRB.INFINITY})

    outgoing = {i:[j for j in nodes if (((k[0],k[1]), k[2]) for k in idx) in arcs] for i in nodes}
    incoming = {i:[j for j in nodes if ((k[2], (k[0],k[1])) for k in idx) in arcs] for i in nodes}

    mod = gp.Model('team-eliminated')

    x = mod.addVars(arcs, lb=0, ub=arcs, vtype=GRB.CONTINUOUS, name='x')

    for i in nodes : 
        mod.addConstr(gp.quicksum(x[i,j] for j in outgoing[i]) - gp.quicksum(x[j,i] for j in incoming[i]) == 0)

    mod.setObjective(x['t', 's'], sense=GRB.MINIMIZE)

    mod.update()
    mod.optimize()

    return team_eliminated, wins


def which_teams_remaining(wins, games_to_play):
   
    remaining = []
    eliminated = []
    for t in wins:
        elim = team_eliminated(t, wins, games_to_play)
        if (elim):
            eliminated.append(t)
        else:
            remaining.append(t)
    return remaining, eliminated

I'm not getting an error in all of this, no changes except indents:

def team_eliminated(team, wins, games_to_play):

    ## Compute the best possible win total for the given team
    W = wins[team] + sum([games_to_play[p] for p in games_to_play if p[0] == team or p[1] == team])

    ## Construct sets which don't contain the team
    teamlist = [t for t in wins if t != team]
    pairs = [p for p in games_to_play if p[0] != team and p[1] != team]

    ## Construct the index sets for the x variables
    idx = [(h, a, h) for h, a in pairs] + [(h, a, a) for h, a in pairs]

    ## Boolean variable that you need to modify
    team_eliminated = None

    nodes = ['s', 't'] + [i for i in pairs] + [j for j in teamlist]

    arcs = ({((k[0], k[1]), k[2]) for k in idx})

    arcs.update({('s', i): games_to_play[i] for i in games_to_play})

    arcs.update({(j, 't'): wins[j] for j in wins})

    arcs.update({('t','s'): GRB.INFINITY})

    outgoing = {i:[j for j in nodes if (((k[0],k[1]), k[2]) for k in idx) in arcs] for i in nodes}
    incoming = {i:[j for j in nodes if ((k[2], (k[0],k[1])) for k in idx) in arcs] for i in nodes}

    mod = gp.Model('team-eliminated')

    x = mod.addVars(arcs, lb=0, ub=arcs, vtype=GRB.CONTINUOUS, name='x')

    for i in nodes : 
        mod.addConstr(gp.quicksum(x[i,j] for j in outgoing[i]) - gp.quicksum(x[j,i] for j in incoming[i]) == 0)

    mod.setObjective(x['t', 's'], sense=GRB.MINIMIZE)

    mod.update()
    mod.optimize()

    return team_eliminated, wins


def which_teams_remaining(wins, games_to_play):
   
    remaining = []
    eliminated = []
    for t in wins:
        elim = team_eliminated(t, wins, games_to_play)
        if (elim):
            eliminated.append(t)
        else:
            remaining.append(t)
    return remaining, eliminated
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文