Binary Variable Involving Case Brackets
回答済みHi,
I am trying to write a Gurobi expression for a binary variable X_ij which depends on another variable F_ij. 
F_ij is a list of variables each of which could have a different value depending on (i, j). I have already written in my code F_ij as a dictionary of key, value pairs - (i_j), F[i, j] but I am having a hard time translating X_ij into code.
Please, can you give me suggestions, example codes, or resources that could help me in coding X_ij.
Thank you.
-
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 -
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 -
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 unique0
サインインしてコメントを残してください。
コメント
3件のコメント