Iterating through a Tuple - Network Flows

75 Views Asked by At

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

1

There are 1 best solutions below

3
gerald On

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