Skip to main content

Binary Variable Involving Case Brackets

Answered

Comments

4 comments

  • Official comment
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jaromił Najman
    • Gurobi Staff

    We discuss how to model conditional statements in the Knowledge Base article How do I model conditional statements in Gurobi? In the linked article, we discuss a strict inequality where we need a small tolerance \(\epsilon > 0 \). This is not required in your case because your condition \(F_{i,j} \geq 1\) is not a strict inequality.

    Best regards, 
    Jaromił

    0
  • Suli Adeniye
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you Jaromil.

    I would like to add that F_ij is defined as:

    X_ij is:

    The binary variable representing flow[(k, (i, j)] is written as:

    flow_k_on_i_j = [(k, (i, j)) for k in pmu_nodes for i in nodes for j in nodes if i != j and (i, j) in edges]

    flow = model.addVars(flow_k_on_i_j, vtype=GRB.BINARY, name="flow")

    Following the notes you provided, I wrote code the snippets involving F_ij, and X_ij as:

    F_ij = {}
    for edge in edges:
        F_ij[edge] = sum(flow[(k, edge)] for k in pmu_nodes)
    X = model.addVar(vtype=GRB.BINARY, name="X")
    model.addConstr((F_ij[edge] for edge in edges) >= (1 - M * (1 - X[edge] for edge in edges)), name="X_edge_equal_1")
    model.addConstr((F_ij[edge] for edge in edges) <= (M * (1 - X[edge] for edge in edges)), name="X_edge_equal_equal_0")

    # note that pmu_nodes is a list containing nodes 2 to n, and flow[(k, edge)] is a data structure storing flow from node k passing through an edge (i, j) in the graph, edges is a list containing each edge (i, j)

    My code has been throwing an error on the first constraint that should ensure X_ij be 1 when F_ij is greater than or equal to 1:

    model.addConstr((F_ij[edge] for edge in edges) >= (1 - M * (1 - X[edge] for edge in edges)), name="X_edge_equal_1")

    TypeError: unsupported operand type(s) for *: 'int' and 'generator'

    Is there anythying I am doing wrong?  Thank you.

    0
  • Jaromił Najman
    • Gurobi Staff

    You are trying to access \(\texttt{X[edge]}\) but you define only a single variable \(\texttt{X}\).

    F_ij = {}
    for edge in edges:
        F_ij[edge] = sum(flow[(k, edge)] for k in pmu_nodes)
       X[edge] = model.addVar(vtype=GRB.BINARY, name="X") # here the name of X should be made unique

     

    0

Post is closed for comments.