首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >遍历元组网络流

遍历元组网络流
EN

Stack Overflow用户
提问于 2022-05-20 08:37:58
回答 1查看 59关注 0票数 0

希望得到一些帮助,试图定义一个通过创建网络流来确定团队何时被淘汰的函数,我非常肯定,我几乎已经到了那里,但是似乎有一个错误是我遗漏的&无法确定它是什么?任何帮助都很感谢!

最初的问题:

完成下面的team_eliminated函数,该函数使用存储在team中的团队名称,以及winsgames_to_play字典(这是上面compute_numbers函数的输出),并检查该团队是否被删除。如果团队被淘汰,您的函数应该返回布尔True,如果不是这样,则返回False

这是定义函数

def team_eliminated(团队,胜利,games_to_play):

代码语言:javascript
复制
## 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 which_teams_remaining(wins,games_to_play):

代码语言:javascript
复制
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: 在这里输入图像描述

EN

回答 1

Stack Overflow用户

发布于 2022-05-20 10:25:45

在所有这些方面,我没有得到错误,除了缩进之外,没有任何更改:

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72315925

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档