通过元组迭代 - 网络流动
希望提供一些帮助,试图定义一个函数,该功能可以通过创建网络流量来确定团队何时被取消,我可以肯定的是我几乎在那里,但是似乎有一个错误,我缺少了我&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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有在所有这些方面遇到错误,除了凹痕以外没有更改:
I'm not getting an error in all of this, no changes except indents: