def hub_balance(m, i, j, co, t):
    """Calculate commodity balance in an edge {i,j} from/to hubs. """
    balance = 0
    for h in m.hub:
        if (h, co) in m.r_in_dict:
            balance -= m.Epsilon_hub[i,j,h,t] * m.r_in_dict[(h, co)] # m.r_in = 1 by definition
        if (h, co) in m.r_out_dict:
            balance += m.Epsilon_hub[i,j,h,t] * m.r_out_dict[(h, co)]
    return balance