How to get the index value of binary variable using indicator constraint
AnsweredThis thread resumes Indicator constraint which contains constant value.
I adopted indicator constraint previously you recommended and focused on programming which means that constant A equals constant B, in the right side of indicator constraint operator, >>.
In the following sample code, I used t to get the value of index i of b[i]. And z consisting of t becomes input for the formulation included in objective function.
For instance, if b[1], b[3] and b[5] ==1, then t=1, 3, 5. However this part brought the error, TypeError: unsupported operand type(s) for -=: 'NoneType' and 'float'. At this point, I have no idea how to fix it. If the error is solved, z will become [1,3,5]. And z is entered as parameter of tmp_f(), a part of objective.
# Rest of model
def tmp_f(z):
k = np.array([1 for i in range(10)])
for j in z:
k[j] = 0
return sum((k+1)**2)
# Indicator constraints
z = [] # ← ← Initialization of an input for nonlinear-type formulation
t = -1
for i in range(10):
m.addConstr((b[i] == 1) >> (t == i))
z.append(t)
# Objective function
obj = gp.quicksum(x[i] + y[i] for i in range(10))
m.setObjective(obj + tmp_f(z), GRB.MAXIMIZE)
# Rest of model
-
Official comment
This post is more than three 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. -
This question has been answered in Indicator constraint which contains constant value.
0
Post is closed for comments.
Comments
2 comments