希望得到一些帮助,试图定义一个通过创建网络流来确定团队何时被淘汰的函数,我非常肯定,我几乎已经到了那里,但是似乎有一个错误是我遗漏的&无法确定它是什么?任何帮助都很感谢!
最初的问题:
完成下面的team_eliminated函数,该函数使用存储在team中的团队名称,以及wins、games_to_play字典(这是上面compute_numbers函数的输出),并检查该团队是否被删除。如果团队被淘汰,您的函数应该返回布尔True,如果不是这样,则返回False。
这是定义函数
def team_eliminated(团队,胜利,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 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, eliminatedThe Error: 在这里输入图像描述
发布于 2022-05-20 10:25:45
在所有这些方面,我没有得到错误,除了缩进之外,没有任何更改:
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, eliminatedhttps://stackoverflow.com/questions/72315925
复制相似问题